Tool showing grammatical structure of Java code

A simple tool showing how a java source file is parsed, using the Java grammar from Openjdk javac compiler project with ANTLR-based Java grammar . Here's a screenshot:

You can grab the source or runnable jar from the attachments on this page.

The tool is based on a very simple idea: embedding code in the grammar to create a node for every rule invocation. The result is a parse tree. As the parser parses a Java file, the tree is built as each rule is invoked.

Take the package declaration rule for example:

packageDeclaration 
    :   'package' qualifiedName
        ';'
    ;

Tree construction code is embedded into this rule like this:

packageDeclaration returns [parent]
@init{
    parent = create a parent node called "package Declaration"
}
    :   'package'      { add "package" as a child to parent }
         qualifiedName { add return node from qualifiedName as a child }
        ';'
    ;

In fact, the code is general enough to be applied to every rule. So it's pretty easy to make such changes with an ANTLR grammar for ANTLR grammars. This tool uses ANTLRv3.g to instrument the Java.g grammar in order to build parse trees. ANTLR has a built-in facility to build parse trees, but this tool demonstrates how to inject code into structured text files (another grammar in this case).

You should be able to run the file directly, or if not, save a copy of the tree.jar, then run

java -jar tree.jar