[antlr-interest] Getting Token Text in C

Jim Idle jimi at temporal-wave.com
Tue Jun 9 09:29:09 PDT 2009


Florian Leitner-Fischer wrote:
> Dear All,
>
> I'm currently using the following C code to parse Java files for a 
> university project.
>
> This works very nice and I get an output like
>
> package
> identifier
> ;
> and so on for my Java File.
>
> The only thing I am missing now is to get the name of the identifier.
>
> Is this somehow possible?
> ...
>
>     {
> ANTLR3_INT32 T;
> T = 0;
> while (T != ANTLR3_TOKEN_EOF)
> {
> T = tstream->tstream->istream->_LA(tstream->tstream->istream, 1);
> tstream->tstream->istream->consume(tstream->tstream->istream);
> printf("%s \n", (psr->pParser->rec->state->tokenNames)[T], T);
> }
> }
> ...
>
> Best Regards and thanks in advance,
>
>   
Florian, where is this code? In an action? Driver code? Don't use the 
direct code like this as if I cahnge it, then all your code will break. 
There are macros, such as PARSER and so on that you should use. Look at 
the top of the generated code for more details.

If you use LT rather than LA, then you will get a token pointer and it 
has pointers to the input stream where the token starts and a length field.

Jim



More information about the antlr-interest mailing list