Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR: mismatched input

Tags:

antlr

I couldn't understand a bug in my grammar. The file, Bug.g4, is:

grammar Bug;

text: TEXT;

WORD: ('a'..'z' | 'A'..'Z')+ ;
TEXT: ('a'..'z' | 'A'..'Z')+ ;

NEWLINE: [\n\r] -> skip ;

After running antlr4 and javac, I run

grun Bug text -tree
aa
line 1:0 mismatched input 'aa' expecting TEXT
(text aa)

But if I instead use text: WORD in the grammar, things are okay. What's wrong?

like image 989
user2191332 Avatar asked Mar 04 '26 02:03

user2191332


1 Answers

When two lexer rules each match the same string of text, and no other lexer rule matches a longer string of text, ANTLR assigns the token type according to the rule which appeared first in the grammar. In your case, a TEXT token can never be produced by the lexer rule because the WORD rule will always match the same text and the WORD rule appears before the TEXT rule in the grammar. If you were to reverse the order of these rules in the grammar, you would start to see TEXT tokens but you would never see a WORD token.

like image 85
Sam Harwell Avatar answered Mar 06 '26 04:03

Sam Harwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!