【问题标题】:Boost spirit x3 parser doesn't work with multiple attributeBoost Spirit x3 解析器不适用于多个属性
【发布时间】:2017-12-29 17:16:21
【问题描述】:

Spirit X3 解析器函数在使用 1 个属性时效果很好。当我尝试从具有多个属性的documentation 编译代码时,它不起作用。

#include <boost/spirit/home/x3.hpp>
#include <iostream>
using namespace std;
using namespace boost::spirit;

string a = "3.2 4.5";
auto begin = a.begin();
auto end = a.end();
double d1 = 0.0, d2 = 0.0;
x3::phrase_parse(begin, end ,
                 x3::double_ >> x3::double_,
                 x3::space,
                 d1, d2);  // doesn't work. Accept only 1 attribut

它返回以下错误:

/home/sacha/Dev/vql/vqlcompiler.cpp:20: erreur : no matching function for call to ‘phrase_parse(__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, boost::spirit::x3::sequence<boost::spirit::x3::real_parser<double>, boost::spirit::x3::real_parser<double> >, const space_type&, double&, double&)’
                      x3::double_ >> x3::double_, x3::space, d1, d2);
                                                                   ^

似乎它不是可变参数模板。那么,是我还是文档?

【问题讨论】:

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


    【解决方案1】:

    确实如此。在 X3 中,可变参数过载被丢弃了。

    我敢打赌,这是自 Spirit V2 以来消除设计中不必要的复杂性的一般制度的一部分。

    当然你自己也可以很容易地包起来:

    auto parse = [](auto& b, auto e, auto const& p, auto&... binds) {
        auto attr = std::tie(binds...);
        return x3::phrase_parse(b, e, p, x3::space, attr);
    };
    

    演示

    Live On Coliru

    #include <boost/fusion/adapted/std_tuple.hpp>
    #include <boost/spirit/home/x3.hpp>
    #include <iostream>
    
    namespace x3 = boost::spirit::x3;
    
    int main() {
        auto parse = [](auto& b, auto e, auto const& p, auto&... binds) {
            auto attr = std::tie(binds...);
            return x3::phrase_parse(b, e, p, x3::space, attr);
        };
    
        std::string const s = "3.2 4.5";
    
        double d1, d2;
        auto begin = s.begin(), end = s.end();
    
        if (parse(begin, end, x3::double_ >> x3::double_, d1, d2)) {
            std::cout << "Parsed: " << d1 << ", " << d2 << "\n";
        } else {
            std::cout << "Parse failed\n";
        }
    
        if (begin != end)
            std::cout << "Remaining unparsed input: '" << std::string(begin, end) << "'\n";
    }
    

    打印

    Parsed: 3.2, 4.5
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多