gpt4 book ai didi

antlr - 用 gradle 编译 3.2 Antlr 语法

转载 作者:行者123 更新时间:2023-12-04 22:49:35 27 4
gpt4 key购买 nike

我正在尝试用 gradle 编译我的 antlr 语法。我对 gradle 很陌生,所以我无法解决如何解决这个问题。

我认为它正在尝试使用 2.7 antlr 进行编译(因为我已经看到其他一些人在使用错误版本时报告了类似的错误),因此会引发错误。

我怎样才能:

  • 显示正在尝试使用哪个版本的 Antlr gradle?
  • 获取 gradle 以正确编译?

  • 这是我的语法:
    grammar Test;

    options {
    language = Java;
    }

    rule: ;

    这是我的gradle脚本:
    apply plugin: 'java'
    apply plugin: 'antlr'

    repositories {
    mavenCentral()
    }

    dependencies {
    antlr 'org.antlr:antlr:3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    }

    这是尝试编译的输出:
    $ gradle compileJava
    :generateGrammarSource
    /home/admin/workspace/BuildTools/src/main/antlr/Test.g:1:1: unexpected token: grammar
    :compileJava UP-TO-DATE

    BUILD SUCCESSFUL

    Total time: 2.458 secs

    编辑:

    似乎 gradle 尚不直接支持 Antlr3。

    有一个 pull request to add antlr3 supportdiscussed here gradle。

    这是 another version of including support for Antlr3 手动。

    最佳答案

    为了完整起见,我为我的项目提出了以下 gradle 构建文件,该文件采用了来自 tapestryjava blogspot 的版本。并添加了一些评论。

    我唯一需要改变的是不使用动态属性来删除警告。

    apply plugin: 'java'

    project.ext.grammarpackage = "eclipse"

    repositories {
    mavenCentral()
    }

    configurations {
    antlr3
    }

    dependencies {
    compile 'org.antlr:antlr-runtime:3.2'
    antlr3 'org.antlr:antlr:3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    }

    task antlrOutputDir << {
    mkdir(generateGrammarSource.destinationDir)
    }

    task generateGrammarSource(dependsOn: antlrOutputDir, type: JavaExec) {
    description = 'Generates Java sources from Antlr3 grammars.'

    destinationDir = "$buildDir/generated-sources/antlr"
    def antlrSource = 'src/main/antlr'

    inputs.dir file(antlrSource)
    outputs.dir file(destinationDir)

    def grammars = fileTree(antlrSource).include('**/*.g')

    main = 'org.antlr.Tool'
    classpath = configurations.antlr3
    args = ["-o", "${destinationDir}/${project.ext.grammarpackage}", grammars.files].flatten()
    }

    compileJava {
    dependsOn generateGrammarSource
    source generateGrammarSource.destinationDir
    }

    关于antlr - 用 gradle 编译 3.2 Antlr 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10615966/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com