[antlr-interest] StringTemplate

Joan Pujol joanpujol at gmail.com
Tue Sep 21 00:50:33 PDT 2004


Hi,

I read the CodeGeneration article of Terr and I thought that I can use
for my pseudocode language compiler because I have to generate Java
code also for didactic purposes.

But I have some problems using it  from an AST generated by ANTLR.
Well, in fact, I haven't started to do it yet. But I made a simple
test code simulating it.

For example I have:

#/(DECCONST name:IDENT type:IDENT)   

And to test it I have done this simple template string (I have tried a
lot of variants, but none have worked):

group Java;

algorisme()::= <<
public class Algorisme {

   public static Main(String argv[]) {
       <decconst>
   }

}
>>

decconst()::= <<
   <t:{final <tipus> <nom> <if(valor)> = <valor> <endif>}>;
>>


And I write this test program simulating that I'm parsing the AST and
I have the name and type of the constant declaration.


public class TestStringTemplate  {
 public static void main(String[] args) throws Exception{

   StringTemplateGroup plantilles = new StringTemplateGroup(new
FileReader("java.stg"),
AngleBracketTemplateLexer.class);

   StringTemplate algorisme= plantilles.getInstanceOf("algorisme");
   StringTemplate decconst= plantilles.getInstanceOf("decconst");

   for(int i=0;i<5;i++) {
     decconst.setAttribute("t.{nom,tipus}" , "nom"+i,"tipu"+i);
   }

   System.out.println(decconst.toString());

   algorisme.setAttribute("decconst",decconst.toString());

 }
}


I know (or better, I think) that I can do it creating a bean. But it's
not a little unproductive to have to create a bean for everything from
the AST?
My question is what is the better way to use StringTemplate to
generate code from a ANTLR AST?
And I can use it without having to generate a bean for everithing?

My code is a Catalan pseudocode language similar to Java then I have
things like (previously translated to English):

(#IF expr THEN block ELSE block)
(#FOR expr TO expr DO block)
...

A lot of thanks in advance,

-- 
Joan Jesús Pujol Espinar


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list