Crafting Interpreters

by: Robert Nystrom (0)

Despite using them every day, most software engineers know little about how programming languages are designed and implemented. For many, their only experience with that corner of computer science was a terrifying "compilers" class that they suffered through in undergrad and tried to blot from their memory as soon as they had scribbled their last NFA to DFA conversion on the final exam.

That fearsome reputation belies a field that is rich with useful techniques and not so difficult as some of its practitioners might have you believe. A better understanding of how programming languages are built will make you a stronger software engineer and teach you concepts and data structures you'll use the rest of your coding days. You might even have fun.

This book teaches you everything you need to know to implement a full-featured, efficient scripting language. Youā€™ll learn both high-level concepts around parsing and semantics and gritty details like bytecode representation and garbage collection. Your brain will light up with new ideas, and your hands will get dirty and calloused.

Starting from main(), you will build a language that features rich syntax, dynamic typing, garbage collection, lexical scope, first-class functions, closures, classes, and inheritance. All packed into a few thousand lines of clean, fast code that you thoroughly understand because you wrote each one yourself.

The Reviews

I have not yet purchased a printed copy (it's going on my Christmas list!), but I have worked through the electronic version a number of times, and I have nothing but positive things to say about it. The author has been developing this content for years through his website. I have read through the materials multiple times, and I frequently return to it for reference.The roadmap that Robert presents is quite intuitive. Each chapter leaves you with a new, usable feature. You can see the interpreter coming to life each step of the way. Lox, the language that you create if you follow along, is fairly feature rich as is, but you can also do the challenge exercises and tack on new features to make the language your own. I ended up going through the book once to follow along closely with Lox. Then I came back through a second time (speed reading this time!) to create my own language with the book as a general roadmap.Robert has done a fantastic job finding a balance between simplifying the content for newbies in the interpreter/compiler writing world and providing technical details and theory. I never felt over my head or lost in overly advanced concepts, and it never felt like I was building a toy language that was so simplified as to be useless.On top of providing an incredible resource in this book, Robert was also responsive to questions that I submitted via email regarding my own custom language. I asked for his thoughts on a few custom features that I was implementing. He took the time to write very insightful and thorough responses, far above and beyond what I expected from someone who does not know me. I attribute this to Robert's passion for sharing what he has learned over his career to bring others along in this rewarding field.

Iā€™ve read hundreds of technical books over the last 40 years. Crafting InĀ­terĀ­preters is an instant classic, and far more readable and fun than many of the classics.Nystrom covers a lot of ground in this book, building two very different inĀ­terĀ­preters for Lox, a small dynamic language of his own design. He takes us through every line of jlox, a Java-based tree-walk inĀ­terĀ­preter, and of clox, a bytecode virtual machine written in C.For the first imĀ­pleĀ­menĀ­taĀ­tion, jlox, he covers such topics as scanning, parsing exĀ­presĀ­sions with recursive descent, evaluating exĀ­presĀ­sions, control flow, functions and closures, classes, and inĀ­herĀ­iĀ­tance.Starting with an empty slate, Nystrom adds just enough code to implement the topic of each chapter, having a working albeit incomplete imĀ­pleĀ­menĀ­taĀ­tion of the inĀ­terĀ­preter by the end of the chapter. He adds new code as he goes, inserting an extra case into a switch here or writing a new function there, or replacing a few lines of an earlier imĀ­pleĀ­menĀ­taĀ­tion with something thatā€™s just been explained. Knuthā€™s Literate ProĀ­gramĀ­ming explains a finished imĀ­pleĀ­menĀ­taĀ­tion, broken into separate pieces for exposition. Nystromā€™s continual, ever-evolving exposition is slower to get to the point, but itā€™s excellent pedagogy. I would be remiss if I didnā€™t mention the hundreds of hand-drawn ilĀ­lusĀ­traĀ­tions, which add a quirky flavor to the tone of the book. He has a blog post on how he pulled this orĀ­gaĀ­niĀ­zaĀ­tion off and another on how he created a physical book from the text.clox is a very different second imĀ­pleĀ­menĀ­taĀ­tion of a Lox inĀ­terĀ­preter. Instead of a slow inĀ­terĀ­preter walking an abstract syntax tree, he develops a stack-based virtual machine, compiles Lox into bytecode, and interprets the bytecode. He covers theory and practical conĀ­sidĀ­erĀ­aĀ­tions for creating a bytecode virtual machine, makes use of Prattā€™s ā€œtop-down operator precedence parsingā€, and implements closures and classes in C. In jlox, he used Javaā€™s HashMap to manage idenĀ­tiĀ­fiers and relied on Javaā€™s garbage collection for memory management. For clox, he implements a hash table and a mark-and-sweep garbage collector. Although he has to cover similar topics (parsing, local variables, closures) each time, he finds a fresh perĀ­specĀ­tive for the second imĀ­pleĀ­menĀ­taĀ­tion.I read the entire book for free at https://craftingĀ­inĀ­terĀ­, but I liked it so much that Iā€™ve ordered a physical copy. In fact, I actually read much of the book on the website in 2020, but life intervened and I didnā€™t finish it, so this month, I read it again from the start.This book is not a textbook and you donā€™t get an exhaustive inĀ­troĀ­ducĀ­tion to building inĀ­terĀ­preters, much less compilers. In the final year of my Computer Science degree at Trinity College Dublin in 1986ā€“87, I studied the Dragon Book when the first edition was brand new. Crafting InĀ­terĀ­preters is a lot more fun than the Dragon Book.Highly recĀ­omĀ­mendĀ­ed!

This book has many code blocks, with colored segements to get special attention from readers.However, paper prints only show them with faded gray color so that it is quite difficult to read those colored codes despite authors intention is totally opposite. Besides that serious mistake, the cotents of the book is O.K. in my opinion, but the publisher should have checked out how their printed book will be shown to their readers.

Received a brand new copy of the book. Flipped through it and I am going to love it.

I'd like the technical book talk in detail, it provides such rich information. I don't think I can get them from anywhere else. It's definitely a great book by a great writer

One of the best tech books I've ever read. The author implements an interpreter for the same (made up) language, twice. Once in Java, and again, in a much more low-level way, in C.The writing is crisp and clear, and the examples are complete. (the full code is available on GitHub, and he walks you through everything)You'll get as much out of this book as you want. Highly recommend.

Nystrom's website is awesome. The paperback book is terrific. The content is amazing.The Kindle version may be the worst e-Book I've ever purchased.The book has many sidebar annotations. The Kindle version doesn't treat them as sidebars, though and embeds them directly in the text, sometimes in the middle of a sentence. This makes it incredibly difficult to read. I had hoped that it was just a formatting issue with my Kindle Oasis, but the PC & iPad versions have the same issue.Buy the book. It's a great book. But buy the version you can put up on your shelf.

I would highly recommend this book. In the past, I have read multiple books on compiler designs - but most of them focus too much on the academic part. While it is important to know about those fundamentals, practical modern compilers are not built that way. This book, on the other hand, provides a good practical implementation of a modern programming language that supports both OOP and functional programming.

This is a hard book to review because on the surface it's a fantastic feat of technical writing, but when you dig deeper it has fundamental flaws that ruin the experience.The first flaw is that the first 200ish pages are spent implementing a tree-walk interpreter in Java which results in a slow, unusable interpreter that is academic but not practical. I think he would have been better off cutting that out and dedicating fifty pages or so to explaining the high level overview of how one works to lay the foundation for the rest of the book.The second flaw is that he wrote the book such that every single line of code present in the implementation is also in the book, and everything is built up slowly jumping across multiple files and functions. The trouble with programming books is that the example code often has typos or errors because the author has to copy from their code editor, paste into a document without syntax highlighting, edit for formatting, and then update the book code later if they make changes. Nystrom got around this issue by writing his own tools that automatically keep the book text and the source code in sync (Donald Knuth called it Literate Programming, and the book Physically Based Rendering does the same thing).In theory this sounds wonderful because it means you can follow along with him and know exactly where to insert every line of code that he demonstrates. But I instead found it hard to understand the big picture because my brain had to constantly shift from English explanations to code minutiae.That method of presentation means that it's hard to re-read bits for further understanding. Instead you've got to wade through mountains of code snippets with bits of explanation strewn throughout. It also means that he'll present a code snippet that references functions or variables that haven't been shown/explained yet. And it also means that he must sometimes insert temporary stub code only to replace the contents later.His system of keeping the code in sync is present in the book's GitHub repository, so exploring the code in its entirety means wading through comments that exist solely for his generation tools. I would have much preferred the traditional approach of verbose English explanations and diagrams with small code snippets that occasionally have errors in them, but supplemented with a code repository on GitHub with tags per chapter so you can see things build up over time.The third flaw is related to the second flaw in that the code is hard to understand. I think this is likely due to the fact that he had to write the code in such a way that it could be displayed in small chunks in the text. That results in the repeating pattern of multiple singletons that have their contents modified by functions that take no arguments but change the state of the program.He says in the text that he uses the singletons so that he can save on book space and not pass a variable around to different functions, which is fine in principle when done sparingly, but it makes it hard to learn from the code snippets because you see a call to "advance()" repeatedly and forget what exactly that function does. This is related to another issue I have with the style which is many small functions that have maybe one or two lines. Something like "advance()", for example, does nothing more than increment a pointer (which is inside of a singleton).My preference is for more explicit and verbose code as I find it more readable, but that goes against the sort of code required for a book where every single line of code is present in the text. He needed to create a bunch of tiny functions to save on space.In short, the book is a marvel of technical writing in that it is very readable and enjoyable. He writes well and his drawings are charming (though I could do with fewer marginal anecdotes personally). He explains things well when he's using English. But I think the book is difficult to understand and falls apart at the actual code level which is unfortunate.I would still recommend the book because it does more right than it does wrong.Note: The entire book is free online and Nystrom should be commended for that.

This book is a gem for those who wish to learn about and build compilers. It gets the balance between teaching you concepts and then showing you how to do it. It builds both the concepts and the code necessary step by step, and each step is easy to consume. Before you know it, you've already mastered and coded up what would have seem like a daunting task. The choice of teaching you to build the compiler twice, first in a higher level language to get you familiar with the end to end process, and the second exposes you to very advanced techniques in C such as byte code single pass generation and execution, garbage collection, efficient implementation of closures, etc., also dramatically simplified the overall learning experience.

Very useful, well written. The big respect to the author.

Crafting Interpreters offers some great reading for those who are looking for an introduction into the field of developing programming languages.

I own a dozen or so books on compilers and programming languages, and I have to say that this one is probably the easiest to read and understand. It's one of the very few books I have read cover to cover. The author has a great writing style, and I hope he writes more.The content of the book is available online for free, but I had to get a printed copy. I owe that to the author.

The formatting for the Kindle copy is problematic to such an extent that it makes the book difficult to read. All horizontal white space in the code samples was stripped out and the various asides are inserted into the body text without anything to demarcate that they are asides. This leaves the reader with the added task of identifying the non-sequiturs as asides and skipped ahead to where the body text resumes.The overall content is great, however. It is a very engaging read on a topic that the author is clearly expert and passionate about. For those for which this is their entry into this topic, the book serves as a great introduction that provides plenty of references should you want to learn more about the various concepts introduced by the book.

Some technical books on Amazon are shorter than advertised or simply substitutes for documentation. This book will take you through the how and why of implementing an interpreter for a scripting language in Java and C. Worth three times the price.

Iā€™d rate the content itself 5 stars. Itā€™s been a blast going through the book and writing code alongside it. However, a couple days after I started reading, I noticed several pages in the front (the table of contents up to the beginning of Chapter 1) arenā€™t fully attached to the binding of the book. Iā€™d guess this happened during printing because some of those pages are printed at an angle, but I canā€™t be 100% sure.The rest of the bookā€™s print quality so far has been good.

Crafting Interpreters
ā­ 4.8 šŸ’› 423
kindle: $27.99
paperback: $41.86
Buy the Book