[antlr-interest] How do I skip a subtree walking in a tree grammar?

Shihwei Li lishihwei at gmail.com
Mon Mar 16 14:50:19 PDT 2009


Gavin,

I resolved the problem about the 'a' scope problem by enhancing the grammar
your provided. Thank you very much for your answer. That points to the right
direction for me.

Here is what I did: have a memeber variable boolean_short_cut flag. Change
the grammar to set up the flag and use the flag in the gated semantic
predicates:


bool_exp
^(OR a=bool_exp {} { boolean_short_cut = (a == true); }
( {boolean_short_cut}?=> ignore { $value = true; }
|
b=bool_exp { $value = $b.value; }
))
| ^(AND a=bool_exp { boolean_short_cut = ( a != true ); }
( {boolean_short_cut}?=> ignore { $value = false; }
|
b=bool_exp { $value = $b.value; }
))


--peter

2009/3/16 Shihwei Li <lishihwei at gmail.com>

> Ok. time to learn more about predicates..
>
> Still got problems. After removing '=>', compilation errors were reduced
> from 35 to 34. All erros are the same: 'a' can not be resolved.
>          if ( ((a)) ) {s = 18;}
>          else if ( (true) ) {s = 19;}
>
> Any idea why? Antlr I am using is 3.1.1
>
> Here is the grammar I touched:
>
> bool_exp returns [boolean value]
>     throws ConditionExpressionException
>     :
>         not_bool_exp                  { $value = $not_bool_exp.value; }
>     |
> ^(OR a=bool_exp
>    ( {$a.value}? ignore { $value = true; }
>    | b=bool_exp { $value = $b.value; }
>    ))
>  | ^(AND a=bool_exp
>    ( {$a.value}? b=bool_exp { $value = $b.value; }
>    | ignore { $value = false; }
>    ))
>     ;
>
> --peter
>
> 2009/3/16 Gavin Lambert <antlr at mirality.co.nz>
>
>> At 09:09 17/03/2009, Shihwei Li wrote:
>>
>>  Is this related to syntactic predicate? I can sort of understand the idea
>>> behind, but I am not familiar with the syntactic predicate yet. So still
>>> need some help from you.
>>>
>>
>> These are actually semantic predicates.
>>
>>  I tried the tree grammar you provided, and I got compilation errors in
>>> the generated java code, such as
>>>
>>>  if ( ((a)) ) {s = 18;}
>>>  else if ( (true) ) {s = 19;}
>>>
>>> Saying that 'a' is not defined.
>>>
>>
>> Ah, ok; looks like the predicates are being hoisted out of context.  Try
>> removing the => arrows.  I can never remember when one should be used vs.
>> the other :)
>>
>>
>
>


-- 
--peter
http://lishihwei.googlepages.com/home
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090316/e56dfdd1/attachment.html 


More information about the antlr-interest mailing list