怎么做" stream"在Boost Spirit X3中解析?

时间:2016-05-06 18:52:24

标签: c++ boost boost-spirit boost-spirit-x3

我试图找出使用x3从istream解析的正确方法。较旧的文档引用multi_pass内容,我还可以使用它吗?或者是否有其他方法来缓冲X3的流,以便它可以回溯?

1 个答案:

答案 0 :(得分:4)

你仍然可以使用它。只需加入

#include <boost/spirit/include/support_istream_iterator.hpp>

示例 Live On Coliru

#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <iostream>
#include <sstream>

int main() {
    std::istringstream iss("{ 123, 234, 345, 456, 567, 678, 789, 900, 1011 }");
    boost::spirit::istream_iterator f(iss), l;

    std::vector<int> values;

    namespace x3 = boost::spirit::x3;

    if (x3::phrase_parse(f, l, '{' >> (x3::int_ % ',') >> '}', x3::space, values)) {
        std::cout << "Parse results:\n";
        for (auto v : values) std::cout << v << " ";
    } else
        std::cout << "Parse failed\n";
}

打印

Parse results:
123 234 345 456 567 678 789 900 1011