[英]Antlr string matching order
I'm triying to use ANTLR with a kind of file where the value to retrieve can be any sequence of chars excluding { and }. 我正在尝试将ANTLR与一种文件一起使用,其中要检索的值可以是除{和}外的任何字符序列。
text = {Valid;String}
text = {Another"Valid"-String}
But now VALUE is matching the line from the begining: 但是现在VALUE从一开始就与行匹配:
line 1:0 mismatched input 'text = ' expecting 'text' 第1:0行不匹配的输入'text ='期待'text'
What I'm doing wrong? 我做错了什么? Should not match with TEXT first?
不应该先与TEXT匹配?
grammar Example;
example : (TEXT '=' '{' VALUE '}')+;
WS : [ \t\r\n]+ -> skip ;
TEXT : 'text';
VALUE : ~('{'|'}')+;
我认为这是因为ANTLR 4会尝试匹配最长的字符串,因此“ text ...”将匹配到VALUE。
As Terence (The ANTLR Guy) mentioned, the rule VALUE
greedily matches text =
. 正如特伦斯(ANTLR Guy)所述,规则
VALUE
贪婪地匹配text =
。 You could let the VALUE
rule include the braces instead of matching them as separate tokens: 您可以让
VALUE
规则包含花括号,而不是将它们作为单独的标记进行匹配:
example : (TEXT '=' VALUE)+;
WS : [ \t\r\n]+ -> skip ;
TEXT : 'text';
VALUE : '{' ~('{'|'}')+ '}';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.