[英]ANTLR4 Grammar extraneous / mismatched input error
I would like to declare a string which contains any character in my grammar, but doesn't works. 我想声明一个字符串,该字符串包含我的语法中的任何字符,但是不起作用。
Here is my grammar: Syrius.g4 这是我的语法: Syrius.g4
When I run it, I got the following error: 运行它时,出现以下错误:
$ grun Syrius program
string test = "testString";
line 1:6 extraneous input ' ' expecting ID
line 1:11 mismatched input ' ' expecting ';'
What could be the problem with the grammar? 语法可能有什么问题?
Your token STR : .;
您的令牌
STR : .;
will match any character. 将匹配任何字符。
STR
is defined before WS
lexeme thus, it will consume all the whitespace characters instead. STR
是在WS
lexeme之前定义的,因此它将代替所有空白字符。 When parsing string test = "testString";
解析
string test = "testString";
the lexer will produce sequence of these tokens: string
, STR
, ID
, ... and so on. 词法分析器将生成这些标记的序列:
string
, STR
, ID
,...等等。 But the parser is looking for a declaration
rule which consists of string
, ID
, ... tokens. 但是解析器正在寻找一个由
string
, ID
,...标记组成的declaration
规则。
Define STR
token properly. 正确定义
STR
令牌。 Use this token in declaration
parser rule: 在
declaration
解析器规则中使用此令牌:
STR : '"' .*? '"';
// (...)
declaration
: 'int' ID ('=' INTEGER)? ';'
| 'string' ID ('=' STR)? ';'
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.