https://jollygoodsw.wordpress.com/2025/03/13/working-through-writing-a-c-compiler/ Jolly Interesting Stuff + About me Old programmer discusses stuff about software (mainly) Working through 'Writing A C Compiler' 13.3.2025 Some time ago I decided I wanted to create a c compiler for the Hack processor in the nand2tetris book. After several dead end attempts that I might describe later I found this book : Writing a C Compiler | No Starch Press [writing_a_c_compilercover-1] I decided to work my way through it. I plan to do blog posts as I progress through it, probably chapter by chapter. I have emailed the author a couple of times and have got helpful timely replies! - thank you What makes the book so good? Step by step By the end of chapter 1 you have a working compiler. Each subsequent chapter adds more features. The chapter 1 compiler can only handle int main(void){ return ; } But it works producing a complete executable Chapter 2 adds unary operators so we can now do something like. int main(void){ return ~3; } This contrasts with most other books I have worked with on compilers like the also excellent Crafting Interpreters and Home | nand2tetris The fact that we are building a real language as opposed to a made up one is great too. Side note: Some c features are not implemented in the book but are suggested as 'extra credit'. I intend to implement all those. Some features are not implemented and are not covered. More on those later. Test suite The other great thing is that there is a large test suite provided. For each chapter tests are added so running the , say, chapter 5 tests runs the tests for 1,2,3 and 4 as well. You can say if you want to test: * extra credit features * just the lexer pass * the lex and parser pass * semantic analysis * everything Even the simple compiler in chapter 1 has 24 tests This is a tremendous resource and seeing the test suite say PS C:\work\forks\writing-a-c-compiler-tests> python .\test_compiler C:\work\mycc\target\debug\mycc.exe --chapter 1 --extra-credit ---------------------------------------------------------------------- Ran 24 tests in 2.843s OK is a real pleasure. Time to fire up gitui (gitui-org/gitui: Blazing fast terminal-ui for git written in rust ) and commit Repos Nora has two github repos for this book * nlsandler/writing-a-c-compiler-tests: Test cases for Writing a C Compiler this is the test suite * nlsandler/nqcc2: Reference implementation for Writing a C Compiler this is an ocaml implementation of the compiler. It does not implement the extra credit features I have two as well * pm100/writing-a-c-compiler-tests: Test cases for Writing a C Compiler a fork of the original with a few changes and bug fixes. Mainly to do with Windows support (see later post) * pm100/mycc my implementation in rust. Like the nqcc2 repo it has (mainly) a commit per chapter Posts I will probably do one blog post per chapter, including one for the introduction. I got to the extra credit section of chapter 8 before I thought of blogging the process. So there will be a bunch of posts in a rush, that cover completed work and will be in the past tense. As I move forward, they will be chattier as I debate with myself how to do things, and spread out to the pace of my progress Final Note This is a great book. Any comments I make in later chapter regarding things that I think are missing, wrong, confusing etc. should not take away from the fact that this is an extremely good book. Share this: * Click to share on X (Opens in new window) X * Click to share on Facebook (Opens in new window) Facebook * Like Loading... More Posts * 13.05 Working Through WACC - chapter 15, arrays and pointer arithmetic * 18.04 Working Through WACC - Chapter 14 , pointers * 16.04 Working Through WACC - Chapter 13 Floats * 09.04 Workinig Though WACC - chapter 12, unsigned types * 06.04 Working Through WACC - Chapter 11 , Long Integers * 06.04 Working Through WACC - Pause after part 1 6 responses to "Working through 'Writing A C Compiler'" 1. Working through 'WACC' - chapter 5, Local Variables - Jolly Interesting Stuff Avatar Working through 'WACC' - chapter 5, Local Variables - Jolly Interesting Stuff 2025.03.14 [...] This is a post in a long series about the book .Writing A C Compiler'. The first post that explains the project is here Working through 'Writing A C Compiler' - Jolly Interesting Stuff [...] LikeLike Reply 2. Working Through WACC - Chapter 7, Compound statements - Jolly Interesting Stuff Avatar Working Through WACC - Chapter 7, Compound statements - Jolly Interesting Stuff 2025.03.15 [...] Because we haven't had it for a while here's the front cover. If you are not sure what this post is about check out episode 1 https://jollygoodsw.wordpress.com/2025/03/13/ working-through-writing-a-c-compiler/ [...] LikeLike Reply 3. Working through 'Writing A C Compiler' - CodeGurus 2025.07.12 [...] Article URL: https://jollygoodsw.wordpress.com/2025/03/13/ working-through-writing-a-c-compiler/ [...] LikeLike Reply 4. Lesley Lai Avatar Lesley Lai 2025.07.12 I am also working through the book. Currently stuck on chapter 10 and was busy doing some other stuff for a few month, but I am back and making progress again recently. https://github.com/LesleyLai/mcc LikeLike Reply 1. paulmoore100 Avatar paulmoore100 2025.07.12 Are you using the tests from the book?. They are a fantastic resource. By end of chapter 18 there are > 1000 tests LikeLike Reply 1. Lesley Lai Avatar Lesley Lai 2025.07.12 Yes, I am. However, I write my own test infrastructure because the book's tests run pretty slowly. Most of my test cases are still copied and adapted from the book, and I occasionally also run the book's test directly LikeLike Leave a comment Cancel reply [ ] [ ] [ ] [ ] [ ] [ ] [ ] D[ ] Search [ ] Designed with WordPress Loading Comments... Write a Comment... [ ] Email (Required) [ ] Name (Required) [ ] Website [ ] [Post Comment] * Comment * Reblog * Subscribe Subscribed + [croppe] Jolly Interesting Stuff [ ] Sign me up + Already have a WordPress.com account? Log in now. * + [croppe] Jolly Interesting Stuff + Subscribe Subscribed + Sign up + Log in + Copy shortlink + Report this content + View post in Reader + Manage subscriptions + Collapse this bar %d [b]