Grammar file Expr.g4:
grammar Expr;
expr: expr ('*'|'/'|'+'|'-'|'%') expr
| '(' expr ')'
| INT
;
INT : [0-9]+ ;
WS : [ \t\n]+ -> skip ;
I use the current ANTLR-Version 4.7.1: In ./bashrc:
alias antlr4='java -jar ~/antlr4/antlr-4.7.1-complete.jar'
alias grun='java org.antlr.v4.runtime.misc.TestRig'
With this grammar the expression '1(' shouldn't be recognized as valid, but it is.
grun Expr expr -tokens
on the command line with input '1(' and CTRL-D for EOF gives:
[@0,0:0='1',<INT>,1:0]
[@1,1:1='(',<'('>,1:1]
[@2,2:1='<EOF>',<EOF>,1:2]
While the expression '1(' is not rejected, ')1' is:
[@0,0:0=')',<')'>,1:0]
[@1,1:1='1',<INT>,1:1]
[@2,2:1='<EOF>',<EOF>,1:2]
line 1:0 extraneous input ')' expecting {'(', INT}
Did I miss something?