如何在 Boost Spirit X3 中进行"stream"解析?

How to do "stream" parsing in Boost Spirit X3?

本文关键字:stream 解析 Boost Spirit X3      更新时间:2023-10-16

我正试图找到使用x3从istream解析的正确方法。旧的文档指的是multi_pass的东西,我还能用这个吗?或者有没有其他方法可以缓冲X3的流,以便它可以回溯?

您仍然可以使用它。只包括

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

示例在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 failedn";
}

打印

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