I have been playing lately with compiler generators and related stuff for something I’m working on. I had to re-read some parts of the dragon book (my memory is pretty bad) and I also found out that a new edition of the dragon book was released this year:
The best tool I have found is ANTLR. It is completely free, it can generate a lexer, a parser and a tree parser, so it can be used to generate all the needed stuff without having to use different tools. It supports C#, Java, C++ and Python. Also, it generates descendant recursive parsers so the code generated can be understood by a human (no more transition tables like yacc/bison). Even if predictive top down parsers can not handle all the context free grammars, this one supports pred-LL(k) grammars (LL(k) means Left to right Left most derivation with k tokens of lookahead, and the pred prefix means that it supports predicates to see if a rule has to be applied or not) with backtracking so it is really powerful.
The latest version (v3) is still in beta but it has some nice improvements over v2 so that’s the one I’m using. For v3 there isn’t much documentation so the learning curve is a bit high but it seems worth the effort. IMHO one of the bests things of ANTLR is that it has a great development IDE called ANTLRWorks that is amazing. Just look at this screenshots I took of some C# 2.0 grammar tests I did:
You have real time error checking, you can see a syntax diagram of the current rule, you can view the generated parse tree, debug the grammar, see rules dependencies, the DFA for the tokens, automatically remove left recursion from rules… awesome!
If you are wondering if ANTLR is good enough for you, think that JetBrains (the company that created ReSharper) uses it.
Enough talking, back to ANTLR…