[Update: I started to get a bit lost, as I was reading it without a computer next to me, so I went back and decided to start at the calculator again, this time porting his code over to C#. I'm doing it TDD, which is helping a lot, especially since I'm actually taking the stance of treating his methods as black boxes, sending some inputs in, then seeing what comes out. These end up as the tests for my own code. I've finished porting his simple expression tokenizer, now onto the parser. I took a look at my code, then his code, and I noticed that he is using a lot of old VB stuff (such as Instr and Mid), which mine doesn't. Personally, I like my code a bit better, seems a little cleaner. Somehow, I ended up with it being able to support alternate operators, as well, with only an addition of one line of code (wrote the operator lookup as a hashtable). It still only supports single-character operators, though. I could make a small change to fix that, but I don't think I will.
So far, so good, the book is still keeping me interested.]
Well, I got a new book today that I'm eager to get into.
Build Your Own .Net Language and Compiler
I read the intro, and he already has impressed me with a quote from his introduction. The examples are in VB.Net, and he asks why not C#:
“Why not C#? I didn't choose C# because of a simple theory of mine. All, or nearly all, C# programmers know Visual Basic, but not all Visual Basic programmers have made the transition to C#.“
This is a great quote from someone writing a book on compilers, and it really struck me. I'm a C# guy to the bone, but I still think it is important to realize that there are tons of VB people still out there.
[Update: Chapter 3, A Compiler Flyover]
This chapter really went into BNF grammars and created one for a simple integer calculator. The BNF was then translated into a parser and interpreter for the calculator. It is very cool, and I can see how you would go about building something simple like this. It is a very brute-force way of parsing the tree (basically using a bunch of Mid statements), and I think it would be very tedious on a larger grammar. The book's eventual goal is a quickbasic parser, which will be very cool.
A comment about VB.Net. I've seen some ex-VB people asking to have the With statement added to C#, so we could write something like the following:
with myObjectVariable
{
.property = newValue;
}
Edward uses this quite often in the code, and I find that I am very glad it isn't in C#. The first time I saw it, I was a bit taken aback, but then I remembered With. UGH! What a nasty piece of syntax. Yeah, it does cut down on typing, but isn't that what Ctrl-Space is for?
In any case, I stepped through the code (downloaded from APress) of the calculator, and it is very neat. The first read-over of the chapter was a bit confusing, but having the code definitely helped.
So far, so good. If the rest of the book keeps up with the quality of the writing so far, this will be a definite recommendation for people.