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 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. By the way I am awaiting
Terrence's chapter on Code Generation. 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});
   }
;

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to