[antlr-interest] Re: tree parser generating templates

Sohail Somani sohail at taggedtype.net
Thu Dec 15 23:55:52 PST 2005


On Thu, 2005-12-15 at 14:01 -0500, duboimat at iro.umontreal.ca wrote:
> >
> > On Dec 15, 2005, at 10:24 AM, duboimat at iro.umontreal.ca wrote:
> >> class LangWalker extends TreeParser;
> >> {
> >> StringTemplateGroup group; TextReader str;
> >> StringTemplate program_st,header_st,block_st,thread_st,
> >> assign_st,plus_st,moins_st,div_st,star_st;
> >> StringTemplate code;
> >>
> >> public void init()
> >> 	{
> >> 	str = new StreamReader("program.st");
> >> 	group= new StringTemplateGroup(str);
> >
> > Ok, first thing:use a group file format and StringTemplateGroup.  See 
> >  the article I have up on using ANTLR+ST.
> >
> >>        ...
> >>
> >> To build the program I have to put a template into another one.
> >> if i build lines I have to regroup it to build a block .
> >>
> >> It work only when i use st for a subset of the program . I can't  
> >> regroup all
> >> subset to built a program without having repetition from another  
> >> subset with a
> >> new value. May be a need to reset the template with its value.
> >
> > I have that functionality but I've never once used it...
> >
> >> Is there another to do a manually request in v2?
> >
> > see the article :)
> >
> > Ter
> >
> I have seen this article
> Language Translation Using ANTLR and StringTemplate
> I have begun my project with this article
> 
> my group file is included in program.st file.
> In my program.st
> 
> group sim;
> program(header,block) ::=<< Created by $header$   Program: $block$ >>
> header()::=<<header>>
> block(thread) ::=<< Block: $thread$ >>
> thread(statement) ::= <<Thread : >>
> ...
> assign(lhs,rhs) ::= << $lhs$ = $rhs$; >>
> plus(lhs,rhs) ::= << $lhs$ + $rhs$ >>
> moins(lhs,rhs) ::= << $lhs$ - $rhs$ >>
> div(lhs,rhs) ::= << $lhs$ / $rhs$ >>
> star(lhs,rhs) ::= << $lhs$ * $rhs$ >>
> 
> In my example , the program is composed of block, the block is composed of
> thread , the thread is composed of statement ...
> 
> The article doesn't show how to use antlr + st with tree.
> if I call template from tree parser class
> like
>         code = template("variable");        }
>         code.setAttribute("type", t);
>         code.setAttribute("name", d);
> 
> String template will not regroup all template together with its value to get a
> final code.

You have to do something like:

class StringTemplateGenerator extends SomeParser;

myTreeRule[returns StringTemplate ret=template("my_language_element");]
	: ... {ret.setAttribute("my_attribute",some_value);}
	;

then in the driver:

StringTemplateGenerator stgen = new StringTemplateGenerator(...);

StringTemplate result = stgen.myTreeRule();
System.out.println(result);

This is the way I would've liked to have done it, but I did it like the
article told me to and did it in the parser.

Oh well, I'll fix it in my 0.2 release :)

Sohail



More information about the antlr-interest mailing list