[antlr-interest] Tokens

Ronald Sok ronald.sok at gmail.com
Fri Nov 27 15:05:56 PST 2009



Kevin J. Cummings wrote:
> You are close.  What you have here is keywords as opposed to reserved
> words.  When implementing the former, you will need to do something like
> (at least this is what I do using ANTLR 2.7.7):
>
> id : ID
>    | k:keyword
>       { #k->setType(ID); }
>       // This changes the token type of a keyword to an ID
>    ;
>
> keyword
>    : APPLE | PEAR | ORANGE
>    ;
>
> someName
>    :     'Name:' id NEWLINE
>    ;
>
> You could reduce the number of productions by folding, but the principal
> of changing the token type of keywords is what is important here.  And
> you may have to find out how to do this with ANTLR 3.x.
>
>
>   
Ok, I tried to change this into ANTLR 3 syntax, but ran into the fact that
the result of keyword is a subtype of ParserRuleReturnScope, which
definitely is not a Token type. So trying to change the type of the token
like how you showed in ANTLR 2.7.7 syntax does not work for me.
This is what I have, which results in an error while generating the grammar.

id : ID
   | keyword
      { $type=ID; }
      // from http://www.antlr.org/wiki/display/ANTLR3/Migrating+from+ANTLR+2+to+ANTLR+3
   ;




Ronald


More information about the antlr-interest mailing list