6

I have problem generating my grammar defintion with antlr v4:

grammar TagExpression;

expr : not expr
| expr and expr
| expr or expr
| '(' expr ')'
| tag
;

tag : [a-zA-Z]+ ;

and : '&' ;

or : '|' ;

not : '!' ;

WS : [ \t\n\r]+ -> skip ;

The syntax error happens here: tag : [a-zA-Z]+ ;

error(50): c:\temp\antlr\TagExpression.g4:10:6: syntax error: 'a-zA-Z' came as a complete surprise to me while matching alternative

The examples I saw had very similar constructs. Any idea why this happens?

Thanks

user3749218
  • 61
  • 1
  • 2

2 Answers2

21

The character set notation can only be used in a lexer rule (rules that start with a capital letter, and produce tokens instead of parse trees).

Tag : [a-zA-Z]+;
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
0

the problem is the syntax in ANTLR should be '[a-zA-Z]+'