Sadique
Sadique

Reputation: 22823

Lexical Analysis

My target is to make a very simple and basic C syntax checker. (Not a full Compiler but just a basic Program which take a source code as Input and would print out the code back showing the errors). I want to use C++ as the language for this.

Can anyone guide me to write regular expression in 'c++' language for the following:

  1. Assignment
  2. For
  3. switch

Any details/suggestions/guidance further would be much appreciated.

Upvotes: 3

Views: 2755

Answers (5)

user1524750
user1524750

Reputation:

To recognize C statements, lexical analysis is not sufficient, because lexers work at the character level. What you need, in addition to a C lexer, is a C parser which will do syntax analysis of the source code. The LRSTAR Parser Generator supplies a C project which will get you up and running.

Upvotes: 0

Alex Chamberlain
Alex Chamberlain

Reputation: 4207

Use the clang libraries. A tutorial can be found on GitHub.

Upvotes: 2

ZeRemz
ZeRemz

Reputation: 1801

Checking the syntax of C code requires a lot more than regular expressions. You'll need a tool that supports parsing algebraic gammars instead, I'd suggest looking at http://www.gnu.org/software/bison/ which will generate the "skeleton" of a parser in C++ so you can edit it and add your syntax-checking code.

Upvotes: 3

Mat
Mat

Reputation: 206679

I don't think you can parse C with regular expressions alone. You'll need to start looking into lexers, parsers, grammars, etc...

A good starting point could be this: Quick Starter on Parser Grammars - No Past Experience Required.

You can find C grammars online for yacc/lex.

Upvotes: 6

Mihran Hovsepyan
Mihran Hovsepyan

Reputation: 11088

Try boost spirit

Upvotes: 1

Related Questions