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

Dr. Kocher, Hartmut h.kocher at pharmatechnik.de
Wed Dec 21 00:05:06 PST 2005


Hi Terence,

actually, it's worse :-)

I want to generate GUI classes (controls like Textfield, Checkbox, etc.). Because each control has different properties to set, I have different templates for each control type. In the window, I have to combine those templates into one Form class (.NET). Therefore, I have two lists of templates: one for setting up the fields in the form, another to initialize the controls. Therefore, I have to call the templates in my subrules but use it in the main rule.

The actual grammar goes something like this:

form : FORM ID '{' control* '}';
control: textfield|checkbox|... ;
textfield: TEXTFIELD ID label SEMI;

You get the idea...

For textfield I have to generate at least two templates with

Textfield <name>;

And another

<name> = new Textfield();
<name>.Text = <label>;
...

Those may be different for each control.
In the form I expand these in two places: as a field declaration and in the contructor.

So it would be useful to be able to
- call multiple templates per rule.
- add templates to lists (currently, there's only a shortcut for the one and only return value (p+=... syntax).

BTW, I think it would be useful if a rule like 'control' above would return the template of textfield or checkbox by default instead of nothing. If the user doesn't use the template, no harm is done. If he wants another template he can override it using the -> syntax.

Finally, another solution to my problem would be to make multiple passes thru the grammar, one to get the fields, one to build the contructor method. So I could build a tree parser first. Then make multiple passes thru the AST. Can I specify multiple parsers in the same combined grammar? That would be nice.

Kind regards
Hartmut

-----Ursprüngliche Nachricht-----
Von: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] Im Auftrag von Terence Parr
Gesendet: Dienstag, 20. Dezember 2005 22:06
An: ANTLR Interest
Betreff: Re: [antlr-interest] How to use a value more than once?


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