HN Books @HNBooksMonth

The best books of Hacker News.

Hacker News Comments on
Retargetable C Compiler, A: Design and Implementation

David Hanson, Christopher Fraser · 7 HN comments
HN Books has aggregated all Hacker News stories and comments that mention "Retargetable C Compiler, A: Design and Implementation" by David Hanson, Christopher Fraser.
View on Amazon [↗]
HN Books may receive an affiliate commission when you make purchases on sites after clicking through links on this page.
Amazon Summary
Designed as a self-study guide, the book describes the real-world tradeoffs encountered in building a production-quality, platform-retargetable compiler. The authors examine the implementation of lcc, a production-quality, research-oriented retargetable compiler, designed at AT&T Bell Laboratories for the ANSI C programming language. The authors' innovative approach-a "literate program" that intermingles the text with the source code-uses a line-by-line explanation of the code to demonstrate how lcc is built.
HN Books Rankings

Hacker News Stories and Comments

All the comments and stories posted to Hacker News that reference this book.
Unless you are talking about something like "A Retargetable C Compiler: Design and Implementation", it is definitly not easy.

https://www.amazon.com/Retargetable-Compiler-Design-Implemen...

During the early 80's, the best home computers could get was Small-C.

https://en.wikipedia.org/wiki/Small-C

randomifcpfan
Small-C was the best one with open source, but there were many others. This one was my favorite: https://www.bdsoft.com/resources/bdsc.html
pjmlp
What I got back in the day came in book form, it was also another flavour.

"A book on C"

https://link.springer.com/book/10.1007/978-1-349-10233-4

It uses a K&R C subset with bytecodes and a bytecode-> machine language translation.

I just never bothered to type it in though.

Jan 08, 2020 · plinkplonk on C Compiler from Scratch
This might be relevant

A Retargetable C Compiler: Design and Implementation by David Hanson and Chritopher Fraser

https://www.amazon.com/Retargetable-Compiler-Design-Implemen...

Written in a literate programming style.

jacquesm
Happy new year to you Ravi!
plinkplonk
Thanks Jacques. Same to you, (and everyone on HN!)
A Retargetable C Compiler: Design and Implementation

https://www.amazon.com/Retargetable-Compiler-Design-Implemen...

A most excellent grimoire.

useanalias
Misread this as "A Regretable C Compiler" -- likely what would happen to me without a text like this!
I wouldn't go so far to say that the Dragon Book is outdated and irrelevant. (I'm assuming you're referring to the 2nd edition from 2006.) Unless you're focusing on back-end optimization and code generation techniques (something a new compiler writer typically does NOT do), the bulk of the theory and material you'd cover in a first semester compiler course is fairly static.

But if a person is merely looking to bang out a compiler without getting overwhelmed with how to convert NFAs to DFAs for lexing, etc., some good alternative books are:

A Retargetable C Compiler: Design and Implementation, by Hanson and Fraser (http://www.amazon.com/Retargetable-Compiler-Design-Implement...). This book constructs and documents the explains the code for a full C compiler with a recursive descent approach (no flex/lex or bison/yacc). I have some experience augmenting this compiler, so I can vouch for the book's ability to clearly convey their design.

Compiler Design in C, by Allen Holub (http://www.holub.com/software/compiler.design.in.c.html). Downloadable PDF at that link as well. A book from 1990 in which Holub constructs his own version of lex and yacc, and then builds a subset-C compiler which generates intermediate code.

Practical Compiler Construction, by Nils Holm (http://www.lulu.com/shop/nils-m-holm/practical-compiler-cons...). A recent book which documents the construction of a SubC (subset of C) compiler and generates x86 code on the back end.

sklogic
It is actually an outdated view, to split a compiler into dedicated monolithic front-end and back-end parts. The more modern approach is very much the opposite. It is a nearly continuous, very long sequence of very simple transforms, rewriting a code seamlessly, all the way down from a front-end (i.e., a parser) to a back-end or multiple back-ends. And this approach is very alien to anything you'd find in the Dragon Book.

As for parsing, as I already said elsewhere in this thread, all the techniques from Dragon Book are not practical any more and are not used in the modern compilers. There are far better ways, which are not covered in the book, and they're far simpler, not even deserving a dedicated book at all.

lcc(1) the C compiler. As well as having readable C source, you'd learn something about the innards of C, how it is implemented, and there's a good accompanying book by Fraser and Hanson that covers the complete source as a "literate program". https://github.com/drh/lcc/blob/master/src/lex.c http://www.amazon.com/exec/obidos/ASIN/0805316701/mqq-20
Search the net for one or another of the SmallC compilers (Ron Cain wrote the original one) published in Dr. Dobbs Journal. Later authors tarted up the original code adding more of C to the mix to make it more useful and managed to confuse things.

Chris Fraser and Dave Hanson's A Retargetable Compiler is another good example system with code to read and modify.

http://www.amazon.com/Retargetable-Compiler-Design-Implement...

10 years ago, I loved Hanson and Fraser:

http://www.amazon.com/exec/obidos/ASIN/0805316701

This is a good tour of front-end and back-end stuff, a decent intro to code gen, and is built on a hand-written recursive-descent C parser. That it's specific to one C compiler made it better to read straight through, but I rarely refer back to it.

So far this year, I've been liking Muchnick:

http://www.amazon.com/exec/obidos/ASIN/1558603204

This is pretty heavy on the backend and codegen, which is where my head is at these days.

Compilers is a big topic. What part interests you most? Parsing? Optimization? Semantics and error detection? Code generation?

HN Books is an independent project and is not operated by Y Combinator or Amazon.com.
~ yaj@
;laksdfhjdhksalkfj more things
yahnd.com ~ Privacy Policy ~
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.