[antlr-interest] How exclude a symbol from lexer rule?

Jim Idle jimi at temporal-wave.com
Tue Oct 7 10:22:38 PDT 2008


On Tue, 2008-10-07 at 19:08 +0200, Dennis Benzinger wrote:

> Am 07.10.2008 16:40, Sergiy Karpenko schrieb:
> > Hi, all    
> > For axample we have a lexer rule:    
> >     
> > CHAR : ('a'..'z')|('0'..'9');    
> >     
> > But, I want exclude from this sequence 'k' symbol.     
> > Somethig like    
> > EXTCHAR: CHAR ~'k' ;    
> > [...]
> 
> You can do it the other way around:
> 
> EXTCHAR: ('a'..'j')|('l'..'z')|('0'..'9');
> CHAR: EXTCHAR|'k';
> 

Except that you now have two lexer rules that are ambiguous, so you
can't.

In general, when you start seeing things like this, it should trigger
that you are probably trying to do too much in the lexer. For good error
reporting, you want to move errors as high up the food chain as
possible. A semantic error such as "Cannot use character 'k' in a foo
bar" is usually vastly clearer than "No viable alt at 'k' ". 

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081007/275a43cb/attachment.html 


More information about the antlr-interest mailing list