Search code examples
antlr4lexerexcept

How to write the rule: “some characters but except” in ANTLR4?


ID: (['_'a-zA-Z])(['_'a-zA-Z0-9])*;
INT_LIT: 'INT';
FLOAT: 'FLOAT';

What I want is that ID cannot be 'INT' or 'FLOAT'
What should I do ?? Thanks
(Sorry for my bad English)


Solution

  • Just move INT_LIT and FLOAT rules higher than ID. When two rules match the same text, the first one wins. I.e. "INT" will always be INT_LIT rather than ID with this setup.