Learn C and Build Your Own Programming Language
Ratings1
Average rating3
Aghh. This is hovering between a did-not-finish (at 69%) and a “want to read” for me. I might pick it up again.
Basically: I picked this up because I had two goals - (1) I wanted to write a toy programming language for my CS50 final project, and (2) I wanted to do more C.
Unfortunately, this book started strong but then lost a lot of steam for me. Plus, I found Destroy All Software's A compiler from scratch video, which was basically the final nail in the coffin.
This book has two goals, and they're admirable: first, introduces people to the basic theory behind how programming languages are interpreted/compiled, and, second, it teaches them C. The process is very similar to Destroy All Software's video: you read in some input string, you tokenize those strings according to your language, you parse them into an appropriate tree structure, and then you pipe that tree into the compilation language. In the case of this book, you do all of that process: input, tokenize, parse, and compile in C. In Destroy All Software, he does the inputting, tokenizing and parsing in Ruby and then - mysteriously/wackily - compiles to JAVASCRIPT (what in the world). I'm planning to parse/tokenize/etc in Python and compile to C.
I made serious headway with the book, but eventually found myself just pecking out 100s of lines of C without understanding what the eff was going on. And the reason is that I think this book's second goal - to assume it's understandable to C beginners - isn't really realistic. I have about ~20 hours of C under my belt (i.e. 5 problem sets in CS50), and I have a shaky grasp of pointers and memory allocation, and this book quickly got away from me. I ended up just blindly trying stuff, but then I was - well - struggling with C concepts, rather than compiler concepts. With the DAS video, instead, I've switched to Python and am finding it much easier to follow - now, instead of getting lost in the syntax of my parsing language, I can just focus on building a compiler.
Oh yeah, and another point: the author abstracts some of the tokenizing away from you with a custom-built C library he wrote called mpc
. I'm not sure about the pedagogy, but - in general - I don't like it when books provide custom-built libraries that you need to do to work, e.g. Allen Downey (who I generally admire very much!) did this with Think Stats and it kinda killed the book for me - let the reader build from the smallest building blocks! Don't give us a half-built contraption upon which we're meant to build more - that just means we spend more time trying to figure out the contraption!
Anyway, I feel bad saying all this. I think the book would be GREAT for people that are already up to speed with C. Also, it's free and the author offers a lot of humor and encouragement. Bah!