2017-10-20 84 views
1

我试图使用Boost 1.65.1中的Spirit X3来创建解析器。我减少了我的问题,下面的小例子用更简单的结构:没有默认构造函数的Boost-Spirit(X3)解析

#include <boost/config/warning_disable.hpp> 
#include <boost/spirit/home/x3.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 

#include <iostream> 
#include <vector> 

struct MyPair { 
    MyPair(int x, int y) : mx(x), my(y) {}; 
    //MyPair() {} // No default constructor - neither needed nor wanted. 
    int mx; 
    int my; 
}; 

/* 
BOOST_FUSION_ADAPT_STRUCT(
    MyPair, 
    (int, mx) 
    (int, my) 
) 
*/ 

int main() 
{ 
    using boost::spirit::x3::int_; 
    using boost::spirit::x3::parse; 

    std::vector<MyPair> pairs; 
    char const *first = "11:22,33:44,55:66", *last = first + std::strlen(first); 
    auto pair = [&](auto& ctx) { return MyPair(1, 2); }; 
    bool parsed_some = parse(first, last, ((int_ >> ':' >> int_)[pair]) % ',', pairs); 

    if (parsed_some) { 
     std::cout << "Parsed the following pairs" << std::endl; 
     for (auto& p : pairs) { 
      std::cout << p.mx << ":" << p.my << std::endl; 
     } 
    } 
    return 0; 
} 

我不想默认构造函数添加到我的类型(这里MyPair)。如果没有默认的构造函数,我得到了以下错误:

'MyPair::MyPair': no appropriate default constructor available ...\boost\utility\value_init.hpp 

但我不想改变我的结构有一个默认的构造函数。假设,我也加一个,最后一个错误我得到的是:

binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) TestParsing ...\x3\support\traits\move_to.hpp 

但因为我手动构造属性的语义动作,我不明白为什么我需要一个融合的定义。 (注意,目前它使用硬编码值,直到我解决这个问题,然后得到适当的值)。

如何使用Spirit X3构造没有默认构造函数的用户定义类型的属性?

回答

1

想到的唯一方法是避免有一个规则/解析器完全绑定到MyPair属性。

幸运X3是足够的灵活性,结合反复的容器属性:

auto pair = x3::rule<struct pair_, std::vector<MyPair> > {} 
      = (int_ >> ':' >> int_) 
       [([&](auto& ctx) { 
        auto& attr = x3::_attr(ctx); 
        using boost::fusion::at_c; 
        return x3::_val(ctx).emplace_back(at_c<0>(attr), at_c<1>(attr)); 
       })] 
      ; 

Live On Coliru

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

struct MyPair { 
    MyPair(int x, int y) : mx(x), my(y) {}; 
    int mx; 
    int my; 
}; 

int main() 
{ 
    namespace x3 = boost::spirit::x3; 
    using x3::int_; 

    std::vector<MyPair> pairs; 
    char const *first = "11:22,33:44,55:66", *last = first + std::strlen(first); 

    auto pair = x3::rule<struct pair_, std::vector<MyPair> > {} 
       = (int_ >> ':' >> int_) 
        [([&](auto& ctx) { 
         auto& attr = x3::_attr(ctx); 
         using boost::fusion::at_c; 
         return x3::_val(ctx).emplace_back(at_c<0>(attr), at_c<1>(attr)); 
        })] 
       ; 

    bool parsed_some = parse(first, last, pair % ',', pairs); 

    if (parsed_some) { 
     std::cout << "Parsed the following pairs" << std::endl; 
     for (auto& p : pairs) { 
      std::cout << p.mx << ":" << p.my << std::endl; 
     } 
    } 
} 

打印

Parsed the following pairs 
11:22 
33:44 
55:66 
+0

感谢。但我不确定我能否应用这种方法。我想要的输出“AST”是大多非默认可构造类型的层次结构,例如: A a(“Simple/Primitive argument”)... B b(24.8); //最低层 X x(a,c)... Y y(b); //最低+ 1层 P(x,y)//最低+2层 ......依此类推...... – senevoldsen

+0

然后你就会有精神上的困难。我会解析成中间表示并转换它。 X3有特点使转换“自动” – sehe

+0

我害怕我不得不做另一个中间表示;很多样板。你有链接到一些关于如何自动化这些转换的文档吗? – senevoldsen

相关问题