Ask YC: Good compiler books?
Background: CS/EE undergrad... love studying programming languages... eager to study & write a compiler...
I'm going to take a compilers class next semester, but researched the required book on amazon: http://www.amazon.com/Modern-Compiler-Implementation-Andrew-Appel/dp/052182060X
Doesn't look too promising...
Any suggestions for good compiler books or related books?
24 comments
[ 5.3 ms ] story [ 141 ms ] threadI'm really interested in Python, Ruby, JavaScript... Not looking to write a massive, enterprise-scale compiler/interpreter/parser/whatever in C, C++, or Java... possibly Java... but you get the point...
I've been checking out projects like Mozilla Rhino, Pypy, and keeping up to date with ECMAScript 4 progress... just want a solid book or two or more... :)
http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniqu...
It's been updated to cover parallelization and other topics that were considered specialties when it was originally written. I have an older copy and am now wondering whether it mightn't be fun to trade up. We'll see.
Have you played with ANTLR? That's a neat tool. The Ragel state machine compiler is a fun toy, as well.
But that may be too massive for what you are looking for. I found that Crockfords essay http://javascript.crockford.com/tdop/tdop.html really helps if you know javascript. And reading something like tinypy http://www.philhassey.com/blog/2008/02/28/tinypy-10-mit-lice... may help.
Also not necessarily a compiler book, but The Little Schemer does have you building a basic scheme interpreter at the end of the book.
I do wish they covered optimization in more depth, though.
http://www.amazon.com/exec/obidos/ASIN/0805316701
This is a good tour of front-end and back-end stuff, a decent intro to code gen, and is built on a hand-written recursive-descent C parser. That it's specific to one C compiler made it better to read straight through, but I rarely refer back to it.
So far this year, I've been liking Muchnick:
http://www.amazon.com/exec/obidos/ASIN/1558603204
This is pretty heavy on the backend and codegen, which is where my head is at these days.
Compilers is a big topic. What part interests you most? Parsing? Optimization? Semantics and error detection? Code generation?
The Dragon Book is not the best book these days, it focuses too much on stuff you won't care about and not enough on the stuff you do care about. (e.g. it focuses a lot on parser generators and says almost nothing about analysis and optimization)
EDIT: A good followup text is "Advanced Compiler Design and Implementation" by Muchnick
I'm not even that interested in actually making one, but the way they work is fascinating, and this book was excellent, and I kept it after the class was over.
2. The definitive ANTLR reference http://www.pragprog.com/titles/tpantlr
1. gives you a good introduction in how to write parsers from scratch (without lex/yacc'ish parsing frameworks), and is probably a good warm-up before the book you mention.
2. gives you an introduction to state-of-the parsing with a framework (antlr) + a some about compilation. Note: antlr also has a nice IDE for rapid developing/prototyping of parsers - antlrworks. See http://antlr.org for more info.
A very nice parsing framework for Python is dparser. It allows you to write grammars as docstrings to methods, which makes it very easy to try out things http://www.ibm.com/developerworks/linux/library/l-cpdpars.ht... http://dparser.sourceforge.net/
Definite Clause Grammars for Prolog is also worth a look (at least for reference)
at least at stanford, you will end up writing a small compiler for a toy java like language. kind of nice. from there you can go onto more mature stuff e.g. llvm.
edit: it would be even more fun, if you could take the tests too (within stipulated times ofcourse).
And if you want to be more pragmatic, here is a shorter one: http://www.cl.cam.ac.uk/teaching/2004/CompConstr/NEJ/report....
The dragon book is a MUST for compiler researcher and people who are serious on compiler, but it is hard to follow, and I do not recommend it as your first compiler book.
For ruby, you can take a look of the xruby compiler I did: http://xruby.googlecode.com (google code's server has some problem now, you may have to wait for a while)
Then proceed to Dragonbook.
After finishing Dragonbook, you might like this Linkers and loaders (http://www.amazon.com/Linkers-Kaufmann-Software-Engineering-...)
In general follow this guy Jeff Ullman and you would be good(http://infolab.stanford.edu/~ullman/)
http://www.diku.dk/~torbenm/Basics/
1. Understanding how a compiler works without getting bogged down by programming language details - This means that you should try and look at compilers written in Standard ML or Ocaml (my favorite), since that would be much easier to follow. For example -- a datatype can be much more succinctly expressed as type any_value = Int | String | Float
rather than across 4 classes (as in the Java case).
2. Start small, and understand it in chunks. For these, a lot of web based resources are ideal. For example, to understand regular expressions, it would be nice if you were able to visualize them, and play around with them visually -
http://osteele.com/archives/2006/02/reanimator
I would also look at simple examples of interpreters, and build up from that, looking at examples of toy compilers:
http://min-caml.sourceforge.net/index-e.html
These will help, of course alongside a book like Appel's or the Dragon Book.
About half our class didn't finish the base compiler. Fortunately for us, we were using the Java version of the text, so most of the students already knew the implementation language; I know a course at another school using the ML text, and none of them finished their compiler, because they had to learn the language at the same time as coding the compiler.
Here's an online book that covers interpretation of programming languages in Scheme (touching on functional language compilation in several places):
http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/200...
http://www.wiley.co.uk/wileychi/grune/
There is also a great book by the same author on parsing.
http://www.cs.vu.nl/~dick/PTAPG.html
Good Luck
Programming Language Pragmatics http://www.amazon.com/Programming-Language-Pragmatics-Second...
I own this in addition to the Dragon Book. PLP is a lot easier for me (a student) to read and understand. I definitely recommend it!