---
created_at: '2015-12-24T02:57:10.000Z'
title: Want to Write a Compiler? Read These Two Papers (2008)
url: http://prog21.dadgum.com/30.html
author: rspivak
points: 237
story_text: 
comment_text: 
num_comments: 70
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1450925830
_tags:
- story
- author_rspivak
- story_10786842
objectID: '10786842'

---
[Source](http://prog21.dadgum.com/30.html "Permalink to Want to Write a Compiler? Just Read These Two Papers.")

# Want to Write a Compiler? Just Read These Two Papers.

[programming in the  
twenty-first century][1]

It's not about technology for its own sake. It's about being able to implement your ideas.

# Want to Write a Compiler? Just Read These Two Papers.

Imagine you don't know _anything_ about programming, and you want learn how to do it. You take a look at Amazon.com, and there's a highly recommended set of books by Knute or something with a promising title, _The Art of Computer Programming_, so you buy them. Now imagine that it's more than just a poor choice, but that **all** the books on programming are at written at that level.

That's the situation with books about writing compilers.

It's not that they're bad books, they're just too broadly scoped, and the authors present so much information that it's hard to know where to begin. Some books are better than others, but there are still the thick chapters about converting regular expressions into executable state machines and different types of grammars and so on. After slogging through it all you will have undoubtedly expanded your knowledge, but you're no closer to actually writing a working compiler.

Not surprisingly, the opaqueness of these books has led to the myth that compilers are hard to write.

The best source for breaking this myth is Jack Crenshaw's series, [Let's Build a Compiler!][2], which started in 1988. This is one of those gems of technical writing where what's assumed to be a complex topic ends up being suitable for a first year programming class. He focuses on compilers of the Turbo Pascal class: single pass, parsing and code generation are intermingled, and only the most basic of optimizations are applied to the resulting code. The original tutorials used Pascal as the implementation language, but there's a C version out there, too. If you're truly adventurous, Marcel Hendrix has done a [Forth translation][3] (and as Forth is an interactive language, it's easier to experiment with and understand than the C or Pascal sources).

As good as it is, Crenshaw's series has one major omission: there's no internal representation of the program at all. That is, no abstract syntax tree. It is indeed possible to bypass this step if you're willing to give up flexibility, but the main reason it's not in the tutorials is because manipulating trees in Pascal is out of sync with the simplicity of the rest of the code he presents. If you're working in a higher level language--Python, Ruby, Erlang, Haskell, Lisp--then this worry goes away. It's trivially easy to create and manipulate tree-like representations of data. Indeed, this is what Lisp, Erlang, and Haskell were designed for.

That brings me to [A Nanopass Framework for Compiler Education][4] [PDF] by Sarkar, Waddell, and Dybvig. The details of this paper aren't quite as important as the general concept: a compiler is nothing more than a series of transformations of the internal representation of a program. The authors promote using **dozens or hundreds of compiler passes**, each being as simple as possible. Don't combine transformations; keep them separate. The framework mentioned in the title is a way of specifying the inputs and outputs for each pass. The code is in Scheme, which is dynamically typed, so data is validated at runtime.

After writing a compiler or two, then go ahead and plunk down the cash for the infamous [Dragon Book][5] or one of the alternatives. Maybe. Or you might not need them at all.

[permalink][6] _June 29, 2008_

# previously

* [A Spellchecker Used to Be a Major Feat of Software Engineering][7]
* [Coding as Performance][8]
* [Don't Be Afraid of Special Cases][9]
* [Purely Functional Retrogames, Part 4][10]
* [Purely Functional Retrogames, Part 3][11]

# [archives][12]

[**twitter][13]** / [**mail][14]**

I'm James Hague, a [recovering programmer][15] who has been designing video games since the 1980s. [Programming Without Being Obsessed With Programming][16] and [Organizational Skills Beat Algorithmic Wizardry][17] are good starting points. For the older stuff, try the [2012 Retrospective][18].

Where are [the comments][19]?

[1]: http://prog21.dadgum.com/
[2]: http://compilers.iecc.com/crenshaw/
[3]: http://home.iae.nl/users/mhx/crenshaw/tiny.html
[4]: http://www.cs.indiana.edu/~dyb/pubs/nano-jfp.pdf
[5]: http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools
[6]: http://prog21.dadgum.com/30.html
[7]: 29.html
[8]: 28.html
[9]: 27.html
[10]: 26.html
[11]: 25.html
[12]: http://prog21.dadgum.com/archives.html
[13]: https://twitter.com/dadgumjames
[14]: mailto:james.hague%40gmail.com
[15]: http://prog21.dadgum.com/56.html
[16]: http://prog21.dadgum.com/195.html
[17]: http://prog21.dadgum.com/177.html
[18]: http://prog21.dadgum.com/162.html
[19]: http://prog21.dadgum.com/57.html

