[antlr-interest] Tree construction
Marcin Rzeźnicki
marcin.rzeznicki at gmail.com
Wed Dec 16 05:43:49 PST 2009
On Tue, Dec 15, 2009 at 10:36 PM, Jim Idle <jimi at temporal-wave.com> wrote:
> You will need to bring the EQUALS up in to the rule and rewrite accordingly:
>
> expression
> :
> ce=conditionalExpression
> (
> EQUALS e1=expression -> ^(STORE $ce $e1)
> | ao=assignmentOperator e2=expression
> ->^(STORE $ce ^($ao $ce $e2))
> )?
> ;
>
> assignmentOperator
> : PLUS_EQUALS ->PLUS[$PLUS_EQUALS]
> | MINUS_EQUALS ->MINUS[$MINUS_EQUALS]
> ;
>
That's unnatural I think. Because assignmentOperator rule, in your
example, does not cover all possible operators. I was really asking
whether it was possible to mark in rewrite rule a 'point of
attachment'.
> Though there isn’t really a need to change the token types, you can just behave accordingly with PLUS_EQUALS etc.
>
I think it is convenient. Because, after these transformations, I can
encode compound operations like += in simpler terms directly in AST.
Of course '+' is reused in "standard" addition.
> Jim
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of Marcin Rzeznicki
>> Sent: Monday, December 14, 2009 7:17 PM
>> To: antlr-interest at antlr.org
>> Subject: [antlr-interest] Tree construction
>>
>> Hi to you all dear antlr-interest members,
>> I am wondering whether it is possible to specify somehow where AST
>> nodes should be attached. Let me explain my problem on this short
>> example.
>> Let's consider expressions like i += 5. I want to build AST that
>> breaks this up into simple operation, like STORE and MUL in this case.
>> In other words, I want my final AST for this case to be like the one
>> below:
>> ^(STORE i ^(MUL i 5)).
>> Grammar part which is responsible for parsing these expressions:
>> expression
>> :
>> conditionalExpression ( assignmentOperator expression )?
>> ;
>>
>> assignmentOperator
>> :
>> EQUALS
>> | PLUS_EQUALS
>> | MINUS_EQUALS
>> ...
>> ;
>>
>> I could not find any clean way to achieve what I wanted. Finally, I
>> came up with something that works but is utmost ugly:
>>
>> expression
>> :
>> ( lhs = conditionalExpression
>> -> $lhs )
>> (
>> op = assignmentOperator[$lhs.tree] rhs = expression
>> -> {$op.start.getType() != EQUALS}?
>> ^(
>> STORE[$op.start] $lhs
>> ^( $op $rhs )
>> )
>> ->
>> ^( STORE[$op.start] $lhs $rhs )
>> )?
>> ;
>>
>> assignmentOperator[CommonTree leftHand]
>> :
>> EQUALS
>> ->
>> | op = PLUS_EQUALS
>> ->
>> ^( ADD[$op] {$leftHand} )
>> | op = MINUS_EQUALS
>> ->
>> ^( SUB[$op] {$leftHand} )
>> ///
>> ;
>>
>> I hope you share my pain :-) If I could move the actual tree
>> generation to assignmentOperator, it would give me much cleaner result
>> - but for now, due to facts that I am not knowing right hand side
>> expression in advance and I am not able to append it to the correct
>> place in the resulting tree, I think that this is impossible.
>> So here comes the question. Can you see any better way to achieve the
>> desired effect? I'll be happy to hear your opinions and share your
>> experience. Thank you in advance.
>>
>> --
>> Greetings
>> Marcin Rzeźnicki
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
>
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
--
Pozdrawiam
Marcin Rzeźnicki
More information about the antlr-interest
mailing list