Ask HN: Best book about compilers?

  • I doubt people who actually do write compilers would get much of use of any book out there.

    There are 2 main tasks a compiler does:

    * parsing * optimized code generation

    While nominally parsing is covered by books and there's loads of theory on it, production compilers (gcc, clang, Go, swift) pretty much always end up with hand-written recursive-descent parser, which are a lot work but their structure is rather simple.

    Code generation is universally done by converting to SSA and then writing optimization passes on top of that. Again, lots of work and lots of optimization rules, but nothing earth shattering conceptually. Read up on SSA and study existing code like https://github.com/golang/go/tree/master/src/cmd/compile/int... for real-life optimization.

    Then there's linker stuff like generating pe/elf/mach-o executables, generating symbols etc., which isn't covered in books but there's documentation on-line on all those subjects and you can study existing code like https://github.com/golang/go/tree/master/src/cmd/link/intern...

  • The book "Engineering a Compiler" (https://www.amazon.com/Engineering-Compiler-Second-Keith-Coo...) is a good resource and reference; I keep it on my desk at work. Like other compiler books, it falls short in meeting all practical application purposes and does great with theory. Ideally, this book should be combined with looking at modern compiler implementations.

  • I'm not sure it is the best, but it is clearly an alternative to the Dragon book.

    https://compilers.iecc.com/crenshaw/

  • Is the dragon book really recommended nowadays? I don't think so, especially for starters.