简体   繁体   中英

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).

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 ." The first token after logical finishes is ID (or similar) for DUE_DATE . ifStatement consumes it to fulfill (~(THEN)) . This leaves < YESTERDAY to fulfill expression , which fails.

The following input would be accepted by the ifStatement in your question because ENDIF fulfills (~(THEN)) :

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

This would work as expected because the first ENDIF is consumed only to match (~(THEN)) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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