Antlr4 import maven eclipse combination does not work, cannot find or load grammar

WITH

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

I am trying to use the ANTLR4 grammar import function. cm

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

This grammar is basically an attempt to divide https://github.com/antlr/grammars-v4/blob/master/iri/IRI.g4 into three parts:

  • syntactical analyzer
  • lexer
  • imported base lexers

At this point, I get an error:

can't find or load grammar LexBasic

I checked

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

and wrote an article on the wiki: http://wiki.bitplan.com/index.php/ANTLR_maven_plugin

Then I put the LexBasic.g4 file in the src / main / antlr4 / exports directory and the error persists. Only when I put LexBasic.g4 in src / main / antlr4 on the command line will everything work well. But this is similar to what the documentation says at http://www.antlr.org/api/maven-plugin/latest/ .

At https://gist.github.com/sharwell/4979017 I found a comment that the build-helper maven plugin is no longer needed. I still have to try.

During my trials over the past few days, I have had many quirks with different pom.xml configurations. I get error messages like:

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

or the eclipse environment will begin to regenerate java source files from .g4 files in an endless loop. I would need to call maven build from the menu to stop such a loop.

I'm still not sure if the correct way to set up the project using the parser lexicon and the imported lexer file would use antlr4, maven and eclipse.

  • What would be the right way to fix this?
  • Where can I find an example project?
+1
source share
2 answers

@Wolfgang, I am now in the middle of a similar task. I did not know about import. Thanks for this. Using the Maven configuration, as indicated here, in Antlr4 maven the plugin cannot find grammar files in different directories , I was able to get all compiled with the same success as using the import statement.However, I think the problem is with the ANTLR4 IDE plugin. Grammar compilation, but Eclipse should recognize the import. Also check out Sam's comments in the above post. I know that he was the author of ANTLR, and his insight sounds: perhaps what I (and you) are trying to do is not worth the effort. I will let you know what else I find. Greetings.

+1
source

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

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

Update

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

Another notice (while I was playing with your project) is that the antlr4ide plugin will generate LexBasic.g4 lexer every small change in each grammar file (depending on it), which seems annoying. I would recommend disabling the tool in the project settings ( ANTLR4/Tool ). In any case, you will compile the project through maven. That way, you can still use the basic antlr4ide functions (e.g. syntax highlighting, indexing) for the project, but it will not generate grammar for every small change.

+1
source

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


All Articles