eclipse 如何使用 ANTLR4 和 Maven 自动生成词法分析器+解析器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14926060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automatically generate lexer+parser with ANTLR4 and Maven?
提问by Vertex
I'am new to ANTLR4, and it seems that there is no Eclipse-Plug-In for v4. So it would nice to build automatically the Java sources from the .g4 grammars. I have a simple, empty Maven-project with src/main/java, src/test/java. Where to place the .g4 files? How can I automatically build the grammars with Maven?
我是 ANTLR4 的新手,似乎没有适用于 v4 的 Eclipse-Plug-In。因此,最好从 .g4 语法自动构建 Java 源代码。我有一个简单的空 Maven 项目,其中包含 src/main/java、src/test/java。将 .g4 文件放在哪里?如何使用 Maven 自动构建语法?
My own POM-test failed:
我自己的 POM 测试失败了:
<repository>
<id>mvn-public</id>
<name>MVNRepository</name>
<url>http://mvnrepository.com</url>
</repository>
...
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<goals>
<goal>antlr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Eclipse says:
Eclipse 说:
Failure to find org.antlr:antlr4-maven-plugin:pom:4.0.0 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of
central has elapsed or updates are forced
回答by Sam Harwell
I created the following Gist with a pom.xml
designed solelyfor supporting automatic code generation from ANTLR 4 grammars during an Eclipse build. It includes the necessary lifecycle information for m2e to know that the code generation is necessary, and explicitly adds the code generation folder using the build-helper-maven-plugin
since Eclipse seemed to have some trouble locating it otherwise.
我创建具有以下要旨pom.xml
设计仅用于期间Eclipse构建4个语法从ANTLR支撑自动代码生成。它包括必要的生命周期信息,让 m2e 知道代码生成是必要的,并使用 显式添加代码生成文件夹,build-helper-maven-plugin
因为 Eclipse 似乎很难找到它。
In this configuration, grammar files (*.g4
) are placed alongside the other Java source files. The Maven plugin will automatically add the proper package ...
statement to the generated files, so you shouldn't include a @header{package ...}
line in the grammar itself.
在此配置中,语法文件 ( *.g4
) 与其他 Java 源文件放在一起。Maven 插件会自动将正确的package ...
语句添加到生成的文件中,因此您不应@header{package ...}
在语法本身中包含一行。
回答by Kevin
Check out this Antlr4 plugin for Eclipse
查看这个用于 Eclipse 的 Antlr4 插件