聚合Boost.灵气解析为规则间接产生错误

aggregating Boost.Spirit Qi parsers into rule indirectly gives error

本文关键字:错误 规则 Boost 聚合      更新时间:2023-10-16

我有这个语法

template<typename Iterator>
struct brainDSL : qi::grammar<Iterator, _problem(), ascii::space_type>
{
    brainDSL() : base_type(p_rule)
    {
        p_rule = problem_;
    }
    qi::rule<Iterator, _problem(), ascii::space_type> p_rule;
};

这些是解析器:

struct type_ : qi::symbols<char, _problem::_type>
{
    type_()
    {
        add
            ("single_stage", _problem::_type::single_stage)
            ("two_stage", _problem::_type::two_stage)
            ("multi_stage", _problem::_type::multi_stage)
        ;
    }
} type_;
struct command_ : qi::symbols<char, _problem::_command>
{
    command_()
    {
        add
            ("maximize", _problem::_command::maximize)
            ("minimize", _problem::_command::minimize)
        ;
    }
} command_;
auto name_ = qi::lexeme[*(qi::char_ - ':')];
auto problem_ =
    type_
    >> command_
    >> name_ >> ':'
;

我会添加structs和fusion宏,但我认为没有必要。对测试字符串执行此操作,此代码可以正确编译,但在boost内部引发异常。

如果我删除name_解析器并将problem_保留为这样:

auto problem_ =
        type_
        >> command_
    ;

它匹配正确。但如果我添加任何内容,即使是qi::eps,我也会再次得到异常:

auto problem_ =
        type_
        >> command_
        >> qi::eps
    ;

我在这里违反了Boost Spirit的基本规则是什么?

为了不让问题得到解答,我还将添加一些关于解决后条件的注释。

最好的解决方案是改用Spirit X3,它完全支持使用auto和lambdas,正如@sehe所建议的那样。

我必须做出的几个重大改变:

  • symbols现在只接受一个模板参数,即它映射的类型
  • X3没有语法,只有规则
  • 下面的教程建议使用宏来定义规则。这在MSVC上是个坏主意,最好只使用构造然后定义

示例:

auto r = rule<...> { "Name" }
       = definition;