Compiling 3.2 Antlr grammar with gradle
antlr, gradle
Solution
Not very knowledgeable with Gradle myself, but `generateGrammarSource` uses ANTLR 2.7.x (not sure which exact version) because if I use it on a 2.7 grammar, the .java files are properly generated.
You can always do something like this to use ANTLR 3 and Gradle:
task generateWithANTLR3(type:Exec) {
commandLine = ['java', '-cp', 'antlr-3.2.jar', 'org.antlr.Tool', 'T.g']
}
(assuming the ANTLR jar and the grammar file are in the same dir as your Gradle build file)
EDIT
And you can also let the `Tool` output the generated source files in a particular directory. The following task:
task generateWithANTLR3(type:Exec) {
commandLine = ['java', '-cp', 'antlr-3.2.jar', 'org.antlr.Tool', '-o', 'src/x/y/generated/', 'T.g']
}
will put the generated files in `src/x/y/generated/`.
Problem
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: - Show what version of Antlr gradle is trying to use? - 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.