views:

1405

answers:

15

I've been wanting to play around with writing my own language for a while now (ostensibly for the learning experience) and as such need to be relatively grounded in the construction of Parsers, Interpreters, and Compilers. So:

  • Does anyone know of any good resources on constructing Parsers, Interpreters, and Compilers?

EDIT: I'm not looking for compiler-compilers/parser-compilers such as Lex, Yacc and Bison...

+4  A: 

Not very easy, but exhaustive: the dragon book

Ralph Rickenbach
+3  A: 

Compiler Construction: Principles and Practice is a best book on the subject.

aku
+5  A: 

Aho's "Dragon book" is the standard reference, but another good choice is Andrew Appel's Modern Compiler Implementation in Java (also available in ML and C flavors). It walks you step-by-step through each of the necessary components of a compiler/interpreter and provides a lot of useful source code.

Chris Conway
+1, own them, good books.
Aiden Bell
+1  A: 

RE: lex, yacc, bison

Do you mean you don't need pointers to them or that you aren't interested in using them? I don't recommend writing a lexer or parser by hand.

EDIT: You may have an exaggerated sense of what a "compiler-compiler" can accomplish: the really hard part of compilation begins after you've built the AST. The Aho, Louden, and Appel books are all quite good.

Chris Conway
s/hard/hard and fun/g
Aiden Bell
+4  A: 

This highly-entertaining blog post by Steve Yegge (Rich Programmer Food) won't tell you everything you need to know about how to build a compiler (although it does include a fair amount of good insight), but it does a great job of explaining why you want to understand compliers.

Heck, that post almost got me to start studying compilers, and I have a full-time job, a 3-year-old at home, and about -1.5 free hours per day on average.

McKenzieG1
A: 

@Chris Conway

Since one primary reason for looking into this subject is to learn about it, I'm looking more at building something like this from the ground up. I am aware of quite a few compiler-compilers etc. and I will likely look into how they are implemented, but other than that I'm wanting resources which will help me better understand how parsers, interpreters, and compilers work and how to implement them.

akdom
+2  A: 

I recommend Compiler Design in C which you would have to find in a used book site unfortunately. The only real problem with the book is that it was written back when speed of compilation was an important factor so the compiler is written in C. That's enough of a low level language that sometimes the implementation theory is buried under the implementation code.

You mentioned both Interpreters and Compilers. I'd actually recommend starting with an Interpreter rather than a Compiler. It's much easier to get started with an Interpreter and they tend to be more fun to work on because you can get immediate feedback on how you're doing.

slm
+2  A: 

I recently read Programming Language Processors in Java and would recommend it to you as it starts with basic concepts and then takes you through the implementation of a recursive-descent compiler including code generation with plenty of discussion of alternative approaches. I've not read the other suggested books in this thread though, so can't offer a comparison with those. Don't be put off by the In Java part, if you don't know java, as the concepts and their implementation are well enough explained that you get the gist without needing a detailed understanding of Java.

robaker
I think this book is much better than the dragon book for a starter. Highly recommended.
utku_karatas
+1  A: 

Just in case you:

  • Are willing to play
  • Like the chance to build something quicker, then go deeper
  • Are ok with using .NET at an underlying platform

...then I'd recommend you to check Microsoft's Dynamic Language Runtime, which is provided as source code inside the whole Dynamic Silverlight SDK.

http://www.codeplex.com/sdlsdk

Some nice things about it is that it provides lots of ready-to-use features like a console, a common type system, a generic abstract syntax tree, etc. And the package includes three implementations: Python, Ruby and Javascript.

Maybe it is a good chance to implement a short language other the DLR, then start checking the actual DLR code, and then start building the basic stuff on your own.

In any case, best luck from another language freak!

Martin Salias
+2  A: 

The MIT OpenCourseWare(OCW) site has a class whose project is to build a complier...they refer to the Dragon book and Appel's book, but in addition, there are also class notes and lectures available. The class is 6.035 - Computer Language Engineering.

I found the class notes very useful for explaining recursive-descent parsers.

Mark Krenitsky
+2  A: 

I've found "Let's build a compiler" - the tutorial by Jack Crenshaw, very easy to follow and understand. Jack builds a compiler for Pascal, from the ground up, with very detailed explanations of every step.

Eli Bendersky
A: 

It's an old book, and rather dated, but I've found Brinch Hansen on Pascal Compilers to be a very practical introduction to creating a language and building the elements of a compiler. Despite the name, it's not specific to Pascal. It's very worthwhile if you can find a copy.

Bruce Alderman
A: 

FYI - There's more on this topic in this thread.

Alan
A: 

My ANTLR tutorial talks about writing a recognizer for a language. It's currently for ANTLR 2.x (I'm working on updating it to 3.x)

http://javadude.com/articles/antlrtut/index.html

Enjoy!

Scott Stanchfield
A: 

The best paper I ever read on compilers is dated 1964 "META II a syntax-oriented compiler writing language" by Val Schorre. (http://doi.acm.org/10.1145/800257.808896)

In 10 pages, he shows you how to build an astoundingly simple but very effective compiler-compiler, provides you with with the compiler-compiler grammar and provides you with enough details for you to hand implement it in an afternoon (with one astonishing moment when you realize how it recurses), and just for grins implements an algol-like language. The paper is a complete gas and really should be required reading for anybody working with compiler technology.

Here's a link to play with an great tutorial on metacompiling, based on the paper. http://www.bayfronttechnologies.com/mc_tutorial.html

Ira Baxter