简体   繁体   English

ANTLR规则不匹配

[英]ANTLR Rule Not Matching

This should be a simple question. 这应该是一个简单的问题。 Given this parser rule: 给定此解析器规则:

ifStatement
 : expr3b=IF logical (~(THEN)) expression* (ELSE expression *)? ENDIF // missing THEN
 ;

Why does this not match this String? 为什么这不匹配此字符串?

"IF CODE=\"10\" DUE_DATE < YESTERDAY ENDIF"

( IF , THEN , ELSE , and ENDIF are tokens defined to exactly what you'd assume they are. logical and expression are other rules). IFTHENELSEENDIF是完全定义为您假设的标记。 logicalexpression是其他规则)。

I assume the following line is verbatum from your grammar. 我认为以下行是您的语法中的惯用语。

ifStatement : expr3b=IF logical (~(THEN)) expression* (ELSE expression *)? ENDIF;

If that's the case, then you'll want to change it to this: 如果是这种情况,那么您需要将其更改为:

ifStatement : expr3b=IF logical expression* (ELSE expression *)? ENDIF;

As it is, (~(THEN)) says "match any one token, as long as it isn't THEN ." 实际上, (~(THEN))说“匹配任何一个令牌,只要它不是THEN 。” The first token after logical finishes is ID (or similar) for DUE_DATE . logical结束后的第一个标记是DUE_DATE ID (或类似值)。 ifStatement consumes it to fulfill (~(THEN)) . ifStatement它来实现(~(THEN)) This leaves < YESTERDAY to fulfill expression , which fails. 这使得< YESTERDAY可以满足expression ,但失败。

The following input would be accepted by the ifStatement in your question because ENDIF fulfills (~(THEN)) : 由于ENDIF满足(~(THEN)) ,因此ifStatement将接受以下输入:

IF CODE=\"10\" ENDIF DUE_DATE < YESTERDAY ENDIF

This would work as expected because the first ENDIF is consumed only to match (~(THEN)) . 这将按预期工作,因为第一个ENDIF 用于匹配(~(THEN))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM