问题描述
我在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令牌文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!