[antlr-interest] Duplicating Tree Nodes (in rewrite grammar)

Robin Hamilton-Pennell robin at pozytron.com
Fri May 30 19:08:59 PDT 2008


Dan,

You are the man, you saved me from my own ignorance! adaptor.dupTree()
is exactly what I needed.

Thanks,
Robin

On Fri, May 30, 2008 at 7:37 PM, Dan Delorey <dandelorey at gmail.com> wrote:
> How about adaptor.dupTree()?
>
> Sent from my iPhone
>
> On May 30, 2008, at 7:17 PM, "Robin Hamilton-Pennell" <robin at pozytron.com>
> wrote:
>
>> Thanks for the reply.
>>
>> Actually, that won't work in my case, since the number of duplicates
>> is not based on any existing tokens, but on some member variables set
>> during semantic analysis (I actually have a Set<String> containing the
>> "names" for each of the generated blocks). Unfortunately the specifics
>> of my grammar are pretty esoteric, and so explaining it is difficult.
>>
>> There's no repeated elements in the rule I want to do the duplication
>> in (this is an actual clip of the grammar, slightly simplified):
>>
>> statement
>>   : ^(TYPEDEF l=lvalue b=basicBlock)            ->  // ?????
>>   | // other rules...
>>       ;
>>
>> So, let's say after semantic analysis, I determine I need 6 duplicates
>> of $b above. What could I put on the right side of the rule to do
>> this? Is it possible to create 6 imaginary tokens based on some
>> context variables, and then use the technique you showed? I'm sorry if
>> I'm being dense, I just have a hard time wrapping my mind around what
>> I need to do...
>>
>> Thanks again for your help,
>> Robin
>>
>> On Fri, May 30, 2008 at 4:11 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
>>>
>>> ANTLRWill automatically build duplicates as necessary. Is this what you
>>> want?
>>>
>>> d : type ID (',' ID)* -> ^(DECL type ID)+ ;
>>>
>>> ANTLR should make n copies of DECL and n-1 copies of type's tree.
>>> Ter
>>> On May 30, 2008, at 3:08 PM, Robin Hamilton-Pennell wrote:
>>>
>>>> Hi all,
>>>>
>>>> Been stumbling against a problem lately which I'd like to get some
>>>> feedback on.
>>>>
>>>> I need to duplicate a tree node at a certain point in my AST rewrite
>>>> grammar, including all of its children (recursively). If I knew the
>>>> number of copies necessary in advance, I imagine I could do something
>>>> like this:
>>>>
>>>> foo:
>>>>  ^(BAR baz)  ->  ^(BAR baz)  ^(BAR baz)  ^(BAR baz)
>>>>  ;
>>>>
>>>> However, the number of copies necessary is not knowable until runtime.
>>>>
>>>> I tried simply using CommonTree.dupNode(), but it doesn't copy the
>>>> children (after reading through the source I know this is correct
>>>> behavior).
>>>>
>>>> So I guess my question is this: How can I make an arbitrary number of
>>>> copies of a tree node and all sub-trees? I don't actually care about
>>>> making changes to the tree yet, I just want a verbatim copy.
>>>>
>>>> Any advice would be appreciated.
>>>>
>>>> Thanks in advance,
>>>> Robin
>>>
>>>
>


More information about the antlr-interest mailing list