697

Preferred languages: C/C++, Java, and Ruby.

I am looking for some helpful books/tutorials on how to write your own compiler simply for educational purposes. I am most familiar with C/C++, Java, and Ruby, so I prefer resources that involve one of those three, but any good resource is acceptable.

4
  • ANTLR all the way. All the resources proposed below looks like an overkill to me. ANTLR is always a compiler designer best friend. A
    – A_Var
    Dec 19, 2010 at 4:25
  • If your main focus is to learn how compiling ideas work in general - you can check and SICP short for Structured Interpretation of Computer program based in Scheme ( List) but teaches the general principles . mitpress.mit.edu/sicp . I was recommended this book by a veteran who works for a company and does these works compilation and interpretation for a living !
    – Nishant
    Apr 27, 2011 at 15:32
  • A shameless plug: my answer on a similar question.
    – 9000
    Mar 1, 2014 at 0:21
  • I wrote an article on creating a compiler on my blog: orangejuiceliberationfront.com/how-to-write-a-compiler It focuses on the very basics and getting started, really. There's a bunch more compiler/codegen/parser/language design-related articles on there.
    – uliwitness
    Mar 8, 2014 at 12:33

38 Answers 38

1
2
4

Not included in the list so far is this book:

Basics of Compiler Design (Torben Mogensen) (from the dept. of Computer Science, University of Copenhagen)

I'm also interested in learning about compilers and plan to enter that industry in the next couple of years. This book is the ideal theory book to begin learning compilers as far as I can see. It's FREE to copy and reproduce, cleanly and carefully written and gives it to you in plain English without any code but still presents the mechanics by way of instructions and diagrams etc. Worth a look imo.

1
  • Added it to the list thanks :)
    – Anton
    Mar 12, 2014 at 17:11
3

As an starting point, it will be good to create a recursive descent parser (RDP) (let's say you want to create your own flavour of BASIC and build a BASIC interpreter) to understand how to write a compiler. I found the best information in Herbert Schild's C Power Users, chapter 7. This chapter refers to another book of H. Schildt "C The complete Reference" where he explains how to create a calculator (a simple expression parser). I found both books on eBay very cheap. You can check the code for the book if you go to www.osborne.com or check in www.HerbSchildt.com I found the same code but for C# in his latest book

3

The Dragon Book is too complicated. So ignore it as a starting point. It is good and makes you think a lot once you already have a starting point, but for starters, perhaps you should simply try to write an math/logical expression evaluator using RD, LL or LR parsing techniques with everything (lexing/parsing) written by hand in perhaps C/Java. This is interesting in itself and gives you an idea of the problems involved in a compiler. Then you can jump in to your own DSL using some scripting language (since processing text is usually easier in these) and like someone said, generate code in either the scripting language itself or C. You should probably use flex/bison/antlr etc to do the lexing/parsing if you are going to do it in c/java.

1
  • I wouldn't say "too complicated", I would say "badly written".
    – anon
    Mar 18, 2010 at 20:43
3

Missing from the list: Garbage Collection: Algorithms for Automatic Dynamic Memory Management, by Jones and Lins.

(Assuming you're writing the compiler and runtime system, and that you're implementing a garbage collected language.

2

The quickest approach is through two books:

1990 version of An Introduction to Compiling Techniques, a First Course using ANSI C, LeX, and YaCC by JP Bennett - a perfect balance of example code, parsing theory and design- it contains a complete compiler written in C, lex and yacc for a simple grammar

Dragon Book (older version) - mostly a detailed reference for the features not covered in the former book

2

If you're not just looking for books, but also interested in web sites that have articles on the topic, I've blogged about various aspects of creating a programming language. Most of the posts can be found in my blog's "Language Design" category.

In particular, I cover generating Intel machine code manually, automatically generating machine- or bytecode, creating a bytecode interpreter, writing an object-oriented runtime, creating a simple loader, and writing a simple mark/sweep garbage collector. All of this in a very practical and pragmatic way instead of boring you with lots of theory.

Would appreciate feedback on these.

1

If you are like me, who has no formal computer science education, and is interested in building/want to know how a compiler works:

I am recommend "Programming Language Processors in Java: Compilers and Interpreters", an amazing book for a self-taught computer programmer.

From my point of view, understanding those basic language theory, automate machine, and set theory is not a big problem. The problem is how to turn those things into code. The above book tells you how to write a parser, analysis context, and generate code. If you can not understand this book, then I have to say, give up building a compiler. The book is best programming book I have ever read.

There is an other book, also good, Compiler Design in C. There is a lot of code, and it tells you everything about how to build a compiler and lexer tools.

Building a compiler is a fun programming practice and can teach you heaps of programming skills.

Do not buy the Dragon book. It was a waste of money and time and is not for a practitioner.

-4

Whenever I want to try out a new language idea, I just write a simple parser, and have it generate some language that's easy to get good compilers for, like C.

How do you think C++ was done?

1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.