[antlr-interest] Keeping lookahead low

Ciaran Treanor ciaran.treanor at gmail.com
Thu Aug 25 00:49:35 PDT 2005


First off, apologies for the double post (it seems to take quite a
while for mails to be redistributed by the mailer)

> Why don't you change your ID rule check if the matched ID starts with Thing
> (or whatever logic you have for THING_ID) and do $setType(THING_ID)

Are you thinking of something along the following lines?
// Comment out old THING_ID
//THING_ID
//	:	"Thing"		
//		( INT
//		| CHAR (CHAR | DIGIT)* { $setType(ID); } )
//	;

ID
options {
	testLiterals = true;
}
	:	('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*
		{
			String t = $getText;
			if( $getText.startsWith( "Thing" ) )
			{
				boolean isNumber = true;
				for( int i = 5; i < t.length(); i++ )
				{
					char c = t.charAt(i);
					if( c < '0' || '9' < c ) isNumber = false;
				}
				if( isNumber ) $setType( THING_ID );
			}
		}
	;

This definitely works (thank for the sample code Alexey). I'm going to
us it. Maybe it's just me but that approach doesn't feel elegant
though - just out of interest, is there another option?

Cheers,
ct


More information about the antlr-interest mailing list