views:

658

answers:

6

In order to continue my dive into reading real code, I'd like to read and understand the source code of an actual in-use compiler. Is there one that you think is particularly well-written, elegant, and perhaps, still relatively small? Any suggestions? Any experiences?

Resolution: I've decided to read the Lua source code. I've been bookmarking (the few) resources available to help reading and understanding the Lua code.

+2  A: 

GCC is opensource so by definition you can read its source http://gcc.gnu.org/ and it is highly used.

And it supports: C, C++, Objective-C, Fortran, Java, and Ada

Unkwntech
The way GCC compiles java might be unnecessarily surprising to someone from a java background. javac source is at http://openjdk.java.net/groups/compiler/, but I've never looked at it so I can't offer enough suggestions or experiences to constitute an actual answer...
Steve Jessop
just dive in to gcc code? good luck with that.
shoosh
Well (s)he asked for something that would compile java, and was used.Not being a developer who uses gcc, or any other compiler for that mater.
Unkwntech
+7  A: 

Lua is compact and has very good performance for a byte-code compiler. The language is simple but powerful, and does many things right: lexical scoping, closures, gc, etc. There is also a jit version once you are ready for more performance.

Though the compiler and runtime source code are not designed as a teaching vehicle, it is quite clean, and the language and its development are well documented.

Doug Currie
Thanks! That's a great suggestion. Even though I usually find C a little daunting, the code looks terse and clean.
namin
+7  A: 

I most highly recommend A Retargetable C Compiler: Design and Implementation by Chris Fraser and Dave Hanson. It describes lcc, a production-quality C89 compiler that was heavily used from its release in the early 1990s to the release of the c99 standard. These two guys are masters of their craft, and they spent 10 years working on a compiler that they could use as a research and teaching tool.

There are lots of simpler compilers out there but none that is better designed, and none that is as well documented.

It is unfortunate that the compiler itself is written in C. I once asked Hanson, "So you and Chris have spent ten years on this incredibly carefully crafted compiler, and now it is finally published as a book. What did you learn?" His response: "C is a lousy language to write a compiler in."

I couldn't agree more.

Norman Ramsey
A: 

You might want to consider Ruby as an alternative. You can download the source code from http://www.ruby-lang.org/en/downloads/.

tvanfosson
+1  A: 

I would highly recommend this book:

http://www.amazon.com/Scripting-Mastery-Premier-Press-Development/dp/1931841578

The code is in C, but if you are a current programmer you should be able to catch on quickly. It not only goes over the complete creation of a procedural compiler, assembler and virtual machine - it will explain the theory involved in all steps. You will get a much better understanding of everything from how compilers compile and Annalise code to how the CPU and memory run it. In fact, you build a VM (virtual memory/virtual processor/excettra) during the book.

I could not give a higher recommendation for you considering your interest into how the computer compiles and runs code.

nlaq
I hated this book. Takes 1200 pages to do what a good author could do in 400, and a great author in 300.
Norman Ramsey
There was a lot of theory then that you overlooked
nlaq
+2  A: 

You can download the source to the D programming language compiler (front end) at digitalmars

Walter Bright