Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically generate lexer+parser with ANTLR4 and Maven?

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
like image 231
Vertex Avatar asked Feb 17 '13 21:02

Vertex


People also ask

In which folder will you find ANTLR grammar definition files?

This file is located in /src/main/antlr (as the default source folder of the antlr production grammar files).

What is Lexer and parser in ANTLR?

A lexer (often called a scanner) breaks up an input stream of characters into vocabulary symbols for a parser, which applies a grammatical structure to that symbol stream.

How does ANTLR parser work?

ANTLR (ANother Tool for Language Recognition) is a tool for processing structured text. It does this by giving us access to language processing primitives like lexers, grammars, and parsers as well as the runtime to process text against them. It's often used to build tools and frameworks.

What does ANTLR generate?

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.


2 Answers

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

like image 57
Sam Harwell Avatar answered Oct 14 '22 19:10

Sam Harwell


Check out this Antlr4 plugin for Eclipse

https://github.com/jknack/antlr4ide

like image 41
Kevin Avatar answered Oct 14 '22 18:10

Kevin