C++ 如何在x3中用继承的属性重写qi解析器?

C++ 如何在x3中用继承的属性重写qi解析器?,c++,boost,boost-spirit,boost-spirit-qi,boost-spirit-x3,C++,Boost,Boost Spirit,Boost Spirit Qi,Boost Spirit X3,如果继承的属性用于语义操作,我们可以使用x3::with指令 如果我们想将属性用作解析器的一部分,该怎么办?例如,一个简单的解析器匹配1个或多个字母字符,但该字符来自参数char集 qi::rule<std::string::const_iterator, qi::unused_type(char const*)> rule = +(qi::alpha - qi::char_(qi::_r1)); qi::规则= +(qi::alpha-qi::char_(qi::_r1)

如果继承的属性用于语义操作,我们可以使用
x3::with
指令

如果我们想将属性用作解析器的一部分,该怎么办?例如,一个简单的解析器匹配1个或多个字母字符,但该字符来自参数char集

qi::rule<std::string::const_iterator, qi::unused_type(char const*)> rule =
    +(qi::alpha - qi::char_(qi::_r1));
qi::规则=
+(qi::alpha-qi::char_(qi::_r1));
或者参数char集可以用作惰性解析器

qi::rule<std::string::const_iterator, qi::unused_type(char const*)> rule =
    +(qi::alpha - qi::lazy(qi::_r1));
qi::规则=
+(qi::阿尔法-qi::懒惰(qi:r1));

with指令将此本地值放入上下文中。我不确定我们是否可以在语义操作之外使用这个上下文,并最终生成一个解析器

简单地放弃一切规则化的旧习惯

#include <boost/spirit/home/x3.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;

template <typename... Args>
auto negate(Args&&... p) {
    return +(x3::char_ - x3::char_(std::forward<Args>(p)...));
};

int main() {
    std::string input("all the king's men and all the king's horses"), parsed;
    if (parse(input.begin(), input.end(), negate("horse"), parsed))
        std::cout << "'" << input << "' -> '" << parsed << "'\n";
}
,打印:

“所有国王的人和所有国王的马”->“所有国王的人和所有国王的马”

更复杂的东西 您还可以在自定义解析器中聚合子解析器:

如果您需要关于传递的复活规则,我建议
x3::with
(虽然我不确定上下文是否为
with
构建重入状态,但您需要测试精确的语义,除非您能找到相关文档)

#include <boost/spirit/home/x3.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;

template <typename Sub>
auto negate(Sub p) {
    return +(x3::char_ - x3::as_parser(p));
};

int main() {
    std::string input("all the king's men and all the king's horses"), parsed;
    if (parse(input.begin(), input.end(), negate("horse"), parsed))
        std::cout << "'" << input << "' -> '" << parsed << "'\n";
}