1

To manage all the permutations of this key: mess

a possible solution can be:

key : 'mess' | 'MESS'| 'meSs'| 'mEss'| 'Mess'| 'mESs'| 'MeSs'| 'MEss';

or maybe something like:

key: MESS;    
MESS: M E S S;
fragment M: [mM];  
fragment E: [eE];
fragment S: [sS];  

is there a better solution to manage this permutation? considering having several similar case

  • The usual way case insensitivity for a particular keyword is achieved is via the "fragment" code you show. However, if the language is "case insensitive", then you should use the "case folding" code https://github.com/antlr/antlr4/tree/master/doc/resources. Antlr 4.10 will implement case insensitive as an option. – kaby76 Feb 11 '22 at 19:45
  • thanks, will use the second solution. unfortunately my language is not case insensitive – Daniel Scerpa Mar 12 '22 at 12:18

1 Answers1

0

The first option is more like a hardcode solution. It is easy for small keywords, but will be more and more complex as long as your keyword length will increase.

The second option will make it more easy for you to make your keyword insensitive, and you will be able to reuse those fragments for other insensitive keywords. It's also the solution that is recommended in the ANTLR4 documentation. You can find this information on this link : ANTLR4 case insensitive lexing documentation

I hope this answers your question