[stringtemplate-interest] [ST4] Help needed with recursive template to traverse a tree

Udo Borkowski ub at abego-software.de
Mon Feb 7 09:37:36 PST 2011


> my suggestion is to avoid walking data structures in the template. 

I guess this highly depends on the application.

In my case the model (a tree) is very simple. 

Actually it was very easy to render the tree in "XML style", with proper indentation and everything. I.e. I got an output like this:

<A>
    <B1>
        <C1>...</C1>
        <C2>...</C2>
    </B1>
    <B2>
        <C3>...</C3>
        <C4>...</C4>
    </B2>
</A>

with a simple four line ST4 template.

I thought displaying the tree in a different representation in an even "easier" way, with just the pathes of the nodes, like: 

A
A/B1
A/B1/C1
A/B1/C2
A/B2
A/B2/C3
A/B2/C4

should be possible, too. I am still trying to understand why this seems to be harder to do in ST4 than in other languages I used. 


Udo



On 05.02.2011, at 21:11, Terence Parr wrote:

> my suggestion is to avoid walking data structures in the template.  i've come to conclusion it is most proper for model / controller to do this.
> 
> Ter
> On Feb 1, 2011, at 7:26 AM, Udo Borkowski wrote:
> 
>> I need some help with a recursive template.
>> 
>> Assume you have a tree like this:
>> 
>>       A
>>      / \
>>     /   \
>>   B1     B2
>>  /  \   /  \
>> C1  C2 C3  C4
>> 
>> A node is implemented as a map with a "name" key (String) and a "children" key (list of node objects).
>> 
>> How to get a list of all "pathes" to the nodes (in preorder)?
>> 
>> I.e. I want this output:
>> A
>> A/B1
>> A/B1/C1
>> A/B1/C2
>> A/B2
>> A/B2/C3
>> A/B2/C4
>> 
>> I tried things like the following but run into problems with cycles. E.g. I need to refer to the "outer" prefix and node.name to build the new prefix and assign it to the argument:
>> 
>> allTreeNodePathes(node,prefix) ::= <<
>> «prefix»«node.name»
>> «node.children:
>>     {child|«allTreeNodePathes(
>>                node=child,
>>                prefix={«prefix»«node.name»/})»}»
>> >>
>> 
>> 
>> main(p) ::= <<
>> «(allTreeNodePathes(node=p,prefix=""))»
>> >>
>> 
>> 
>> Any suggestions?
>> 
>> Udo
>> 
>> _______________________________________________
>> stringtemplate-interest mailing list
>> stringtemplate-interest at antlr.org
>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110207/faa63f39/attachment-0001.html 


More information about the stringtemplate-interest mailing list