What is minimal sample Gradle project for ANTLR4 (

2020-07-06 04:35发布

I have created new Gradle project, added

apply plugin: 'antlr'

and

dependencies {
    antlr "org.antlr:antlr4:4.5.3"

to build.gradle.

Created src/main/antlr/test.g4 file with the following content

grammar test;
r   : 'hello' ID;
ID  : [a-z]+ ;
WS  : [ \t\r\n]+ -> skip ;

But it doesn't work. No java source files generated (and no error occurred).

What I missed?

Project is here: https://github.com/dims12/AntlrGradlePluginTest2

UPDATE

I found my sample is actually works, but it put code into \build\generated-src which I was not expecting :shame:

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-07-06 05:26

The Gradle sample noted by @Mark Vieira only got me halfway there. I found that I had to specify the package in the header of my ANTLR grammar file in order for everything to be seen in both directions (generated code able to access hand-written code and vice-versa).

grammar MyGrammar;

@header {
    package com.mypackage;
}

Prior to switching to Gradle, I had been using the ANTLR plugin in IntelliJ, which filled in the package for me. Upon switching to Gradle, the package went away, which caused problems.

Source: https://stackoverflow.com/a/1655920/1877143

查看更多
登录 后发表回答