C– Compiler, coded in C++

As part of one of my Programming modules, we were tasked with creating a C– turned SCL compiler, both coded in C++. This project was divided into four phases.

  • The lexer – the part where all the lexical tokens were assigned
  • The syner – the part where all the syntax was assigned
  • Code Generation – the part where the code is generated and assembled in machine code using JASPer
  • Conversion – converting the first three phases to accept another, different simple language called SCL

In Phase1, the lexis of a program is analysed, and everything that is not whitespace is given a “tag” to identify it by. The characters are read space by space, and appropriate sub-programs are called to take care of literals. Phase2 then analyses the syntax of the program, setting the appropriate variables based on the tags, as well as error checking. Declarations are stored in a linked list called the SymTab (Symbol Table), whereas statements are stored in a linked list called the AST (Abstract Syntax Tree).

In Phase3, the AST entries are reversed into the correct order and then the code is generated using JASPer. Lastly, Phase4 applies these 3 phases and had me writing a parser for the fictional language SCL. SCL did not have certain features of C–, and C– did not have certain features of SCL, so some code was removed and other parts were added. For example, a declaration in SCL looks like: “let int number = 6 in”, so code has to be added to check for the let and the in, and removed to check for the semicolon. This was probably the hardest phase of all, despite the other three phases aiding greatly, as there was very little information given about the functionality of SCL.

However, in reflection, this project was well worth the time as it educated me greatly on how a compiler works, as well as effective ways to program and control data flow.

Project Type

  • University Work

Status

  • Completed