如何在Boost Spirit X3中进行“流式”解析?

exp*_*ite 5 c++ boost boost-spirit boost-spirit-x3

我试图找出istream使用x3 解析的正确方法。较旧的文档指的是multi_pass东西,我仍然可以使用它吗?还是有其他方法来缓冲X3的流,以便它可以回溯?

seh*_*ehe 6

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

#include <boost/spirit/include/support_istream_iterator.hpp>
Run Code Online (Sandbox Code Playgroud)

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";
}
Run Code Online (Sandbox Code Playgroud)

版画

Parse results:
123 234 345 456 567 678 789 900 1011 
Run Code Online (Sandbox Code Playgroud)

  • 我认为一般来说需要 `iss.unsetf(std::ios::skipws); // 没有空格跳过!`。在这里,它的工作似乎是偶然的。 (2认同)