简体   繁体   English

Boost.Spirit:Lex + Qi错误报告

[英]Boost.Spirit: Lex + Qi error reporting

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. 我正在编写一个解析器,用于使用缩进等非常复杂的配置文件。我决定使用Lex将输入分解为令牌,因为它似乎使生活更轻松。 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. 问题是我找不到任何使用Qi错误报告工具( on_error )的示例,其中解析器使用令牌流而不是字符进行操作。

Error handler to be used in on_error takes some to be able to indicate exactly where the error is in the input stream. 要在on_error中使用的错误处理程序需要一些能够准确指示错误在输入流中的位置。 All examples just construct std::string from the pair of iterators and print them. 所有示例只是从迭代器对构造std::string并打印它们。 But if Lex is used, that iterators are iterators to the sequence of tokens, not characters. 但是如果使用Lex,则迭代器是标记序列的迭代器,而不是字符。 In my program this led to hang in std::string constructor before I noticed invalid iterator type. 在我的程序中,这导致在我注意到无效迭​​代器类型之前挂起std::string构造函数。

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<> ). 这是默认属性类型(如果类型类似于lex::lexertl::token<> )。 But if I want my token to contain something more useful for parsing ( int , std::string , etc), those iterators are lost. 但是如果我希望我的令牌包含对解析更有用的东西( intstd::string等),那些迭代器就会丢失。

How can I produce human friendly error messages indicating position in the input stream while using Lex with Qi? 如何在使用带Qi的Lex时生成指示输入流中位置的人性化错误消息? Are there any examples of such usage? 有这种用法的例子吗?

Thanks. 谢谢。

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 . 我现在为Spirit添加了一个新的词法分析器示例: conjure_lexer It is a modified version of the conjure (Qi) example implementing a small programming language. 它是实现小编程语言的conjure (Qi)示例的修改版本。 The main difference is that it is using a lexer instead of a pure Qi grammar. 主要区别在于它使用词法分析器而不是纯Qi语法。

The new conjure_lexer example demonstrates several things: a) it is using a new position_token class, which extends the existing token type. 新的conjure_lexer示例演示了几件事:a)它使用了一个新的position_token类,它扩展了现有的token类型。 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.). 它总是存储指向相应匹配输入序列的迭代器对(除了通常的信息,如标记id,标记值等)。 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. b)它正在使用这个位置信息进行错误报告c)并且沿着这条线,它演示了如何使用词法分析器来简化语法。

The new example is in SVN (trunk) and will be available in Boost V1.47 (to be released soon). 新的例子在SVN(主干)中,将在Boost V1.47中发布(即将发布)。 It's in this directory: $BOOST_ROOT/libs/spirit/example/qi/compiler-tutorial/conjure_lexer. 它在这个目录中:$ BOOST_ROOT / libs / spirit / example / qi / compiler-tutorial / conjure_lexer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM