Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling 3.2 Antlr grammar with gradle

Tags:

gradle

antlr

I'm trying to compile my antlr grammar with gradle. I'm very new to gradle, so am having trouble working out how to fix the issue.

I think it's trying to use the 2.7 antlr to compile (as I've seen a few other people reporting similar errors when using wrong version), and hence throwing errors.

How can I:

  1. Show what version of Antlr gradle is trying to use?
  2. Get gradle to compile properly?

Here's my grammar:

grammar Test;

options {
  language = Java;
}

rule: ;

Here's my gradle script:

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

repositories {
    mavenCentral()
}

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

Here's the output trying to compile:

$ 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

EDIT:

Seems Antlr3 isn't supported directly in gradle yet.

There's a pull request to add antlr3 support to gradle that's discussed here.

Here's another version of including support for Antlr3 manually.

like image 298
Mark Fisher Avatar asked Dec 28 '22 00:12

Mark Fisher


1 Answers

For completeness, I came up with the following gradle build file for my project which takes the version from tapestryjava blogspot and adds in some of the comments.

The only thing I need to change is not using dynamic properties to remove the warning.

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
}
like image 85
Mark Fisher Avatar answered Dec 31 '22 12:12

Mark Fisher