[antlr-interest] unexpected token @

Mark Volkmann r.mark.volkmann at gmail.com
Wed Dec 5 13:02:24 PST 2007


On Dec 5, 2007 2:55 PM, David Holroyd <dave at badgers-in-foil.co.uk> wrote:
>
> On Wed, Dec 05, 2007 at 02:46:29PM -0600, Mark Volkmann wrote:
> > The tree grammar rule below causes the error "syntax error: antlr:
> > MathTree.g:42:1: unexpected token: @", but I don't understand why.
> > Polynomial is a Java class in the same package of the classes
> > generated from the grammar.
> > Do you see anything syntactically wrong with this?
> >
> > polynomial returns [Polynomial result]
> > @scope {
> >   Polynomial currentPolynomial;
> > }
> >   :   ^(POLYNOMIAL term*) {
> >     $result = currentPolynomial = new Polynomial();
> >   }
> >   ;
>
> scope doesn't take an '@' prefix, as far as I remember.
>
> Maybe you want something like,
>
>   polynomial returns [Polynomial result]
>     :   ^(POLYNOMIAL term*) {
>       $result = MyScope::currentPolynomial = new Polynomial();
>     }
>     ;
>
> with
>
>   scope {
>     Polynomial currentPolynomial;
>   }
>
> defined at the top of your grammar?
>
>
> you need to add a,
>
> myrule
>   scope MyScope;
>   : ...
>   ;
>
> to a rule if you want to push a new 'scope frame' into the 'scope
> stack', or something like that.

I can do that to use a global scope, but I want a scope that is
limited to the rule that defines it and any rules invoked by it. It
turns out there is a typo on page 87 in the ANTLR book in the section
on "Dynamic Rule Attribute Scopes". It shows using "@scope", but it
should be just "scope".

-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list