【问题标题】:Ambiguous variant and boost spirit x3模棱两可的变种和提升精神x3
【发布时间】:2013-06-20 16:01:36
【问题描述】:

尝试调整 boost spirit x3 calc 示例以解析可以将函数作为参数的函数。但是它不编译。

namespace client{ namespace ast{
    struct ts;
    struct fnc;
    typedef boost::variant<
    ts,
    boost::recursive_wrapper<fnc>
    > node;
    struct ts{
        unsigned int id;
    };
    struct fnc{
        std::vector<char> id;
        std::vector<node> args;
    };
}}
BOOST_FUSION_ADAPT_STRUCT(
    client::ast::ts,
    (unsigned int, id)
)
BOOST_FUSION_ADAPT_STRUCT(
    client::ast::fnc,
    (std::vector<char>, id)
    (std::vector<client::ast::node>, args)
)
namespace client{
    namespace x3 = boost::spirit::x3;
    namespace calc_grammar{
        using x3::uint_;
        using x3::alpha;
        using x3::alnum;
        using x3::lit;
        using x3::char_;
        x3::rule<class funct, ast::fnc> const funct("function");
        x3::rule<class ts, ast::ts> const ts("timeseries");
        x3::rule<class funct_name, std::vector<char>> const funct_name("function_name");
        auto const funct_def = funct_name >> lit('(') >> -((ts|funct)%lit(',')) >> lit(')');
        auto const ts_def = lit('#') >> uint_ >> lit('#');
        auto const funct_name_def = lit('@') >> alpha >> *(alnum|char_('_'));
        auto const calc = x3::grammar(
            "calc", 
            funct = funct_def, 
            ts = ts_def,
            funct_name = funct_name_def
        );
    }
    using calc_grammar::calc;
}

错误 C2665: 'boost::detail::variant::make_initializer_node::apply::initializer_node::initialize': 5 个重载都不能转换所有参数类型

variant.hpp 中还有一条给用户的注释

// NOTE TO USER :
// Compile error here indicates that the given type is not 
// unambiguously convertible to one of the variant's types
// (or that no conversion exists).

然而我并不聪明......

【问题讨论】:

  • 在你得到更好的答案之前,添加一个什么都不做的默认构造函数和一个接受无符号整数并将其存储在 id 中的构造函数到你的 ts 结构似乎使它对我有用g++ 4.8.1.
  • @cv_and_he 完全同意分析; @user2515328 您可能想在[spirit-general] 用户列表中报告此问题 - 我认为开发人员正在积极征求反馈意见(boost-spirit.com 目前似乎已关闭)
  • @cv_and_he 感谢您的提示,但仍然没有解决。使用 mvsc,我想我应该把它放在我最初的帖子中。
  • @not-sehe 我会试试的,我先把它放在这里,因为它看起来不那么令人生畏。
  • @user2515328 我同意:所以很甜蜜。但是,boost-spirit-x3 目前的受众非常有限,因此您在列表中的机会更大(您的反馈也将传达给感兴趣的开发者!)。干杯。 (顺便说一下,我创建了新标签)

标签: c++ visual-c++ boost-spirit boost-spirit-x3


【解决方案1】:

我发现了这个老问题。与此同时,X3 也发生了一些变化,但无论如何我现在都会回答。

我怀疑主要问题可能是变体成员上的(缺少)(隐式)构造函数。

无论如何,这里有一个更轻量级语法的现场演示:

namespace grammar_def {
    using namespace x3;

    rule<class funct, ast::fnc> const funct("function");

    auto const ts        = lexeme [ '#' >> uint_ >> '#' ];
    auto const fname     = lexeme [ '@' >> raw [ alpha >> *(alnum | '_') ] ];
    auto const expr      = ts|funct;

    auto const funct_def = fname >> '(' >> -expr % ',' >> ')';

    BOOST_SPIRIT_DEFINE(funct)
}

