[antlr-interest] Specifying limit on number of digits in the Grammar in ANTLR

Eric Fesh fesh at VerariSoft.Com
Thu Apr 26 06:36:40 PDT 2007


I'm currently using V2 until V3 comes out of beta. Does option 3 work with V2?

--

Eric Fesh
Software Engineer
Verari Systems Software, Inc.
110 12th Street North, Suite D103
Birmingham, AL 35203
Phone +1-205-397-3141 ext. 3151
Fax +1-205-397-3142
fesh at verarisoft.com
http://www.verarisoft.com/

The information contained in this communication may be confidential and is
intended only for the use of the recipient(s) named above.  If the reader of
this communication is not the intended recipient(s), you are hereby notified
that any dissemination, distribution, or copying of this communication, or
any of its contents, is strictly prohibited.  If you are not a named
recipient or received this communication by mistake, please notify the sender
and delete the communication and all copies of it.


Alexandre Porcelli wrote:
> This problem is well described in the new Ter book (page 292, in my
> last beta version).
> 
> In fact there is 3 ways to do it:
> 
> 1) Simple context-free grammar
> 
> ID: DIGIT DIGIT DIGIT DIGIT DIGIT
> | DIGIT DIGIT DIGIT DIGIT
> | DIGIT DIGIT DIGIT
> | DIGIT DIGIT
> | DIGIT
> ;
> 
> 2) Verify the size using actions
> 
> ID: ( d+=DIGIT)+ {if ( $d.size() > 5 ) <<error action>> }?
> ;
> 
> 
> 3) Using validating semantic predicate
> 
> ID: ( d+=DIGIT)+ {$d.size() <= 5}?
> ;
> 
> 
> What is the best solution? It always depends (on my point of view) but
> if sometimes you will need to change your grammar to accept 7 instead
> of 5 digits… I would choose the third one.
> 
> Alexandre Porcelli
> 
> 
> On 4/26/07, Bharath R <rbharath24 at yahoo.com> wrote:
>> Hi,
>>
>>       I'm a newbie to ANTLR.
>>         I am trying to write a simple parser, that accepts number whose
>> number of digits vary from 1 to 5.
>>
>>     Below is the ABNF format
>>  ID: 1*5(DIGIT)
>>  DIGIT: '0'..'9'
>>
>>  i.e the ID can be any number varying from a 1 digit number to a max of
>> 5digit number. Each digit varying between 0 and 9.
>>  How do I achieve this in ANTLR? I have been trying to write an 
>> equivalent
>> EBNF format but without success.
>>  Thanks for you help.
>>
>> Regards,
>> Bharath
>>
>>
>>  ________________________________
>> Ahhh...imagining that irresistible "new car" smell?
>>  Check out new cars at Yahoo! Autos.
>>
>>


More information about the antlr-interest mailing list