A mini compiler for C language in Java
Compilers can be decomposed into several phases, each of which converts one form of source program into another
There are different phases of compiler as follow:
Source program is scanned to read the stream of characters and those characters are grouped to form a sequence called lexemes which produces token as output.
```
Syntax analysis is the second phase of compiler which is also called as parsing.
Parser follow production rules (Grammer) defined by means of context free grammar (CFG).
Grammer ex:
program -> decl_list
decl_list -> decl decl_list'
decl_list' -> decl decl_list' | ϵ
decl -> var_decl | fun_decl
var_decl -> type_spec IDENT var_decl'
var_decl' -> ; | [ ] ;
type_spec -> VOID
| BOOL
| INT
| FLOAT
...
...
...
The way the production rules are implemented divides parsing into two types : top-down parsing and bottom-up parsing. I use Recursive descent parser is a kind of top-down parser.
Input
int main(){
int x;
}
Genereted Tokens from Lexical analyzer
<INT>: int
<ID>: main
<LEFT_ROUND_B>: (
<RIGHT_ROUND_B>: )
<LEFT_CURLY_B>: {
<INT>: int
<ID>: x
<SEMICOLON>: ;
<RIGHT_CURLY_B>: }
Genereted Parse Tree from Syntax Analyzer