Monday, June 13, 2005

Writing a compiler

The problem I remember most from being a young programmer (13 years old or so) was figuring out how to parse and evaluate expressions. It seemed like a black art. It was Jack Crenshaw's compiler tutorial which eventually sorted me out, several years later.

In homage to Crenshaw, I'm going to write a little series on how to write a compiler, updated to use C# 2.0 / .NET. Maybe somebody else out there will find it enlightening.

The project will cover the following areas:
  1. How compilers are structured.
  2. A lexer for lexical analysis.
  3. A parser for syntactic analysis and parse-time evaluation.
  4. Designing of a virtual machine.
  5. A simple virtual machine.
  6. A new parser generating code for the virtual machine.
  7. Type analysis and operator overloading.
Each area will be covered in one or more blog posts (as needed), building up from the previous posts to a final finished little language; how capable remains to be seen.

3 comments:

Bill said...

Crenshaw's series was a revelation to me, as well. I had had glimmerings of comprehension, as Turbo Pascal evolved, and some of what goes on under the covers inevitably became apparent. But Jack's clear style, and his so fundamental approach to the mechanics of implementation, gave me a much greater comprehension of what goes on.

Mind you, I have still not written a compiler of my own -- I'm more than happy with Delphi and C# -- but now I know that I could, and that the result could at least be competent and workmanlike.

Anonymous said...

You said you'd write a series of articles but as far as I can see this was the first and only one. Are you going to continue?

Viktor Korsun said...

This open-source project may be useful http://code.google.com/p/delphibasic in writing an expression evaliator or even a programming language on Delphi.