[antlr-interest] Keywords not context-free

Johannes Luber jaluber at gmx.de
Tue Oct 16 05:25:56 PDT 2007


Clifford Heath wrote:
> Yes, that will probably work. It's a pain though, because in this context,
> all but three of the language's keywords need to be accepted as normal
> identifiers, as part of an arbitrary length string of such identifiers.
> Ugly, because I need to list all the other keywords of the language, so
> I wondered whether there was a better way :-).

How about using always IDENTIFIER tokens except for the context-free
keywords and test at the appropriate places, if the IDENTIFIER contains
the correct string?

contextual_keyword[string identifier]
	:	 { input.LT(1).Text() == $identifier }? IDENTIFIER
	;

should work. Then use contextual_keyword["keyword"] instead KEYWORD.

Johannes


More information about the antlr-interest mailing list