0
votes

I'm trying to parse one token with pattern <int1>..<int2> in antlr4, and what I'm doing now is a trivial approach like:

main: INT1 '..' INT2;
INT1: NUMBER;
INT2: NUMBER;
NUMBER: [0-9]+ ;

However, when I tried to test the rule main with input 1..10 I got the error:

mismatched input '1' expecting NUMBER

which means I will always miss match the first int, BUT MY second int will be matched.

I was really confused, I think given the token <int1> and <int2> the same NUMBER lexer rule should result in same match, but clearly it doesn't do the job in my case. Anyone has any ideas? Thanks

3
Try some other tests to make sure you understand exactly where it's failing - for example, 1..2, 3..45, 67..89 - the error you're receiving doesn't actually indicate whether it's failing on the 1 by itself or the 1 that is part of 10. ā€“ J Earls
I tried all those combinations, they all failed to match only the first integer ā€“ RandomEli

3 Answers

1
votes

You confused concepts of tokens and syntax rules. Your grammar contains two tokens (INT and '..') and one rule:

main: int1=INT '..' int2=INT;

INT: [0-9]+ ;
0
votes

As @KvanTTT mentioned in his answer, I messed up the token rules and syntax rules. My approach also works in this way:

main: int1 '..' int2;
int1: NUMBER;
int2: NUMBER;
NUMBER: [0-9]+ ;
0
votes

Your mistake is this: you have 2 lexer rules matching the same input. Only one can match, however. ANTLR uses the longer match. If there are multiple rules matching the same length then the first one wins.

Btw. an important tip if you have trouble matching your input: print your tokens as they came in (use the token stream for that). In your case you probably got: INT1, .., INT1, which obviously doesn't match your grammar.