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

Johannes Luber jaluber at gmx.de
Mon Mar 16 23:37:41 PDT 2009


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



More information about the antlr-interest mailing list