top | item 31835818

Crafting Interpreters: A Review

461 points| chidiw | 3 years ago |chidiwilliams.com

153 comments

order

bow_|3 years ago

I hesitated a little bit before deciding to purchase and go through the book. Had already read many glowing reviews then, but was not sure if the choice of language (Java) for the first half of the book would be a hindrance (it's not the stack I am most familiar with).

Decided to buy it anyway, primarily because the two implementations of the toy language piqued my interest (most other books only go through one implementation).

Boy was I glad I did.

Writing is superb, end-of-chapter challenges are instructive, code samples are meticulously and beautifully done. Here and there I also encountered references to other languages (e.g. had a brief detour to Lua internals [1], thanks to a side note), which I have enjoyed too.

Went through the first half twice: first using Java as it is written and then secondly implemented the interpreter in Rust (my initial fear turned out to be unfounded). Currently going through the second half.

So if you're reading this, thank you, Bob :).

[1] https://www.lua.org/doc/jucs05.pdf

Sakos|3 years ago

I wish people would be less opinionated about things like this. I've completed exercises for books or courses in C, C++, Lisp, Scheme, Go, Python, Javascript, Ruby, Rust, Java, Scala, Clojure, Prolog, Assembly, etc. It's not a big deal. Nobody is telling anybody they have to then program in that language for the rest of their lives. Be open to new experiences. Java really isn't difficult or alien or bad enough to complain that it's the wrong language and expecting book x to be in your preferred language z when there are dozens one could choose is so...closed-minded.

Waterluvian|3 years ago

I wrote the first one in Python without issue. I guess any sufficiently expressive language should be fairly easy to “adjust the recipe to” while still following the book.

e12e|3 years ago

> was not sure if the choice of language (Java)

Two books I enjoyed despite, rather than because of the language:

"Program Generators with XML and Java" (aka "program generators for fun and profit") https://www.amazon.com/Program-Generators-Java-Craig-Cleavel...

And:

"Designing Active Server Pages" https://www.amazon.com/Designing-Active-Server-Pages-Mitchel...

The latter mostly for techniques to keep programs structured even when writing mostly in template soup (like php, asp, coldfusion etc).

RyanHamilton|3 years ago

I fully agree. Superb, "meticulously and beautifully done". I read it online and followed along writing code then bought the book just to support the author. Thanks Bob.

danhau|3 years ago

Does the book go into more „modern“ parser architectures ala Roslyn Red Green Trees?

runevault|3 years ago

See to me Java was an advantage because it ensured I would do part 1 in Not Java (in my case c#). Mind you c# is great because it is similar enough you can follow along while also being different enough that you still are forced to engage and think. At some point I want to redo part 1 in F# to REALLY push myself (and also force me to better engage with F#).

heavyset_go|3 years ago

Any trouble with the Rust implementation? I wanted to give the book a go using Rust, but have been sitting on it because I don't want to get halfway through it only to find that Rust's intentional design and limitations might make it difficult to build an interpreter via the assumptions in the book.

rightbyte|3 years ago

> was not sure if the choice of language (Java) for the first half of the book would be a hindrance

I think it is refreshing he uses a "boring" language.

62951413|3 years ago

Using Java was a poor choice (especially without the latest improvements such as records and switch expressions):

* new books should use more modern popular languages to age well and to attract younger audience

* on the JVM there are languages better suited for the topic (e.g. Scala by a large margin)

* There's already the "Language Implementation Patterns" book (https://www.amazon.com/Language-Implementation-Patterns-Doma...) which not only is great for teaching because of its pattern-based approach but also gives you real life skills (e.g. ANTLR is used in most big data query engines)

Derbasti|3 years ago

Without a doubt one of the best technical books I have ever read.

To me, it was a missing piece of the big puzzle of "how do computers work". I read many a book to answer this question, and came away with three books:

- CODE by Charles Petzold explains the CPU

- Operating Systems: Three Easy Pieces by Arpaci-Dusseau explain OSes

- Crafting Interpreters by Robert Nystrom explains programming languages

Masterfully done.

shanusmagnus|3 years ago

These look great, and judging by other people's comments, are instances of a class of book I will poorly describe as "lovingly-crafted works of art on highly technical CS topics", a genre that I am always on the lookout for more examples of.

From my own experience, some examples might be:

- SICP, by Abelson and Sussman

- Paradigms of Artificial Intelligence Programming, by Norvig

By general acclaim, some examples might be:

- How to Design Programs, by Felleisen et al

- Beautiful Racket, by Butterick

- LISP In Small Pieces, by Queinnec

- The Art of Computer Programming, by Knuth

- The Elements of Computing Systems, by Nisan and Schocken

What else comes to mind? (My list is very LISPy, but that's just bc I have high awareness there.)

hnxs|3 years ago

FYI there is a second edition of CODE coming out at the end of August.

I will check out Three Easy Pieces! Thanks for the rec.

e19293001|3 years ago

Could you please consider this book?

Compiler Construction Using Java, JavaCC, and Yacc[0]

In my opinion, this has been underrated book. I learned a lot from this book. I enjoyed it and plan to read it again.

If you could check it out. It contains solid explanation from theory to implementation. I'm not affiliated with the author, I just wanted to show I'm grateful for his work.

[0] - https://onlinelibrary.wiley.com/doi/book/10.1002/97811181127...

vips7L|3 years ago

OSTEP is the best technical book I’ve ever read.

lalwanivikas|3 years ago

Exactly the comment I was looking for. Thanks for the OS book recommendation.

PS: you might want to reformat your list.

easterncalculus|3 years ago

I have started reading Introduction to Computer Organization by Bob Plantz, and I think it would also tell someone a lot about how computers work. It starts with hardware and builds into Assembly and eventually higher level concepts like OOP.

volkadav|3 years ago

Similar vein re: CPU, targeted at an undergrad level, you might enjoy _Computer Systems: A Programmer's Perspective_ by Bryant and O'Hallaron (https://csapp.cs.cmu.edu/) At least I found it a really well-written and integrative text coming from knowing C and wanting to learn the next level down well. Ties in with the OS bits on the "top side" too.

sg47|3 years ago

Any defining book for databases? I'm interested in going deeper into the query planner and indexes.

skrtskrt|3 years ago

Any defining book for networks?

hardwaregeek|3 years ago

I love this book! My only gripe is that I wish it was a whole series, no, a whole genre of books. We need more hands on, code first books that take you through 1-2 extended projects where you can type along and get working code at the end. These books should have CS theory, but still be fundamentally focused on implementation. It'd be really fun to have one of these for a more functional compiler (type checking, code generation, optimization), for a language service (query based compilation, concrete syntax trees, language server protocol), for databases, operating systems, and so on. Ideally they'd be a cross between an O'Reilly book and a SerenityOS stream

dunefox|3 years ago

Yeah, I'd love to read this book with a more functional language as target, and compilers as you mentioned.

alexobenauer|3 years ago

Truly one of the best books in our field; very well done.

Doing the tree-walk interpreter (the first “half” of the book) took two weekends, working much of the day on Saturdays and Sundays.

When I went through it, I built my interpreter in another language, and added other features as I went along. Both helped solidify the concepts. I often had to pull out my additional features as I learned better in subsequent chapters; getting to see the difference between my amateur approach versus the author’s experienced methods was quite helpful!

d23|3 years ago

This book is a work of art. It's one of the best technical books I've ever read. Truly a masterpiece along every dimension.

marai2|3 years ago

As the footer on the online version of the book says "Handcrafted by Robert Nystorm" - it really is lovingly hand crafted. It shows that the author went over the content many, many times polishing it with every go. The content builds up really well as it goes along.

On a minor note, whenever I read a technical book I always come across some mistake in the copy-editing, a missed article, a misplaced article, a typo. I'm 70% of the way in and haven't come across any! Which again speaks to the well-craftedness and hand-craftedness of the book!

Though I do feel the author ran out of steam with respect to the lovely, quirky artwork in the book. I'm disappointed Bob, I want my money back.

recursivedoubts|3 years ago

highly, highly recommend this book

when i was given a compilers class to teach i wanted to teach hand-written recursive descent because it is simple, elegant and makes the recursive nature of grammars so readily apparent without hiding concepts behind obscure tools

i was despairing that there were no good books around on recursive descent, but stumbled on crafting interpreters before it was even released as a book and immediately structured my class around it

absolute life saver and my students all love it

cannot say enough good things about it!

ufo|3 years ago

This is one thing I find funny about this book. Despite the name, it's a great intro to compilers in general.

munificent|3 years ago

Thank you for the excellent review!

Also, I'll second the comments here: The design of your blog is beautiful.

chidiw|3 years ago

You're welcome - and thank you!

chapliboy|3 years ago

The author says it took about 6 months to thoroughly go through the book and examples.

This is really useful in setting expectations.

thundergolfer|3 years ago

I got through all chapters in around 1.5 months, spending 50-60 hours[1]. I'd do solid 4-5 hour blocks on Saturdays and Sundays when I was most into it.

I'm following teachyourselfcs.com[2], so I'm hoping to solidify the concepts the book intro'd by reimplementing the bytecode interpeter in Rust (rlox) and following Alex Aiken's lectures online. OP appears to have reimplemented Lox in Golang (glox) for reinforcement.

So far the time has been well spent. Compilers are now much more 'just another program', and I have new conceptual tools for tackling software engineering problems.

1. https://github.com/thundergolfer/uni/tree/main/books/craftin...

2. https://teachyourselfcs.com/#languages

Noe2097|3 years ago

Following a slightly different path, "Modern compiler implementation" goes over several topics mentioned in the review (lexing, parsing, type checking, code optimization and generation, runtime environments aspects e.g. garbage collection).

There exists various flavors of the book (in ML, Java, and C).

wrp|3 years ago

The literature on interpreters (vs. compilers) is pretty thin. I enjoyed reading these:

1. Peter John Brown. 1979. Writing Interactive Compilers and Interpreters.

2. Timothy Budd. 1987. A Little Smalltalk.

3. Ralph E. Griswold. 2000. The Icon Programming Language, 3e.

4. Pat Shaughnessy. 2013. Ruby Under a Microscope: An Illustrated Guide to Ruby Internals.

This is a new one I haven't read yet, but it looks very practical:

5. Clinton L. Jeffery. 2021. Build Your Own Programming Language: A Programmer's Guide to Designing Compilers, Interpreters, and DSLs for Solving Modern Computing Problems.

I don't know anything about this one, but people have told me they prefer the first edition:

6. Ronald Mak. 1991. Writing Compilers and Interpreters: An Applied Approach.

collinvandyck76|3 years ago

This is the kind of book that really pays dividends into the future. Once you realize that lexing and parsing is not really that difficult, it makes a whole class of problems much easier.

pdpi|3 years ago

> it makes a whole class of problems much easier.

The other big realisation is just how freaking enormous that class of problems is.

hyperzhi|3 years ago

I've been wanting to read this book, but I'm not sure how I should go about it. Are you supposed to read and type out the code examples? I'm just wondering if I will learn anything that way?

MatheusRich|3 years ago

The way that worked better for me was doing the book in a different language (I did it in Crystal and Rust). I had to fully understand the intent being every code example so I could translate it correctly.

This adds another layer of difficulty, depending on how comfortable you are with Java and the language of your choice, but it was the way that worked better for me. I truly felt that I created a language, not just copied some code.

dgb23|3 years ago

Well yes, the code is pretty much there 1:1. However the juice is in the straight forward explanations and excellent writing.

I used the book by translating the code into my favorite programming languages and heavily played around with different approaches, which is way more time consuming but I don't think it is necessary at all.

I think if you are already a seasoned programmer you might find it even useful to just read the book and the code without doing much of coding at all. Given that you are good at understanding code without executing it.

lolinder|3 years ago

He does a great job of breaking down the code samples into chunks that are small enough to be meaningful. The longest chunk I remember seeing was around 10 lines. Each of those small blocks of code is surrounded by lots of very helpful and approachable explanatory text. The results was, at least for me, that I never felt like I was copying something I didn't understand.

jtolmar|3 years ago

I copy/pasted the code examples, reformatted them so they'd work in a different language (javascript), and refactored it to write my own language instead of Lox.

Then I did it again with a different destination language design, and had to reference the book less.

Whenever I end up writing an interpreter for a third time, I'll probably be able to do it without referencing Crafting Interpreters at all, if I had to. But I expect I'll still check the book before doing certain fiddly parts of the parser.

dragon96|3 years ago

I read the beginning sections of each chapter until I understand conceptually what the chapter implements by the end. (I needed more assistance for the initial chapters not knowing what the output of scanning or parsing would be, or where to start, but by the later chapters, it becomes pretty clear what the output of implementing say, classes, is).

Then I would try to implement it on my own without reading, having Chapter 3, the language specs, open in a different tab for reference. I chose Java because (1) I wasn't very familiar with it, and (2) I wanted the option to use their code snippets.

My goal is to pass all of the edge test cases the book mentions. So finally I would read the chapter and take some notes on the different design choices and test cases I missed. The reading is quite fast once you've already implemented because you know what to look for.

dilap|3 years ago

the approach i took was to read it for understanding, and then go back and see if i could implement what i had just learned, in a different language, without reference to book. (so implementation would use same strategy, but not be exactly the same.)

it worked well for me.

i think this book along w/ spivak's calculus is probably tied for position #1 in technical book that i found most enjoyable and had the biggest influence on me. cannot recommend it enough.

edit: i should mention if i got stuck i went back to reading the book! the point was, "read to understand, try to implement, if understanding not there, repeat". rather than just typing directly out.

KineticLensman|3 years ago

I read the chapters and looked at the samples. If there was a place where several followed a similar pattern then I’d only follow the first one from the book and then figure the others out myself (eg different types of statement or expression). There are also some exercises that don’t provide immediate answers which encourage you to think ahead.

slingnow|3 years ago

Talk about analysis paralysis. You won't read a book because you're not sure how the book _should_ be read?

scrapheap|3 years ago

I read it through but didn't type out the code, mainly as I know that I would get focused on the code rather than the concepts the book is teaching.

chidiw|3 years ago

Yes, I read and typed out all the code snippets by hand and it worked really well.

vertexmachina|3 years ago

I'm in the middle of this book at the moment and I have mixed feelings on it.

It's definitely well-written and you can feel the love and care that went into producing it. But I think it would have been stronger had Nystrom skipped the Java version and spent those pages on theory instead before jumping into the C implementation. While going through the Java stuff (implementing in C# instead because I have an emetic reaction to Java) I found myself just wanting to hurry up and get to the good stuff in C. And I found the visitor pattern and code generation stuff to be a distraction.

The code can also be a bit hard to follow at times because he jumps around from file to file and function to function. He'll present a function that calls a function that doesn't exist yet, and then define the new function, etc. He does it that way because he wanted to ensure that every single line of code was present in the book (he wrote a tool to ensure that was the case), and it certainly helps if you're trying to copy his code verbatim, but not so much for high-level understanding. Maybe that's a failing on my part.

Finally I wish he had gone into more detail on how one might implement a register-based VM because they are faster and more interesting (to me) than a stack-based one.

native_samples|3 years ago

The Java part is probably very lucky to have if he ever writes a second edition though. The reason is, for many dynamic languages the best way to make an interpreter fast is now to use the Truffle framework, not write a bytecode interpreter in C. Truffle changes the whole space around language interpreters so radically that it feels like it should definitely be worth a mention in any future take on the topic.

With Truffle you start with a Java based tree walking interpreter (could use Kotlin too for less boilerplate), so the very easiest stuff. Then you annotate it in some places, add a dependency on Truffle and ... that's it. Now you have a JIT compiler for your interpreted language. The next step is to start refining the use of the annotations and to write specializations in order to accelerate your JIT by incorporating standard techniques like polymorphic inline caches.

Finally you can compile your new interpreter+JIT engine down to a Graal native image (single standalone native binary), thus ensuring it can start as fast as an interpreter written in C and can warm up as fast as V8. The binary can also be used as a shared library.

Given that this tech exists now, people who choose to write their interpreter in Java will have a massive edge in performance over people walking the traditional path. It's still relatively new technology but it's hard to see how it doesn't change interpreter design forever.

Banana699|3 years ago

I sympathize with your criticisms about java because the language is... not my favorite. It would be helpful here to look at its choice as a result of Nystrom solving the intersection of multiple optimization problems:

- Manual Memory Management Is Hard. Interpreters are complex pieces of software, you don't need another rabbit hole to dive into while you're learning your first parser. You don't need to agonize over where to put the contents of the file buffer you're parsing before writing your first lexing switch. People spend years with C and C++ and still get MMM wrong. The book is supposed to be fun.

- Data Structures Are Hard. This doesn't apply to C++ or really any modern language, but since you wanted it done in C the first time, that would entail the obligatory "Implement your own universe from scratch" exercise C is infamous for. I don't mind, I always like implementing my own universes (although I despise C even more than Java, it can't be over-emphasized how badly engineered that language is). But again, Pedagogy says that you should introduce the minimum possible surface area while approaching a new topic, ideally a single topic at a time.

- Interpreters Should Be Fast, so overly dynamic languages like python and javascript are out.

- A teaching language should be popular and familiar. The obvious benefit is accessibility to as many learners as possible, but a less obvious one is the availability of tools and cross platform support.

Out of the vast array of available programming languages and their toolchains, the combination of GC, powerful standard library and reasonable performance excludes a whole lot. The popularity requirement basically only leaves Java, C# and Golang standing.

b3morales|3 years ago

Eh, you don't have to use Java for the first part. I didn't, and I've seen many other people in discussions of the book say they didn't either. It's readable enough, and the explanations are clear enough, that you can follow along in any other memory-managed language that you're comfortable with.

shaftoe444|3 years ago

This is an excellent book. I got so much out the first half I keep building little toy interpreted languages and haven't got round to the second half yet!

dingosity|3 years ago

This is the type of content I come here for. I also enjoyed the "other posts" listed at the bottom of this article. Well done, Chidi!

nphard85|3 years ago

Just completed part 1 (used Python instead of Java to spice it up a bit) and it was a great experience! Looking forward to working through part 2, but it will be a while before I can get around to it. I hope more books follow this engaging writing pattern (when it makes sense). Kudos to Bob Nystrom for this great book.

cpard|3 years ago

And it's so much fun to go through. Most CS books are usually very dry, this one is such a fun read. I would encourage everyone to go and read at least the web version of the book, I'm sure that you will end up buying it at the end.

respondo2134|3 years ago

Nand2Tetris is another fun one IMO, you can do a chapter a week (an hour of reading and a couple of implementation sessions) and be done in a few months

geekraver|3 years ago

If you’ve never read it, a fun article on Bob from some years back.

https://www.wired.com/2013/01/comedy-in-the-hacker-world/amp

I was lucky enough to be working on the Dart team in Seattle at the time with Bob, who is one of the smartest, funniest and nicest people I have ever met (although the whole team was an exceptional bunch).

mhh__|3 years ago

I think this book is ever so slightly overrated when it comes to actually implementing programming languages, but it contains such a nice introduction to parsing that I have for years been using it as my goto "You're not ready yet, read this" book when it comes go guiding people going down the dark unhappy path of working on language implementations.

jesselawson|3 years ago

PLT and implementing languages is something that's always fascinated me, so I enjoy hearing everyone's experiences in this domain. What makes you call it a "dark unhappy path"?

twh270|3 years ago

Has anyone worked their way through both this and Beautiful Racket? I'd love to get a comparison of your experience with both.

EamonnMR|3 years ago

I keep meaning to pick this back up. It was my go-to project during air travel and train travel for a while, but I stopped going in to the office so much and command line only projects have trouble competing for mind space with visual stuff, at least for me.

MarquesMa|3 years ago

This is my favorite book!

But I think it could be more straight forward if the first interpreter didn't use Java, but a modern main stream language that can avoid code generation and Visitor Pattern. Those parts feels like unnecessary detour.

munificent|3 years ago

> but a modern main stream language that can avoid code generation and Visitor Pattern.

Yeah, a language with nice algebraic datatypes and pattern matching does eliminate the need for Visitor patterns.

But the reality is that many programmers are working in object-oriented languages where the Visitor pattern is useful, so I deliberately picked Java in order to teach that approach. The book tries to meet you where you are and not where you might wish you were.

xbar|3 years ago

Thanks Chidi for a great review and a blog with a lovely design.

ParallelThread|3 years ago

What other books are of the same caliber that HN would recommend?

323|3 years ago

The programming meme books that you must have on your shelf (reading optional) are:

- The C Programming Language (original C book)

- The Art of Computer Programming (TAOCP)

- Structure and Interpretation of Computer Programs (SICP)

- Compilers: Principles, Techniques, and Tools (Dragon book)

- Don't Make Me Think (design)

- Zen and the Art of Motorcycle Maintenance

criddell|3 years ago

The author, Bob Nystrom, also wrote Game Programming Patterns which I think is just as good.

You might also like Charles Petzold's book Code.

vertexmachina|3 years ago

CODE: The Hidden Language of Computer Hardware and Software - Charles Petzold

The Elements of Computing Systems - Noam Nisan, Shimon Schocken

Operating Systems: Three Easy Pieces - Remzi H. Arpaci-Dusseau, Andrea C. Arpaci-Dusseau

paulhodge|3 years ago

The Nature of Code - Daniel Shiffman

alfiedotwtf|3 years ago

If by caliber you mean blow your socks off, The Little Schemer

twh270|3 years ago

I recommend "The Pragmatic Programmer" and "Code Complete" regularly. (2nd editions.)

icholy|3 years ago

I'm glad to see this book get the attention it deserves.

mengibar10|3 years ago

Beautiful typeface, well organized and very appealing to eyes.

sydthrowaway|3 years ago

Java is such a god damn turn off.

EamonnMR|3 years ago

I think of it as an excuse to implement it in a language you actually like. Makes it more interesting and keeps you honest (since you can't copy pasta your way through it.)