【问题标题】:How to do "stream" parsing in Boost Spirit X3?如何在 Boost Spirit X3 中进行“流”解析?
【发布时间】:2016-05-06 18:52:23
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您仍然可以使用它。只需包含

    #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 
    

    【讨论】:

    • 我认为一般需要iss.unsetf(std::ios::skipws); // No white space skipping!。在这里它似乎是偶然的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多