[antlr-interest] Using labels in actions of a tree parser
Kay Röpke
kroepke at classdump.org
Wed Aug 13 07:47:50 PDT 2008
On Aug 13, 2008, at 4:27 PM, Oliver B. Fischer wrote:
> Dear all,
>
> I would like to use labels in the actions of my tree parser to have
> an alternative way to access the tree nodes. Unfortunately ANTLR
> reports an error I can't understand.
>
> Here is my action definition:
>
>
> term returns [DataType rt]
> : factor { $rt = $factor.rt; }
> -> template(f={$factor.st})"<f>"
> | ^(ASTERISK t1=term t2=term)
> | ^(SLASH t1=term t2=term)
> | ^(AND t=term term)
> { System.out.println($t); }
> ;
>
> And this is the error:
>
> ANTLR Parser Generator Version 3.0.1 (August 13, 2007) 1989-2007
> error(117):
> C:\javaproj\c1compiler.trunk\build\gensrc\de\fischer\c1compiler\antlr
> \C1Tree.g:243:5:
> missing attribute access on rule scope: t
>
> My someone of you can explain the reason for this error to me?
Sure,
ANTLR requires you to access some know attribute of `term` (via label
t), i.e. accessing a plain rule reference is not allowed in parser or
tree parser grammars.
IIRC this is because it would be ambiguous with respect to dynamic
scopes (where it is permissible to write $term, but that gives you the
Stack object for the rule scope).
For tokens it should work.
Try printing $t.st or $t.rt (if that has a toString() method).
cheers,
-k
--
Kay Röpke
http://classdump.org/
More information about the antlr-interest
mailing list