ANTLR4 - Lexer 的 "Illegal Escape in String" 表达式
ANTLR4 - "Illegal Escape in String" expression for Lexer
作业要求是:
"Illegal escape in string: " + wrong string: When the lexer detects an illegal
escape in string. The wrong string is from the beginning of the string to the
illegal escape.
All the supported escape sequences are as follows:
\b backspace
\f formfeed
\r carriage return
\n newline
\t horizontal tab
\’ single quote
\" double quote
\ backslash
我使用 "String" 的代码与此相同 post 建议:
ANTLR4 - Need an explanation on this String Literals
STRINGLIT: '"' ( '\' [btnfr"'\] | ~[\b\t\f\r\n\"] )* '"';
并且还为 "Unterminated (or Unclosed) String" 修正了一点,如下所示:
UNCLOSE_STRING: '"' ( '\' [btnfr"'\] | ~[\b\t\f\r\n\"] )* ;
所以我尝试像这样写下该需求的原型:
ILLEGAL_ESCAPE: '"' .*? ESCAPE ;
fragment ESCAPE: [\b\f\r\n\t'"\]
谁能帮我弄清楚是否对它做错了什么,我认为 STRING 和 ILLEGAL_ESCAPE 之间有些东西不清楚,所以结果不正确。
如果您能再次修复它以满足我之前提到的要求,我将不胜感激。提前致谢!!
尝试使用以下词法分析器规则:
ILLEGAL_ESCAPE: '"' ('\' ~[btnfr"'\] | ~'\')*;
作业要求是:
"Illegal escape in string: " + wrong string: When the lexer detects an illegal escape in string. The wrong string is from the beginning of the string to the illegal escape.
All the supported escape sequences are as follows:
\b backspace
\f formfeed
\r carriage return
\n newline
\t horizontal tab
\’ single quote
\" double quote
\ backslash
我使用 "String" 的代码与此相同 post 建议:
ANTLR4 - Need an explanation on this String Literals
STRINGLIT: '"' ( '\' [btnfr"'\] | ~[\b\t\f\r\n\"] )* '"';
并且还为 "Unterminated (or Unclosed) String" 修正了一点,如下所示:
UNCLOSE_STRING: '"' ( '\' [btnfr"'\] | ~[\b\t\f\r\n\"] )* ;
所以我尝试像这样写下该需求的原型:
ILLEGAL_ESCAPE: '"' .*? ESCAPE ;
fragment ESCAPE: [\b\f\r\n\t'"\]
谁能帮我弄清楚是否对它做错了什么,我认为 STRING 和 ILLEGAL_ESCAPE 之间有些东西不清楚,所以结果不正确。 如果您能再次修复它以满足我之前提到的要求,我将不胜感激。提前致谢!!
尝试使用以下词法分析器规则:
ILLEGAL_ESCAPE: '"' ('\' ~[btnfr"'\] | ~'\')*;