[antlr-interest] Clarification on Attribute Reference in Actions

Randall R Schulz rschulz at sonic.net
Thu Jul 5 06:48:31 PDT 2007


On Thursday 05 July 2007 06:14, Kay Roepke wrote:
> On Jul 5, 2007, at 1:40 PM, Gavin Lambert wrote:
> > At 11:22 5/07/2007, Kay Roepke wrote:
> > >term returns [Term t]
> > >
> > >	:	^(ComplexTerm term termSequence)
> > >
> > >	{
> > >		Object o = asObject($term.t);
> > >	}
> > >	;
> >
> > [...]
> >
> > >Notice how it references retval.t in my version. I think
> > >this is a bug in the actionparser code. It should warn
> > >you that $term.t will refer to the rule's return
> > >value and not the subtree reference's retval object.
> >
> > I don't know, the current behaviour makes a certain amount of sense
> > to me.  If the action block was inside the parentheses, though, I'd
> > definitely expect it to refer to the recursive term, and not the
> > parent rule.

I'm not sure I think that parentheses should alter the interpretation of 
any given element or element label reference.


> Definitely. What I was proposing was that it merely warns you of the
> ambiguity, it should still pick the return value. However, if you
> meant the rule reference `term` ANTLR should tell you what to do.
> It was only about the warning, not about changing the behavior.

There are a few things that come to mind:

1) Bottom line: When an ambiguity like this exists, which will be in 
most any rule that includes itself directly on its right-hand-side or 
that of one of its alternatives, it must be possible to refer to 
either / any of the possible referents. That currently is the case and 
must remain so, one way or another.

2) As it stands, there are two identical ways to refer to the rule's 
return value, in this case $term.t and $t. That doesn't really seem 
right, and it forces one to invent an element label, which seems 
somewhat sub-optimal.

3) Given the inherent confusion possible, a diagnostic might not be a 
bad idea, but as ever, a chatty compiler is not a friendly thing. If 
heuristic warnings are going to be added, there should be some way of 
controlling whether they're emitted or not.


> cheers,
> -k


Randall Schulz


More information about the antlr-interest mailing list