Antlr plugin: compiling two sets of grammar, the second depending from the first


(Luca Arzeni) #1

HI, I’m trying to migrate an ant script to a gradle one. The ant script generates a set of classes from antlr source files. This is a two step process: in first phase the script generates low level parser (expansion of labels, expression evaluation, and low level lexer), in the second phase two grammars are generated using grammars generated in first step.

Here is the ant:

<apply executable="runantlr" dir="${antlr.grammars.dir}" verbose="true" ignoremissing="false">
    <arg value="-o ./"/>
    <filelist dir="${antlr.grammars.dir}">
        <file name="LabelExpansion.g"/>
        <file name="MappingExpression.g"/>
        <file name="MappingLexer.g"/>
    </filelist>
  </apply>
    <apply executable="runantlr" dir="${antlr.grammars.dir}" verbose="true" ignoremissing="false">
   <arg value="-o ./ -glib ./MappingLexer.g"/>
   <filelist dir="${antlr.grammars.dir}">
       <file name="MappingInputParser.g"/>
       <file name="MappingOutputParser.g"/>
   </filelist>
  </apply>

I canconvert this ant to gradle without problems, using ant task within gradle, but I’m wondering if it could be done with the antlr plugin.

My first attempt was to put all files in the same dir, but the build failed with the (in)famous:

Execution failed for task 'generateGrammarSource'.
> java.lang.NullPointerException (no error message)
  1. I think that antlr plugin should provide a better message in this case… is it possible? 2) I imagine that I could put the two sets of grammars in two separate sourcesets, but then, using the plugin, how can I pass the first set of grammars as parameter to the second one?

Thanks, Luca


(Luke Daley) #2

It’s impossible to answer 1) without seeing your Gradle build script.

It would definitely be possible to do 2), though it would require you looking through the Antlr plugin and seeing how things are wired together, then creating some extra tasks and wiring them in. If you can provide a small project with two sets of grammars in a similar kind of arrangement I could put together a sample for you.


(Luca Arzeni) #3

Hi Luke, thanks for you answer.

Problem 1: I’ve put up a little project to show you the problem. Can you say me how can I attach a file to this forum?

Anyway, the (broken) gradle script is very simple:

apply plugin: "java"
apply plugin: "antlr"
  sourceCompatibility = 1.6
  repositories {
        mavenCentral()
}
dependencies {
        antlr 'antlr:antlr:2.7.7'
}

while the working gradle script is:

apply plugin: "java"
  sourceCompatibility = 1.6
  project.ext.antlr_sourceDir = "src/main/antlr"
  sourceSets {
    main {
        java {
            srcDir(antlr_sourceDir)
        }
    }
}
  task antlr_clean(type: Delete) {
        delete(
                fileTree(antlr_sourceDir){ exclude "**/*.g" },
                fileTree(antlr_sourceDir){ include "**/expanded*.g" }
        )
}
  ant.importBuild 'antlr_generate_grammars.xml'
antlr_generate_grammars {
        // ridefine task to avoid regeneration of source
        inputs.files(
                fileTree(antlr_sourceDir){
                        include "**/*.g"
                        exclude "**/expanded*.g"
                }
        )
        outputs.files(
                fileTree(antlr_sourceDir) {
                        include "**/*.*"
                        exclude "**/*.g"
                },
                fileTree(antlr_sourceDir) {
                        include "**/expanded*.g"
                }
        )
}
  clean.dependsOn antlr_clean
compileJava.dependsOn antlr_generate_grammars

Regarding problem 2, further investigations showed me that the problem lies within the antlr gradle task, which is closely modeled over the antlr ant task.

The antlr ant task doesn’t allow to pass supergrammars, (the value="-o ./ -glib ./MappingLexer.g" in the second ant task) ant this may be the reason why the gradle build crashes.

Is it possible to rebuild the antlr gradle task?

I wait your instruction on how to send the file. Thanks, Luca