[antlr-interest] Imaginary nodes in Antlr 3

Kay Roepke kroepke at classdump.org
Sat Jun 16 07:26:12 PDT 2007


Hi geoff!

On Jun 16, 2007, at 3:38 PM, geoff wrote:

> stat:   expr NEWLINE! {System.out.println($expr.value);}
>     |   ID '=' expr NEWLINE  -> ^(ASSIGN ID expr)
>     |   NEWLINE!
>     ;
>
> On p166 Adding Imaginary Nodes the book says "simply refer to its  
> token type and, and
> Antlr will create a Token object with that token type", which is  
> what I expected to happen
> to ASSIGN.
>
> But, this does not work, and I get
> (135) ... Reference to undefined token in rewrite rules: ASSIGN
> as well as red wavy underlining in AntlrWorks.
>
> On a guess I then tried adding the token to the grammar
>
> ASSIGN : ;
>
> and everything compiled and ran as expected, tree built perfectly.
>
> Is there something I'm missing on automatic creation of imaginary  
> tokens? Clues welcome.

ANTLR won't generate the token in the grammar. The text refers to the  
Token object. You still need to tell ANTLR about the token in the  
grammar, either by declaring a lexer rule or, if you want to have  
imaginary tokens, via the tokens {} section in your grammar.
Refer to page 99 in the book.

HTH,
-k
-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list