如何为ANTLR4输入编写此语法表达式?
原始表达:
<int_literal> = 0|(1 -9){0 -9}
<char_literal> = ’( ESC |~( ’|\| LF | CR )) ’
<string_literal> = "{ ESC |~("|\| LF | CR )}"
我尝试了以下表达式:
int_literal : '0' | ('1'..'9')('0'..'9')*;
char_literal : '('ESC' | '~'('\'|'''|'LF'|'CR'))';
但是它返回了:
syntax error: '\' came as a complete surprise to me
syntax error: mismatched input ')' expecting SEMI while matching a rule
unterminated string literal
答案 0 :(得分:0)
您的报价不匹配:
'('ESC' | '~'('\'|'''|'LF'|'CR'))'
^ ^ ^ ^ ^ ^ ^
| | | | | | |
o c o c o c error
o
已打开,c
已关闭
我这样读"{ ESC |~("|\| LF | CR )}"
:
// A string literal is zero or more chars other than ", \, \r and \n
// enclosed in double quotes
StringLiteral
: '"' ( Escape | ~( '"' | '\\' | '\r' | '\n' ) )* '"'
;
Escape
: '\\' ???
;
还要注意,ANTLR4具有简写字符类([0-9]
等于'0'..'9'
),因此您可以这样做:
IntLiteral
: '0'
| [1-9] [0-9]*
;
StringLiteral
: '"' ( Escape | ~["\\\r\n] )* '"'
;
词法分析器规则也不以大写字母开头!否则,它们将成为解析器规则(请参阅:Practical difference between parser rules and lexer rules in ANTLR?)。