1

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?

Community
  • 1
  • 1
monojohnny
  • 5,894
  • 16
  • 59
  • 83

1 Answers1

2

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.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Thanks for this - I just checked my pom.xml - and it has this bit set up correctly - what I'm trying to do is test my compiled Java Parser/Lexer - and to do that I have have to references to these classes before they 'exist'. (So I'm struggling with the fact that I have generated code from antlr - and a manually built test-rig which refers to those classes). – monojohnny Dec 27 '12 at 17:44
  • That is no problem, cause Maven will make sure that the grammar has been used to generate the parser/lexer before the real code will be compiled and the test code will be compiled based on the [life-cycle](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html). Or may be i misunderstand your question? – khmarbaise Dec 27 '12 at 17:47
  • I think I might have a very specific problem with my Netbeans setup or something - the IDE is insisting that my (manual/test-rig) code will not compile upon build....it sounds like in principle this should work though - I'll take a look at your github project as well - if I can get that compiled and working, maybe it'll give me the clue I need. Thanks again. – monojohnny Dec 27 '12 at 17:50
  • Where is the manual/test-rig folder located? src/test/java ? If it's not than your a completely wrong in basic Maven usage (unit tests have to be located into src/test/java/. – khmarbaise Dec 27 '12 at 17:52
  • ah - that could be it....yes, I should (obviously now I think about it) treat this as a unit test, rather than a 'calling' program wrapping round the parser/lexer - I'll give that a go - thanks again. – monojohnny Dec 28 '12 at 10:09
  • To be more accurate this is not really a unit test but in this case it's ok to look at it as a unit test. It's of course an integration test. To be scrupulous it should be named like *IT.java and see as an integration and let it run in the integration test lifecycle phase via maven-failsafe-plugin but it's ok. – khmarbaise Dec 28 '12 at 10:15
  • ok - I think is turning out to be a general problem with my Maven configuration - my test classes are simply not able to pick up the the required generated->compiled Lexer/Parser classes - I get an 'Uncompilable source code - cannot find symbol symbol : class myLexer location mypackage' when running unit tests. (I re-wrote my test rig as 'pretend' unit test, so I can use 'mvn test' to drive it)..... – monojohnny Dec 28 '12 at 10:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21816/discussion-between-monojohnny-and-khmarbaise) – monojohnny Dec 28 '12 at 10:57
  • Thanks for all the tips here - It allowed me to eliminate certain cases of error - and I'll mark this as the accepted answer as well - since your answer was correct and confirmed the correct configuration of the ANTRL plugin ; the issue just turned out to be a confusion of what the generated classes's packages were. Thanks – monojohnny Dec 28 '12 at 14:10