goparsingcompiler-constructionantlr4lexer

Alternative that can match an empty string with simple grammar in ANTLR4


I'm building a small parser with ANTLR4, but I'm confused by an error I've come across. The definition of the lexer is as follows: lexer grammar LexerRules;

INT             :   'Int';
FLOAT           :   'Float';
STRING          :   'String';
BOOL            :   'Bool';
CHAR            :   'Character';
LIT_INT         :   '0' | DIGIT DIGIT*;
LIT_FLOAT       :   DIGIT+ '.' DIGIT+;
LIT_STRING      :   '"' (ESCAPE|.)*? '"';
LIT_CHAR        :   '\'' (CHARACTER | ESCAPE) '\'';
LIT_NIL         :   'nil';
WS              :   [ \t]+ -> skip;
ID              :   [a-zA-Z_][a-zA-Z0-9_]+;
LINE_COMMENT    :   '//' .*? '\r'? '\n' -> skip;
COMMNET         :   '/*' .*? '*/' -> skip;

fragment
DIGIT           :   [0-9];
fragment
CHARACTER       :   [a-zA-Z];
fragment
ESCAPE          :   '\\"' | '\\\\' || '\\n';

However in the rule for LIT_STRING the following error appears: rule LIT_STRING contains a closure with at least one alternative that can match an empty string. I don't really understand what is the alternative that can match the empty string. Is there something I am missing?


Solution

  • I had placed a | extra in the fragment. Always check the writing of expressions.