[antlr-interest] tree grammar template output using rule return object instead of just st
Suresh Kannan
kanaero at gmail.com
Mon Sep 7 08:09:41 PDT 2009
I have parsed my code, built the AST and done semantic checks. I want
to generate a .h and .cpp file. I have written a tree grammar that
describes my AST and as I walk it, I want the different rule elements to
generate different snippets of code which will be used by the calling
rule appropriately. This way I just have one grammar and I do not have
to have multiple flags figuring out where I am etc. Just return all
the code snippets and let the calling rule use what it desires.
In the code below the problem appears when collecting items as a list
i.e., classes={$c} when appending to list_c in the generated java code
it uses the default 'st' by way of getTemplate(). In my templates I
want to say something like <c:{<it>.hcode}>. Of course I cannot do
that. I wish the rule return object would be passed to the template
instead of the class_declaration_return.st; that way I can use any of
the rule return attributes such as hcode, ccode or st itself.
Am I wrong in thinking this way ? Should I set various combinations of
flags/scopes to generate the right snippet. Perhaps there is a canonical,
easy way to do this that I do not know about.
regards,
suresh.
tree grammar Gen;
options {
tokenVocab = r;
ASTLabelType = rAST;
output=template;
}
file returns [StringTemplate hcode, StringTemplate ccode]
: (c+=class_declaration)+
{
$hcode = %h_file(classes={$c});
$ccode = %c_file(classes={$c});
}
;
class_declaration returns [StringTemplate hcode, StringTemplate ccode]
: ^('class' name=ID (^('extends' sup=ID))? ^(MEMBERS m+=member*))
{
$hcode = %class_declaration(name={$name.text},sup={$sup.text});
$ccode = %class_definition(name={$name.text},sup={$sup.text});
}
;
More information about the antlr-interest
mailing list