The WorkChapter 03 / 05

Chapter 03 / 2025 / Languages

Mini C Compiler

A compiler is the program that turns code a person writes into instructions a computer runs. This one reads MiniC, a small version of the C language, checks it for mistakes and produces a real program for an ordinary PC.

What it is

From text to a running program

To the computer, source code is just a long string of characters.

A compiler is the program that turns code a person writes into instructions a computer runs.
A small MiniC program from the compiler's tests: a function with a loop and a choice, ready to be compiled.
Who it is forCoursework for the compilers module at Warwick. Every program anyone runs has been through a compiler, and writing one shows how code becomes something a machine can do.

How it works

Every stage, source to binary

I wrote every stage, from reading the text to the finished program.

1. Read the wordsThe text the lexer reads: a MiniC program from the tests, which it splits into names, numbers and symbols.
2. Build the treeThe grammar the parser checks the tokens against, one rule a line, as it builds the tree.
3. Check the meaningEvery name here is declared with its type before it is used: this is what the semantic checks confirm.
4. Write it outOn the right of the drawing, the IR the compiler writes, with the lines optimisation removes struck through.
5. Make a programThe compiler built and tested on GitHub after the last change: every check passed, in under a minute.

What it measured

Smaller code, tested

A fifth less code, and every test still passes.

20%The code the compiler writes out is a fifth smaller once it drops work that can be done early or never runs.
120+More than 120 automatic checks of what the compiler accepts, rejects and produces.

My part and the stack

My partWrote the compiler himself, from lexer to code generation, with its optimisation passes and tests.
The automatic test run on GitHub after the last change. Every check passed, in under a minute.