[antlr-interest] Why don't parsers support character ranges?

Daniels, Troy (US SSA) troy.daniels at baesystems.com
Wed Apr 23 15:39:34 PDT 2008


 

> 
> 
> You could alternatively use:
> 
> grammar test4;
> foo : BEFORE '@' AFTER;
> BEFORE : A_TO_L | M_TO_Z;
> AFTER : M_TO_Z;
> fragment A_TO_L: 'a'..'l';
> fragment M_TO_Z: 'm'..'z';
> 

Actually, you can't.  Nothing will ever match AFTER, since BEFORE will
consume it.  If you make BEFORE and AFTER parser rules, that would work.

grammar test6;
 foo : before '@' after;
 before : A_TO_L | M_TO_Z;
 after : M_TO_Z;

A_TO_L: 'a'..'l';
M_TO_Z: 'm'..'z';


Troy


> But I suppose it is easier for error messages, if you leave 
> A_TO_L in for AFTER and check it in a later stage for correctness.
> 
> grammar test5;
> foo : ALPHA '@' ALPHA;
> ALPHA: 'a'..'z';
> 
> Johannes
> 


More information about the antlr-interest mailing list