[antlr-interest] Tree Grammars, Complex Data and StringTemplateGroups
Roland Braito
r.b.lists at gmx.net
Tue Feb 7 04:08:20 PST 2012
Hello all,
Sorry if this was answered before, but i didn't find an answer yet...
Short version:
am using antlr 3.3.jar and the packaged version of ST
i want to build a retargetable code generator.
using the pattern of DSL to AST to output via StringTemplate
i am having severe problems getting the AST to StringTemplate mapping to
work
Is there a way to build such a thing with more than one level of nesting in lists without traversing the AST by hand?
Long version:
I follow the usual pattern of parsing a DSL into an AST and am now
trying to feed that AST to a tree grammar.
Since i have to create multiple very diverse output files form the
single input tree, i was planning to create a StringTemplateGroup file
for every output language needed.
The AST is deeply nested and has repeating elements at various levels
(think Object hierarchy)
I have one "interface" template called all(a1,a2,a3,a4) which takes
several lists of objects and then calls the various templates needed for
the language from the group files.
(e.g. sql.stg, java.stg,...)
I do not want to have any template definitions in the tree walker.
The problem is with grammar constructs like:
list_of_list
: ^( LIST1 list2+ )
;
list2 : ^( L2 another_list+ element+)
;
another_list
: ^( AL bla)
;
bla : ID
;
element : ID
;
i want to give the list_of_lists to the StringTemplateGroup, since the
hierarchy encodes valuable information, which i want to map to something like
header ::=<<
<list_of_lists:process_l2()>
>>
process_l2() ::=<<
<another_list:etc()> <element; separator=", ">
>>
and so on.
how do i map the repeating elements to the templates in the group file?
Things i tried:
^( LIST1 l+=list2+ ) -> template(list={l})""
gives emtpy results ( empty template because i dont want to have any
definitions in the grammar, but probably the "" interferes with the stg)
defining a return value and setting it via
^( LIST1 l+=list2+ ) {$list=$l}
antlr generates code like
if (list_p==null) list_p=new ArrayList();
list_p.add(p.getTemplate());
but the template would itself be a list of templates or at least objects
I tried to define the templates manually in {} and in ->{} and assign
them attributes manually, but i cant seem to access those in the stg
file.
The only thing which almost worked was to define helper classes in the
grammar, fill them up via {} code in the rules and pass those object
trees to the StringTemplateGroup.
But this failed at populating the object lists, since the code to add
another instance was generated by antlr to be out of the loop traversing the tree nodes and thus only got the last element of repeating sub-trees ( another_list+).
Is there a way to build such a thing with more than one level of nesting in lists without traversing the AST by hand?
Thanks for any help!
More information about the antlr-interest
mailing list