[antlr-interest] RE Lexer rule for number range matching ?

Nagesh, Harsha harsha.nagesh at credit-suisse.com
Thu Apr 24 10:52:45 PDT 2008


I tried these suggestions, but I am getting the following warning in
ANTLRWorks when I include this rule

"The following token definitions are unreachable:.."

not sure what is the issue here

Nagesh, Harsha schrieb:
> Hi,
> 
> How can specify a lexical rule to match a range of integers. For
example
> '0'...'9' is used for digits.
> 
> Lets say I want to match any integer from 1 to 12 for valid months, is
> there a simple way to do this ? Similarly I want to match a range of
> numbers. What is the best way to do this in antlr V3 ?

You can use something like that:

MONTH : '1'..'9' | '10' | '11' | '12';

The problem with that rule is, as it has been already pointed out, that 
error messages aren't really insightful to the user.

MONTH : '1'..'9' ('0'..'9')*;

This one allows easier error checking either in the parser or with a 
validating sempred in the lexer like "{IsValidMonth(MONTH.text)}?" 
before the semicolon.

Johannes

==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================



More information about the antlr-interest mailing list