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

Johannes Luber jaluber at gmx.de
Thu Apr 24 09:55:24 PDT 2008


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


More information about the antlr-interest mailing list