Skip Navigation

Writing a C Compiler | Build a Real Programming Language from Scratch | Nora Sandler | July 2024 | No Starch Press | 792 pages | ISBN-13: 9781718500426

nostarch.com Writing a C Compiler

A fun, hands-on guide to writing your own compiler for a real-world programming language.

Writing a C Compiler

Book Description

Writing a C Compiler will take you step by step through the process of building your own compiler for a significant subset of C—no prior experience with compiler construction or assembly code needed. Once you’ve built a working compiler for the simplest C program, you’ll add new features chapter by chapter. The algorithms in the book are all in pseudocode, so you can implement your compiler in whatever language you like. Along the way, you’ll explore key concepts like:

  • Lexing and parsing: Learn how to write a lexer and recursive descent parser that transform C code into an abstract syntax tree.
  • Program analysis: Discover how to analyze a program to understand its behavior and detect errors.
  • Code generation: Learn how to translate C language constructs like arithmetic operations, function calls, and control-flow statements into x64 assembly code.
  • Optimization techniques: Improve performance with methods like constant folding, dead store elimination, and register allocation.

Compilers aren’t terrifying beasts—and with help from this hands-on, accessible guide, you might even turn them into your friends for life.

Author Bio

Nora Sandler is a software engineer based in Seattle. She holds a BS in computer science from the University of Chicago, where she researched the implementation of parallel programming languages. More recently, she’s worked on domain-specific languages at an endpoint security company. You can find her blog on pranks, compilers, and other computer science topics at https://norasandler.com.

5
Hacker News @lemmy.smeargle.fans bot @lemmy.smeargle.fans
BOT
Book – Writing a C Compiler: Build a Real Programming Language from Scratch 2024

You're viewing a single thread.

