Compiler For Flat Tiny C
Compiler For Flat Tiny C
Compilers
A Compiler translates a program in a source language to an equivalent program in a target language
Source Program
Compiler
Target Program
Typically
Source Languages - C, C++, ADA, FORTRAN etc.
code.
GCC
Source Progra m
IR
IR
Target ALP
An Optimizer
Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code
progra m Lexical Analyzer token stream Syntax Analyzer syntax tree Symbol Table Semantic Analyzer syntax tree Intermediate Code Generator intermediate representation Machine-Independent Code Optimizer intermediate representation Code Generator
Back End
target-machine code
target-machine code
Scanner
Tokens
Parser
IR Error s
Parser
Takes as input a stream of tokens
the language
If the input program is syntactically correct
Context Free Grammars and Programming Languages Expr Binop Expr Binop Expr | Expr | ! Expr | ( Expr ) Arithop | Relop | Eqop | Condop
Eqop
== | !=
Condop && | ||
|
|
if ( Expr ) Block
if ( Expr ) Block else Block
| continue ; | Block
Programming languages can be specified using a special sub-class of CFGs for which efficient parsing techniques and automatic parser generators exists.
These special class of CFGs also allow for automatically capturing ambiguities in
the language
CFGs impose a structure on the program which facilitates easy translation to
Parsing Approaches
Cocke-Younger-Kasami (CYK) algorithm can construct a parse tree
Yacc (Bison)
Yacc
flex can generate the yylex() function using the lexical specification.
Yacc
x+2-y
This is much more concise ASTs are one kind of intermediate representation (IR)
expr subtree
statement subtree
expr subtree
statement subtree
if-else
expr subtree
ifstatement subtree
elsestatement subtree
Note: This yacc specification is not complete. I highlighted only the important parts.
expr: expr + expr { $$ = getNewAstnode(); $$->op = plus; $$->left = $1; $$->right = $3; }
| expr - expr { $$ = getNewAstnode(); $$->op = minus; $$->left = $1; $$->right = $3; } | expr * expr { $$ = getNewAstnode(); $$->op = mult; $$->left = $1; $$->right = $3; } | expr / expr { $$ = getNewAstnode(); $$->op = div; $$->left = $1; $$->right = $3; }
expr subtree
statement subtree
expr subtree
statement subtree
if-else
expr subtree
ifstatement subtree
elsestatement subtree
AST Construction
Note: The way statement lists are handled here is different from the code I have shown in the class.
Symbol Tables