14

I'am new to ANTLR4, and it seems that there is no Eclipse-Plug-In for v4. So it would nice to build automatically the Java sources from the .g4 grammars. I have a simple, empty Maven-project with src/main/java, src/test/java. Where to place the .g4 files? How can I automatically build the grammars with Maven?

My own POM-test failed:

<repository>
    <id>mvn-public</id>
    <name>MVNRepository</name>
    <url>http://mvnrepository.com</url>
</repository>

...

<build>
    <plugins>
        <plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-maven-plugin</artifactId>
            <version>4.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>antlr</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Eclipse says:

Failure to find org.antlr:antlr4-maven-plugin:pom:4.0.0 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of
central has elapsed or updates are forced
Vertex
  • 2,682
  • 3
  • 29
  • 43

2 Answers2

17

I created the following Gist with a pom.xml designed solely for supporting automatic code generation from ANTLR 4 grammars during an Eclipse build. It includes the necessary lifecycle information for m2e to know that the code generation is necessary, and explicitly adds the code generation folder using the build-helper-maven-plugin since Eclipse seemed to have some trouble locating it otherwise.

In this configuration, grammar files (*.g4) are placed alongside the other Java source files. The Maven plugin will automatically add the proper package ... statement to the generated files, so you shouldn't include a @header{package ...} line in the grammar itself.

https://gist.github.com/sharwell/4979017

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Thank you! When I comment the build-helper-maven-plugin out in pom.xml, ANTLR builds the lexer and parser for the placed grammar. But with build-helper-maven-plugin Eclipse says: "Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:build-helper-maven-plugin:1.7:add-source (execution: default, phase: generate-sources)". Without manually adding target/generatet-sources/antlr4 as a src-folder, I can't use the generated lexer/parser. And another question: How to put arguments? `org.example` doesn't work. – Vertex Feb 18 '13 at 20:31
  • Eclipse will automatically download the proper extension to work with the build helper plugin if you tell it to. The package clause is always added automatically by the antlr4 goal based on the location of the .g4 file within your source structure. To change the package of the generated code, move the grammar itself to the desired package in the source tree. – Sam Harwell Feb 18 '13 at 20:39
  • Do you have a NetBeans version? – Jeffrey Guenther Aug 12 '13 at 18:49
  • 1
    @JeffreyGuenther NetBeans works great with "plain" Maven configurations, so the biggest change is to simply strip out the Eclipse-specific stuff. https://gist.github.com/sharwell/6214103 – Sam Harwell Aug 12 '13 at 19:15
  • May I add that it's worth typing `true` where the gist just says ``, so that the generated code is refreshed everytime the grammar source is updated? – Adrian Panasiuk Nov 11 '13 at 21:01
  • @Vertex to add arguments, place the configuration tag directly in the `` tag: `......` – Benjamin Spiegel Jul 06 '21 at 16:43
1

Check out this Antlr4 plugin for Eclipse

https://github.com/jknack/antlr4ide

Kevin
  • 164
  • 2
  • 4