Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Gradle 2.10将ANTLR lexer语法导入另一个语法?_Java_Gradle_Antlr_Antlr4 - Fatal编程技术网

Java 如何使用Gradle 2.10将ANTLR lexer语法导入另一个语法?

Java 如何使用Gradle 2.10将ANTLR lexer语法导入另一个语法?,java,gradle,antlr,antlr4,Java,Gradle,Antlr,Antlr4,我一直在学习关于ANTLR 4的特伦斯·帕尔的《权威ANTLR 4参考》,到目前为止,我一直在使用Gradle 2.10及其内置的ANTLR插件进行跟踪。然而,我在获取一些代码时遇到了一些问题,我从第4章第38-41页改编了这些代码,以便与我的Gradle构建脚本一起正常工作。(我之所以使用Gradle,而不是直接使用ANTLR,是因为我想最终将ANTLR集成到一个Java web应用程序中,这是我为我的论文所做的,我强烈希望使用一个构建工具来实现这一点,这样我就可以自动化ANTLR到Java代

我一直在学习关于ANTLR 4的特伦斯·帕尔的《权威ANTLR 4参考》,到目前为止,我一直在使用Gradle 2.10及其内置的ANTLR插件进行跟踪。然而,我在获取一些代码时遇到了一些问题,我从第4章第38-41页改编了这些代码,以便与我的Gradle构建脚本一起正常工作。(我之所以使用Gradle,而不是直接使用ANTLR,是因为我想最终将ANTLR集成到一个Java web应用程序中,这是我为我的论文所做的,我强烈希望使用一个构建工具来实现这一点,这样我就可以自动化ANTLR到Java代码的生成过程,并轻松地管理我的依赖关系。)

我创建了两个ANTLR 4语法(我在这个问题的末尾粘贴了它们):src/main/ANTLR/org/jbduncan/Expr.g4(标准语法)和src/main/ANTLR/org/jbduncan/CommonLexerRules.g4(词法语法),其中Expr.g4通过
import CommonLexerRules
语句依赖于CommonLexerRules.g4。但是,当我尝试使用我的build.gradle(也粘贴在问题的末尾以及我的gradle.properties)在命令行上运行
gradlew generateGrammarSource
)时,我得到以下错误:

12:45:21: Executing external task 'generateGrammarSource'...
:generateGrammarSource
error(110): org\jbduncan\Expr.g4:3:7: can't find or load grammar CommonLexerRules
warning(125): org\jbduncan\Expr.g4:12:11: implicit definition of token NEWLINE in parser
warning(125): org\jbduncan\Expr.g4:13:6: implicit definition of token ID in parser
warning(125): org\jbduncan\Expr.g4:19:6: implicit definition of token INT in parser
:generateGrammarSource FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':generateGrammarSource'.
> There was 1 error during grammar generation

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.166 secs
There was 1 error during grammar generation
12:45:24: External task execution finished 'generateGrammarSource'.
看看这个错误消息,在我看来,Gradle的ANTLR插件能够找到Expr.g4,但不知何故无法找到CommonLexerRules.g4

我曾尝试使用两个外部Gradle插件(和)而不是内置插件来解决此错误,但当我尝试每一个插件时,它们都引入了我无法解决的问题

使用直接从ANTLR网站下载的ANTLR 4.5.2 jar确实让我可以毫无问题地编译我的两个语法,但正如我前面所讨论的,这对我来说是一个非常不可取的选择,因为我必须手动编译语法,而我相信Gradle可以为我自动完成

我的问题是:如何解决上述错误,并让Gradle将我的lexer语法导入到我的其他语法中?


build.gradle

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'antlr'

sourceCompatibility = 1.8
targetCompatibility = 1.8

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

group = 'org.jbduncan'
version = '1.0-SNAPSHOT'
mainClassName = 'org.jbduncan.Application'

repositories {
    jcenter()
}

dependencies {
    antlr "org.antlr:antlr4:$antlrVersion"
    compile "com.google.guava:guava:$guavaVersion"
    testCompile "com.google.guava:guava-testlib:$guavaVersion"
    testCompile "com.google.truth:truth:$truthVersion"
    testCompile "junit:junit:$junitVersion"
}

// Send all generated source code to a directory other than build/, to workaround an issue in
// IntelliJ IDEA where it fails to recognise source files in build/.
def generatedJavaSourcesDir = 'src/generated/java'

