Antlr4 maven plugin cannot find grammar files in different directories
Asked Answered
H

2

4

I'm using the antlr4 maven plug-in to build my maven project which uses antlr4:

     <groupId>org.antlr</groupId>
     <artifactId>antlr4-maven-plugin</artifactId>
     <version>4.0</version>

I started with one grammar file and got my pom.xml set-up and everything was building nicely.

Then I decided to split my grammar into logical parts and therefore used several grammar files but in different directories (so the generated code would be put into separate packages) but still all under the same root src/main/antlr4 directory.

I use the import statement in my "top" level grammar file to import the other required files.

But now maven gives me the following error when trying to build:

[ERROR] Message{errorType=CANNOT_FIND_IMPORTED_GRAMMAR

Why can't antlr find the other files that I am importing?

thanks, Ryan.

Honebein answered 13/6, 2013 at 14:19 Comment(0)
W
5

I had the same problem and solved with the following configuration:

<libDirectory>${basedir}/src/main/antlr4/yourGrammarDirectory</libDirectory>

Look here: How to import grammar in Antlr4 to build with maven

Worried answered 30/10, 2013 at 10:31 Comment(0)
B
1

For the ANTLR 4.0 release, no testing was performed on imports across multiple directories.

Due to the limited benefits (IMO) provided by the current grammar import mechanism, this is currently a very low priority feature. Currently, sharing grammar files by using imports will not allow you to share code for the generated parsers or the parse trees they produce. I've been using ANTLR for years on dozens of products (including commercial releases), and not once have I found composite grammars to provide more benefits than trouble. (Note that I'm talking about the import statement here. Separating lexer and parser grammars into separate files in the same directory is frequently beneficial and my preferred way to work.)

Bottoms answered 13/6, 2013 at 15:11 Comment(2)
Thanks alot for the clarification. I've reverted back to a single grammar file again.Honebein
Now this is really sad. I got frustrated with the URL parsing which involves lots of rules but is just a single element of a grammar. It would be very useful to modularize grammars in some way. If the import statement doesn't cut it what does? See #46872905 and the link to the bug report I filed regarding useability.Guadalcanal

© 2022 - 2024 — McMap. All rights reserved.