[antlr-interest] Multiple outputs ANTLR + ST

fridi fridi70 at gmx.de
Mon Feb 15 01:01:39 PST 2010


Hi Damien,

the trick could be, that not ANTLR calls the tmpl method but you do it 
twice with some java code with the same values that return from type, 
name, arguments, body

Maybe try something like this:

method : t=type n=name a=arguments b=body
                {
                    useTemplates(t, n, a, b);
                }
           ;

"useTemplates" is a method you would code in the @members area of your 
grammar file


@members {
    private ArrayList mfile ....
    private ArrayList hfile ...


    // take the params from the antlr rules and do call the templates 
manually
    public void useTemplates(type_return type, name_return name, 
arguments_return args, body_return body) {

        StringTemplate stHeader = 
templateLib.getInstanceOf("methodHeaderFile",
              new STAttrMap().put("type", type).put("name",  
name).put("arguments", args).put("body", body));

       hfile.add(stHeader);

       StringTemplate stMFile = templateLib.getInstanceOf("methodMFile",
              new STAttrMap().put("type", type).put("name",  
name).put("arguments", args).put("body", body));

       mFile.add(stMFile);
    }

}

  
So at the end you will have two list of temples - one for the header and 
one for mfile - and only have to iterate through them, call toString for 
each item and collect the resulting strings and save them to 2 different 
files.
The code above is only an example, for the details how the templates get 
called, please take a look at the code that antlr generates when 
rewriting into a string template

I don't know if there is a more elegant way to do this directly from the 
grammar..

Hope this helps,
  Fridi


Damien Urruty schrieb:
> Hi Fridi, thanks for your answer,
>
> With your solution, I don't see how i can differentiate the output : I 
> want to save the function prototypes in a.h file, and the function 
> bodies in a .m file. To do this, I parse my input, and I have a rule 
> method for example : 
>
> method : type name arguments body
>            ;
>
> I have to call 2 different template functions, one that returns only 
> the prototype, and the second one that returns the entire method. For 
> example : 
>
> methodHeaderFile(type, name, arguments, body) ::=<< <type> <name> 
> <arguments; separator=" : "> >>
> methodMFile(type, name, arguments, body) ::=<< <type> <name> 
> <arguments; separator=" : ">{
>    <body>
> }
> >>
>
> And I don't see how to do it with your solution, because the same 
> template is called (foo1Tmpl()), and will return always the same code.
>  
> Anyway thanks for your answer !
>
> Bye, Dam.
>
> > Date: Thu, 11 Feb 2010 20:45:14 +0100
> > From: fridi70 at gmx.de
> > To: lepilache at hotmail.com
> > Subject: Re: [antlr-interest] Multiple outputs ANTLR + ST
> >
> > Hi Dam,
> >
> > one approach that I have used was to redirect the templates output by
> > my own java code that I added to the ruls as actions.
> > So if a rule returns a template, i.e. add that template to a list of
> > your choice.
> >
> > @members {
> > ArrayList<StringTemplate> myTmpls = new ArrayList<StringTemplate>();
> > }
> > ...
> >
> >
> > rule1
> > : f1=foo1 foo2 { myTmpls.add(f1.st); }
> > ;
> >
> > foo1
> > : i=IDENT -> foo1Tmpl({value=$i.text})
> > ;
> >
> > foo2
> > : ...
> >
> > So I think you do not have to return the templates up to the
> > entryRule, you can do whatever you want with them.
> >
> > Hope that helps,
> > fridi
> >
> >
> > Damien Urruty schrieb:
> > >
> > >
> > >
> > > Hi there,
> > >
> > > First of all, thanks a lot for this brilliant tool ! I have a 
> problem, and I don't know how to solve it.
> > >
> > > I want to generate multiple output files, for example a .h and a 
> .m for Objective-C. The point is that I can bind only one template to 
> my parser, with :
> > >
> > > parser.setTemplateLib(tmpl);
> > >
> > > when I call parser.entryRule(), then the toString(), I can get 
> only the generated code for this template tmpl.
> > >
> > > Is it possible to bind multiple templates, and parse once my 
> input, or do I have to parse it 2 times, assigning a different 
> template each time ?
> > >
> > > Hope I'm clear.
> > >
> > > Thanks in advance !
> > >
> > > Dam.
> > >
> > > _________________________________________________________________
> > > Discutez en direct avec vos amis sur Messenger !
> > > http://www.windowslive.fr/messenger
> > >
> > > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > > Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> > >
> > >
> >
> >
>
> ------------------------------------------------------------------------
> Avec Internet Explorer, surfez en toute discrétion sur internet 
> Cliquez ici ! <http://clk.atdmt.com/FRM/go/182932252/direct/01/>



More information about the antlr-interest mailing list