How can I allow keywords as identifiers?
This grammar allows "if if call call;" and "call if;".
grammar Pred; prog: stat+ ; stat: keyIF expr stat | keyCALL ID ';' | ';' ; expr: ID ; keyIF : {input.LT(1).getText().equals("if")}? ID ; keyCALL : {input.LT(1).getText().equals("call")}? ID ; ID : 'a'..'z'+ ; WS : (' '|'\n')+ {$channel=HIDDEN;} ;
You can make those semantic predicates more efficient by intern'ing those strings so that you can do integer comparisons instead of string compares.
The other alternative is to do something like this
identifier : KEY1 | KEY2 | ... | ID ;
which is a set comparison and should be faster.