Gradle找不到Antlr令牌文件 [英] Gradle can't find Antlr token file

查看:276
本文介绍了Gradle找不到Antlr令牌文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