5 comments
  • I'm writing my own C compiler but in OCaml. https://github.com/Chubek/Cephyr I have re-initialized the project several times. What you see in this Github repository is not what is in my local directory. Which I deleted. For writing this compiler, I am reading some real books, and papers, not a garbage book by No Starch Press. This publishing house never publishes anything good! Their books are not scientific.To be frank, textbooks are garbage too. Like, they contain wisdom, but not 'state of art' information.

    c9x.me has some good material -- books and papers -- for amateur compiler authors. Muchnick's book is good, Appel's 3 books, especially the one on ML, are good, but what I appreciate are Appel's book "Compiling with Continuations". His treatise on 'SSA is Functional' is also good. Although useless to me since I'm implementing an imperative language. I am however, implementing VEBLEN, a functional language too but I put a pause on it. There's dozens of books that are better than this one. I read the pre-release version of this book. Just pure garbage!

    No offense to the author of this book. She's probably paid a measly amount to write this. That is why I hate No Starch Press. It never happens that someone wants to write a book, and NSP commissions them. NSP commissions the books based on whatever some suites deem 'popular'! Other publishing houses like this are Manning, and Packt. These publishing houses are the epitome of 'programming as pop-culture' as Kay puts it. Just cancer.

    If you wish to just 'try' writing a compiler, Thosten Ball has you covered. Otherwise you're in for a treat! Writing a compiler teaches you basic computer science. It taught me, for example, how to use Google Scholar. Now, for a job making After Effects plugins, I gathered all the papers I needed from it! If you want an interpreter, Robert Nystrom has you covered.

    Ball and Nystrom's book come from 'the heart'. That's why they are better than this commissioned garbage!

    I can also list a lot of books on language authorship. Just ask and I will.

    Remember that papers are great too. Use Google Scholar for papers.

    At the moment, Cephyr is not what I am concerned with. I am writing a very simple, Lua-like interpreted language that's ought do:

    1- Partial Evaluation 2- Meta-tracing

    If you want I can explain further. I have developed a habit of dumping everything in one file. No repository! I'm currently writing the visitor. Here's what I got so far. https://pastebin.com/wRATk4G4

    "Interpreters are easier to write than compilers" -- Iulian Chezarus

    I also plan on writing a Pascal compiler, based on JVM. Or CIL? Who knows!

    I'm also writing a compiler for the Lucid language. Use Google Scholar to read about Lucid. It's a dataflow language!

    (I wonder if I can use a Lucid-like syntax for the dataflow portion of an IL?)

    Remember these are all probable. I deleted all my files. Intentionally. I had written a lot for the Lucid compiler. But it's all gone now :(

    • I'm not sure what to make of your comment.

      • My thesis is that books by this publication house are garbage, if you want an 'easy' and 'pop-culture' book oriented around writing a compiler, Thoston Ball's "Writing a Compiler in Go" is better, and if you want to write an interpreter, Rob Nystrom's "Crafting an Interpreter" is a much better choice. These are 'pop-culture' books, mind you, 'easy' books. If you want hard books, scientific books and the such, then go to Google Scholar and find the book that suits your needs. Steven Mucknick's book is a good scientific compiler-writing book. c9x.me has a good list of papers and books.

        Remember I don't wish to slander the author. I kept a close eye on this book, ever since it was incomplete and parts of it were available on places around the web. It's just not a good book. But it's got nothing to do with the author. The publishing house is at fault.

        Publishing houses like:

        1- Manning;

        2- Packet;

        3- No Starch Press;

        Only publish pure garbage. They want programmers with deep pockets to buy their books, they don't AT ALL care about the quality of their books. Their books are 'pop-culture', you will not see their books pop up on, say, Google Scholar! True that, the two books I mentioned above don't pop up in Google Scholar either (unless you try) but they are 'good' because they have been written from the heart. Ball and Nystrom really want to educate people. The author of this book was probably paid a measly sum to write a 'coffee-table book' because that's what this book is, it's been crafted for people to take pictures of and put on Twitter! It's got zero scientific worth.

        Just don't waste your money on this book! Start with a textbook, then start reading papers and subject-specific books. Then you'll be able to write any compiler you want.

        spoiler

        (And btw, if you want to buy a book and put its pictures on Twitter, Pragmatic Bookshelf is your friend! This publishing house behaves like these 3, but actually publishes good book. I would gladly fondle the balls of Pragmatic Bookshelf's editors because I sooo want to publish my book by them! And no, I don't work for them to spread pro-Pragmatic Bookshelf propaganda, I honestly love their books --- they employ authors of FLOSS software and pay them to write books about them. Terrance Parr has published several books with them).

        • I appreciate your sharing of recommendations for other resources that you are familiar with. That's genuinely helpful.

          However, while I agree that those you mentioned publishers don't have high minimum standards for author selection or editing (to varying degrees among them), I disagree that all of their books are garbage and that none of their authors have a genuine interest in passing on knowledge.

          With that in mind, I'd be interested in reading what distinguishes this book as a "coffee table book" beyond it being published by No Starch Press. That is, what about the content of this book falls short as an introductory resource for the subject for those curious about compilers?

          • Keep in mind that this is true for the pre-release version that I read. The chapters available were more interested in giving 'code' as examples. For most people, writing a compiler is the most theroy-heavy interest that they're going to have, and job of the author for such a book is to direct them towards theory -- otherwise, say the reader copies their code into their editors, compilesr and runs it, what then? What is the computational worth of that code if it's neither educational, nor does it nudge the user towards a path in understanding theory of computation? So my gripes with this book is, it does not need to be there. It does not solve an issue. All it brings about is shoddy compilers by people who copy the source of this code! I want everyone who reads this book, to use it to branch off! Into a path of theoretical CS. The chapters that i had, they did not do this. They just said 'here's how it's done, champ'. This is why I hate all books from these 3 publications, "Axis of Evil". The worst book on Assembly language, the worst book on Embedded programming, etc etc, every 'worst' I have read is from these 3! Now, I say all these as a learner. I, as a learner, hated the preview version of this book, and I hate everything this publication houses publishes. AS A LEARNER! But someone who is a "master", so to speak, they will have other gripes! I just ask you this: Why the blurbs? Does a real academic, scientific textbook need blurbs?