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

1

8

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.

Flavone answered 8/8, 2013 at 10:46 Comment(0)
L
12

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.

Lapful answered 30/10, 2013 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.