[antlr-interest] Attribute Access in Tree Grammar Actions
Benjamin Niemann
pink at odahoda.de
Sat Jun 30 04:42:21 PDT 2007
Randall R Schulz wrote:
> On Friday 29 June 2007 15:28, Gavin Lambert wrote:
>> At 10:16 30/06/2007, Randall R Schulz wrote:
>> >atomicSentence
>> >returns [ Formula f ]
>> >
>> > : equation
>> >
>> > {
>> > f = $equation;
>> > }
>> >
>> > | atom
>> >
>> > {
>> > f = $atom;
>> > }
>> > ;
>> >
>> >
>> >I get these diagnostics from ANTLR:
>> >
>> >error(117): CLIFBuilder.g:1157:3: missing attribute access on
>> >rule scope: equation
>> >error(117): CLIFBuilder.g:1162:3: missing attribute access on
>> >rule scope: atom
>>
>> I think you need to use $equation.value and $atom.value. Or
>> something like that :)
>
> Evidently it's "or something like that."
>
> When I look at the generated source code, equation(), atom() and
> atomicSentence() all declare plain, unadorned Formulas, as their return
> types.
>
> How to I get at the return values of rules when those rules appear on
> the right-hand-side of another rule?
If equation looks like
equation returns [ Formula f ]
...
you'll have to use "$equation.f" to get at the return value.
>> And I think you're also supposed to say $f rather than just
>> f. Gives the code generator the opportunity to move things around.
>
> Hmm... OK. I see that's what I did in an earlier (non-tree) grammar I
> wrote. But "move things around?" Why would that be a good thing? Order
> of execution is kind of important in imperative languages...
Stuff is not moved around, but ANTLR may translate '$f = ...' to
'f = ...' or 'retval.f = ...', depending on the use of other attributes.
So 'f = ...' may work, but break later on a seemingly unrelated change to
the rule (e.g. if some other rule far away in the grammar used
$atomicSentence.start).
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
More information about the antlr-interest
mailing list