1
votes

With

  • ANTLR 4.7
  • ANTLR 4.7 maven plugin
  • Eclipse Neon 4.6.2

i am trying to use the grammar import feature of ANTLR4. see

with the example iri grammar https://github.com/BITPlan/com.bitplan.antlr/tree/master/src/main/antlr4/com/bitplan/iri

That grammar is basically the attempt to split the https://github.com/antlr/grammars-v4/blob/master/iri/IRI.g4 into three parts:

  1. parser
  2. lexer
  3. imported basic lexer parts

At this point I am getting the error message:

can't find or load grammar LexBasic

I checked

and filed a bug report with https://github.com/antlr/antlr4/issues/2061

and wrote a wiki article at: http://wiki.bitplan.com/index.php/ANTLR_maven_plugin

I then put the LexBasic.g4 file into the src/main/antlr4/imports directory and the error persisted. Only when i put LexBasic.g4 into src/main/antlr4 on the command line things would work nicely. But that seems to be different then what the documentation at http://www.antlr.org/api/maven-plugin/latest/ says.

In https://gist.github.com/sharwell/4979017 I found a comment that the build-helper maven plugin is not needed any more. I'll still have to try this.

During my trials of the last few days I had lots of quirks with different pom.xml configurations. I would get error messages like:

  • can't find or load grammar LexBasic
  • reference to undefined rule

or the eclipse environment would start regenerating the java source files from the .g4 files in a non ending loop. I would have to call maven build from the menu to stop such a loop.

I am still not sure what the correct way to setup a project with parser lexer and imported lexer file would be using antlr4, maven and eclipse.

  • What would be the right way to set this up?
  • Where can I find an example project?
2

2 Answers

1
votes

@Wolfgang, I'm actually in the middle of a similar task right now. I didn't know about the imports option. Thanks for that. By using the Maven configuration as referenced here in Antlr4 maven plugin cannot find grammar files in different directories, I was able to get everything compiled with as much success as using the import statement. However, I think the issue is with ANTLR4 IDE plugin itself. The grammars compile, but Eclipse should recognize the import. Also check out Sam's comments in the above post. I know he has been a contributor to ANTLR and his insight is sound: perhaps what I (and you) am trying to do is not worth the effort. I'll let you know what else I find. Cheers.

1
votes

I had the same issue with ANTLR 4.7, ANTLR v4 Maven Plugin, ANTLR 4 IDE Eclipse Plugin, and Eclipse Oxygen.

If you see the error message can't find or load grammar in the problems view of Eclipse, it comes from the ANTLR 4 IDE Eclipse Plugin. So if you followed the guidelines of the antlr4 maven documentation (e.g. you put your grammar/lexer files under the src/main/antlr4 directory), that's fine and your maven project should compile without errors. As I understand it from your question, this is the case for you too. To get rid of the error message of the eclipse plugin I also needed to add the same directory as a source directory to my project (i.e., go to project settings/Java Build Path/(Source tab), and add e.g. the folder of src/main/antlr4/.../(YourGrammar.g) to the source folders.

Update

I tried to compile your github repo, and your pom seemed to be fine regardless some missing dependencies. I also noticed that you use org.antlr.v4.runtime.tree.gui.TreeViewer in the example which is not available since antlr 4.5.1, so you either use an earlier antlr version (e.g. 4.5), or uncomment this code. I created a pull request for you, so you can see my changes.

One other notice (while I was playing with your project) is that the antlr4ide plugin will generate the LexBasic.g4 lexer every small change in each grammar file (depending on it) which seems to be annoying. I would recommend deactivating the tool in the project settings (ANTLR4/Tool). You will compile the project through maven anyway. So you can still use the basic features of antlr4ide (e.g. syntax highlight, indexing) for the project but it will not re-generate the grammar for every small change.