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

hwk.cortex-brainware at t-online.de hwk.cortex-brainware at t-online.de
Fri Dec 23 03:12:52 PST 2005


I think there are some common scenarios:

1) Call different templates (even when I only return one template, I might
use some templates during calculation). We could use a common syntax here
like $ST["name"] to extract a template form the group. This would be a
reserved word. Better yet: How about defining a string template group in the
grammar:

group MyTemplates;  // This only a defines a property in the parser to set a
string template group. Later, we could allow to define the templates inline.

Then one could call templates of the group using a syntax like
MyTemplates::Name. There would be a default group, which is used if the
group name is omitted. 

-> xxx  calls default group "xxx" and returns this as result
-> MyTemplates::xxx calls "xxx" in the the MyTemplates group and returns
this as result.

Within actions $MyTemplates::xxx would call the template. 

Now, we could nest them:

-> ResultTemplate(MyTemplates::xxx($Name), MyTemplates::yyy($Id)), or
whatever...

2) Build a collection of templates. These are useful to pass them to other
templates...

Currently, only the result of a rule can be added to a list. This could be
expanded to support multiple lists, one per template.

Just allow the same syntax to be used in front of each template call:
myList+=MyTemplates::xxx adds the template to a list called myList.

Maybe we should add the possibility to define lists in a scope:
scope global {
Templatecollection myList;  // or something like that
}

Use it like this: global::myList+=MyTemplates::xxx(...).

Overall, I think, template manipulation within ANTLR should be expanded. Of
course, I can code that now within actions but it would be far more user
friendly if normal cases could be expressed in a standard and uniform way.

I have several 2.7.5 grammars in C# and Java, which only differs in the
action code because of different method and class names. Using ANTLR 3 with
templates I was hoping to move all differences to a set of templates. This
only works if templates can be manipulated within ANTLR itself.

Kind regards


BTW: I was thinking about using multiple passes using the same action code,
but using different template groups for each pass...

 
Dr. Hartmut Kocher
Cortex Brainware GmbH


-----Ursprüngliche Nachricht-----

> So it would be useful to be able to
> - call multiple templates per rule.

We'd need a naming scheme to refer to them from the invoking 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.

True.  Was wondering about this.  THe problem is that it's only  
useful when you refer to a single rule in a production...not that  
common.  When there are multiple elements in a production, what do  
you do?

> 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.

That gets awkward.  You need to have actions that say "if (pass==1)"  
etc...

Ter




More information about the antlr-interest mailing list