This is a C- Lexical and Syntax Analyzer which is the curriculum project of Principles of Complier.
Implemented C- Syntax Analyzer by using C++. Removed the left recursion of grammar first, made analysis through top-down and built an Abstract Syntax Tree coded from C-.
The code to implement it is in compiler file. The /report/2015141462312_赵文腾_《编译原理课程设计》课程报告.doc shows how to realize compiler syntax.
For lexical analyzer, here is the DFA.
For syntax analyzer, here is the BCNF grammar, 29 productions.
- program → declaration-list
- declaration-list → declaration-list declaration | declaration
- declaration → var-declaration | fun-declaration
- var-declaration → type-specifierID ; | type-specifierID [NUM];
- type-specifier→ int | void
- fun-declaration → type-specifierID (params)compound-stmt
- params→ param-list | void
- param-list→ param-list , param| param
- param→ type-specifierID | type-specifierID [ ]
- compound-stmt → { local-declarations statement-list }
- local-declarations → local-declarations var-declaration | empty
- statement-list → statement-list statement | empty
- statement → expression-stmt | compound-stmt| selection-stmt | iteration-stmt | return-stmt
- expression-stmt → expression ;| ;
- selection-stmt → if( expression ) statement | if ( expression ) statement else statement
- iteration-stmt → while( expression ) statement
- return-stmt → return ; | return expression ;
- expression → var = expression | simple-expression
- var → ID| ID[ expression ]
- simple-expression → additive-expression relop additive- expression | additive-expression
- relop → <= | < | > | >= | == | !=
- additive-expression → additive-expression addop term | term
- addop → + | -
- term → term mulop factor | factor 25. mulop → * | /
- factor → ( expression ) | var | call | NUM
- call → ID( args )
- args → arg-list | empty
- arg-list → arg-list , expression | expression