If I have the following the grammar to parse a list of Integers separated by whitespace:
grammar TEST;
test
: expression* EOF
;
expression
: integerLiteral
;
integerLiteral
: INTLITERAL
;
PLUS: '+';
MINUS: '-';
DIGIT: '0'..'9';
DIGITS: DIGIT+;
INTLITERAL: (PLUS|MINUS)? DIGITS;
WS: [ \t\r\n] -> skip;
It does not work! If I pass "100" I get:
line 1:0 extraneous input '100' expecting {<EOF>, INTLITERAL}
However if remove the lexer INTLITERAL rule and place it just under the parser rule integerLiteral like this
integerLiteral
: (PLUS|MINUS)? DIGITS
;
Now it seems to work just fine!
I feel that if I am able to understand why this is I'll begin to understand some idiosyncrasies that I am experiencing.