[antlr-interest] How to ignore TOKEN in a String

John B. Brodie jbb at acm.org
Tue Mar 22 16:33:24 PDT 2011


On Tue, 2011-03-22 at 14:40 -0700, Hiten R wrote:
> John/All
> 
> I followed 'antlr wiki' example but then it stopped working
> completely. Previous way it was complaining about the second line but
> after the change it could not get past the first line 'funny boys are
> Tom Hardy Donald '. 
> 
> Exception
> line 1:0 no viable alternative at input 'funny'
> 
> I am not much familiar with ANTLR but can you give me another example
> or point me to getting quoted string resolved.
> 
> Thx
> Hitender
> 
> BEFORE [ignore caps please]
> start
>   : 'funny' call_funny_parse
>   | 'serious' call_serious_parse
>   ;
> 
> AFTER - ANTLR WIKI [ignore caps please]
> start
>  : lexer_funny
>  | lexer_serious
>  ;
> 
> lexer_funny : {input.LT(1).getText().equals("funny")}?
> call_funny_parse;
> lexer_serious : {input.LT(1).getText().equals("serious")}?
> call_serious_parse;

i have not used token look ahead very much so I am fuzzy about this.

but i think LT(1) looks ahead 1 token without actually consuming any
token. so after your predicate the token stream is still positioned at
the "funny" token so you need to consume it before the call_funny_parse.

something like (very much untested!!!)

start
  : {input.LT(1).getText().equals("funny")}? TOKEN call_funny_parse
  | {input.LT(1).getText().equals("serious")}? TOKEN call_serious_parse
  | /* whatever is appropriate if neither funny nor serious is present*/
;


hope this helps...




More information about the antlr-interest mailing list