4

概要

我想利用 Boost Spirit 的流解析器 APIstd::istream增量解析。但是,我找不到一个很好的例子来说明如何将它与基于迭代器的语法一起使用。从概念上讲,我的目标是解析无限类型的对象流T

细节

T具有 type和 skipper属性的 Qi 语法S通常具有以下形式:

template <typename Iterator>
struct grammar : qi::grammar<Iterator, T(), S>;

如何在基于流的 API 中使用这样的语法?具体来说,我对流 API 的心智模型是我可以按照以下方式做一些事情:

// Callback invoked for each successfully parsed instance of T.
void f(T const& x)
{
}


// What iterator type?
grammar<???> parser;
skipper<???> skipper;

T x;

std::ifstream ifs("/path/to/file");
ifs.unsetf(std::ios::skipws)
while (! ifs.eof())
{
    ifs >> phrase_match(parser, skipper, x);
    if (ifs.good() || ifs.eof())
        f(x);
}

我正在努力整合需要迭代器的传统语法。它如何与流 API 配合使用?

4

1 回答 1

3

您缺少Spirit 多通道迭代器。但是请注意,除非您竭尽全力确保语法具有最小的回溯,否则不会增量地解析流。

于 2012-04-27T20:56:49.493 回答