英文:
Alternative that can match an empty string with simple grammar in ANTLR4
问题
我正在使用ANTLR4构建一个小型解析器,但我对遇到的错误感到困惑。词法分析器的定义如下:
词法分析器规则;
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;
COMMENT : '/*' .*? '*/' -> skip;
fragment
DIGIT : [0-9];
fragment
CHARACTER : [a-zA-Z];
fragment
ESCAPE : '\\'' | '\\\\' || '\\n';
然而,在LIT_STRING规则中出现了以下错误:规则LIT_STRING包含至少一个可以匹配空字符串的替代项。 我不太明白哪个替代项可以匹配空字符串。我是否漏掉了什么?
英文:
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?
答案1
得分: 1
我在片段中放了一个|多余的部分。始终检查表达式的书写。
英文:
I had placed a | extra in the fragment. Always check the writing of expressions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论