generateGrammarSource {
    arguments += ['-visitor']
    outputDirectory = file(generatedJavaSourcesDir)
}

sourceSets {
    main {
        java {
            srcDir generatedJavaSourcesDir
        }
    }
}

clean {
    delete generatedJavaSourcesDir
}

task wrapper(type: Wrapper) {
    distributionUrl = "http://services.gradle.org/distributions/gradle-$gradleVersion-bin.zip"
}
gradle.properties

gradleVersion=2.10

# Dependency versions
antlrVersion=4.5
guavaVersion=19.0
junitVersion=4.12
truthVersion=0.28
src/main/antlr/org/jbduncan/Expr.g4

grammar Expr;

import CommonLexerRules; // includes all rules from CommonLexerRules.g4

@header {
    package org.jbduncan;
}

/** The start rule; begin parsing here. */
prog: stat+;

stat: expr NEWLINE # printExpr
    | ID '=' expr NEWLINE # assign
    | NEWLINE # blank
    ;

expr: expr op=('*'|'/') expr # MulDiv
    | expr op=('+'|'-') expr # AddSub
    | INT # int
    | ID # id
    | '(' expr ')' # parens
    ;

MUL: '*'; // assigns token name to '*' used above in grammar
DIV: '/';
ADD: '+';
SUB: '-';
lexer grammar CommonLexerRules; // note "lexer grammar"

ID: [a-zA-Z]+ ; // match identifiers
INT: [0-9]+ ; // match integers
NEWLINE: '\r'? '\n' ; // return newlines to parser (is end-statement signal)
WS: [ \t] -> skip ; // toss out whitespace
src/main/antlr/org/jbduncan/CommonLexerRules.g4

grammar Expr;

import CommonLexerRules; // includes all rules from CommonLexerRules.g4

@header {
    package org.jbduncan;
}

/** The start rule; begin parsing here. */
prog: stat+;

stat: expr NEWLINE # printExpr
    | ID '=' expr NEWLINE # assign
    | NEWLINE # blank
    ;

expr: expr op=('*'|'/') expr # MulDiv
    | expr op=('+'|'-') expr # AddSub
    | INT # int
    | ID # id
    | '(' expr ')' # parens
    ;

MUL: '*'; // assigns token name to '*' used above in grammar
DIV: '/';
ADD: '+';
SUB: '-';
lexer grammar CommonLexerRules; // note "lexer grammar"

ID: [a-zA-Z]+ ; // match identifiers
INT: [0-9]+ ; // match integers
NEWLINE: '\r'? '\n' ; // return newlines to parser (is end-statement signal)
WS: [ \t] -> skip ; // toss out whitespace
src/main/java/org/jbduncan/Application.java

package org.jbduncan;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public final class Application {
  public static void main(String[] args) throws IOException {
    String inputFile = (args.length > 0) ? args[0] : null;

    InputStream inputStream = (inputFile == null) ? System.in : new FileInputStream(inputFile);
    ANTLRInputStream input = new ANTLRInputStream(inputStream);
    ExprLexer lexer = new ExprLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExprParser parser = new ExprParser(tokens);
    ParseTree tree = parser.prog(); // parse; start at prog

    System.out.println(tree.toStringTree(parser)); // print tree as text
  }
}

我在Gradle上发布了我的问题后,终于找到了答案:

generateGrammarSource{

参数为什么要使用
导入CommonLexerRules;
而不是
选项{tokenVocab=CommonLexerRules;}
?@KvanTTT:原因很简单,因为我不知道
选项的存在,因为我在书中还没有涉及到它们(如果有涉及的话)。我现在不打算在睡觉的时候测试你的建议,但我明天应该有时间,所以我会让你知道它是否适合我。@KvanTTT:Hmm,它似乎不适合我。将导入替换为选项并运行
gradlew clean build
会产生一个错误,它声称
src\generated\java\Com找不到monLexerRules.tokens
(即使我可以在我的计算机上的项目文件夹中看到它).tokens文件是生成解析器所必需的…我想知道Gradle的ANTLR插件是否无法找到
CommonLexerRules.g4
CommonLexerRules.tokens
在某种程度上是相关的…您对@KvanTTT有其他建议吗?