For our second year Language Processors module, we were tasked to build a C-compiler, as well as a C to Python translator. More details about the specifications can be found in the course repo here
We decided to use Java and ANTLR to build our compiler. The translator uses a Listener and traverses the parse-tree once. The compiler uses a visitor, and completes the whole compilation in one pass. Yet, for some nodes, we visit the children twice as the contexts are dependent on each another (such as the types within a two-operands operation)
Please note we had to export .class files in order to get them working on vagrant VM as we couldn't get to control directory and defaults in the VM during the boot. The compiler was to be tested on one of those, which is why the last few commits were about exporting java classes and setting up java in the VM without directory EXPORT control.
Our compiler scored 92% overall (rank 1st out of 36 compilers)
The project milestones and efforts can be viewed in details in the issues of the project. Issue 15 refers to more details about the different efforts and startegies. Overall, we have completed ~90% of the goals setup. Many of the expecations were altered by the sudden Coronavirus outbreak (hopefully gone by the time you are reading this)
Jaafar Rammal (github) Raphael Bijaoui (github)
In your terminal, go to the directory EE2-Compiler
Setup using vagrant up
then connect with vagrant ssh
Main directory in cd /vagrant
Leave the vagrant with exit
or ctrl+d
Destroy the vagrant using vagrant destory
make bin/c_compiler
To run compiler as per spec (only creating MIPS assembly)
make bin/c_compiler
bin/c_compiler -S [source-file.c] -o [dest-file.s]
To run translator as per spec (only creating Python output)
make bin/c_compiler
bin/c_compiler -translate [source-file.c] -o [dest-file.y]
There are three testbenches: one for the translator and two for the compiler (one as a driver and one as a main). To run the testbench on a specific compiler:
[TESTBENCH NAME] [COMPILER DIRECTORY] [TESTS DIRECTORY] > [RESULTS OUTPUT DIRECTORY]
To run compiler in debug mode (creating test binary, running on custom MIPS simulator)
make bin/c_compiler
make simulator
make parser
bin/c_compiler -TEST [source-file.c] -o [dest-file.s]
On a linux machine
mips-linux-gnu-gcc -mfp32 -o test_program.o -c a.s
mips-linux-gnu-gcc -mfp32 -static -o test_program test_program.o driver.c
qemu-mips test_program
You will need to setup some paths correctly first and setup the ANTLR package. You can add the export and aliases to your ~/.bash_profile for consistency.
To build java files properly
sudo cp ./antlr-4.8-complete.jar /usr/local/lib
export CLASSPATH=".:/usr/local/lib/antlr-4.8-complete.jar:$CLASSPATH"
alias antlr4='java -jar /usr/local/lib/antlr-4.8-complete.jar'
alias grun='java org.antlr.v4.gui.TestRig'
Cleanup with make clean
Build project with make java
Some example trees are here. You can generate a tree by running either of these commands depending on the grammar you would like to study
make tree/compiler
make tree/translator