c++ - spirit :不能在其规则定义中使用 x3::skip(skipper)[一些递归规则]

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

假设我们要像这样解析一个递归 block 。当“skip_comments_tag”作为 block 的前缀时,我们递归地跳过该 block 内的所有评论(/*...*/)。

{
    {}
    {
        skip_comments_tag{
            {} /*comments*/ 
            { /*comments*/ } 
        }
    }
}

Coliru 中那样很容易想出一个递归解析器.

namespace Parser {
    auto const ruleComment = x3::lit("/*") >> *(x3::char_ - "*/") >> "*/" | x3::space;

    x3::rule<struct SBlockId> const ruleBlock;
    auto const ruleBlock_def = x3::lit('{') >> *(ruleBlock | "skip_comments_tag" >> x3::skip(ruleComment)[ruleBlock]) >> '}';

    BOOST_SPIRIT_DEFINE(ruleBlock)
}

但它不会编译(当 parse 函数被调用时)因为它会生成一个无限上下文(通过 x3::make_context in x3: :skip_directive). x3::no_casex3::with 也有这个问题,因为它们在实现中都使用了x3::make_context

问题:

  1. 是否总有更好的方法来为这种类型编写解析器? 避免此类编译错误的问题以及如何避免?
  2. 或者 x3::make_context 实现是否被认为存在此类问题的缺陷?

最佳答案

老实说,我确实认为这是 make_context 工具中的一个限制,是的,它以前曾困扰过我。

您可以通过使用 TU 分离(BOOST_SPIRIT_DECLAREBOOST_SPIRIT_DEFINEBOOST_SPIRIT_INSTANTIATE 宏)。

老实说,我会在邮件列表中报告它:[spirit-general]

另见 http://boost.2283326.n4.nabble.com/Horrible-compiletimes-and-memory-usage-while-compiling-a-parser-with-X3-td4689104i20.html (FWIW 我觉得“序列分区”问题无关)

关于c++ - spirit :不能在其规则定义中使用 x3::skip(skipper)[一些递归规则],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45282293/

相关文章:

C++11 错误不匹配调用 main 之外的 Ifstream

c++将数组传递给函数问题

c++ - boost asio 需要在 m 个工作完成后才发布 n 个工作

boost - 构建 boost 1.65.0 : vcvarsall. 未找到 bat

c++ - 在 Boost Spirit 2.5.2 中按层次拆分语法

c++ - 如何并行运行Qt GUI和Linux消息队列接收线程?

c++ - 我正在使用未定义 SDL_main 的 SDL 函数。这样好吗?

c++ - 编译简单的boost spirit语法

c++ - Boost 的 y=Ax 的线性代数解决方案

c++ - Boost Spirit Qi Custom Syntesized Attribute(通过语义操作设置结构属性的特定成员)