Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a grammar in Antlr4 when building with Maven?

Tags:

maven

antlr

I'm trying to build an ANTLR project with Maven, the project structure is like

src/main +
      |-antlr4
             |-fo/bar
                   |-G1.g4
                   |-G2.g4
             |-imports

in which G1 imports G2 like:

grammar G1;
import G2;

However, when I build with Maven, I get the following error:

[ERROR] Message{errorType=CANNOT_FIND_IMPORTED_GRAMMAR, args=[G2, fo\bar\G1.g4], e=null, fileName='null', line=-1, charPosition=-1}

It works with no problem if I put G1.g4 and G2.g4 directly under antlr4 directory. It seems to me that, when put the Grammar files in a package, it needs special syntax in the import statement.

I noticed the question Antlr4 maven plugin cannot find grammar files in different directories , but here, the grammars are in one directory.

like image 251
Wudong Avatar asked Aug 08 '13 10:08

Wudong


1 Answers

I think you need the G2.g4 grammar in the imports package (src/main/antlr4/imports), but you will have duplicated your grammar. To avoid this, change the import setting of the ANTLR plugin like this:

<build>
  <plugins>
    <plugin>
      <groupId>org.antlr</groupId>
      <artifactId>antlr4-maven-plugin</artifactId>
      <version>4.1</version>
      <!-- vvv key part here vvv -->
      <configuration>
        <libDirectory>${basedir}/src/main/antlr4/fo/bar</libDirectory>
      </configuration>
      <!-- ^^^ key part here ^^^ -->
      <executions>
        <execution>
          <goals>
            <goal>antlr4</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Note the configuration libDirectory.

like image 59
Roberto Avatar answered Oct 27 '22 01:10

Roberto