Exception when building with ANTLR

(Max Mustermann) #1

I can’t get ANTLR to work. I did the following:

  • gradle init --type java-application
  • According to the ANTLR plugin documentation page add id 'antlr' to the plugins and implementation 'org.antlr:antlr4:4.7.1' to the dependencies section.
  • mkdir src/main/antlr
  • touch src/main/antlr/Hello.g4. The contents of Hello.g4 are taken from the getting started example:
// Define a grammar called Hello
grammar Hello;
r  : 'hello' ID ;         // match keyword hello followed by an identifier
ID : [a-z]+ ;             // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
  • ./gradlew build

Then I get:

Task :generateGrammarSource FAILED
/home/user/basic-demo/src/main/antlr/Hello.g4:2:1: unexpected token: grammar
error: Token stream error reading grammar(s):
/home/user/basic-demo/src/main/antlr/Hello.g4:3:8: expecting ‘’’, found ‘e’
/home/user/basic-demo/src/main/antlr/Hello.g4:2:1: unexpected token: grammar
error: Token stream error reading grammar(s):
/home/user/basic-demo/src/main/antlr/Hello.g4:3:8: expecting ‘’’, found ‘e’
/home/user/basic-demo/src/main/antlr/Hello.g4:2:1: rule grammar trapped:
/home/user/basic-demo/src/main/antlr/Hello.g4:2:1: unexpected token: grammar
TokenStreamException: expecting ‘’’, found ‘e’

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ‘:generateGrammarSource’.
ANTLR Panic: TokenStreamException: expecting ‘’’, found ‘e’

Could you tell me what I am doing wrong?

0 Likes

(Max Mustermann) #2

My bad, it has to be antlr 'org.antlr:antlr4:4.7.1' and not implementation (as stated in the documentation).

0 Likes