如何单独指定由 Antlr 生成的 Java 文件路径和 tokens 文件路径?

huangapple go评论52阅读模式
英文:

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/importsantlr4-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 -->

  &lt;plugin&gt;
    &lt;groupId&gt;org.antlr&lt;/groupId&gt;
    &lt;artifactId&gt;antlr4-maven-plugin&lt;/artifactId&gt;
    &lt;executions&gt;
      &lt;execution&gt;
        &lt;goals&gt;
          &lt;goal&gt;antlr4&lt;/goal&gt;
        &lt;/goals&gt;
        &lt;configuration&gt;
          &lt;visitor&gt;true&lt;/visitor&gt;
          &lt;listener&gt;false&lt;/listener&gt;
        &lt;/configuration&gt;
      &lt;/execution&gt;
    &lt;/executions&gt;
  &lt;/plugin&gt;

This assumes you have your grammar file in src/main/antlr4/&lt;package&gt;/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 &lt;outputDirectory&gt;src/main/java&lt;/outputDirectory&gt; 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.

huangapple
  • 本文由 发表于 2023年2月8日 21:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定