[antlr-interest] Fixed length tokens again

Jim Idle jimi at temporal-wave.com
Thu Jul 5 13:11:19 PDT 2007


Try this:

tokens
{
	N4;
}

N3
	: Digit Digit Digit
			(Digit { $type = N4;})?
;

Though you need to be careful trying to count specific digits and so on
in lexers as you can easily start trying to write the parser in the
lexer without thinking.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Stefan Wohlgemuth
> Sent: Wednesday, July 04, 2007 11:48 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Fixed length tokens again
> 
> Is there a way to define tokens which have a fixed length?
> Say I would like to define a lexer rule N3 which defines a token of
> three and another N4 of four numeric characters.
> 
> Something like this:
> 
> test: N3 N4;
> 
> N3:  Digit Digit Digit;         //  This in combination with N4 does
> not
> work
> N4:  Digit Digit Digit Digit; //
> 
> fragment
> Digit: '0'..'9'
> 
> 
> Thanks
> 
> 
> --
> aloba ag
> Lyssachstrasse 15
> CH-3400 Burgdorf



More information about the antlr-interest mailing list