Reputation: 22823
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:
Any details/suggestions/guidance further would be much appreciated.
Upvotes: 3
Views: 2755
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
Reputation: 4207
Use the clang
libraries. A tutorial can be found on GitHub.
Upvotes: 2
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
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