Boost-Spirit:将列表解析为结构的不同字段

时间:2017-10-01 13:08:26

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

我很想开始使用boost-spirit,但是我被卡住了,而且它的错误消息并不容易理解。

首先,我有以下文字格式:

(111, 222, 333, ...) {
    X 231
    Y 227
    X 54
    Z 41156
    Y 1112
    ...
}

包含整数列表和包含无序序列(X | Y | Z)的主体的标题,后面跟一个数字。 我想将它解析为以下结构:

struct my_struct {
        std::vector<int> head;
        std::vector<int> X;
        std::vector<int> Y;
        std::vector<int> Z;
};

到目前为止,我已经有了这个解析器:

BOOST_FUSION_ADAPT_STRUCT(
        my_struct,
        (std::vector<int>, head)
        (std::vector<int>, X)
        (std::vector<int>, Y)
        (std::vector<int>, Z)
)

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

template<typename Iterator, typename Skipper = ascii::space_type>
struct my_parser : qi::grammar<Iterator, my_struct(), Skipper> {
        my_parser() : my_parser::base_type(start) {
                start %= '(' >> head >> ')' >> '{' >> body >> '}';
                head = int_ % ',';
                body = +(X | Y | Z);
                X = 'X' >> int_;
                Y = 'Y' >> int_;
                Z = 'Z' >> int_;
        }

        qi::rule<Iterator, my_struct(), Skipper> start;
        qi::rule<Iterator, std::vector<int>(), Skipper> head;
        qi::rule<Iterator, std::vector<int>(), Skipper> body;
        qi::rule<Iterator, std::vector<int>(), Skipper> X;
        qi::rule<Iterator, std::vector<int>(), Skipper> Y;
        qi::rule<Iterator, std::vector<int>(), Skipper> Z;
};

到目前为止,这很好地编译,但结果显然是错误的。 使用上面的例子,结果是:

my_struct {
    header = [ 111, 222, 333 ]
         X = [ 231, 227, 54, 41156, 1112 ]
         Y = []
         Z = []
}

我很确定我需要的是下面的内容,但我无法编译,我不明白为什么。

template<typename Iterator, typename Skipper = ascii::space_type>
struct my_parser : qi::grammar<Iterator, my_struct(), Skipper> {
        my_parser() : my_parser::base_type(start) {
                start %= '(' >> head >> ')' >> '{' >> body(_val) >> '}';
                head = int_ % ',';
                body = +( X(bind(&my_struct::X, _r1))
                        | Y(bind(&my_struct::Y, _r1))
                        | Z(bind(&my_struct::Z, _r1))
                        );
                X = 'X' >> int_[push_back(_r1, _1)];
                Y = 'Y' >> int_[push_back(_r1, _1)];
                Z = 'Z' >> int_[push_back(_r1, _1)];
        }

        qi::rule<Iterator, my_struct(), Skipper> start;
        qi::rule<Iterator, std::vector<int>(), Skipper> head;
        qi::rule<Iterator, void(my_struct), Skipper> body;
        qi::rule<Iterator, void(std::vector<int>), Skipper> X;
        qi::rule<Iterator, void(std::vector<int>), Skipper> Y;
        qi::rule<Iterator, void(std::vector<int>), Skipper> Z;
};

1 个答案:

答案 0 :(得分:1)

我真的不喜欢使用语义动作,但考虑到AST和输入的选择,我认为没有多少选择。

我会简化语法:

    rule  = '(' >> (int_[head_(_val, _1)] % ',') >> ')'
        >> '{' >> +(
                'X' >> int_[X_(_val, _1)]
              | 'Y' >> int_[Y_(_val, _1)]
              | 'Z' >> int_[Z_(_val, _1)]
          )
        >> '}';

我会创建凤凰函数来推送元素:

template <std::vector<int> my_struct::*m> struct push {
    void operator()(my_struct& ms, int v) const { (ms.*m).push_back(v); }
};

现在,它很简单:

px::function<push<&my_struct::head> > head_;
px::function<push<&my_struct::X> > X_;
px::function<push<&my_struct::Y> > Y_;
px::function<push<&my_struct::Z> > Z_;

演示

<强> Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;

struct my_struct {
    std::vector<int> head;
    std::vector<int> X;
    std::vector<int> Y;
    std::vector<int> Z;
};

template<typename Iterator>
struct my_parser : qi::grammar<Iterator, my_struct()> {
    my_parser() : my_parser::base_type(start) {
        using namespace qi;
        using px::push_back;

        rule  = '(' >> (int_[head_(_val, _1)] % ',') >> ')'
            >> '{' >> +(
                    'X' >> int_[X_(_val, _1)]
                  | 'Y' >> int_[Y_(_val, _1)]
                  | 'Z' >> int_[Z_(_val, _1)]
              )
            >> '}';

        start = skip(space) [rule];
    }
  private:
    template <std::vector<int> my_struct::*m> struct push {
        void operator()(my_struct& ms, int v) const { (ms.*m).push_back(v); }
    };
    px::function<push<&my_struct::head> > head_;
    px::function<push<&my_struct::X> > X_;
    px::function<push<&my_struct::Y> > Y_;
    px::function<push<&my_struct::Z> > Z_;

    qi::rule<Iterator, my_struct()> start;
    qi::rule<Iterator, my_struct(), qi::space_type> rule;
};

int main() {
    using It = boost::spirit::istream_iterator;
    It f(std::cin >> std::noskipws), l;

    my_struct data;
    if (parse(f, l, my_parser<It>{}, data)) {
        std::cout << "Parsed:";
        std::copy(data.head.begin(), data.head.end(), std::ostream_iterator<int>(std::cout << "\nhead: ", " " ));
        std::copy(data.X.begin(), data.X.end(), std::ostream_iterator<int>(std::cout << "\nX: ", " " ));
        std::copy(data.Y.begin(), data.Y.end(), std::ostream_iterator<int>(std::cout << "\nY: ", " " ));
        std::copy(data.Z.begin(), data.Z.end(), std::ostream_iterator<int>(std::cout << "\nZ: ", " " ));
        std::cout << "\n";
    } else {
        std::cout << "Parse failed\n";
    }


    if (f != l)
        std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}

输入(111, 222, 333) { X 231 Y 227 X 54 Z 41156 Y 1112 }打印:

Parsed:
head: 111 222 333 
X: 231 54 
Y: 227 1112 
Z: 41156 
Remaining unparsed input: '
'