Want to write a compiler? Just read these two papers (2008) (prog21.dadgum.com)

by downbad_ 159 comments 509 points
Read article View on HN

159 comments

[−] jll29 30d ago
*Donald Knute -> Donald Ervin Knuth is the author of the book "The Art of Computer Programming" (in progress for a couple of decades, currently volume 4c is being written). It is quite advanced, and it will likely not cover compilers anymore (Addison-Wesley had commissioned a compiler book from Knuth when he was a doctoral candidate, now he is retired and has stated his goal for the series has changed).

I disagree with the author's point: the "Dragon book"'s ("Compilers: Principles, Techniques, and Tools" by Aho et al.) Chapter 2 is a self-sufficient introduction into compilers from end to end, and it can be read on its own, ignoring the rest of the excellent book.

Another fantastic intro to compiler writing is the short little book "Compilers" by Niklaus Wirth, which explains and contains the surprisingly short source code of a complete compiler (the whole book is highly understandable - pristine clarity, really) and all in <100 pages total (99).

(I learned enough from these two sources to write a compiler in high school.)

[−] projektfu 30d ago
The dragon book almost convinced me never to try to write a compiler. I don't know why people recommend it. I guess you're a lot smarter than I am.

There are some excellent books out there. In its own way, the dragon book is excellent, but it is a terrible starting place.

Here are a bunch of references from the same vintage as OP. I recommend starting with a book that actually walks through the process of building a compiler and doesn't spend its time exclusively with theory.

https://news.ycombinator.com/item?id=136875

[−] rangerelf 30d ago
You're not the only one. In college I took a compilers course and we used the dragon book, to me it sucked the joy out of the magical concept of making a compiler.

Some years later I (re-) discovered Forth, and I thought "why not?" and built my own forth in 32-bit Intel assembly, _that_ brought back the wonder and "magical" feeling of compilers again. All in less than 4KB.

I guess I wasn't the right audience for the dragon book.

[−] _false 30d ago
Great thread. If you have 1 hour to get started, I recommend opening Engineering a Compiler and studying Static Single-Assignment (SSA) from ch 9.3.

The book is famous for its SSA treatment. Chapters 1-8 are not required to understand SSA. This allows you to walk away with a clear win. Refer to 9.2 if you're struggling with dominance + liveness.

http://www.r-5.org/files/books/computers/compilers/writing/K...

[−] unclad5968 29d ago
I bought this book when I was working on a toy language and I think I was too stupid to understand most of it. The first few chapters were great, but it quickly surpassed my capacity to understand. Seeing it mentioned makes me want to revisit.
[−] pjmlp 29d ago
It was a product of its time I guess, much better ones from similar vintage,

The Tiger book (with C, Standard ML, and Java variants)

https://www.cs.princeton.edu/~appel/modern/

Compiler Design in C (freely available nowadays, beware this is between K&R C and C89)

https://holub.com/compiler/

lcc, A Retargetable Compiler for ANSI C

https://drh.github.io/lcc/

Or if one wants to go with more clever stuff,

Compiling with Continuations

Lisp in Small Pieces

[−] drob518 29d ago
Another vote for Lisp in Small Pieces. Great high level compiler book that teaches you how to build a Lisp and doesn’t get bogged down in lexing and parsing.
[−] rurban 29d ago
Instead of Lisp in Small Pieces I'd recommend SICP instead. No continuation passing, but much better written.
[−] pjmlp 28d ago
And no information on how to actually do a compiler, end to end, only a self hosted interpreter.

The authors don't have the same audience in mind.

I would recommend both, one is about actual Lisp compilers, the other alternative computation models.

[−] randomNumber7 30d ago
Imho the problem is the fixation on parser generators and BNF. It's just a lot easier to write a recursive descent parser than to figure out the correct BNF for anything other than a toy language with horrible syntax.
[−] torginus 29d ago
Imo BNF (or some other formal notation) is quite useful for defining your syntax, my biggest gripe with BNF in particular is the way it handles operator precedence (through nested recursive expressions), which can get messy quite fast.

