[antlr-interest] Skip subtree in tree grammar

Sam Harwell sharwell at pixelminegames.com
Tue May 5 10:08:23 PDT 2009


Hi Martijn,

The state stack is unreliable in combination with semantic predicates and "cannot" be used in that manner. Instead you should use a construct like . or ^(IF .*) to skip the subtree in your walker.

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Martijn Reuvers
Sent: Tuesday, May 05, 2009 11:56 AM
To: Micha; antlr-interest at antlr.org
Subject: Re: [antlr-interest] Skip subtree in tree grammar

Hello Micheal,

Somehow we've managed to forget to include the antlr-list in our
conversation, so here the update and solution for the others.

I've used the stack solution (which I can probably use for other
conditionals too). It seems to work fine, I wrote the following
method:

 public boolean isContentEvaluable() {
    return phase > 0 && (isConditionalStackEmpty() || peekConditionalResult());
  }	

The method is used in function_content's rules and determines if each
rule should evaluate or not. As said the solution is backed by a stack
which is pushed/popped in @init and @after with boolean results.
Ignore the phase, which is not relevant for this solution.

Finally: I am still curious if you can skip a tree grammar subtree
altogether (the if statement in this mail conversation), so if someone
knows,  I'd love to hear that too!

Thanks for thinking along!

Martijn



On Tue, May 5, 2009 at 6:00 PM, Micha <micha-1 at fantasymail.de> wrote:
> On Tuesday 05 May 2009 17:39:55 you wrote:
>> Hi Michael,
>>
>> Thanks for your reply. Preferably I would just like to move the
>> treestream up to after the if-statement (if the value is false). But I
>> don't know how to calculate the proper index from here and to properly
>> pop/push it.
>>
>> With your solution you mean overread_function_content* contains the
>> same rules as function_content, except without any logic?
>
> yes
>
>> Another thing I was thinking of but haven't tried yet, is to push the
>> conditional result on a stack and in function_content (its subrules),
>> peek the stack and if the stack is either empty (we're not in a
>> conditional check) or the top returns true - evaluate, else do
>> nothing. This might work, but I haven't tried it yet.
>
> that was another solution I thought about. I think this depends on the
> grammar...
>
>
> cheers
>  Michael
>
>

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list