Search code examples
antlr4grammar

How to differentiate two forms of percent in a grammar?


I have two forms of "percent" that I need in a grammar:

PERCENT_1
    : 'PERCENT'
    ;

PERCENT_2
    : '%'
    ;

One is the word PERCENT, for example used with something like LIMIT 10 PERCENT and the other is the modulo operator. What might be a good way to differentiate these two things?


Solution

  • One is a keyword, the other an operator. I usually name keyword tokens <name>_SYMBOL and operators <name>_OPERATOR (see the MySQL grammar). Of course this is totally up to you, but should be consistently used throughout your grammar(s).