[antlr-interest] How do I use a custom token object? Wiki FAQ sugestion does not seem to work ...

Uwe Lammers Uwe.Lammers at sciops.esa.int
Tue Aug 26 09:05:00 PDT 2008


Uwe Lammers wrote:
>
>
> So, that's one problem solved. The next question is, how can I access my 
> custom
> field in the parser rules?
>
>  From something like
>
> arith: t=NLITERAL { here I want to access t.x };
>
> antlr (3.1) generates
>
> t=(Token)match(input,NLITERAL,FOLLOW_NLITERAL_in_arith615);
>
> and Token does not know anything about the x field.
Ok, here is one solution

Put MyToken in a separate class then

@lexer::header {
  import MyToken;
}

@parser::header {
  import MyToken;
}

and in the parser rule

arith: t=NLITERAL                   { MyToken to = (MyToken)t;
                                      System.out.println(to.x);
                                    }


What a fudge ... wonder if there is a more elegant way to accomplish this.

Cheers
Uwe



================================================================================================
This message and any attachments are intended for the use of the addressee or addressees only. The
unauthorised disclosure, use, dissemination or copying (either in whole or in part) of its content
is prohibited. If you received this message in error, please delete it from your system and notify
the sender. E-mails can be altered and their integrity cannot be guaranteed. ESA shall not be liable
for any e-mail if modified.
=================================================================================================



More information about the antlr-interest mailing list