英文:
How to specify separately the path of the java file and the tokens file generated by Antlr?
问题
抱歉,代码部分不提供翻译。您描述的问题是关于使用ANTLR4 Maven插件解析自定义格式文件时的路径设置问题。目前,您希望能够分别设置.tokens
文件和.java
文件的路径,而不是使用通用路径。您想将.tokens
文件放在src/main/resources
,而.java
文件放在src/main/java/org/trance233/antlr/
。
英文:
I`m attempting to use antlr4 in my project to parse files in custom format. But when I use antlr4 maven plugin, I cannot find a parameter to set separately the path of tokens file and java file, as it can only set a common path.
Here`s the configuration of antlr4 maven plugin:
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.11.1</version>
<executions>
<execution>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<libDirectory>src/main/antlr4/imports</libDirectory>
<sourceDirectory>src/main/antlr4</sourceDirectory>
<outputDirectory>src/main/java</outputDirectory>
<listener>true</listener>
<visitor>true</visitor>
<treatWarningsAsErrors>true</treatWarningsAsErrors>
</configuration>
</plugin>
The grammar file test.g4
is in <sourceDirectory>/relativePath(src/main/antlr4/org/trance233/antlr/
).
The .tokens
file is in <outputDirectory>(src/main/java/
), the .java
file is in <sourceDirectory>/relativePath(src/main/java/org/trance233/antlr/
).
Is there any way that can set the path separately? Such as the .tokens
file above is put in src/main/resources
, and the .java
file is put in src/main/java/org/trance233/antlr/
.
答案1
得分: 1
没有。
但是你可以移除.tokens
文件:它在运行时不需要用于生成的解析器类。
英文:
> Is there any way that can set the path separately?
No.
But you can remove the .tokens
file: it is not needed at runtime for your generated parser classes.
答案2
得分: 0
antlr4的配置通常非常简单,如下所示:
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
<configuration>
<visitor>true</visitor>
<listener>false</listener>
</configuration>
</execution>
</executions>
</plugin>
假设你的语法文件位于 src/main/antlr4/<package>/test.g4
(默认位置)。无需进行其他配置,因为按照约定生成的类将位于 target/generated-sources/antlr4/
目录中。在该目录中,你将找到 *.tokens
文件。导入目录默认定义为 src/main/antl4/imports
。antlr4-maven-plugin 的默认配置定义了一些非常有用的默认值,并强烈建议遵循这些约定。
我强烈建议不要配置 <outputDirectory>src/main/java</outputDirectory>
,因为这将在你的 src/
目录树中生成代码,可能会引发版本控制系统的问题。
顺便说一句,将代码生成绑定到 compile
阶段非常不好,因为代码生成应该在编译阶段之前完成。generate-sources
阶段正是为这种情况而设计的。antlr4-maven-plugin 默认使用此阶段。
英文:
A configuration for antlr4 is usually very simple like this:
<!-- language:language-xml -->
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
<configuration>
<visitor>true</visitor>
<listener>false</listener>
</configuration>
</execution>
</executions>
</plugin>
This assumes you have your grammar file in src/main/antlr4/<package>/test.g4
(default location). There is no need to configure such things etc. because by conventions the generated class will be available in target/generated-sources/antlr4/
. Exactly in this directory you will find the *.tokens
files. The imports directory is defined by default as src/main/antl4/imports
. The default configuration for the antlr4-maven-plugin defines some very useful defaults and it is strongly recommended to follow those conventions.
I strongly recommmend NOT to configure the <outputDirectory>src/main/java</outputDirectory>
because that will generate code in your src/
directory tree which can cause issues with your versions control system.
BTW: Binding the code generation to the phase compile
is very bad because the code generation should be done before the compile phase. The generate-sources
phase is exactly intended for such cases. This phase is used by default by the antlr4-maven-plugin.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论