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

Terence Parr parrt at cs.usfca.edu
Sat Feb 5 12:11:04 PST 2011


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

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


More information about the stringtemplate-interest mailing list