Course description:
The theory, design and implementation of a practical compiler. Finite automata, LL and LR parsing, attribute grammars, syntax-directed translation, symbol tables and scopes, type systems and representations, abstract syntax trees, intermediate representation, basic blocks, data and control flow optimizations, assembly language generation including register and instruction selection. Students apply tools such as Flex and Bison to writing a functional compiler for a subset of a real programming language such as C.
Ensure you are running a 32-bit Linux distro with an Intel CPU. The generated assembly instruction (from genassembly.out) are intended for 32-bit Intel architectures. The compiled executable will also not run on Windows Subsystem for Linux (WSL) aka Bash on Windows: track this issue.
-
The lexical analyzer flex:
apt-get install flex
-
The parser generator bison:
apt-get install bison
-
Run
make
to generate miragecompiler.out andmake genassembly.out
to generate genassembly.out -
Generate intermediate representation (quads) for simple C programs (with loops, conditional statements, or function calls), and save them in a textfile by redirecting the output
./miragecompiler.out < simpleprog.c > simplequads.txt
-
Use the assembly generator (genassembly.out) to create 32-bit assembly instructions
./genassembly.out < simplequads.txt > simpleassembly.s
-
Use gcc's assembler and linker to generate the executable using the 32-bit assembly instructions
gcc -m32 simpleassembly.s [-o simpleprog.out]
-
Run the executable (a.out if you didn't specify a new name)
./simpleprog.out