HOWTO – make a compiler

Computers & Mobile Technology

compiler_20090210.jpg

Most hackers can spend their whole careers creating amazing software without ever needing to write a compiler or design a programming language. Like many professions, a lot of useful work can be accomplished with the established tools, rather than focusing efforts on the invention and craft of the tools themselves. Most carpenters purchase their tools at a hardware store. I’d probably raise an eyebrow if my doctor tried to measure my blood pressure with something she cobbled together in the garage (on second thought, that’d be pretty cool).

Unlike most other professions, however, the software developer has a unique advantage: the tools of the trade are made using the trade itself. A compiler is software that builds other software. I think that’s what intrigues programmers and simultaneously scares them away from language and compiler design. It’s magic.

Joel Pobar exposes a bit of this mystery in an introduction to compiler design for MSDN Magazine:

Compiler hackers are celebrities in the world of computer science. I’ve seen Anders Hejlsberg deliver a presentation at the Professional Developers Conference and then walk off stage to a herd of men and women asking him to sign books and pose for photographs. There’s a certain intellectual mystique about individuals who dedicate their time to learning and understanding the ins and outs of lambda expressions, type systems, and assembly languages.

The article is a walkthrough on the creation of a simple programming language and a corresponding compiler that targets the .NET CLR virtual machine. It’s a nice introduction to compiler design, and you can download the C# source for the sample compiler that’s discussed. Perhaps this will be the starting point for the creation of your own programming language.

Create a Language Compiler for the .NET Framework

8 thoughts on “HOWTO – make a compiler

  1. http://www.brokersys.com/~jguthrie/links.html says:

    I don’t run Windows on any of my computers, so I don’t read MSDN and I don’t program in C# even though there is a C# implementation for Linux, but the LLVM (Low Level Virtual Machine) tutorial takes you through creating a compiler with LLVM. If you have an interest, my favorite book on the subject of compiler writing is which I found to be quite readable and understandable.

    I do take exception to the summary that implies that it is almost never necessary to design a programming language. I have found that it is often useful to design a special-purpose programming language to simplify large or open-ended tasks. When I’ve done that in the past, though, I usually create an interpreter rather than a compiler because interpreters are, at least conceptually, simpler than compilers because you build the parse tree and then you just traverse it doing whatever it says to do.

  2. Joe Ardent says:

    Yes, for those who wish to stay away from Maker-unfriendly products like Microsoft’s CLR, I ran across this recently:

    http://compilers.iecc.com/crenshaw/

    “This fifteen-part series, written from 1988 to 1995, is a non-technical introduction to compiler construction. You can read the parts on-line or download them in a ZIP file.”

    There is also the amazing “The Elements of Computing Systems”:

    http://www1.idc.ac.il/tecs/

    which takes you through constructing a real computer, starting with NAND gates, and concluding with a graphical application written in a language for which you wrote the compiler, VM, and operating system (it runs as simulated hardware, on a Java-based open-source emulator that the authors supply). I cannot speak highly enough of it.

  3. elmegil says:

    I suppose there’s not typically something where a woodworker builds their own saw, but there certainly is a lot of building other tools to make using the saw more effective, repeatable, and precise. Jigs and hand tools are frequently built using existing tools and materials. Planes, sanding tools, mallets of various sorts, etc are all very common woodworking projects, and can get used to make more of the same.

    Perhaps the developer has a unique advantage at the way they can use tools to self-replicate as it were (building compilers with compilers) but that seems a very restrictive subset of the kinds of tool building that goes on.

    None of which of is intended to say that compiler writers aren’t Ghods among Men when it comes to programming chops :-D

  4. Mo Me says:

    check out pkjl.com

Comments are closed.

Discuss this article with the rest of the community on our Discord server!
Tagged

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK