[antlr-interest] Passing information into the parser

Gavin Lambert antlr at mirality.co.nz
Sun Dec 28 22:21:15 PST 2008


At 19:13 29/12/2008, Gokulakannan Somasundaram wrote:
>          I have a requirement, where in before starting the 
> parsing, i need to tell the parser whether the first token is 
> T_FUNCTION / T_TRIGGER(say callType). (i.e) this token is not 
> available as part of the text to be parsed. In Bison, it is just 
> declared with %token T_FUNCTION and %token T_TRIGGER. The 
> scanner makes sure that it emits the callType token before it 
> emits the tokens to be parsed. Although, i am able to come up 
> with some ways to do this, they are too ugly.
>             Can someone provide me a clean way of passing 
> information into the parser and make the scanner emit that as 
> the first token, even before the actual tokens are emitted? I am 
> trying to do this in C-Language target.

If you want to emit custom tokens from the lexer due to arbitrary 
conditions then you need to override the lexer's nextToken method.

You can see an example of doing this (albeit for a different 
reason) here:
   <http://www.antlr.org/wiki/pages/viewpage.action?pageId=3604497>

That should hopefully get you started.  (Basically, the first time 
nextToken is called you should create and return your extra token, 
and then on all subsequent calls just call the base method.)



More information about the antlr-interest mailing list