[antlr-interest] Tree grammar conditionally run subrule

Fridi fridi70 at gmx.de
Wed May 28 23:38:25 PDT 2008


I have simply tried

: ^(OR a = expression
	{
	  if (a) 
	    return true; 
	}
       b = expression
       {
	  $r = a || b;
       }
    )


... but that leads to mismatched tokens exception, and I guess that is 
because the tokens (nodes) are not consumed as if it would run the 2nd. 
expression


Any other idea?


fridi70 at gmx.de schrieb:
> Hi,
>
> I have a tree grammar that handles some AND and OR logic and works very well. For performance reasons, in 'OR' I want to run 'b = expression' only if a is false. How can I realize this in the grammar?
> Thanks for any answer,
>   fridi
>
>
>
>
>
> expression returns [bool r = false]
> : ^(OR a = expression
>        b = expression
>        {
> 	  $r = a || b;
>        }
>     )
>
> | ^(AND a = expression
> 	b = expression
> 	{
> 	  $r = a && b;
> 	}
>
> | a = condition 
> 	{
> 	  $r = a;
> 	}
>       
> )
>
>
>   



More information about the antlr-interest mailing list