Ask HN: Resources for learning to write compilers?
I'm very interested in learning to write compilers, starting with just a simple made up language.
Can anyone share resources? I prefer bound books, but anything is helpful! I've been reading the source of TempleOS for tips as well.
25 comments
[ 2.8 ms ] story [ 78.8 ms ] threadCompilers Second Edition [0]
Engineering a Compiler, Second Edition [1]
Computer Systems: A Programmer's Perspective (3rd Edition) [2]
[0]https://www.amazon.com/Compilers-Principles-Techniques-Alfre... [1]https://www.amazon.com/Engineering-Compiler-Second-Keith-Coo... [2]https://www.amazon.com/Computer-Systems-Programmers-Perspect...
One thing that helped get started was slides from a CMU class. Hang on:
https://www.cs.cmu.edu/~15745/syllabus.html
Excellent work and so nice of them to post it for free.
The "dragon" books is one of the classics. (Compilers: Principles, Techniques, and Tools) ... linked to in another comment.
http://www.exmortis.narod.ru/index_eng.html
https://www.amazon.com/Compiler-Construction-Using-Java-Java....
This book taught me how to write a compiler.
Here is its description from its website:
* Comprehensive treatment of compiler construction.
* JavaCC and Yacc coverage optional.
* Entire book is Java oriented.
* Powerful software package available to students that tests and evaluates their compilers.
* Fully defines many projects so students can learn how to put the theory into practice.
* Includes supplements on theory so that the book can be used in a course that combines compiler construction with formal languages, automata theory, and computability theory.
If you already know C or C++ or Java then this book is for you. In my opinion, you can learn many computer science concepts and be able to apply to your field. The book will teach you how to write a grammar then write a parser from it then eventually be able to improve it as you go on reading and doing the exercises. It was a great moment when I feel comfortable writing recursive functions since grammars are composed of recursive functions. You'll also learn a nice way on how you can get your compiler to generate assembly code. Another feature of the book is the chapter on Finite Automata wherein you'll learn how to convert between regular expressions, regular grammars and finite automata and eventually write your own 'grep' which was for me is a mind-blowing experience. There are lots of other stuffs in this book that you could learn.
I have no relation to the author. I'm just a happy customer.
Modern Compiler Implementation in ML (I'm not a fan of the C/Java versions)
https://www.amazon.com/Modern-Compiler-Implementation-Andrew...
If you are fluent in a mainstream OO language - Java, C#, Ruby etc
Programming Language Processors in Java: Compilers and Interpreters
( the code is in Java but can be trivially ported into any OO language )
https://www.amazon.com/Programming-Language-Processors-Java-...
Most compiler programming books use lex/yacc versions for lexing and parsing. Imo, this isn't a good way to learn lexing/parsing, and using recursive descent or combinator parsing approaches is (imho) the right way to begin.
If you want to know how tools like lex and yacc are built, then Holub's "Compiler Construction in C" is very comprehensive and goes into great detail about the required CS theory- (automata DFA, NFA etc).
The book seems to be out of print, but used copies are worth buying (imho)
https://www.amazon.com/Compiler-Design-C-Prentice-Hall-softw...
Personally, I only find the backend (Optimization) interesting: Steven Muchnick's Advanced compiler design and implementation, is the only book that I know of that is mainly focused on optimization. It is literally a encyclopedia of optimizations.
ANTLR http://www.antlr.org
“Compiler Construction” by Niklaus Wirth (2014) [pdf] https://news.ycombinator.com/item?id=10764672
The list is huge, so I recommend you start with the Compilers course by Stanford and go through one of the two following books as a parallel reading: 1. Engineering a Compiler, K. Cooper, L. Torczon. 2. Modern Compiler Implementation in ML, by A. Appel.
If you want a smaller book, the I recommend the Basics of Compiler Design by T. Mogensen, it is a nice and short one.
All of these resources are linked to in the list.
Happy tinkering!
T3X is a tiny (made up!?) procedural language.