How to import grammar into Antlr4 for assembly using maven

I am trying to create an antlr project with maven, the project structure is similar to

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

in which G1 imports G2 as such:

 grammar G1; import G2; 

However, when compiling with maven, I got the following error:

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

This is not a problem if I put G1.g4 and G2.g4 directly in the antlr4 directory. It seems to me that when placing Grammer files in a package, does it need a special syntax to reference it in the import statement?

I notice this problem: Antlr4 maven plugin cannot find grammar files in different directories

but here the grammars are in the same directory .

+6
source share
1 answer

I think you need G2.g4 grammar in the import package (src / main / antlr4 / import). But you will duplicate your grammar. To avoid this, change the import setting of the antlr plugin as follows:

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

Check out the libDirectory configuration.

+7
source

Source: https://habr.com/ru/post/951299/


All Articles