Pratt parsers dont even use this recursion, they only have a concept of 'binding strength', which means in laymans terms that if I'm parsing the left side of say a '' expression, and I managed to parse something a binary subexpression, and the next token I'm looking at is another binary op, do I continue parsing that subexpression, which will be the RHS of the '' expression, or do I finish my original expression which will then be the LHS of the new one?

It represents this through the concept of stickiness, with onesimple rule - the subexpression always sticks to the operator that's more sticky.

This is both quite easy to imagine, and easy to encode, as stickiness is just a number.

I think a simpler most straightforward notation that incorporates precedence would be better.

[−] aleph_minus_one 29d ago

> I think a simpler most straightforward notation that incorporates precedence would be better.

For example YACC provides a way to specify how a shift/reduce conflict

> https://www.gnu.org/software/bison/manual/html_node/Shift_00...

and a reduce/reduce conflict

> https://www.gnu.org/software/bison/manual/html_node/Reduce_0...

should be resolved; see also

> https://www.gnu.org/software/bison/manual/html_node/Mysterio...

This is actually a way to specify operator precedence in a much simpler and more straightforward way than via nested recursive expressions.

[−] microtherion 29d ago
I would argue the opposite: Being describable in BNF is exactly the hallmark of sensible syntax in a language, and of a language easily amenable to recursive descent parsing. Wirth routinely published (E)BNF for the languages he designed.
[−] marcosdumay 30d ago
The problem with recursive descent parsers is that they don't restrict you into using simple grammars.

But then, pushing regular languages theory into the curriculum, just to rush over it so you can use them for parsing is way worse.

[−] aleph_minus_one 29d ago

> But then, pushing regular languages theory into the curriculum, just to rush over it so you can use them for parsing is way worse.

At least in the typical curriculum of German universities, the students already know the whole theory of regular languages from their Theoretical Computer Science lectures quite well, thus in a compiler lecture, the lecturer can indeed rush over this topic because it is just a repetition.

[−] jjtheblunt 29d ago
(Same for US universities at least 30ish years ago)
[−] wglb 30d ago
When I was professionally writing a compiler professionally (see https://ciex-software.com/intro-to-compilers.html) the Dragon book was the second book that I read. I found it very helpful. That was the first Dragon book. I got the second one later. I would have been ok to start with the Dragon book--the Compiler Generator book was a harder study.
[−] tovej 30d ago
I started with the dragon book, and I found it to be a good introductory text.

A lot of people say the dragon book is difficult, so I suppose there must be something there. But I don't see what it is, I thought it was quite accessible.

I'm curious, what parts/aspects of the dragon book make it difficult to start with?

[−] hmry 30d ago
It's been a few years since I worked with the dragon book, but I think the most common complaint was that it starts with like 350 pages on parser theory: generating bottom-up and top-down parsers from context free grammars, optimizing lexers for systems that don't have enough RAM to store an entire source file, etc... before ever getting to what most people who want to write a compiler care about (implementing type inference, optimizing intermediate representations, generating assembly code). Of course parsing is important, and very interesting to some. But there's a reason most modern resources skip over all of that and just make the reader write a recursive descent parser.
[−] ablob 30d ago
I guess "back in the day" you had to be able to write an efficient parser, as no parser generators existed. If you couldn't implement whatever you wanted due to memory shortage at the parser level, then obviously it's gonna be a huge topic. Even now I believe it is good to know about this - if only to avoid pitfalls in your own grammar.

I repeatedly skip parts that are not important to me when reading books like this. I grabbed a book about embedded design and skipped about half of it, which was bus protocols, as I knew I wouldn't need it. There is no need to read the dragon book from front to back.

  > But there's a reason most modern resources skip over all of that and just make the reader write a recursive descent parser.
Unless the reason is explicitly stated there is no way to verify it's any good. There's a reason people use AI to write do their homework - it just doesn't mean it's a good one. I can think of plenty arguments for why you wouldn't look into the pros and cons of different parsing strategies in an introduction to compilers, "everyone is(or isn't) doing it" does not belong to them. In the end, it has to be written down somewhere, and if no other book is doing it for whatever reason, then the dragon book it shall be. You can always recommend skipping that part if someone asks about what book to use.
[−] torginus 29d ago
The thing about parsing (and algorithms in general) is that it can be hair raisingly complex for arbitrary grammars, but in practice, people have recently discovered, that making simple, unambiguous grammars, and avoiding problems, like context dependent parsing, make the parsing problem trival.

