如何解决 ANTLR 输入不匹配的问题
给出语法
test : 'test' ID '\n' 'begin' '\n' 'end' '\n' -> ^(TEST ID);
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
和测试字符串,
"test blah\n begin\n end\n"
导致
line 1:0 mismatched input 'test blah\\n begin\\n end\\n' expecting 'test'
<mismatched token: [@0,0:21='test blah\\n begin\\n end\\n',<12>,1:0], resync=test blah
begin
end
>
这里出了什么问题?
given the grammar
test : 'test' ID '\n' 'begin' '\n' 'end' '\n' -> ^(TEST ID);
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
and a test string of
"test blah\n begin\n end\n"
resulting in
line 1:0 mismatched input 'test blah\\n begin\\n end\\n' expecting 'test'
<mismatched token: [@0,0:21='test blah\\n begin\\n end\\n',<12>,1:0], resync=test blah
begin
end
>
whats gone wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在语法规则中使用
'\n'
时,您匹配的不是反斜杠+n
而是换行符。看起来您的输入不包含换行符,而是包含反斜杠+n。所以,我的猜测是您需要将
test
规则更改为:生成解析树:
或保留您的
test
规则原样,但将您的输入更改为:生成解析树:
如果情况并非如此,您能否发布一个 SSCCE:一个我(或其他人可以运行)显示此错误的小型完整工作演示?
When you use
'\n'
in your grammar rules, you're not matching abackslash+n
but a new line character. And it looks like your input does not contain new line characters butbackslash+n
's.So, my guess is you need to either change your
test
rule into:resulting in the parse-tree:
or leave your
test
rule as is but change your input into:resulting in the parse-tree:
If that is not the case, could you post a SSCCE: a small, full working demo that I (or someone else can run) that shows this error?