[antlr-interest] identifiers that are not allowed to be like keywords

Dieter Frej dieter_frej at gmx.net
Fri Jun 9 05:48:42 PDT 2006


Hi everybody,

I am still working on the Java grammar for antlr v3 and I do not know
how to make the following "happen" (copied from the Java language
specification and totally independent from the parser/lexer question
because of the capatilized first character):

Identifier:
IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:
JavaLetter
IdentifierChars JavaLetterOrDigit

As a Java developer, I know that e.g. "true" is a reserved keyword which
is not allowed as a identifier. But "atrue" or "truea" ARE indeed
allowed. For the "... but not ..." I used NOT operator (~) from antlr.
Unfortunately it is not working the way I want.


grammar butNot;

production
	:	production2 ~keyword
	;

production2
	:	(OneChar)*
	;
	
keyword
	:	'true'
	;
		
OneChar
	:	'a'
	;


The small grammar above allows inputs like "aaa", but not "atrue" or
"truea". It is more or less correct because keyword is not allowed to be
in the input sequence. Therefore, my question:
Is there any way to realize that or is that already too much logic in
the grammar that should not be in there?

Thanks for you help!

- Didi



More information about the antlr-interest mailing list