Define them before a rule that can match these keywords.
For example, you have a rule that matches identifiers, where an identifier consists of one or more letters, then your reserved if keyword should be placed before the identifier rule in your lexer:
grammar T; // parser rules here IF : 'if' ; IDENTIFIER : ('a'..'z')+ ;
Thus, if will always become an if token, not an IDENTIFIER .
source share