c++ - 从逗号分隔的整数中解析 std::vector<int>

标签 c++ c++11 dsl boost-spirit boost-spirit-qi

我正在尝试实现一个非常具体的语法,它要求我在某个时候解析逗号分隔的整数列表。 qi 规则如下所示:

qi::rule<Iterator, ascii::space_type> ident;
qi::rule<Iterator, ascii::space_type> nlist;

...

ident = char_ >> nlist;
nlist = ("(" >> int_ % "," >> ")");

...

我需要将值传递到 ident规则(表达式 ident 必须创建语法树节点,其中构造函数需要来自 nlist 的解析值)。我考虑过创建并填充 std::vector并使用像_val = vector<int>...这样的语义 Action .我现在不清楚的是如何根据此规则创建任意长度的 vector ,因为我没有对输入的长度做出任何假设,也没有使用像 examples 这样的预定义 vector 。 .

这是否可能,或者有更好的方法吗?

最佳答案

这就是灵气的生计。

只需使用任何兼容的属性类型和利润:

    using nlist_t = std::vector<int>;
    using ident_t = std::pair<char, nlist_t>;

    qi::rule<Iterator, ident_t(), qi::ascii::space_type> ident;
    qi::rule<Iterator, nlist_t(), qi::ascii::space_type> nlist;

Note: For std::pair attribute compatibility, include the relevant fusion header:

Live On Coliru

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

int main()
{
    using nlist_t = std::vector<int>;
    using ident_t = std::pair<char, nlist_t>;

    using Iterator = std::string::const_iterator;
    qi::rule<Iterator, ident_t(), qi::ascii::space_type> ident;
    qi::rule<Iterator, nlist_t(), qi::ascii::space_type> nlist;

    ident = qi::char_ >> nlist;
    nlist = '(' >> qi::int_ % ',' >> ')';

    for (std::string const input : { "a (1,2,3)", "+(881,-2,42)    \n", "?(0)" }) {

        ident_t data;
        if (qi::phrase_parse(input.begin(), input.end(), ident, qi::ascii::space, data)) {
            std::cout << "Parsed: " << data.first << "(";
            for (auto i : data.second) std::cout << i << ",";
            std::cout << ")\n";
        } else
            std::cout << "Parse failed: '" << input << "'\n";
    }
}

打印

Parsed: a(1,2,3,)
Parsed: +(881,-2,42,)
Parsed: ?(0,)

奖金

使用 phoenix::construct 想象的 Ast 类型的版本:

Also Live On Coliru

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/phoenix.hpp>

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

namespace OoShinyAst {

    using MyName = char;
    using MyArgument = int;
    using MyArgumentList = std::vector<MyArgument>;

    struct MyIdent {
        MyName         name;
        MyArgumentList args;

        MyIdent() = default;
        MyIdent(MyName name, MyArgumentList args)
            : name(std::move(name)), args(std::move(args)) { }
    };
}

int main()
{
    using Iterator = std::string::const_iterator;
    qi::rule<Iterator, OoShinyAst::MyIdent(),        qi::ascii::space_type> ident;
    qi::rule<Iterator, OoShinyAst::MyArgumentList(), qi::ascii::space_type> nlist;

    nlist = '(' >> qi::int_ % ',' >> ')';
    ident = (qi::char_ >> nlist) [ qi::_val = px::construct<OoShinyAst::MyIdent>(qi::_1, qi::_2) ];

    for (std::string const input : { "a (1,2,3)", "+(881,-2,42)    \n", "?(0)" }) {

        OoShinyAst::MyIdent data;
        if (qi::phrase_parse(input.begin(), input.end(), ident, qi::ascii::space, data)) {
            std::cout << "Parsed: " << data.name << "(";
            for (auto i : data.args) std::cout << i << ",";
            std::cout << ")\n";
        } else
            std::cout << "Parse failed: '" << input << "'\n";
    }
}

关于c++ - 从逗号分隔的整数中解析 std::vector<int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42191747/

相关文章:

java - ANTLR 提供这些功能吗?

java - 如何中断或取消 Spring Integration Java DSL 流程?

ruby - 在 Ruby 中构建一个 "Semi-Natural Language"DSL

c++ - 静音 gcc 的 "only available with -std=c++XX or -std=gnu++XX"警告

function - 如何将不同的函数签名设置为同一个函数指针?

c++ - 可以在模板化的 typedef 上使用模板特化吗?

c++ - 如何创建一个 "event"系统来接收数据包

c++ - Debug模式与 Release模式下的 Visual Studio (C++) 调试

类 C 实现的 C++ 等效性能版本

c++ - GNU Gettext 和宽字符