[英]Only recognizing one token in antlr4 grammar
I want my grammar to recognize the following expression &COL[0]
. 我希望我的语法能够识别以下表达式
&COL[0]
。 I have built the following grammar: 我建立了以下语法:
array:
ARRAY_NAME L_RIGHT_PAR (ARRAY_DIGIT|STRING) R_RIGHT_PAR;
ARRAY_DIGIT:DIGIT+;
ARRAY_NAME: '&''COL';
STRING : QUOT ('\\"' | ~'"')* QUOT
;
L_RIGHT_PAR : '[' ;
R_RIGHT_PAR : ']' ;
fragment
DIGIT : '0'..'9' ;
This gives the error: 这给出了错误:
mismatched input '[1]' expecting '['
输入“ [1]”不匹配,预期为“ [”
It only works if I write &COL[ 0]
with spaces between the [
and ]
仅当我写
&COL[ 0]
且在[
和]
之间有空格时才有效
I changed the grammar a bit to make it complete enough to run. 我对语法进行了一些更改,以使其足以完成运行。 The text
&COL[0]
lexes fine with this amended grammar. 文本
&COL[0]
与此修订语法相结合。
grammar test1; // different name for my test rig
test1: ARRAY_NAME L_RIGHT_PAR (ARRAY_DIGIT|STRING) R_RIGHT_PAR;
ARRAY_DIGIT:DIGIT+;
ARRAY_NAME: '&''COL';
STRING : QUOT ('\\"' | ~'"')* QUOT
;
QUOT: '"'; // assumed this
L_RIGHT_PAR : '[' ;
R_RIGHT_PAR : ']' ;
fragment
DIGIT : '0'..'9' ;
WS : [ \t\r\n] -> skip; // added whitespace just so I could add \r\n
Here's the tokenized output: 这是标记化的输出:
[@0,0:3='&COL',<ARRAY_NAME>,1:0]
[@1,4:4='[',<'['>,1:4]
[@2,5:5='0',<ARRAY_DIGIT>,1:5]
[@3,6:6=']',<']'>,1:6]
[@4,9:8='<EOF>',<EOF>,2:0]
So this answers the question you asked but I'm still not sure about your definition of STRING
. 因此,这回答了您提出的问题,但是我仍然不确定您对
STRING
的定义。 But &COL[0]
parses great now. 但是
&COL[0]
现在解析很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.