Boost.Spirit:莱克斯+ Qi 错误报告

发布于 2024-11-06 03:09:36 字数 561 浏览 5 评论 0原文

我正在为相当复杂的配置文件编写一个解析器,这些文件使用缩进等。我决定使用 Lex 将输入分解为标记,因为它似乎让生活变得更轻松。问题是我找不到任何使用 Qi 错误报告工具 (on_error) 和对标记流而不是字符进行操作的解析器的示例。

on_error 中使用的错误处理程序需要一些信息才能准确指示错误在输入流中的位置。所有示例都只是从一对迭代器构造 std::string 并打印它们。但如果使用 Lex,则迭代器是标记序列的迭代器,而不是字符的迭代器。在我的程序中,在我注意到无效的迭代器类型之前,这导致在 std::string 构造函数中挂起。

据我了解,令牌可以将一对迭代器保存到输入流作为其值。这是默认的属性类型(如果类型类似于 lex::lexertl::token)。但是,如果我希望我的令牌包含对解析更有用的内容(intstd::string 等),那么这些迭代器就会丢失。

在将 Lex 与 Qi 结合使用时,如何生成人类友好的错误消息来指示输入流中的位置?有这种用法的例子吗?

谢谢。

I am writing a parser for quite complicated config files that make use of indentation etc. I decided to use Lex to break input into tokens as it seems to make life easier. The problem is that I cannot find any examples of using Qi error reporting tools (on_error) with parsers that operate on stream of tokens instead of characters.

Error handler to be used in on_error takes some to be able to indicate exactly where the error is in the input stream. All examples just construct std::string from the pair of iterators and print them. But if Lex is used, that iterators are iterators to the sequence of tokens, not characters. In my program this led to hang in std::string constructor before I noticed invalid iterator type.

As I understand token can hold a pair of iterators to the input stream as its value. This is the default attribute type (if type is like lex::lexertl::token<>). But if I want my token to contain something more useful for parsing (int, std::string, etc), those iterators are lost.

How can I produce human friendly error messages indicating position in the input stream while using Lex with Qi? Are there any examples of such usage?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-11-13 03:09:36

很抱歉回复晚了,但我花了一些时间来准备一个像样的例子来说明您想要实现的目标。我现在向 Spirit 添加了一个新的词法分析器示例:conjure_lexer。它是实现小型编程语言的 conjure (Qi) 示例的修改版本。主要区别在于它使用词法分析器而不是纯 Qi 语法。

新的 conjure_lexer 示例演示了以下几件事:
a) 它使用新的 position_token 类,该类扩展了现有的 token 类型。它始终存储指向相应匹配输入序列的迭代器对(除了令牌 ID、令牌值等常用信息之外)。
b) 它使用该位置信息进行错误报告
c) 沿着这条线,它演示了如何使用词法分析器来简化语法。

新示例位于 SVN(主干)中,并将在 Boost V1.47 中提供(即将发布)。它位于此目录中:$BOOST_ROOT/libs/spirit/example/qi/compiler-tutorial/conjure_lexer。

Sorry for the late reply, but it took me some time to prepare a decent example of what you're trying to achieve. I now added a new lexer example to Spirit: conjure_lexer. It is a modified version of the conjure (Qi) example implementing a small programming language. The main difference is that it is using a lexer instead of a pure Qi grammar.

The new conjure_lexer example demonstrates several things:
a) it is using a new position_token class, which extends the existing token type. It always stores the pair of iterators pointing to the corresponding matched input sequence (in addition to the usual information like token id, token value, etc.).
b) it is using this positional information for error reporting
c) and along the lines, it demonstrates how using a lexer can simplify the grammar.

The new example is in SVN (trunk) and will be available in Boost V1.47 (to be released soon). It's in this directory: $BOOST_ROOT/libs/spirit/example/qi/compiler-tutorial/conjure_lexer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文