[antlr-interest] handling abbreviated keywords in ANTLR

Micheal J open.zone at virgin.net
Mon Jul 9 05:52:53 PDT 2007


> Thank you Michael for the suggestion.
> I was hoping there may be a programmatic alternative (I should have
> mentioned) to defining in the grammar each and every keyword 
> alternative.
> I now see from some archived posts (ANTLR 2) suggestions of 
> over-riding testLiterals method and programmatically checking 
> the 'current' token against a hash.
> Does anybody have an example of how to provide this 
> functionality in ANTLR 3 (other than defining each alternative)?

You could use an action in your generic identifier rule thus:


	: 	( 'a'..'z'| ... )
		{ $type = CheckKeywordsTable(getText()) }

CheckKeywordsTable might simply consult an IDictionary<string,int> map of
all keywords (incl abbreviations). It would return IDENTIFIER for all
non-keyword tokens.

This isn't tested code. Adapt and apply...

> Unfortunately the link he provides is dead:
> http://antlr.org/doc/lexer.html#Keywords_and_literals

That has moved to the new V2-specific website. Try this instead:



The best way to contact me is via the list/forum. My time is very limited.

More information about the antlr-interest mailing list