Accepting such constraints is quite practical, and lead to little to no loss of power.

In fact, most modern languages are designed with little to no necessary backtracking and simple parsing, Go and Rust being noteworthy examples.

[−] aleph_minus_one 29d ago

> In fact, most modern languages are designed with little to no necessary backtracking and simple parsing, Go and Rust being noteworthy examples.

But to understand how to generate grammars for languages that are easy to parse, you have in my opinion to dive quite deeply into parsing theory to understand which subtle aspects make parsing complicated.

[−] torginus 28d ago
My personal context to understand where I'm coming from - I'm working on my own language, which is a curly-brace C-style language with quite, where I didn't try to stray too far from established norms, and the syntax is not that fancy (at least not in that regard). I also want my language to look familiar to most programmers, so I'm deliberately sticking close to established norms.

I'm thankfully past the parsing stage and so far I haven't really encountered much issues with ambiguity, but when I did, I was able to fix them.

Also in certain cases I'm quite liberal with allowing omission of parentheses and other such control tokens, which I know leads to some cases where either the code is ambiguous (as in there's no strictly defined way the compiler is supposed to interpret it) or valid code fails to parse,

So far I have not tackled this issue, as it can always be fixed by the programmer manually adding back those parens for example. I know this is not up to professional standards, but I like the cleanliness of the syntax and simplicity of the compiler, and the issue is always fixable for me later. So this is a firm TODO for me.

Additionally I have some features planned that would crowd up the syntax space in a way that I think would probably need some academic chops to fix, but I'm kinda holding off on those, as they are not central to the main gimmickTM and I want release this thing in a reasonable timeframe.

I don't really have much of a formal education in this, other than reading a few tutorials and looking through a few implementations.

Btw, besides just parsing, there are other concerns in modern languages, such as IDE support, files should be parseable independently etc., error recovery, readable errors, autocomplete hints, which I'm not sure are addressed in depth in the dragon book. These features I do want.

My two cents is that for a simple modern language, you can get quite far with zero semantic model, while with stuff like C++ (with macros), my brain would boil at the thought of having to write a decent IDE backend.

[−] markus_zhang 30d ago
I actually think the parsing part is more important for laymen. Like, there may be a total of 10K programmers who are interested in learning compiler theories, but maybe 100 of them are ever going to write the backend -- the rest of them are stuck with either toy languages, or use parsing to help with their job. Parsing is definitely more useful for most of us who are not smart enough :D
[−] hmry 29d ago
Yeah I agree, that seems vey true. Although the average person probably also benefits more from learning about recursive descent and pratt parsing than LL(k) parser generators, automata, and finding first and follow sets :)
[−] saidnooneever 30d ago
the dragon book is how to write a production grade thing i guess. it has all the interesting concepts very elaborated on which is great but it dives quickly into things that can clutter a project if its just for fun..
[−] emigre 30d ago
It’s academic and comprehensive, that’s the issue. It’s not about writing a production grade compiler, though, in my humble opinion. There are more things to learn for that, unfortunately… is just a pretty big topic with lots of stuff to learn.
[−] saidnooneever 29d ago
the dragon book is all i have on the topic. it was a big investment for me.

