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

Boost-Spirit: Parsing lists into different fields of a struct

我正准备开始使用 boost-spirit,但是我卡住了,而且它的错误消息不太容易理解。

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

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

一个 header 包含一个整数列表和一个 body 包含一个无序序列 (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;
};

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

我会简化语法:

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

然后我会创建 phoenix 函数来推送元素:

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: '
'