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

Udo Borkowski ub at abego-software.de
Tue Feb 1 07:26:32 PST 2011


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

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


More information about the stringtemplate-interest mailing list