it taught me to think very differently but i am sure i am still not ready to write a compiler :D

[−] emigre 19d ago
'Engineering a Compiler' by Cooper & Torczon is a great resource. It gives context on each topic, explains them clearly, and at the same time, I think, it is a really good resource from an academic point of view. It still requires some investment in terms of time and effort, but I found it more approachable than the Dragon book. I would say that both cover more or less the same introductory stage to compilers construction. If you have read the Dragon book, I have the feeling that you are perfectly well prepared to write your own compiler.
[−] keyle 29d ago

     The dragon book almost convinced me never to try to write a compiler.
That was the point. That's why it's not a cute beaver on the cover :)
[−] Findecanor 30d ago
The "Dragon Book" is big on parsing but I wouldn't recommend it if you want to make many optimisation passes or a back-end.

The first edition was my first CS textbook, back in the '90s and as a young programmer I learned a lot from it. A couple years ago, I started on a modern compiler back-end however, and found that I needed to update my knowledge with quite a lot.

The 2nd ed covers data-flow analysis, which is very important. However, modern compilers (GCC, LLVM, Cranelift, ...) are built around an intermediate representation in Static Single Assignment-form. The 2nd ed. has only a single page about SSA and you'd need to also learn a lot of theory about its properties to actually use it properly.

[−] aldousd666 30d ago
Parsing is the front end to a compiler. Can't get semantics without first recognizing syntax. I have a hard time thinking about programming languages without seeing them as a parsing exercise first, every time.
[−] gf000 30d ago
The recommended advice is to start with semantics first. Syntax will change, there is not much point fixing it down too early.

Most of the work is actually the backend, and people sort of illusion themselves into "creating a language" just because they have an AST.

[−] thefaux 30d ago
Syntax and semantics are never orthogonal and you always need syntax so it must be considered from the start. Any reasonable syntax will quickly become much more pleasant to generate an ast or ir than, say, manually building these objects in the host language of the compiler which is what the semantics first crowd seem to propose.

It also is only the case that most of the work is the backend for some compilers, though of course all of this depends on how backend is defined. Is backend just codegen or is it all of the analysis between parsing and codegen? If you target a high level language, which is very appropriate for one's first few compilers, the backend can be quite simple. At the simplest, no ast is even necessary and the compiler can just mechanically translate one syntax into another in a single pass.

[−] ablob 30d ago
I think his point is that "form follows function". If you know what kind of semantics you're going to have, you can use that to construct a syntax that lends itself to using it properly.
[−] bsder 29d ago

> The recommended advice is to start with semantics first. Syntax will change, there is not much point fixing it down too early.

It's actually the reverse, in my opinion. Semantics can change much more easily than syntax. You can see this in that small changes in syntax can cause massive changes in a recursive-descent parser while the semantics can change from pass-by-reference to pass-by-value and make it barely budge.

