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

YINGAnnie yimm8369 at hotmail.com
Tue Mar 17 10:26:58 PDT 2009


 

 

Hi Johannes,

 

Thank you very much for your suggestion!

 

Yes, Math.ceil will always be evaluated after Number.

 

 

Actually, now I have two-pass, one is AS.g and the other is AStree.g.

 

As you said, I should store the Math.ceil value in AS.g and when the second pass AStress.g run, access the stored value and change Number to int.

 

How could I store value in AS.g and how can AS.g and AStree.g communicate? Could you plz give me a small example?

 

Thanks a million!

 

Annie

 

The last approach is to use a two-pass approach. Create in the first
> pass an AST and store the required value. Then create in the second pass
> the ST output and access the stored value.
 
> Date: Tue, 17 Mar 2009 07:37:41 +0100
> From: jaluber at gmx.de
> To: yimm8369 at hotmail.com
> CC: michael.bedward at gmail.com; antlr-interest at antlr.org
> Subject: Re: [antlr-interest] How can I modify a tree node value according to the value of another tree node ?
> 
> YINGAnnie schrieb:
> > 
> > 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".
> 
> Do I understand you correctly that Math.ceil will always be evaluated
> after Number? Then it isn't surprising that the above won't simply work.
> They are three ways to solve this problem. Either you scan the input for
> Math.ceil, when encounter Number. As this has to do be done manually,
> this is a lot of work - and may even require to call a lexer and parser
> manually within parsing the current input. Complicated.
> 
> Easier may be to resolve this issue backwards. You create a variable in
> Number and fill it in Math.ceil. This requires that the variable won't
> be evaluated until Math.ceil has been parsed. I'm not sure when the ST
> output is generated so this solution may not be viable.
> 
> The last approach is to use a two-pass approach. Create in the first
> pass an AST and store the required value. Then create in the second pass
> the ST output and access the stored value.
> 
> Johannes
> > 
> > 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
> >> > (...........(QUALI FIED_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 保护盾,保障 MSN 安全稳定! 现在就下载!
> > <http://im.live.cn/safe/>
> > 
> > 
> > ------------------------------------------------------------------------
> > 
> > 
> > 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/20090318/bee7666c/attachment-0001.html 


More information about the antlr-interest mailing list