[antlr-interest] Re: grammar help please.

Mark markl at glyphic.com
Fri Apr 16 08:34:27 PDT 2004


> I am trying to define a grammer for a 15 digit number.
Not clear exactly what you are trying to achieve.  If it is a number that is exactly 15 digits:

D: '0'..'9'
D15: D D D D D   D D D D D   D D D D D ;

Ugly, but easy.

If it is a number that has between 1 and 15 digits, then it is more difficult:

D15: D (D (D (D (D   (D (D (D (D (D   (D (D (D (D (D )?)?)?)?)? )?)?)?)?)? )?)?)?)? ;

But this is rarely useful.  Consider this: What do you want a string of 20 consecutive digits 
to mean?  Should it be a 15 digit number followed by a 5 digit number or is it an error?

It is more common to make it an error.  In which case define:

NUMBER: (D)+ ;

Then let the code that converts the string of digits into a numeric value notice that there 
are more than 15 digits (or more likely, signal an error when some binary value overflows.)

If you want 20 digits to be parsed as two numbers, then one has to ask how one resolves 
the ambiguity: two 10 digit numbers? a 15 digit number followed by a 5 digit number?  If 
this is a grammar for humans to write, it is generally a bad choice to go down this path.

- Mark




 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list