1

我有一个项目,我想在一段 Kotlin 代码中使用由 ANTLR4 生成的类。

pom.xml中,ANTLR4 配置如下。

<dependencies>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-runtime</artifactId>
        <version>4.7.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-maven-plugin</artifactId>
            <version>4.7.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>antlr4</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

生成的类被放入target/generated-sources/antlr4

在此处输入图像描述

mvn clean package, mvn clean assembly, 以及在 Idea 中重建项目导致以下错误:

在此处输入图像描述

请注意,错误仅发生在 Kotlin 类Transpiler.kt中,而不发生在test中。

我该如何解决这个问题(确保 ANTLR4 生成的类可以在 Kotlin 代码中使用)?

更新1:按照@Bart Kiers 的建议移动语法文件并执行mvn clean antlr4:antlr4后,Idea 中的错误消失了。但是mvn clean antlr4:antlr4 install仍然会导致构建错误

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.4.21:compile (compile) on project elispt: Compilation failure: Compilation failure: 
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[9,21] Unresolved reference: DeplorableLexer
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[11,22] Unresolved reference: DeplorableParser
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[12,21] Unresolved reference: DeplorableParser
4

1 回答 1

3

如果您执行以下操作,它应该可以工作:

  1. 移动Deplorable.g4src/main/antlr4/com/dpisarenko/deplorable/(注意你把它放在里面src/main/antlr4/com.dpisarenko.deplorable/!)
  2. mvn clean antlr4:antlr4
  3. 如果尚未完成,请标记target/generated-sources/antlr4为“Generated Sources Root”(在您的 IDE 中右键单击它并选择Mark Directory as

如果没有,请尝试使用最新的 ANTLR4 版本:(4.9.1不仅是工具和运行时,还有 for antlr4-maven-plugin)。

于 2021-01-19T20:53:09.080 回答