[antlr-interest] How can I modify a tree node value according to the value of another tree node ?

YINGAnnie yimm8369 at hotmail.com
Mon Mar 16 19:10:08 PDT 2009


 

I guess that does not work. I use gloable scope as the following grammar to deal with the problem.

 

Suppose my input is "var t:Number=Math.ceil(1.5); ".

 

Here is the grammar for Number,$IDENTIFIER is equal to Number .

 

qualifiedName 
scope math;
    :  IDENTIFIER (DOT IDENTIFIER)*                      {System.out.print("math"+$math::member);}

 

 

 

Here is the grammar for Math.ceil ,$memberExpression is equal to Math.ceil.

 

callExpression 
scope math;
    :   memberExpression (indexSuffix |propertyReferenceSuffix|argumentSuffix )*  {$math::member=$memberExpression.text;System.out.print("math"+$math::member);}       
  ;

 

In callExpression, I give gloable variable the value of $memberExpression.text, and in qualifiedName , I want to print the value of $memberExpression.text.

But Number and Math.ceil are in the different tree and the parser will parse Number before Math.ceil, therefore, when in the grammar of callExpression , the $math::member should not have the value of $memberExpression.text.

 

And the result is as I expect , it is "math null".

 

How to solve this? How can the parser go back to make some changes?

 

Thanks,

 

Annie


 

 

 

 


 
> Date: Tue, 17 Mar 2009 11:34:03 +1100
> Subject: Re: [antlr-interest] How can I modify a tree node value according to the value of another tree node ?
> From: michael.bedward at gmail.com
> To: yimm8369 at hotmail.com
> CC: antlr-interest at antlr.org
> 
> Hi Annie,
> 
> One way to pass data between sub-trees is to use global scope. See
> section 4.5 in the ANTLR book.
> 
> Hope this helps
> Michael
> 
> 
> 
> 2009/3/17 YINGAnnie <yimm8369 at hotmail.com>:
> >
> > Hi everyone,
> >
> >
> > What I want to do is to replace var t:Number=Math.ceil(1.5); to var
> > t:int=int(1.5); The tree I got is something like this:
> >
> > PROPERTY_DECL (VARIABLE_STATEMENT (VAR_DECL_LIST (VAR_ID_DECL t
> > (QUALIFIED_NAME Number)) = (ASSIGNMENT_EXPRESSION (CONDITION_EXPRESSION
> > (...........(QUALIFIED_NAME Math . ceil))) (ARGUMENT_SUFFIX
> > (.........(PRIMARY_EXPRESSION 1.5))))
> >
> > ....... stands for some other nodes.
> >
> >
> > Replacing Math.ceil(1.5) to int(1.5) is easy to implement.But cos Number
> > rule and Math.ceil rule belong to different tree, how can I replace Number
> > to int when value of the other tree's node equals to Math.ceil?
> >
> > Thanks in advance!
> >
> >
> > Annie
> >
> > ________________________________
> > 微软地图实时路况,为您节省的不仅仅是时间! 立即查看!
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> > http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> >
> >

_________________________________________________________________
MSN 表情魔法书,改变你的对话时代!
http://im.live.cn/emoticons/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090317/49d0ebe5/attachment.html 


More information about the antlr-interest mailing list