Implement the front end of the compiler for C language using Lex and Yacc for the following constructs :
- Structs
- Ternary Operators
- For Loops
Clone this repository and execute sh run.sh. After execution of various phases of the compiler you may use sh clean.sh to remove output files.
Following output files are generated upon execution of run.sh :
- Symbol Table :
./SymbolTable < filename
Symbol table contains keywords and identifiers, their datatypes and values with some preliminary evaluation of simple expressions. The output of this file will be insymbol_table.txtand the errors while creating the symbol table are stored inerrors.txt. The errors handled by the symbol table are
- Undeclared variables
- Multiple declarations of a variable within the same scope
- Invalid value for given datatype
-
Abstract Syntax Tree :
./AST < filename
Usesgraph.candheader.hto display the abstract syntax tree. -
Intermediate Code Generation :
./ICG < filename
Generates anif-gotoform of intermediate representation of code inoutput_file.txt. Handles nested for loops, nested ternary operators too. -
Code Optimization :
python3 optimize.pyorpython3 optimize.py filename
Requiresif-gotoform of intermediate representation of code as input. Iffilenameis not specified, it takes the intermediate code fromoutput_file.txtby default.
Following code optimizations were performed
- Dead code elimination
- Constant folding