[antlr-interest] unexpected token @

Terence Parr parrt at cs.usfca.edu
Wed Dec 5 13:51:20 PST 2007


Sorry. this has fixed in a later edition (2) of book.

scope {...}

Ter

On Dec 5, 2007, at 1:02 PM, Mark Volkmann wrote:

> 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