Antlr token priority
antlr, java
Solution
ANTLR will always use a longer token over a shorter token, so to correct this situation you must do one of the following things:
Make the `FREE_TEXT_WORD` not match more than 3 characters for the input `168:321-331`, e.g. by not allowing it to contain a digit, or possibly removing the rule altogether.
You could also change `FREE_TEXT_WORD` to `FREE_TEXT_CHARACTER`. By limiting the rule to only matching a single character, it will never be longer than another token so its priority will be determined by its position in the grammar. You would then need to create a parser rule for words:
freeTextWord : FREE_TEXT_CHARACTER+;
Move the `FREE_TEXT_WORD` token into a mode which is not enabled at the point where your input reaches `168:321-331`.
Problem
I have a rule definition like this: ``` reference: volume':'first_page'-'last_page ; volume: INTEGER; first_page: INTEGER; last_page: INTEGER; INTEGER: [0-9]+; FREE_TEXT_WORD: NON_SPACE+; fragment NON_SPACE : ~[ \r\n\t]; ``` Given the input "168:321-331", I thought it would match the reference rule. But in reality, the whole string is tokenized as a FREE_TEXT_WORD. How can I make the INTEGER token take preference over FREE_TEXT_WORD in this case? Thanks.