1
votes

I'm using Netbeans 7.1.1 with a Maven project with the ANTLR Maven Plugin to generate a Lexer+Parser Java class - this works.

What I'm struggling to work out:

How can I build a test rig within the same project, such that the Lexer and Parser Class are found at development-time ?

Is there a Maven-way of specifying : build the 'generated source' stuff first (ie, Antlr-generated) THEN my 'manual' source?

Should I build a secondary Maven Project, which relies on the first project ?

Is there is a 'generic' (dynamic?) mechanism for building an ANTLR test rig class that doesn't really on Parser/Lexer class already existing ? (a 'ClassForName' JDBC-Style mechanism ?)

To Clarify the issue; here's a snippet of my Test Rig class, which is uncompilable (due to the Lexer / Parsers not having been built yet....):

...
    myLexer lexer = new myLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    myParser parser = new myParser(tokens);
    parser.file();
...

OK - after the advice below and double-checking the generated java - in fact I hadn't noticed that my Grammar file (although laid out in java package structure) did not contain a @header directive to specify a target java project for the generated classes to live in.

This was the reason that my test (or indeed my main program) was unable to see the Lexer/Parser classes. (I was simply trying to access java classes which were in a different package structure to what I had thought).

This other StackOverFlow case was the final clincher for me:

How to specify a target package for ANTLR?

1

1 Answers

2
votes

The most important thing is to configure the antlr3-maven-plugin correctly like this:

  <plugin>
    <groupId>org.antlr</groupId>
    <artifactId>antlr3-maven-plugin</artifactId>
    <version>3.4</version>
    <executions>
      <execution>
        <goals>
          <goal>antlr</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

furthermore to put the grammar into the correct location src/main/antlr3 plus the package name. By using this the grammer will generate the java code from it and will also make sure to compile the sources as well. I'm using ANTLR my own in a small project you might take a look as an example. Also you can see an example of a unit test which uses the generated parser.