PathetiC

#projects #parsing #c

With this project, I can't be sure if I came up with the idea or the pun "it was all a MIPStake" first. Either way, it ended up being a rather nice concise implementation of a full compiler pipeline - taking in a fairly "pathetic" subset of C, and then compiling the program to MIPS assembly.

More seriously, my motivation for this project was primarily due to the fact that UCL's compiler course, at least at the time I took it, has a rather fragmented structure, and never steps through the process of constructing an entire compiler - for example, there are two courseworks for this course, the first one (frontend side) involves writing a parser for a specific language, wheras the second one (backend side) is completely unrelated to the first language, and involves implementing a bytecode based optimizer for Java.

Unfortunately, I personally found that such a coverage was insufficient for me to fully understand the intricacies of a compiler, and so I decided to try and implement one on my own to help consolidate these concepts (bonus points, I allowed myself to pretend that this actually counted as "revision" for the course).

To give an understanding of how this project ends up shaping out, I'll give an example of what it can compile.

PathetiC compiler can convert a program of the form:

int i = 0; 
int j = 0;
int k = 1;

while(i < 10) {
        int t = j + k;
        j = k;
        k = t;
}

into

li $t0, 0
li $t1, 0
li $t2, 1
flag0:
        slt $t3, $t0, 10
        beq $t3, 0, flag1
        beq $t0, 10, flag1
        add $t4, $t1, $t2
        li $t1, $t2
        li $t2, $t4
        j flag0
flag1:

You can find the project here: pathetiC