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.txt
and 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.c
andheader.h
to display the abstract syntax tree. -
Intermediate Code Generation :
./ICG < filename
Generates anif-goto
form of intermediate representation of code inoutput_file.txt
. Handles nested for loops, nested ternary operators too. -
Code Optimization :
python3 optimize.py
orpython3 optimize.py filename
Requiresif-goto
form of intermediate representation of code as input. Iffilename
is not specified, it takes the intermediate code fromoutput_file.txt
by default.
Following code optimizations were performed
- Dead code elimination
- Constant folding