[antlr-interest] Recursive conditional template output.

craig at palantir.co.za craig at palantir.co.za
Wed Aug 6 02:30:14 PDT 2008


Hi,

I am struggling to arrange tree template output so that a particular construct
can be processed in two different ways by the template.
I cannot find a suitable example.
I want to iterate the template once for header output, and once for body output
using the set of paths for each.

I have a very simple tree grammar as follows.

query
        : declarations+=declaration+ paths+=path+
        -> output(paths={$paths})
        ;

path
        : ^('and' p1=path p2=path) -> and(lvalue={$p1.st}, rvalue={$p2.st})
        | ^('or'  p1=path p2=path) -> or(lvalue={$p1.st}, rvalue={$p2.st})
        | ^(ROOT xs=xname*) -> item(lvalue={$names},
indexed={IsIndexed($names)})
        ;

in the template I have

output(paths) ::= <<
<paths> (Want to process the paths one way with one template)
<paths> (Process them again, with different template output).
>>

and(lvalue, rvalue) ::= <<AND <lvalue> <rvalue> >>
or(lvalue, rvalue) ::= <<OR <lvalue> <rvalue> >>

item(lvalue, indexed) ::= << Do something specific depending on paths:1 or
paths:2 >>

I am happy to recurse manually calling
<paths:{p|<dosomething(p)>}>

But I cannot understand how to get the template to work as the recursion is
implicit in the tree grammar, and 'item' is preempted from there, and not from
the template. I need to iterate that section of the template twice, once for
header output, and once for body output. Maybe dosomething(p) is not the right
approach.

What is the correct pattern for this. It is very simple when you are dealing
with a list of items, because you can just iterate the list using the template
and do one thing or another. When its' a tree it becomes rather difficult to
iterate using the template.



----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




More information about the antlr-interest mailing list