Compiling 3.2 Antlr Grammar with gradle

I am trying to compile my antlr grammar with gradle. I am very new to gradle, so I am having problems with how to solve the problem.

I think he is trying to use 2.7 antlr to compile (since I saw some other people reporting similar errors when using the wrong version) and, therefore, throwing errors.

How can I:

  • Shows which version of Antlr gradle is trying to use?
  • Get gradle to compile correctly?

Here is my grammar:

grammar Test; options { language = Java; } rule: ; 

Here is 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 is the result that is 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:

It seems that Antlr3 is not supported directly in gradle.

There pull the request to add antlr3 support to gradle, which was discussed here .

Here is another version of Antlr3 support manually.

+4
source share
3 answers

Not very knowledgeable myself with Gradle, but generateGrammarSource uses ANTLR 2.7.x (not sure which version), because if I use it on grammar 2.7, .java files will be generated correctly.

You can always do something similar 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 bit and the grammar file are in the same directory as your Gradle assembly file)

EDIT

And you can also let the Tool output the generated source files to a specific directory. Next 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/ .

+4
source

For completeness, I came up with the following gradle build file for my project, which takes a version from tapestryjava blogspot and adds some 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 } 
+5
source

This is a mixture of the answer of Mark Fisher and Bart Kearse, which gets the antlr jar from the dependencies and puts in the classpath. This works for me:

 configurations { antlr3 } dependencies { antlr3 'org.antlr:antlr:3.4' } task generateWithANTLR3(type:Exec) { commandLine = ['java', '-cp', configurations.antlr3.getAsPath(), 'org.antlr.Tool', '-o', 'target/generated-sources/antlr3/t3', 'src/main/antlr3/t2/Exp.g'] } 
+1
source

Source: https://habr.com/ru/post/1412751/


All Articles