Tom Copeland's Recent Posts

RSS Feeds

« Twenty million records | Main | Subversion graphs on RubyForge »

A little JavaCC trick

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:

$ cat Show.java
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 && \
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 <FOO: (["a"-"z"])+ > 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.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451d3c069e200e55070a99b8834

Listed below are links to weblogs that reference A little JavaCC trick:

» A little JavaCC trick from PsychicProgrammer.com
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...Link:... [Read More]

Comments

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.