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:
- Assignment
- For
- switch
Any details/suggestions/guidance further would be much appreciate开发者_如何学Cd.
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
.
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.
Use the clang
libraries. A tutorial can be found on GitHub.
Try boost spirit
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.
精彩评论