我还添加了一些输出流助手。请注意,为了简单起见,我如何将 id 类型更改为 std::string(在不入侵 namespace std 的情况下,很难/不可能为 vector&lt;char&gt; 重载 operator&lt;&lt;):

namespace client { namespace ast {
    static std::ostream& operator<<(std::ostream& os, ts const& v) {
        using namespace boost::fusion;
        return os << tuple_open("") << tuple_close("") << tuple_delimiter("") << as_vector(v);
    }

    static std::ostream& operator<<(std::ostream& os, fnc const& v) {
        using namespace boost::fusion;
        return os << tuple_open("") << tuple_close("") << tuple_delimiter("") << as_vector(v);
    }
    template<typename T>
    static std::ostream& operator<<(std::ostream& os, std::vector<T> const& v) {
        os << "("; for (auto& el : v) os << (&el==&v[0]?"":", ") << el; return os << ")";
    }
} }

演示

这有更多(可选)管道以提供更丰富的调试信息:

Live On Coliru

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

namespace client { namespace ast {
    struct ts;
    struct fnc;

    //using string = std::vector<char>;
    using string = std::string; // for easier printing/debugging

    struct ts {
        unsigned int id;
        ts(unsigned id=0):id(id) {}
    };

    typedef boost::variant<ts, boost::recursive_wrapper<fnc> > node;

    struct fnc {
        string id;
        std::vector<node> args;
    };
} }

BOOST_FUSION_ADAPT_STRUCT(client::ast::ts, id)
BOOST_FUSION_ADAPT_STRUCT(client::ast::fnc, id, args)

//namespace std { static ostream& operator<<(ostream&os, vector<char> const& v) { return os.write(&v[0], v.size()); } }

namespace client { namespace ast {
    static std::ostream& operator<<(std::ostream& os, ts const& v) {
        using namespace boost::fusion;
        return os << tuple_open("") << tuple_close("") << tuple_delimiter("") << as_vector(v);
    }

    static std::ostream& operator<<(std::ostream& os, fnc const& v) {
        using namespace boost::fusion;
        return os << tuple_open("") << tuple_close("") << tuple_delimiter("") << as_vector(v);
    }
    template<typename T>
    static std::ostream& operator<<(std::ostream& os, std::vector<T> const& v) {
        os << "("; for (auto& el : v) os << (&el==&v[0]?"":", ") << el; return os << ")";
    }
} }

namespace client {
    namespace x3 = boost::spirit::x3;
    namespace grammar_def {
        using namespace x3;

        x3::rule<class funct, ast::fnc> const funct("function");

        auto const ts     // = x3::rule<class ts, ast::ts> {"timeseries"}
                             = lexeme [ '#' >> uint_ >> '#' ];
        auto const fname  // = x3::rule<class fname, ast::string> {"function_name"}
                             = lexeme [ '@' >> raw [ alpha >> *(alnum | '_') ] ];
        auto const expr   // = rule<struct expr_, ast::node > {"expr"}
                             = ts|funct;

        auto const funct_def = fname >> '(' >> -expr % ',' >> ')';

        BOOST_SPIRIT_DEFINE(funct)
    }

    auto const& grammar = x3::skip(x3::space) [grammar_def::funct];
}

#include <iostream>

int main() {
    std::string const s {
        "@pow( #1#, \n"
            "     @trunc(\n"
            "           @pi ()\n"
            "   ) )"};
    std::cout << "Parsing '" << s << "'\n";

    auto f = s.begin();
    client::ast::fnc parsed;

    if (parse(f, s.end(), client::grammar, parsed)) {
        std::cout << "Parse succeeded: " << parsed << "\n";
    } else {
        std::cout << "Parse failed\n";
    }

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

打印:

Parsing '@pow( #1#, 
     @trunc(
           @pi ()
   ) )'
Parse succeeded: pow(1, trunc(pi()))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多