Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板精神X3解析器_C++_Boost Spirit_Boost Spirit X3 - Fatal编程技术网

C++ 模板精神X3解析器

C++ 模板精神X3解析器,c++,boost-spirit,boost-spirit-x3,C++,Boost Spirit,Boost Spirit X3,在Boost中,很容易对解析器进行模板化,这样就可以为各种属性类型实例化解析器。我不清楚如何使用X3实现这一点。考虑一下这个删减版本: 与 这显然太天真了。如何正确地做到这一点?在X3中,动态组合解析器不会有太多困难。因此,我将您的示例写为: template <typename Attribute> auto make_roman() { using namespace boost::spirit::x3; struct numbers_ : symbols<

在Boost中,很容易对解析器进行模板化,这样就可以为各种属性类型实例化解析器。我不清楚如何使用X3实现这一点。考虑一下这个删减版本:


这显然太天真了。如何正确地做到这一点?

在X3中,动态组合解析器不会有太多困难。因此,我将您的示例写为:

template <typename Attribute>
auto make_roman() {
    using namespace boost::spirit::x3;

    struct numbers_ : symbols<Attribute> {
        numbers_() { this-> add
            ("I", Attribute{1}) ("II", Attribute{2}) ("III", Attribute{3}) ("IV", Attribute{4})
            ("V", Attribute{5}) ("VI", Attribute{6}) ("VII", Attribute{7}) ("VIII", Attribute{8})
            ("IX", Attribute{9}) ;
        }
    } numbers;

    return rule<class roman, Attribute> {"roman"} = 
        eps        [([](auto &x) { _val(x) = 0; })] 
        >> numbers [([](auto &x) { _val(x) += _attr(x); })];
}

看到了吗

这看起来比QI中恼人的语法结构要好得多。非常感谢你!如果数字是静态的,可以避免返回类型中的类型擦除吗?@ildjarn您能详细说明一下这是什么意思吗?这个答案中没有类型擦除。规则{roman}=不是返回语句类型擦除的一部分吗?@ildjarn不。这就是你想到的精神齐
namespace parser {
// ...
}
template < typename int_t >
struct parser {
// ...
};
template <typename Attribute>
auto make_roman() {
    using namespace boost::spirit::x3;

    struct numbers_ : symbols<Attribute> {
        numbers_() { this-> add
            ("I", Attribute{1}) ("II", Attribute{2}) ("III", Attribute{3}) ("IV", Attribute{4})
            ("V", Attribute{5}) ("VI", Attribute{6}) ("VII", Attribute{7}) ("VIII", Attribute{8})
            ("IX", Attribute{9}) ;
        }
    } numbers;

    return rule<class roman, Attribute> {"roman"} = 
        eps        [([](auto &x) { _val(x) = 0; })] 
        >> numbers [([](auto &x) { _val(x) += _attr(x); })];
}