ANTLR mismatched input

纵然是瞬间 提交于 2019-12-04 05:30:33

问题


I have a simple grammar like this:

grammar mygrammar;

the_rule : 'abc' 'xyz' ;

WS  :  [ \t\r\n\u000C]+ -> channel(HIDDEN) ;

When I parse the text "abc xyz" with the_rule(), I get the expected string tree representation:

(the_rule abc xyz)

However, then I add the following lexer rule, which I think means "anything except for a w":

TEXT : ~[w]+ ;

Now when I parse the text "abc xyz" with the_rule(), I get an error:

line 1:0 mismatched input 'abc xyz' expecting 'abc'

Why would the TEXT lexer rule affect this? I think I'm misunderstanding something simple. :-(


回答1:


ANTLR lexer rules are greedy: the rule that matches the most wins. So, in your case, the entire input, abc xyz, is matched by TEXT. It doesn't matter if your parser is trying to match abc, the lexer operates independently from the parser.



来源:https://stackoverflow.com/questions/31254455/antlr-mismatched-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!