使用 boost::xpressive 语法可以报告语法错误吗?
我正在尝试解析一种自定义语言(与 JSON 不太相似),并且我决定尝试使用 boostexpressive,因为它看起来很有趣。
然而,当表达性匹配失败时,它就失败了。有什么方法可以实现某种错误报告吗?就像'表达式匹配到第 47 个字符(我可以从中获取行号)。
我可以看到如何在查找所需的标记或匹配项后定制每个子表达式以查找其他标记或匹配项,并在这种情况下报告错误,但这似乎是一种非常复杂的方法。
表达中是否有任何功能(或者任何人都可以建议一种方法)可以让我做到这一点?
谢谢。
I'm trying to parse a custom language (not too dissimilar to JSON), and I decided to try using boost expressive, as it looked fun.
However, when an xpressive match fails, it simply fails. Is there any way I can implement some kind of error reporting? Like 'the expression matched up until the 47th character (I can get the line numbers from that).
I can sort of see how one could tailor each sub expression to look for other tokens or matches after looking for the one it wants, and reporting an error in this case, but it seems that would be a very complex way of doing it.
Is there any functionality in expressive (or can anyone suggest an approach) that would allow me to do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 ANTLR 代替。它是 Boost Spirit/Qi 等酷炫、前沿的东西与 lex 和 yacc 等强大工具之间的一个很好的折衷方案。它可以像您想要的那样进行一些更智能的错误报告,而无需花费太多精力。
请注意,目前 ANTLR 版本 2 和 3 都是常用的; 2 包括 C++ 代码生成,而 3 不包括,因此您现在可能想坚持使用“旧”版本(如果 v3 最终有 C++ 目标,移植应该相当简单)。
I suggest using ANTLR instead. It is a good compromise between cool, bleeding-edge stuff like Boost Spirit/Qi and stalwart tools like lex and yacc. It can do some amount of smarter error reporting like you want without too much effort.
Note that there are currently ANTLR versions 2 and 3 are both in common usage; 2 includes C++ code generation whereas 3 does not, so you might want to stick with the "older" version for now (porting should be fairly straightforward if v3 eventually has a C++ target).