ANTLR MismatchedTokenException on a Simple Grammar

I am completely new to ANTLR and EBNF grammars, so this is probably the main problem that I just don't understand.

I have a rule, for example:

version_line : WS? 'VERS' WS? '=' WS? '1.0' WS? EOL ;
WS : ' '+ ;
EOL : '\r' | '\n' | '\r\n' | '\n\r' ;

which matches the statement in my source file that looks like this (with an extra space):

VERSION = 1.0

With the rule form above, I get a successful match, although I get an exception with this form:

version_line : WS? 'VERS' WS? '=' WS? '1' '.0' WS? EOL ;

or this form:

version_line : WS? 'VERS' WS? '=' WS? DIGIT '.0' WS? EOL ;
DIGIT : '1' ;

Why is it different? I discovered this problem, trying to expand the rule even more, hopefully getting something like this:

version_line : WS? 'VERS' WS? '=' WS? DIGIT '.' DIGIT WS? EOL ;
DIGIT : '0'..'9' ;
+3
source share
1 answer

I see no problems, all four grammars give the expected AST:

1

version_line : WS? 'VERSION' WS? '=' WS? '1.0' WS? EOL ;
WS : ' '+ ;
EOL : '\r' | '\n' | '\r\n' | '\n\r' ;

alt text

2

version_line : WS? 'VERSION' WS? '=' WS? '1' '.0' WS? EOL ;
WS : ' '+ ;
EOL : '\r' | '\n' | '\r\n' | '\n\r' ;

alt text

3

version_line : WS? 'VERSION' WS? '=' WS? DIGIT '.0' WS? EOL ;
DIGIT : '1' ;
WS : ' '+ ;
EOL : '\r' | '\n' | '\r\n' | '\n\r' ;

alt text

4

version_line : WS? 'VERSION' WS? '=' WS? DIGIT '.' DIGIT WS? EOL ;
DIGIT : '0'..'9' ;
WS : ' '+ ;
EOL : '\r' | '\n' | '\r\n' | '\n\r' ;

alt text

with input:

VERSION = 1.0
#

( , # !)

ANTLRWorks v1.3.1.

+2

Source: https://habr.com/ru/post/1736199/


All Articles