antlr4 导入maven eclipse 组合不起作用找不到或加载语法 [英] antlr4 import maven eclipse combination does not work can't find or load grammar
问题描述
与
- ANTLR 4.7
- ANTLR 4.7 Maven 插件
- Eclipse 霓虹灯 4.6.2
我正在尝试使用 ANTLR4 的语法导入功能.见
i am trying to use the grammar import feature of ANTLR4. see
以 iri 语法为例https://github.com/BITPlan/com.bitplan.antlr/tree/master/src/main/antlr4/com/bitplan/iri
该语法基本上是试图拆分 https://github.com/antlr/grammars-v4/blob/master/iri/IRI.g4 分成三部分:
That grammar is basically the attempt to split the https://github.com/antlr/grammars-v4/blob/master/iri/IRI.g4 into three parts:
- 解析器
- 词法分析器
- 导入基本词法分析器部分
此时我收到错误消息:
无法找到或加载语法 LexBasic
我检查过
并提交了一个错误报告https://github.com/antlr/antlr4/issues/2061
并在以下位置写了一篇维基文章:http://wiki.bitplan.com/index.php/ANTLR_maven_plugin
and wrote a wiki article at: 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/ 说.
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.
在https://gist.github.com/sharwell/4979017我发现不再需要 build-helper maven 插件的评论.我还是得试试这个.
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.
在过去几天的试验中,我遇到了很多不同 pom.xml 配置的怪癖.我会收到如下错误消息:
During my trials of the last few days I had lots of quirks with different pom.xml configurations. I would get error messages like:
无法找到或加载语法 LexBasic
对未定义规则的引用
或者 eclipse 环境将开始在非结束循环中从 .g4 文件重新生成 java 源文件.我必须从菜单中调用 maven build 来停止这样的循环.
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.
我仍然不确定使用 antlr4、maven 和 eclipse 使用解析器词法分析器和导入的词法分析器文件设置项目的正确方法是什么.
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.
- 正确的设置方式是什么?
- 在哪里可以找到示例项目?
推荐答案
我在 ANTLR 4.7、ANTLR v4 Maven 插件、ANTLR 4 IDE Eclipse 插件和 Eclipse Oxygen 中遇到了同样的问题.
I had the same issue with ANTLR 4.7, ANTLR v4 Maven Plugin, ANTLR 4 IDE Eclipse Plugin, and Eclipse Oxygen.
如果您在 Eclipse 的问题视图中看到错误消息 can't find or loadgrammar
,它来自 ANTLR 4 IDE Eclipse Plugin.因此,如果您遵循 antlr4 maven 文档 的准则(例如,您将src/main/antlr4
目录下的语法/词法分析器文件),这很好,您的 maven 项目应该可以无误地编译.正如我从你的问题中所理解的,这对你来说也是如此.为了摆脱 eclipse 插件的错误消息,我还需要将与源目录相同的目录添加到我的项目中(即,转到 project settings/Java Build Path/(Source tab)
,并将例如 src/main/antlr4/.../(YourGrammar.g)
文件夹添加到源文件夹中.
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.
更新
我尝试编译您的 github 存储库,无论缺少某些依赖项,您的 pom 似乎都很好.我还注意到您在示例中使用了 org.antlr.v4.runtime.tree.gui.TreeViewer
,该示例自 antlr 4.5.1 以来不可用,因此您要么使用较早的 antlr 版本(例如 4.5),或取消注释此代码.我为你创建了一个拉取请求,所以你可以看到我的更改.
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.
另一个注意事项(当我在玩你的项目时)是 antlr4ide 插件将生成 LexBasic.g4 词法分析器每个语法文件(取决于它)的每个小变化,这似乎很烦人.我建议在项目设置 (ANTLR4/Tool
) 中停用该工具.无论如何,您将通过 Maven 编译项目.因此,您仍然可以在项目中使用 antlr4ide 的基本功能(例如语法高亮、索引),但它不会为每个小的更改重新生成语法.
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 导入maven eclipse 组合不起作用找不到或加载语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!