[antlr-interest] How to use a value more than once?

Terence Parr parrt at cs.usfca.edu
Tue Dec 20 13:05:44 PST 2005


On Dec 20, 2005, at 12:17 PM, Dr. Kocher, Hartmut wrote:
> Hi,
>
> I’m trying to build a first prototype using ANTLR3 and ST.
>
> I need to reuse a value more than once because I want it to use in  
> two different templates.
>
> Here’s an artificial example to keep it short:
>
> I parsed two properties name and text (Identifier and String).
>
> Something like:
>
> rules : rule*;
>
> rule : name text -> …???;
>
> Now I have two templates. One generates code like
>
>  private MyClass _<name>;
>
>  The other template generates:
>
>  void Init() {
>
>    _<name> = new MyClass();
>
>   _<name>.Text = <text>;
>
> }
>
> This should be repeated for all parsed values.
Hi Hartmut, can those MyClass and init output fragments be generated  
from the same template?  You could reuse the attribute (iterating  
over it twice).

If the attributes have to be pushed into multiple templates, you'd  
have to do that manually.  I guess you'd also have to return multiple  
templates manually, right?

Hmm...you want to say this:

rule : name text -> t1(args), t2(args) ;

or something?  Interesting.  How would we refer to this though with a  
rule label?  At the moment, $label.st is the sole template generated  
for a rule.

Ter



More information about the antlr-interest mailing list