My actions aren't getting executed when evaluating my syntactic predicates.

Monty Zukowski

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"

Of course the >= would screw up the expression rule, thinking it found 1>= "str" as a relational expression. So I thought I would be fancy and set a variable when I hit a "<" and then use the variable as a semantic predicate to turn off my relationalExpressions. It looked something like this:

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
      )* 
     ;
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact