Boost.Spirit:莱克斯+齐错误报告 [英] Boost.Spirit: Lex + Qi error reporting

查看:113
本文介绍了Boost.Spirit:莱克斯+齐错误报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个解析器相当复杂的配置文件,以利用压痕等我决定使用莱克斯破投入记号,因为它似乎使生活更轻松。问题是,我无法找到使用齐错误报告工具( ON_ERROR )与上记号,而不是字符流操作的解析器。

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.

中的错误处理ON_ERROR 需要一些能够准确指出错误所在的输入流。所有的例子都只是构建的std ::字符串从对迭代器,并打印出来。但如果使用莱克斯,该迭代器是迭代到令牌,而不是字符的序列。在我的计划,这导致在挂的std ::字符串构造函数之前,我注意到无效的迭代器类型。

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.

据我了解令牌可以容纳一对迭代器的输入流作为其值。这是默认的属性类型(如果类型是像 ::法:: lexertl令牌LT;> )。但是,如果我希望我的令牌包含的东西解析更有用的( INT 的std ::字符串等)这些迭代器都将丢失。

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?

感谢。

推荐答案

很抱歉这么晚才回复,但我花了一些时间来prepare的你想达到什么像样的例子。我现在添加了一个新的词法分析器的例子来精神: 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.

新的 conjure_lexer 例子演示了几件事情:
A)这是使用新的 position_token 类,它扩展了现有的标记键入。它总是存储在一对指向相应匹配的输入序列(除了像令牌标识,令牌值等通常的信息)迭代。
b)该使用错误报告此位置信息
c)及沿线,它演示了如何使用词法分析器可以简化语法。

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.

新的例子是SVN(主干),将在升压V1.47可用(即将发布)。这是此目录中:$ BOOST_ROOT /库/精神/例子/气/编译器教程/ conjure_lexer

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.

这篇关于Boost.Spirit:莱克斯+齐错误报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