As part of a module in the second year of University, I had to write a compiler to compile a simple subset of C called C–. I had to write three different parts of the compiler; the Lexical Analyser, Syntax Analyser and Code Generator.

As per the assignment brief the code for this had to be compiled and run on the University Unix server. This meant that the code was all written in either Vi (on a PuTTY terminal window connected to the Unix server) or Gedit (the code could be written/edited on Linux, but only compiled on the server) and compiled using G++.

The Lexical Analyser had to take in a C– source file, read through it and generate tokens based on the contents of the source file. The order of these tokens was then checked by the Syntax Analyser in order to determine in the program has the correct syntax. Finally, if the syntax is correct, the Code Generator generates an assembly code file for the program which can then be run using JASPer. JASPer is a tool that runs assembly code on a simple simulated processor.

The code files for this project can be found here.

  • Using C++, Vi, Gedit, Terminal