单引号兼作运算符的 Antlr 语法
我正在研究 Antlr 语法,其中单引号既用作运算符又在字符串文字中使用,例如:
operand: DIGIT | STRINGLIT | reference;
expression: operand SQUOTE;
STRINGLIT: '\'' ~('\\'|'\'')* '\'';
像 1'
这样的表达式可以正确解析,但是当存在与 ~( 匹配的输入时'\\'|'\'')*
在引号后,例如 1'+2
,词法分析器尝试匹配 STRINGLIT
并失败。我希望能够恢复并发出 SQUOTE
。有什么想法如何实现它吗?
谢谢。
I am working on an Antlr grammar in which single quotes are used both as operators and in string literals, something like:
operand: DIGIT | STRINGLIT | reference;
expression: operand SQUOTE;
STRINGLIT: '\'' ~('\\'|'\'')* '\'';
Expressions like 1'
parse correctly, but when there is input that matches ~('\\'|'\'')*
after the quote, such as 1'+2
, the lexer attempts to match STRINGLIT
and fails. I'd like to be able to recover and emit SQUOTE
. Any ideas how to accomplish it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Antlrworks 中测试了这个语法之后,我认为你的问题是你在这里限制太多了。如果您有一个规则在看到
操作数 SQUOTE
后接受某些内容,Antlr 将能够解析1'+2
。由于 ANTLR 不知道如何处理+2
,因此它会抛出异常。After testing this grammar a bit in Antlrworks, I think that your problem is that you are being too restrictive here. Antlr would be able to parse
1'+2
if you had a rule that accepted something after it has seenoperand SQUOTE
. Since ANTLR doesn't know what to do with the+2
, it throws an exception.