antlr4 import maven eclipse组合不起作用找不到或加载语法 [英] antlr4 import maven eclipse combination does not work can't find or load grammar
问题描述
使用
- ANTLR 4.7
- ANTLR 4.7 maven插件
- Eclipse Neon 4.6.2
我正在尝试使用ANTLR4的语法导入功能. 见
带有示例iri语法示例 https://github.com. com/BITPlan/com.bitplan.antlr/tree/master/src/main/antlr4/com/bitplan/iri
该语法基本上是尝试拆分 https: //github.com/antlr/grammars-v4/blob/master/iri/IRI.g4 分为三个部分:
- 解析器
- lexer
- 导入的基本词法分析器部分
这时我收到错误消息:
can't find or load grammar LexBasic
我检查了
并向提交了错误报告 https://github.com/antlr/antlr4/issues/2061
并在以下位置写了Wiki文章: http://wiki.bitplan.com/index.php/ANTLR_maven_plugin
然后我将LexBasic.g4文件放入src/main/antlr4/imports目录,然后 错误仍然存在.仅当我将LexBasic.g4放入src/main/antlr4时 在命令行上,一切都会很好地进行.但这似乎与 http://www.antlr上的文档有所不同. org/api/maven-plugin/latest/说.
在 https://gist.github.com/sharwell/4979017 我发现有一条评论,即不再需要build-helper maven插件.我仍然需要尝试这个.
在最近几天的试用期间,我有很多具有不同pom.xml配置的怪癖.我会收到如下错误消息:
-
can't find or load grammar LexBasic
-
reference to undefined rule
或eclipse环境将开始以非终止循环从.g4文件重新生成Java源文件.我必须从菜单中调用maven build才能停止这样的循环.
我仍然不确定使用解析器词法分析器和导入的词法分析器文件设置项目的正确方法是使用antlr4,maven和eclipse.
- 什么是正确的设置方式?
- 我在哪里可以找到示例项目?
我在ANTLR 4.7,ANTLR v4 Maven插件,ANTLR 4 IDE Eclipse插件和Eclipse Oxygen中遇到了相同的问题.
如果在Eclipse的问题视图中看到错误消息can't find or load grammar
,则该错误消息来自 ANTLR 4 IDE Eclipse插件.因此,如果您遵循 antlr4 maven文档的指南(例如,您将您的src/main/antlr4
目录下的语法/词法分析器文件),就可以了,并且您的maven项目应该编译没有错误.从您的问题中我了解到,您的情况也是如此.为了摆脱eclipse插件的错误消息,我还需要将与源目录相同的目录添加到我的项目中(即,转到project settings/Java Build Path/(Source tab)
,然后将例如src/main/antlr4/.../(YourGrammar.g)
的文件夹添加到源文件夹. /p>
更新
我试图编译您的github存储库,无论缺少一些依赖项,您的pom看起来都很好.我还注意到您在示例中使用了org.antlr.v4.runtime.tree.gui.TreeViewer
,该示例从antlr 4.5.1开始不可用,因此您可以使用早期的antlr版本(例如4.5),也可以取消注释此代码.我为您创建了一个提取请求,以便您可以看到我的更改.
另一个注意事项(当我在玩您的项目时)是antlr4ide插件将在每个语法文件中的每一个小变化(取决于它)生成LexBasic.g4 lexer,这似乎很烦人.我建议在项目设置(ANTLR4/Tool
)中停用该工具.无论如何,您将通过Maven编译项目.因此,您仍然可以为项目使用antlr4ide的基本功能(例如语法高亮显示,索引编制),但不会为每个小的更改重新生成语法.
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:
- parser
- lexer
- 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?
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.
这篇关于antlr4 import maven eclipse组合不起作用找不到或加载语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!