[antlr-interest] TestRig: use with package-ized lexer/parser?

Graham Wideman gwlist at grahamwideman.com
Mon Nov 12 17:34:40 PST 2012


At 11/12/2012 10:53 AM, Terence Parr wrote:
>I assume you have tried TestRig pack.Grammar?   I don't remember if I thought of this so it might not work but I can't see why not. Should be a trivial string concatenation fix. I will look at the other things as well.

Well, yes, I did go down that path (so to speak) unsuccessfully, but as I now learn, I missed a trick.  

So, here are the details on how to exercise package-ized ANTLR grammars with TestRig as currently designed.

1. Class directory structure must correspond to package
--------------------------------------------------------
If the grammar (mygrammar) specifies a package (let's say com.abc.myparser), then you must arrange that the compiled class files (mygrammarLexer.class and friends) wind up in a directory tree that corresponds to the package. So something like (in NetBeans):

  [MyProject]/build/classes/com/abc/myparser/mygrammarLexer.class etc

[see note A]

2. cd to _root_ of classes
--------------------------
Before running TestRig (or grun) on this from the command line, you must first cd to the _root_ of that class tree:
  [MyProject]/build/classes

... so that java runtime classloader can find the class in question from dot (cwd) using the classname com.abc.myparser.mygrammarLexer .

3. Specify grammar name qualified by package
--------------------------------------------
In the TestRig (or grun) command line, you have to specify the grammar name qualified by the package name, for example:

  grun com.abc.myparser.mygrammar startelement -gui otherargs

[See note B]

Other notes:
------------
A) This probably requires that, upstream, you have ANTLR generate the java source files in a similarly appropriate source directory structure -- ANTLR itself doesn't create or require package-coordinated directories when generating the lexer/parser code.  While ANTLR and javac can be used with simple commands assuming default package, once non-default packages become involved one probably needs a build system or IDE to automate the coordination of packages and directories.

B) Of course, since you have to set the cwd to the classes root for TestRig's benefit, you probably have to adjust the argument that specifies the location of input files.

C) An IDE issue:  When working on a custom version of TestRig in NetBeans, and attempting to run or debug it from the IDE, I still haven't found a good way to add "current dir" to the runtime classpath.  You can add an explicit dir to the Libraries list, but that's a poor substitute, so not a final answer, IMO.  (This is only an issue if you're trying to work on TestRig with its dynamic class loading, not a general problem with ANTLR and packages.)

Hope that helps others.

-- Graham







More information about the antlr-interest mailing list