c++ - X3 : How to create parser to read in sets?

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

如何创建一个规则来读取一组 3 个整数。即...

1 2 3                   OK, 1 set of 3 ints
1 2 3 4 5 6             OK, 2 sets of 3 ints
1 2 3 4 5               ERROR, 1 set of 3 ints, 1 short for 2nd
1 2 3 4 5 6 7 8 9       OK, 3 sets of 3 ints
1 2 3 4 5 6 7 8 9 10    ERROR, 3 sets of 3 ints, 1 short for 4th

我对融合适应结构有疑问(如何使 args 变量的数量)......

BOOST_FUSION_ADAPT_STRUCT(client::ast::number, n1, n2, n3, n4, n5, n6)

并且不确定为什么这条规则行不通。

... = *(int_ >> int_ >> int_);

这是我的尝试... http://coliru.stacked-crooked.com/a/cb10e8096c95fc55

//#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

namespace client { 
    namespace ast {

        struct number {
            int n1, n2, n3, n4, n5, n6;
        };

        struct comment {
            std::string text;
            bool dummy;
        };

        struct input {
            std::vector<comment> comments;  
            std::vector<number> numbers;
        };
    } 
}

BOOST_FUSION_ADAPT_STRUCT(client::ast::comment, text, dummy)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number, n1, n2, n3) // , n4, n5, n6) error
BOOST_FUSION_ADAPT_STRUCT(client::ast::input, comments, numbers)

namespace client {      
    namespace parser {

        namespace x3 = boost::spirit::x3;
        using namespace x3;

        typedef std::string::const_iterator It;

        using namespace x3;

        auto const comment = rule<struct _c, ast::comment> {"comment"} = lexeme[*(char_ - eol)] >> attr(false);
        auto const number  = rule<struct _n, ast::number> {"number"}   = int_ >> int_ >> int_;
        // auto const number  = rule<struct _n, ast::number> {"number"}   = *(int_ >> int_ >> int_); error

        auto lines = [](auto p) { return *(p >> eol); };

        auto const input = 
            repeat(1)[comment] >> eol >>
            lines(number);
    }
}

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

    std::string const iss("any char string here\n1 2 3\n1 2 3 4 5 6");

    auto iter = iss.begin(), eof = iss.end();

    client::ast::input types;

    bool ok = phrase_parse(iter, eof, client::parser::input, x3::blank, types);

    if (iter != eof) {
        std::cout << "Remaining unparsed: '" << std::string(iter, eof) << "'\n";
    }
    std::cout << "Parsed: " << (100.0 * std::distance(iss.begin(), iter) / iss.size()) << "%\n";
    std::cout << "ok = " << ok << std::endl;

    for (auto& item : types.comments) { std::cout << "comment: " << boost::fusion::as_deque(item) << "\n"; }
    for (auto& item : types.numbers)  { std::cout << "number:  " << boost::fusion::as_deque(item) << "\n"; }
}

打印

Parsed: 71.0526%
ok = 1
comment: (any char string here 0)
number:  (1 2 3)

最佳答案

Operator Kleene-star 合成为容器属性(文档显示:vector<T>)

你的结构 number不是容器属性。所以。

另外,我完全不清楚你想要实现什么。你的结构应该是 6 个整数,但你想解析 3 组?这些组的含义是什么?我可能会这样做:

struct number {
    struct group { int n1, n2, n3; };
    std::vector<group> groups;
};

BOOST_FUSION_ADAPT_STRUCT(client::ast::number::group, n1, n2, n3)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number, groups)

这与解析器表达式兼容

 *(int_ >> int_ >> int_)

现场演示

两个注意事项:

  1. 再次需要虚拟属性(考虑 using number = std::vector<number_group>; )
  2. 缺少一个 \n在输入结束时(考虑 eol | eoi )

Live On Coliru

//#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

namespace client { 
    namespace ast {

        struct number {
            struct group { int n1, n2, n3; };
            std::vector<group> groups;
            bool dummy;
        };

        struct comment {
            std::string text;
            bool dummy;
        };

        struct input {
            std::vector<comment> comments;  
            std::vector<number> numbers;
        };
    } 
}

BOOST_FUSION_ADAPT_STRUCT(client::ast::comment, text, dummy)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number::group, n1, n2, n3)
BOOST_FUSION_ADAPT_STRUCT(client::ast::number, groups, dummy)
BOOST_FUSION_ADAPT_STRUCT(client::ast::input, comments, numbers)

namespace client {      
    namespace parser {

        namespace x3 = boost::spirit::x3;
        using namespace x3;

        typedef std::string::const_iterator It;

        using namespace x3;

        auto const comment = rule<struct _c, ast::comment> {"comment"} = lexeme[*(char_ - eol)] >> attr(false);
        auto const number  = rule<struct _n, ast::number> {"number"}   = *(int_ >> int_ >> int_) >> attr(false);

        auto lines = [](auto p) { return *(p >> eol); };

        auto const input = 
            repeat(1)[comment] >> eol >>
            lines(number);
    }
}

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

    std::string const iss("any char string here\n1 2 3\n1 2 3 4 5 6\n");

    auto iter = iss.begin(), eof = iss.end();

    client::ast::input types;

    bool ok = phrase_parse(iter, eof, client::parser::input, x3::blank, types);

    if (iter != eof) {
        std::cout << "Remaining unparsed: '" << std::string(iter, eof) << "'\n";
    }
    std::cout << "Parsed: " << (100.0 * std::distance(iss.begin(), iter) / iss.size()) << "%\n";
    std::cout << "ok = " << ok << std::endl;

    for (auto &item : types.comments) {
        std::cout << "comment: " << boost::fusion::as_deque(item) << "\n";
    }
    for (auto& item : types.numbers) {
        std::cout << "number:  ";
        for (auto& g : item.groups)
            std::cout << boost::fusion::as_deque(g) << " ";
        std::cout << "\n";
    }
}

打印

Parsed: 100%
ok = 1
comment: (any char string here 0)
number:  (1 2 3) 
number:  (1 2 3) (4 5 6) 

关于c++ - X3 : How to create parser to read in sets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45930038/

相关文章:

Python - 从 JSON 创建字典时遇到问题

c++ - X3,什么是attr_gen?

c++ - boost phoenix 没有引用之前的比赛

java - 无法使用 document.getElementById 获取元素,返回 null

c++ - MinGW-w64 gcc 上对 boost::random::random_device 构造函数和析构函数的 undefined reference

java - 如何使用正确的编码存储和检索 HTML?

boost - 您如何使用 boost spirit x3 实现自定义解析器对象,以便它与 skipper 一起玩得很好?

c++ - 如何使用存储在 boost spirit 闭包中的变量作为 boost spirit 循环解析器的输入?

c++ - 无法正确实现upper_bound()

c++ - QT C++ - 在线程类中使用 MainWindow 类 QString