微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么Idea找不到ANTLR4生成的源码?

如何解决为什么Idea找不到ANTLR4生成的源码?

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

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

enter image description here

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

enter image description here

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

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

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

[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

解决方法

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

  1. Deplorable.g4 移至 src/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(不仅是工具和运行时,还包括 antlr4-maven-plugin)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。