gitmyhub

acwj

C ★ 13k updated 14d ago

A Compiler Writing Journey

A 64-part tutorial series that walks you through writing a real C compiler from scratch in C, starting from reading text tokens and ending with a compiler that can compile itself.

Cx86-64Assemblysetup: moderatecomplexity 3/5

This repository documents a step-by-step journey to write a compiler from scratch in C. A compiler is a program that reads source code written by a programmer and translates it into machine instructions a computer can run. The author set out to build a compiler for a large subset of the C programming language, with the specific goal of making it capable of compiling itself, a benchmark in compiler development sometimes called self-compilation or bootstrapping.

The project is organized as a series of 64 numbered parts, each with its own folder and explanatory document. The series starts from the very basics, introducing how a compiler reads and recognizes tokens in source text, then progressively adds more complexity: arithmetic expressions, variables, control flow like if-statements and loops, functions, pointers, arrays, structs, unions, enums, type casting, and eventually a preprocessor. The final parts cover register spilling, lazy evaluation, passing a triple-compilation test, and adding support for a different CPU target.

Each part is written to be followed along, with explanations of both the practical code changes and the underlying theory when relevant. The author's stated approach is to stay focused on practice rather than academic formalism. Someone who wants to understand how programming languages work from the inside, or who is curious about what happens between writing code and running it, would find this series more accessible than most textbook treatments of the subject.

The author has since moved on to a new language project called alic and considers this series complete. The source code is licensed under GPL3, and the written documents are under Creative Commons. Some early code ideas were drawn from an existing open-source compiler called SubC.

Where it fits