Book Review: Writing Compilers And Interpreters

I came across this book in the Colorado State University library, one of my favorite places, and was intrigued.  I was intrigued because it seemed that instead of relying on esoteric computational equations and such, talking about optimization, O notations, and such, this book instead took the approach of diving directly into straight implementation of a compiler and interpreter.  As the sub-title suggests (“An Applied Approach Using C++”) this book is written specifically for programmers proficient in the C++ programming language.  There is no introduction to the C++ language, you must know how to program in C++, or at least be able to grasp what some C++ code means, in order to enjoy and get the full value from this book.

This book was published in 1996, so some of the techniques and coding style are dated but you won’t necessarily know that as it steps you through direct implementation and produces working “utility” programs in just about every chapter.  As far as I know the code in this book should work in any capable C++ compiler.  I thought that a little theory would be covered, in an effort to describe the methods of implementation, but I found that “theory” is only briefly touched upon, and near the end.  For example, I wasn’t aware, being green at compiler and interpreter design, that I was constructing a recursive-descent parser as opposed to a shift-and-replace parser, until I read as such near the end of the book (as I remember it.)

It’s an interesting caveat then, I think, for some people like myself it’s good that this book skips almost all the theory and goes straight into implementation.  I’m a very “implementation” oriented person, and often think of abstract algorithms and such in terms of their direct implementation as opposed to their more abstract mathematical theory (it’s something I’m working on.)  This book allowed me to get a glimpse into the inner workings of a Pascal compiler/interpreter without getting lost in the technicalities of particular algorithms.

But it’s a double-edged sword in a sense as well.  Because it doesn’t go into much theory at all this book won’t be very helpful for people who are not “implementation” oriented, and more abstract.  As well, it doesn’t serve as a textbook to compiler/interpreter design at all, in my opinion, because it basically goes over how to create a specific kind of interpreter, and makes zero effort to tell you why it may do one thing or another.

It does try to show the process of going from a syntax diagram for any particular reserved word in Pascal and what it looks like when its broken down and implemented in code, but like I said it shows one process, that is, one particular way to go about the design.  It would’ve been interesting to me if the book could have included other or alternative ways to accomplish things, but I understand with the code listings and such that would make the scope too large.

With that as well, this explores the construction of a nearly complete Pascal compiler/interpreter (I say nearly complete because it is missing at least one feature, unfortunately I had to return this book the library and I don’t remember what that feature was), which means that it only explores the programming paradigms Pascal supports.

For example, this doesn’t cover how you might program object-oriented systems, or autonomous agent systems, nor does it even touch on concurrency.  Like many books on compilers it doesn’t even conceive, it seems, of language structures such as those found in Prolog or other ‘logical’ languages.  If you’re hoping to get some general knowledge out of this book that can be applied to a Warner virtual machine to implement your own logical language… you’ll learn how to input and output files?  That’s about it.

But, what the book does it does very well.  I had a grasp on how, if I was so inclined, I could implement the construction of my own domain-specific language using something such as a C++ compiler.  In fact, because I got the book at the library, it didn’t come with a CD (I’m not even sure it comes with a CD).  It claimed that I could get all the code in some esoteric place on-line, and before reading it I was hesitant because I wasn’t sure if I could get the code.  In the end though, I just read the code that is included in the book (and it’s quite a bit), and found myself understanding things without a CD or downloaded code.  Of course, I didn’t actually implement the beast on my computer (that would’ve required all the code), but I found that the companion code was unnecessary for just understanding the text.

There is apparently a new edition of this book, which you can see on the left.  This book focuses on making a Pascal compiler/interpreter but instead of using C++ it apparently uses Java.  It also explores some other more modern tools such as UML modeling, XML intermediate structures, and the Java Collections Framework.  If I were a new programmer, or at least someone new to implementing a compiler/interpreter I’d get this book instead of the earlier edition I reviewed above.  However, if you’re looking for something in C++, the above book does offer it’s own nuggets of wisdom with its age.

I have not actually read the third edition of this book, but it’d be interesting to see what’s in there.  Thus, I can’t really recommend this book on a review basis, I only include it to showcase that the book I’m reviewing actually has a modern counterpart edition.

If you are interested or already know the Java language, you’ll probably be better off with the third edition than the previous book, but if you’re “old-school” and like C++ go with the old one.  It’s probably cheaper too.

My only caveat about both books however, is that the coding style or practice in it is a little dated.  A lot of modern programming is focused on dependency reduction, and Mak’s code tends to layer on dependencies a bit thick.  Also the overloading of some functions can leave some better un-gluing to be desired.

All in all if you’ve had difficulty understanding compilers/interpreters because theory kept standing in your way (who learns to optimize a compiler before they even know how to make a compiler?), these are probably the books for you.  With the knowledge that I was able to gain from the original edition (C++) I feel more confident now that I’ll be able to grasp more headier and theory laden books in the future, which is good because some of those dissertations in the CSU library are fierce!

This is part of a list of Books Asher’s Read

If you like my review(s), please consider supporting me through my Patreon.

Unless a monthly commitment is too much for you, otherwise you might consider buying me a coffee!

photo credit: justonlysteve yoga and compilers via photopin (license)

Liked it? Take a second to support kadar on Patreon!

kadar

I'm just a wunk, trying to enjoy life. I am a cofounder of http//originalpursuitssoc.com/ and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: