[antlr-interest] Parsing numeric and alphanumeric

Austin Hastings Austin_Hastings at Yahoo.com
Wed Oct 10 22:00:38 PDT 2007


You have two tokens that can match some of the same elements.

Put the "smaller" one first in the file - in this case, that would be 
Number.

fragment Digit: '0'..'9' ;
fragment Alpha: '_' | 'A'..'Z';

Number: Digit+ ;
Alphanumeric: (Letter | Digit)+ ;


Note that I *could* have specified the difference more clearly - that 
Alphanumeric required at least one 'Letter' in it. That would look more 
complex:

Alphanumeric: (Letter|Digit)* Letter (Letter|Digit)* ;

But because ANTLR uses the order-of-appearance in the input grammar to 
decide which tokens to match, it isn't strictly necessary.

=Austin



Vishal Soni wrote:
> Hi,
>
> I need to parse numeric and alphanumeric tokens in ANTLR. The tokens 
> can be Numbers or Alphanumeric that start with numbers
>
>
> For e.g.
>
> 1111 ===> is a number
> 1AABC_ ===> is a Alphanumeric token
> A_112===>is alphanumeric
> _11111 => is alphanumeric
>
>
> How would I define a grammar for something like this?
>
> -- 
> Thanks,
> Vishal
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.488 / Virus Database: 269.14.6/1061 - Release Date: 10/10/2007 8:43 AM
>   



More information about the antlr-interest mailing list