I have a project in which I want to use classes generated by ANTLR4 in a piece of Kotlin code.
In pom.xml , ANTLR4 is configured as follows.
<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>
The generated classes are put into target/generated-sources/antlr4
:
mvn clean package
, mvn clean assembly
, as well as rebuilding the project in Idea lead to the following error:
Note that the errors occur only in the Kotlin class Transpiler.kt , but not in the test .
How can I fix this (make sure that classes generated by ANTLR4 can be used in Kotlin code)?
Update 1: After moving the grammar file as suggested by @Bart Kiers and executing mvn clean antlr4:antlr4
, the errors in Idea disappeared. However mvn clean antlr4:antlr4 install
still results in 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
It should work if you do the following:
Deplorable.g4
to src/main/antlr4/com/dpisarenko/deplorable/
(note that you placed it inside src/main/antlr4/com.dpisarenko.deplorable/
!)mvn clean antlr4:antlr4
target/generated-sources/antlr4
as the "Generated Sources Root" (right click it in your IDE and select Mark Directory as
) If not, try using the latest ANTLR4 version: 4.9.1
(not just the tool and runtime, but also for antlr4-maven-plugin
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.