My actions aren't getting executed when evaluating my syntactic predicates.
Created Sep 3, 1999
That's the default behavior, so it can backtrack. Syntactic predicates are syntax only, you're not allowed to mess with anything when they are guessing. Only once it knows that the syntax is ok will it really go ahead an match with actions. It can be subtle, though.
In R/BASIC I had a thing where "<" and ">" were used like parenthesis:
a<1>= "str"
idRef
: ID
(
( (LESSTHAN (~(GREATERTHAN | GREATERTHAN_EQUALS ))+ GREATERTHAN)=>
(LESSTHAN {relationalsOK = false;}
exprList
GREATERTHAN {relationalsOK = true;}
)
| LPAREN (exprList)? RPAREN
)*
)
;
relationalExpr
: concatenationExpr
(
( GREATERTHAN_EQUALS
| {relationalsOK}? GREATERTHAN
| LESSTHAN_EQUALS
| LESSTHAN
)
concatenationExpr
)*
;
But this approach didn't work because in a syntactic predicate, no actions are executed, so my relationalsOK variable was never set during the lookahead, which is when I needed it set to block off the relationalExpr from matching the ending GREATERTHAN! I had to scrap that approach and only try to match things higher in precedence than relationalExpr to avoid the GREATERTHAN:
idRef
: ID
( (LESSTHAN concatenationExpr (COMMA concatenationExpr)* GREATERTHAN)=>
(LESSTHAN concatenationExpr (COMMA concatenationExpr)* GREATERTHAN)
| LPAREN (exprList)? RPAREN
)*
;
relationalExpr
: concatenationExpr
(
( GREATERTHAN_EQUALS
| GREATERTHAN
| LESSTHAN_EQUALS
| LESSTHAN
)
concatenationExpr
)*
;