开发者

ANTLR MismatchedTokenException on simple grammar

开发者 https://www.devze.com 2022-12-22 22:02 出处:网络
I\'m completely new to ANTLR and EBNF grammars to begin with, so this is probably a basic issue I\'m simply not understanding.

I'm completely new to ANTLR and EBNF grammars to begin with, so this is probably a basic issue I'm simply not understanding.

I have a rule such as:

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

that matches a statement in my input file that looks like this (with optional whitespace):

VERSION = 1.0

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

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

or this form:

vers开发者_StackOverflowion_line : WS? 'VERS' WS? '=' WS? DIGIT '.0' WS? EOL ;
DIGIT : '1' ;

Why is this different? I discovered this issue when trying to decompose the rule even more, hopefully ending up with something like this:

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


I see no problem, all four grammars produce the expected AST:

1

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

ANTLR MismatchedTokenException on simple grammar

2

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

ANTLR MismatchedTokenException on simple grammar

3

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

ANTLR MismatchedTokenException on simple grammar

4

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

ANTLR MismatchedTokenException on simple grammar

with input:

VERSION = 1.0
#

(Note that the # in the input is a new line char!)

Tested with ANTLRWorks v1.3.1.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号