问题描述
我在 myproject/src/main/antlr/com/mypackage
中创建了一个文件 MyLexer.g4
像:
I created a file MyLexer.g4
inside myproject/src/main/antlr/com/mypackage
like:
lexer grammar MyLexer;
DIGIT : '0' .. '9' ;
...
WS : [ \t\r\n]+ -> skip ;
然后尝试在同一目录中的 MyParser.g4
中编写解析器:
and then trying to write parser in MyParser.g4
in the same directory:
grammar MyParser;
options
{ tokenVocab = MyLexer; }
SHORT_YEAR: DIGIT DIGIT;
不幸的是,当我运行 generateGrammarSource
的 gradle 任务时,出现以下错误:
unfortunately, when I run gradle task of generateGrammarSource
, the following error occurs:
error(160): com\mypackage\MyParser.g4:4:18: cannot find tokens file MYPROJECT\build\generated-src\antlr\main\MyLexer.tokens
即文件在不正确的位置查找.
I.e. file is sought in incorrect place.
实际文件在 MYPROJECT\build\generated-src\antlr\main\com\mypackage\MyLexer.tokens 中创建
推荐答案
正如 Steven Spungin 所说,您需要将 ANTLR 源文件放在目录 src/main/antlr/
中,而不是放在目录中该目录的子目录.
As Steven Spungin said, you need to put your ANTLR source files in the directory src/main/antlr/
and not in a subdirectory of that directory.
您不需要将 @header
添加到您的 ANTLR 源文件中.请改用以下内容.
You do not need to add the @header
to your ANTLR source files. Use the following instead.
在您的 build.gradle
中,您应该具有(针对您的 ANTLR 版本进行修改):
In your build.gradle
you should have (modified for your version of ANTLR):
apply plugin: 'antlr'
dependencies {
antlr "org.antlr:antlr4:4.7.1"
}
generateGrammarSource {
arguments += ['-package', 'com.mypackage']
outputDirectory = new File(buildDir.toString() + "/generated-src/antlr/main/com/mypackage/")
}
这篇关于Gradle 找不到 Antlr 令牌文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!