Search code examples
antlrantlrworks

Match lowercase with ANTLR


I use ANTLRWorks for a simple grammar:

grammar boolean;

// [...]
lowercase_string
        :   ('a'..'z')+ ;

However, the lowercase_string doesn't match foobar according to the Interpreter (MismatchedSetException(10!={}). Ideas?


Solution

  • You can't use the .. operator inside parser rules like that. To match the range 'a' to 'z', create a lexer rule for it (lexer rules start with a capital).

    Try it like this:

    lowercase_string
      :  Lower+ 
      ;
    
    Lower
      :  'a'..'z'
      ;
    

    or:

    lowercase_string
      :  Lower
      ;
    
    Lower
      :  'a'..'z'+
      ;
    

    Also see this previous Q&A: Practical difference between parser rules and lexer rules in ANTLR?