[antlr-interest] Default templates with output=template

Terence Parr parrt at cs.usfca.edu
Wed Dec 5 10:37:36 PST 2007


On Dec 5, 2007, at 8:51 AM, John Ridgway wrote:

> Friends -
> I am attempting to use templates with a grammar and discovering  
> some (to me) surprising behavior.  When I do:
>
> w : x -> xyz2(x=x);
> x : y;
> y : z -> xyz(z=z);
>
> I would anticipate that the template created by using xyz would be  
> passed up through the intermediate rule to the rule for w; but this  
> does not happen.  Instead x has a null template.

Correct...er...correct per my intentions but not what we want in this  
case.

>   Is there a good reason for this, or could (should) ANTLR be  
> changed so that, in a case where no template is specified, the  
> obvious default is used.

Well, I did not know what the right answer was for templates for as I  
do know the answer for trees.  I thought we would simply tried out  
for a while and see what the right answer is.

> I don't believe that I can even do:
>
> x : y -> y;
>
> Am I missing something?

Yeah, you need a template there:

x : y -> {$y.st} ;

Awkward for this case, but oddly I have not needed this very often.   
In general, one does not know what the output or rule should be even  
given the output of the invoke rules.  With trees, this is obvious  
and you can just put them together.   With templates however you do  
not know what to generate (well, I suppose for a case where there is  
an alternative with a single rule reference it may be obvious but I  
decided to go for consistency).

What should be template be for this rule?

decl : 'int' ID (',' ID)* ;

  For consistency, I decided not to specify an automatic template.

Ter




More information about the antlr-interest mailing list