I’m using antlr to analyse and re-write sql query.
I have:
select : SELECT ;
fragment S : 's' | 'S' ;
....
fragment LETTER : 'a'..'z' | 'A'..'Z' ;
SELECT : S E L E C T ;
IDENTIFIER : LETTER+ ;
to define reserved key words and let them to be case-insensitive.
My question is how can I define non-reserved key words?
Your problem seems similar to the problem we had when building the parser for the Drools (www.jboss.org/drools) language (DRL). In DRL, for instance, “rule” is a keyword, but could also be used by a java programmer as a property name in his POJO. So we can’t have that as a reserved keyword.
We called these keywords “soft keywords”.
To do that in ANTLR, we defined only “true”/”false”/”null” as hard keywords in the LEXER:
https://github.com/droolsjbpm/drools/blob/master/drools-compiler/src/main/resources/org/drools/lang/DRLLexer.g#L132
Everything else is an ID. Then in the PARSER, we used semantic predicates for each soft keyword:
https://github.com/droolsjbpm/drools/blob/master/drools-compiler/src/main/resources/org/drools/lang/DRLExpressions.g#L597
This makes it possible to seamlessly integrate with java created POJOs without clashing property names and other things with Drools defined keywords.
Hope it helps.