[antlr-interest] Re: known number of repetitions..
lgcraymer
lgc at mail1.jpl.nasa.gov
Fri Oct 4 14:15:21 PDT 2002
--- In antlr-interest at y..., Sriram Durbha <cintyram at y...> wrote:
> == 1 =========
> this is with reference to writing a rule for numbers
>
> in english : " a number can be any sequence of digits
> upto a maximum of 6; so 0 - 999999 is a valid number
> "
> but 1234567 is not
>
> what is the best way to write such a rule?
>
> also what to do if the known number is really large
> like" a maximum of 1000 records makes a recset "
>
> but anything more than 1000 is invalid ;
>
For answers to 2), and 3), check the FAQs at www.antlr.org.
For this one, the consideration is what to do if numbers are outside
the accepted range. Do you really want to consider 1234567 to be
interpreted as two tokens, 123456 and 7? Or do you want to accept
1234567 as a single token and detect an error condition? I suspect
that the second alternative is preferable, and that the same is true
for recset.
I'll assume that your grammar has a DIGIT rule:
protected
DIGIT
: '0'..'9'
;
The first case is handled in LISP style (lots of irritating single
parentheses:
NUMBER
:
DIGIT
( DIGIT
( DIGIT
( DIGIT
( DIGIT
( DIGIT
)?
)?
)?
)?
)?
;
Yes, it's ugly, but it interprets a number as a sequence of from one
to six digits.
For the second case, you accept any number of digits
NUMBER
returns [ int counter; ]
:
{ counter = 1; }
DIGIT (DIGIT {counter++;)*
;
and either add code to throw an error if counter > 6 in NUMBER (then
you don't need the return value) or do validate the return value where
NUMBER is used and use the ANTLR error handling stuff to throw a
meaningful exception.
--Loring
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list