There is a reason practically every modern language has adopted syntax sigils like (choosing Zig):

    pub fn is_list(arg: arg_t, len: ui_t) bool {
This allows the identification of the various parts and types without referencing or compiling the universe. That's super important and something that must be baked in the syntax at the start or there is nothing you can do about it.
[−] samus 30d ago
Getting an overview of parsing theory is mainly useful to avoid making ambiguous or otherwise hard to parse grammars. Usually one can't go too wrong with a hand-written recursive descent parser, and most general-purpose language are so complicated that parser generator can't really handle them. Anyway the really interesting parts of compiling happen in the backend.

Another alternative is basing the language on S-expressions, for which a parser is extremely simple to write.

[−] antiquark 30d ago
There is still hope for a compiler book. From Knuth's website:

> And after Volumes 1--5 are done, God willing, I plan to publish Volume 6 (the theory of context-free languages) and Volume 7 (Compiler techniques), but only if the things I want to say about those topics are still relevant and still haven't been said.

https://www-cs-faculty.stanford.edu/~knuth/taocp.html

[−] jcranmer 30d ago
I don't think there is hope if you look at actuarial tables and Knuth's age. It's not clear to me if he'll be able to finish volume 4. The outline he has seems to have enough material to fill volumes 4C-4G to my eyes, and he isn't exactly cranking out the volumes.

Admittedly, volumes 5-7 wouldn't be as massive as volume 4 (it sort of turns out that almost all interesting algorithms ends up being categorized as being in volume 4), so you probably wouldn't have a half-dozen subvolumes per topic but, it's still too many books down the line, especially if he plans to revise volumes 1-3 before working on anything else.

[−] kibwen 29d ago
Have no fear, we'll just train an LLM on TAOCP and have it automatically generate the remaining volumes‽
[−] gdwatson 30d ago
I hope that God is indeed willing, but the man is 88 years old and he’s not done with the third tome of volume four. It would require a minor miracle for him to finish volume 7 within this lifetime.
[−] mghackerlady 30d ago
I really hope he ends up completing the whole series. I started volume one recently and it is excellent
[−] Hendrikto 30d ago
[−] znpy 30d ago
i found the same file but that's only 44 pages long ?

This ( https://github.com/tpn/pdfs/blob/master/Compiler%20Construct... ) seems to be a previous version (2005) and it's 131 pages long

[−] guenthert 30d ago
You'll want part 2 as well for a total of 107 pages.
[−] LoganDark 30d ago
I'd never seen Knuth's middle name until your comment. I think it safely could be left out of an article.
[−] soegaard 30d ago
An Incremental Approach to Compiler Construction

Abdulaziz Ghuloum

http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf

Abstract

Compilers are perceived to be magical artifacts, carefully crafted by the wizards, and unfathomable by the mere mortals. Books on compilers are better described as wizard-talk: written by and for a clique of all-knowing practitioners. Real-life compilers are too complex to serve as an educational tool. And the gap between real-life compilers and the educational toy compilers is too wide. The novice compiler writer stands puzzled facing an impenetrable barrier, “better write an interpreter instead.”

The goal of this paper is to break that barrier. We show that building a compiler can be as easy as building an interpreter. The compiler we construct accepts a large subset of the Scheme programming language and produces assembly code for the Intel-x86 architecture, the dominant architecture of personal computing. The development of the compiler is broken into many small incremental steps. Every step yields a fully working compiler for a progressively expanding subset of Scheme. Every compiler step produces real assembly code that can be assembled then executed directly by the hardware. We assume that the reader is familiar with the basic computer architecture: its components and execution model. Detailed knowledge of the Intel-x86 architecture is not required.

The development of the compiler is described in detail in an extended tutorial. Supporting material for the tutorial such as an automated testing facility coupled with a comprehensive test suite are provided with the tutorial. It is our hope that current and future implementors of Scheme find in this paper the motivation for developing high-performance compilers and the means for achieving that goal.

[−] asibahi 30d ago
Inspired by Ghuloum's book is this really nice book by Nora Sandler: Writing a C Compiler https://norasandler.com/book/
[−] agency 30d ago
Another recent book inspired by Ghuloum is Essentials of Compilation: An Incremental Approach (which publishes Python and Racket versions) https://github.com/IUCompilerCourse/Essentials-of-Compilatio...
[−] will_byrd 29d ago
Nada Amin has a nice implementation of Aziz's approach, with tests:

https://github.com/namin/inc

[−] sph 29d ago
One of the greatest papers in computer science. So dense in its 11 pages, yet very approachable.
[−] morphle 30d ago
Compiler writing has progressed a lot. Notably in meta compilers [1] written in a few hundred lines of code and adaptive compilation [3] and just in time compilers. Alan Kay's research group VPRi tackled the problems of complexity (in writing compilers) [4].

[1] Ometa https://tinlizzie.org/VPRIPapers/tr2007003_ometa.pdf

[2] Other ometa papers https://tinlizzie.org/IA/index.php/Papers_from_Viewpoints_Re...

[3] Adaptive compilation https://youtu.be/CfYnzVxdwZE?t=4575

the PhD thesis https://www.researchgate.net/publication/309254446_Adaptive_...

[4] Is it really "Complex"? Or did we just make it "Complicated"? Alan Kay https://youtu.be/ubaX1Smg6pY?t=3605

[−] armchairhacker 30d ago
Nowadays I’ve heard recommended Crafting Interpreters. (https://craftinginterpreters.com)

The Nanopass paper link doesn’t work.

[−] gobdovan 30d ago
Compilers are broad enough that when someone recommends a "compiler book", it's rarely exactly the slice you wanted.

So this made me do a runnable cheat sheet for Crafting Interpreters. I keep parsing demonstrative, and the AST is a little more Lisp-y than the book's.

Disclaimer: it's meant to convey the essence of what you'll learn, it is NOT by any means a replacement for the book. I'd also describe the book as more of an experience (including some things Nystrom clearly enjoyed, like the visitor pattern) than a compilers manual. If anyone's interested, I can do a separate visitor-pattern cheat sheet too, also in Python.

I turned it into a 'public-facing artifact' from private scripts with an AI agent.

[0] https://ouatu.ro/blog/crafting-interpreters-cheat-sheet/

[−] orthoxerox 30d ago
Crafting Interpreters is great, I wish it had a companion book that covered:

  - types and typing
  - optimization passes
  - object files, executables, libraries and linking
Then two of them would be sufficient for writing a compiler.
[−] raxxorraxor 29d ago
Many languages like Pancake Stack are looking for efficient interpreters:

https://esolangs.org/wiki/Pancake_Stack

:)

[−] stupefy 30d ago
One nice piece of advice that I received is that books are like RAMs, you do not have to go through them sequentially, but can do random access to the parts of it you need. With this in mind I find it doable to get one the thick books and only read the part that I need for my task.

But, to also be fair, the above random access method does not work when you don't know what you don't know. So I understand why having a light, but good introduction to the topic is important, and I believe that's what the author is pointing out.

[−] omcnoe 30d ago
Been working on a toy compiler for fun recently.

I have ignored all the stuff about parsing theory, parser generators, custom DSL's, formal grammers etc. and instead have just been using the wonderful Megaparsec parser combinator library. I can easily follow the parsing logic, it's unambiguous (only one successful parse is possible, even if it might not be what you intended), it's easy to compose and re-use parser functions (was particularly helpful for whitespace sensitive parsing/line-fold handling), and it removes the tedious lexer/parser split you get with traditional parsing approaches.

[−] GCUMstlyHarmls 30d ago
Nanopass paper seems to be dead but can be found here at least https://stanleymiracle.github.io/blogs/compiler/docs/extra/n...
[−] WalterBright 30d ago
What taught me how to write a compiler was the BYTE magazine 1978-08 .. 09 issues which had a listing for a Tiny Pascal compiler. Reading the listing was magical.

What taught me how to write an optimizer was a Stanford summer course taught by Ullman and Hennessy.

The code generator was my own concoction, and is apparently quite unlike any other one out there!

I have the Dragon Book, but have never actually read it. So sue me.

[−] blueybingo 30d ago
the article's framing around nanopass is undersold: the real insight isn't the number of passes but that each pass has an explicit input and output language, which forces you to think about what invariants hold at each stage. that discipline alone catches a suprising number of bugs before you even run the compiler. crenshaw is fantastic but this structural thinking is what separates toy compilers from ones you can actaully extend later.
[−] itsmemattchung 30d ago
It's been about 4 years since I took a compilers course (from OMSCS, graduate program) and still shutter ... it was, hands down, the most difficult (yet rewarding) classes I've taken.
[−] georgehm 30d ago
I have fond memories of implementing an optimizing compiler for the CS241 compiler course offered back then by Prof Michael Franz who was a student of Niklaus Wirth, probably the most exhilarating course during my time at UC Irvine. This was in 2009 so my memory is vague but I recall he provided a virtual machine for a simple architecture called DLX and the compiler was to generate byte code for it.

Google search points me to https://github.com/cesarghali/PL241-Compiler/blob/master/DLX... for a description of the architecture and possibly https://bernsteinbear.com/assets/img/linear-scan-ra-context-... for the register allocation algorithm

[−] msla 30d ago
fanf2 on Dec 25, 2015 [dead] | parent | prev | next [–]

I quite like "understanding and writing compilers" by Richard Bornat - written in the 1970s using BCPL as the implementation language, so rather old-fashioned, but it gives a friendly gentle overview of how to do it, without excessive quantities of parsing theory.

[−] knuckleheadsmif 25d ago
All these years I thought The Dragon Book referred to the “Green” Dragon book (which was the text I used in my compiler class in the very early 80s) only to Just realize that when folks refer to “The Dragon Book” that it is the red/purple book by 2 of the same authors from ‘84.

I remember that I ignored most of the stuff when I did my compiler from the book I used and did a recursive descent parser which the book really, if I remember correctly, did not cover except maybe for a brief mention. The instructor was also pushing everyone to use lex/yacc and I thought that was not the right way to go if you wanted to actually understand things. Interesting most of the folks that used lex/yacc had trouble completing the assignment whereas I thought it was straightforward.

[−] anthk 30d ago
https://t3x.org has literal books on that, from a simple C compiler to Scheme (you might heard of s9) and T3X0 itself which can run under Unix, Windows, DOS, CP/M and whatnot.

PD: Klong's intro to statisticks, even if the compiler looks like a joke, it isn't. It can be damn useful. Far easier than Excel. And it comes with a command to output a PS file with your chart being embedded.

https://t3x.org/klong/

Intro to statistics with Klong

https://t3x.org/klong/klong-intro.txt.html

https://t3x.org/klong/klong-ref.txt.html

On S9, well, it has Unix, Curses, sockets and so on support with an easy API. So it's damn easy to write something if you know Scheme/Ncurses and try stuff in seconds. You can complete the "Concrete Abstractions" book with it, and just adapt the graphic functions to create the (frame) one for SICP (and a few more).

And as we are doing compilers... with SICP you create from some simulator to some Scheme interpreter in itself.

[−] khat 30d ago
The biggest issue with technical books is they spend the first 1-2 chapters vaguely describing some area and then follow up with but that's for a later more advanced discussion or we'll cover that in that last 1-2 chapters. Don't vaguely tell me about something you're not gonna go into detail about, because now all I'm thinking about reading the subsequent chapters is all the questions I have about that topic.
[−] LiamPowell 30d ago
See also, Andy Keep's dissertation [1] and his talk at Clojure/Conj 2013 [2].

I think that the nanopass architecture is especially well suited for compilers implemented by LLMs as they're excellent at performing small and well defined pieces of work. I'd love to see Anthropic try their C compiler experiment again but with a Nanopass framework to build on.

I've recently been looking in to adding Nanopass support to Langkit, which would allow for writing a Nanopass compiler in Ada, Java, Python, or a few other languages [3].

[1]: https://andykeep.com/pubs/dissertation.pdf

[2]: https://www.youtube.com/watch?v=Os7FE3J-U5Q

[3]: https://github.com/AdaCore/langkit/issues/668

[−] krtkush 30d ago
I wonder if it makes sense to do the nand2tetris course for an absolute beginner since it too has compiler creation in it.
[−] aldousd666 30d ago
I learned from the Dragon Book, decades ago. I already knew a lot of programming at that point, but I think most people writing compilers do. I'm curious if there really is an audience of people whose first introduction to programming is writing a compiler... I would think not, actually.
[−] petcat 30d ago
And Nystrom's book
[−] bradley13 30d ago
Maybe I'm missing the point of this article? Writing a simple compiler is not difficult. It's not something for beginners, but towards the end of a serious CS degree program it is absolutely do-able. Parsing, transforming into some lower-level representation, even optimizations - it's all fun really not that difficult. I still have my copy of the "Dragon Book", which is where I originally learned about this stuff.

In fact, inventing new programming languages and writing compilers for them used to be so much of a trend that people created YACC (Yet Another Compiler Compiler) to make it easier.

[−] fzeindl 30d ago
A similarly scoped book series is „AI game programming wisdom“, which contains a multitude of chapters that focus on diverse, individual algorithms that can be practically used in games for a variety of usecases.
[−] teleforce 28d ago
The latest edition (3rd Edition) of Introduction to Compiler Design by Mogensen has new topics on SSA form, garbage collection, polymorphism and translation of pattern matching [1].

[1] Introduction to Compiler Design:

https://link.springer.com/book/10.1007/978-3-031-46460-7

[−] mzs 30d ago
archive of forth translation of Crenshaw’s howto

https://web.archive.org/web/20190712115536/http://home.iae.n...

[−] rahen 30d ago
I'm also writing a compiler and CS6120 from Cornell has helped me a lot: https://www.cs.cornell.edu/courses/cs6120/2025fa/self-guided...
[−] ahaferburg 29d ago
I liked this series of lectures on Youtube.

Compilers - Alex Aiken | Stanford

https://www.youtube.com/playlist?list=PLEAYkSg4uSQ3yc_zf_f1G...

Mentioned in another comment, but with a different link.

[−] deterministic 29d ago
Whatever you do don't waste your time on the "Dragon" book.

Google "recursive descent parsing" and it will tell you everything you need to know about the front-end of a compiler.

Google "My First Language Frontend with LLVM" and it will teach you the other half.

[−] notnullorvoid 30d ago
I might be in the minority, but I think the best way to learn how to write a compiler is to try writing one without books or tutorials. Keep it very small in scope at first, small enough that you can scrap the entire implementation and rewrite in an afternoon or less.
[−] dgan 29d ago
If i had a euro for every time I started writing a compiler, and got lost in the parser weeds, i d have ... At least couple of euros
[−] cdcarter 30d ago
All you really need is a copy of "The Unix Programming Environment", where you can implement hoc in a couple hours.
[−] ape4 30d ago
Would a practical approach be parsing the source into clang's AST format. Then let it make the actual executable.
[−] aurohacker 30d ago
Any tips on reading material for code generation and scheduling for parallel engines?
[−] voidUpdate 30d ago
I've been having a look at the Crenshaw series, and it seems pretty good, but one thing that kinda annoys me is the baked-in line wrapping. Is there a way to unwrap the text so its not all in a small area on the left of my screen?
[−] zahlman 29d ago

> The best source for breaking this myth is Jack Crenshaw's series, Let's Build a Compiler!

Right, I've heard of that...

> , which started in 1988.

... Oh. Huh.

(Staring at the red dragon book on my bookshelf, which was my course textbook in the early 00s.)

[−] zionpi 30d ago
[dead]
[−] felishiagreen12 29d ago
[dead]
[−] baarse 30d ago
[dead]
[−] consomida 30d ago
[dead]
[−] blueybingo 29d ago
[dead]
[−] imrozim 30d ago
[dead]
[−] rdevilla 30d ago
[flagged]
[−] lateforwork 30d ago
These days there's an even easier way to learn to write a compiler. Just ask Claude to write a simple compiler. Here's a simple C compiler (under 1500 lines) written by Claude: https://github.com/Rajeev-K/c-compiler It can compile and run C programs for sorting and searching. The code is very readable and very easy to understand.