ANTLR4 在导入时找不到语法 [英] ANTLR4 does not find grammar on import
问题描述
我正在尝试将我的 ANTLR4 语法拆分为多个文件,以便我可以更轻松地测试它们,我在 Java 项目中使用 gradle 作为构建工具.
I am trying to split my ANTLR4 grammar in multiple files so i can test them more easily, i am using gradle as a build tool in a java project.
两种语法都可以通过单独的方式正确编译,但是当我将导入添加到主语法时,出现下一个编译错误
Both grammar compile correctly by separate but when i add the import to my main grammar i get the next compilation error
错误(110):kanekotic/specflow/rider/SpecflowFeature.g4:3:7:无法找到或加载语法SpecflowScenario
error(110): kanekotic/specflow/rider/SpecflowFeature.g4:3:7: can't find or load grammar SpecflowScenario
继承的
语法如下:
grammar SpecflowScenario;
@header {
package kanekotic.specflow.rider;
}
scenario
: 'Scenario: ';
和 main
语法看起来像:
and the main
grammar looks like:
grammar SpecflowFeature;
import SpecflowScenario;
@header {
package kanekotic.specflow.rider;
}
file returns [List<String> values]
@init { $values = new ArrayList<String>(); }
: 'Feature: ' EOF;
我做错了什么?这是不允许的吗?
What am i doing wrong? is this not allowed?
gradle.build
看起来像:
plugins {
id "org.jetbrains.intellij" version "0.1.10"
}
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
apply plugin: 'antlr'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
intellij {
version '143.2370.31'
pluginName 'Specflow Rider'
}
group 'kanekotic.specflow.rider'
version '0.1'
repositories {
mavenCentral()
jcenter()
}
dependencies {
antlr "org.antlr:antlr4:4.5"
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.+"
}
所有开源代码都在下一个链接中:https://github.com/kanekotic/Specflow.Rider/tree/antlr4_multiple_grammar
all the code as it is open sourced is in this next link: https://github.com/kanekotic/Specflow.Rider/tree/antlr4_multiple_grammar
推荐答案
Antlr 插件默认使用 src/main/antlr
作为 lib 目录.由于要包含的语法文件位于 kanekotic/specflow/rider
中,因此请在您的 gradle 文件中使用以下代码来包含此位置:
The Antlr plugin uses src/main/antlr
as lib directory by default. As the grammar file to include is in kanekotic/specflow/rider
, use the following code in your gradle file to include this location:
generateGrammarSource {
arguments << "-lib" << "src/main/antlr/kanekotic/specflow/rider"
}
另见 这个 gradle 线程.
这篇关于ANTLR4 在导入时找不到语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!