A little JavaCC trick

22 Jan 2007

This is another entry that explains why my JavaCC book is not quite yet done - I keep finding little sidetracks to travel down. Anyhow!

Sometimes when you're working with a JavaCC/JJTree grammar it's helpful to see the abstract syntax tree of the grammar itself. org.javacc.jjtree.JJTree has a method that's handy for this; just write up a little driver program like this:

import org.javacc.jjtree.*;
public class Show {
  public static void main(String[] args) {
   new JJTree().main(args);
  }
}

Here's a simple grammar to show this in action:

$ cat foo.jjt
PARSER_BEGIN(Foo)
public class Foo {}
PARSER_END(Foo)
TOKEN : {
  <FOO: "foo">
}
void A() :{} { <FOO>}

Just run the driver program, set the environment variable jjtree-dump, and get a nice AST printout:

$ javac -classpath ~/javacc/bin/lib/javacc.jar Show.java &amp;&amp; \
java -Djjtree-dump=true -classpath ~/javacc/bin/lib/javacc.jar:. Show foo.jjt 
Java Compiler Compiler Version 4.1d1 (Tree Builder)
(type "jjtree" with no arguments for help)
Reading from file foo.jjt . . .
 Grammar
  CompilationUnit
  Productions
   RE
    RESpec
     RENamed
      REStringLiteral
   BNF: A
    BNFDeclaration
    BNFNodeScope
     REReference

You can see the parser productions and the tokenizer productions generally have BNF and RE prefixes. The FOO token has a very simple regular expression; a more complex one like yields an AST like this:

    RESpec
     RENamed
      REOneOrMore
       RECharList
        CharDescriptor

Generating Parsers With JavaCC is coming along steadily, though - only three chapters to go. Writing this book has been a great experience, and I've gotten lots of suggestions and comments from folks. Good times indeed.