A JavaCC/JJTree bug fixed

23 Oct 2007

Paul Cager has been working on various JavaCC and JJTree bugs lately. Just recently he fixed a rather annoying bug; in JavaCC 4.0, the OUTPUT_DIRECTORY option setting wasn't copied from the .jjt file into the .jj file. So you'd have a grammar like this:

$ cat foo.jjt 
options {
  OUTPUT_DIRECTORY="foobar";
}
PARSER_BEGIN(Foo)
public class Foo {}
PARSER_END(Foo)
void a() : {} {" a" } 

And running JJTree on it would result in an error like this:

$ jjtree foo.jjt 
Java Compiler Compiler Version 4.0 (Tree Builder)
(type "jjtree" with no arguments for help)
Reading from file foo.jjt . . .
File "foobar/Node.java" does not exist.  Will create one.
Exception in thread "main" java.lang.Error: java.io.FileNotFoundException:
 foobar/Node.java (No such file or directory)
        at org.javacc.jjtree.NodeFiles.ensure(Unknown Source)
        at org.javacc.jjtree.NodeFiles.ensure(Unknown Source)
        at org.javacc.jjtree.NodeScope.insertOpenNodeCode(Unknown Source)

And if you did create the "foobar" directory and run JJTree again, the "foo.jj" file would be created in the working directory and wouldn't have the OUTPUT_DIRECTORY option. Booo. However, with Paul's changes, it now works fine:

$ ~/javacc/bin/jjtree foo.jjt 
Java Compiler Compiler Version 4.1d1 (Tree Builder)
(type "jjtree" with no arguments for help)
Reading from file foo.jjt . . .
Warning: Output directory "foobar" does not exist. Creating the directory.
File "foobar/Node.java" does not exist.  Will create one.
File "foobar/SimpleNode.java" does not exist.  Will create one.
Annotated grammar generated successfully in foobar/foo.jj

And the OUTPUT_DIRECTORY option is preserved:

$ grep OUTPUT_DIRECTORY foobar/foo.jj 
  OUTPUT_DIRECTORY="foobar";

Good stuff! I think we're getting close to a 4.1 release, which will be nice since it'll have lots of improvements to the Java 1.5 code that JavaCC generates, and it'll be the first official release that's BSD licensed. Hopefully we can get in a few more bug fixes and then get this release out the door.

And, of course, here's a gratuitous plug for my JavaCC book!