Technologies: C++, Flex, Bison, Bash, Command Line
Field: Compiler Design, Language Processing, Software Engineering
Completed: March 2019
C90-to-MIPS Compiler was a second-year pair project as part of EE Language Processing module.
This project was closely related to MIPS Simulator project and was an extension to the developments made from that project. However, the focus of this project was
to design a much more complex and technically challenging compiler which was able to translate a given C code into its equivalent MIPS assembly. We also had to provide an automated test suite
which would automate the compiler testing process and demonstrate compliance with specification requirements. We had to design a lexer (which accepts characters on our defined language and generates
tokens), parser (which arranges the code grammatically and generates Abstract Syntax Tree (AST)) and a code generator (which takes AST and generates assembly code).
Compiler [Flex, Bison, C++]: My role in this project was spread across all three components as I wanted exposure to new languages and key compiler design concepts. I learned how to use Flex to create the lexer
which generated tokens every time a matching regular expression was found. Close collaboration and regular testing was necessary to ensure our designs were compliant with official C90 grammar.
Similarly, I took responsibility for writing the grammar for the functions in the parser in Bison. Designing and visualising an ADT was a challenge so we referred to the official C90 parser for inspiration, yet
we simplified its design for our own understanding. However, code generation was undoubtedly the biggest challenge mainly because transforming ADT into its equivalent machine code requires manipulation
of data and in-depth understanding of stack operations. Much of our code base was Object Orientated hence we sub-divided our code into classes to keep track of implemented features.
Testbench [Bash]:
Creating a comprehensive testbench was a key learning from the MIPS Simulator project. As this was an extension, I applied our learning to create an automated test suite capable of running multiple files and checking
against reference data. The tests for this however were longer as the check was comparing MIPS code as opposed to just simulator exit code.
Python Translator [Python, C++]:
One of the deliverable for this project was to create a C-to-Python translator which only supported a limited C features such as simple functions, arithmetics and if/else statements. The aim was to understand how to lex
C code into a simple-to-understand Python language. This was my first exposure to Python in an academic project. This was the most enjoyable part of the project as it was very self-contained.
Compiler design is a skill that is valued highly across software engineering industry and upon reflection it was a rewarding project. It was difficult managing the project alongside other courseworks, but the skills developed
were very focussed on the technical aspects of compiler design (lexing, parsing and code generation). This was even useful for my third year High Level Programming module where there was significant focus on creating our own
untyped functional language. Nevertheless, I developed collaboration, communication and time management skills by working constructively with my partner.
A link to the project source code on GitHub can be accessed here.