c++ - boost::spirit::karma:将 no_delimit 与替代项一起使用

标签 c++ boost boost-spirit boost-variant boost-spirit-karma

我试图关闭围绕包含替代运算符 ('|') 的规则的定界,但我收到有关不兼容定界符的编译错误。例如,我从 boost 中获取了 calc2_ast_dump.cpp 示例,并将 struct dump_ast 中的 ast_node 规则修改为:

ast_node %= no_delimit[int_ | binary_node | unary_node];

但这给出了编译错误:

/usr/include/boost/function/function_template.hpp:754:17: note: candidate function not viable: no known conversion
  from 'const
boost::spirit::karma::detail::unused_delimiter<boost::spirit::karma::any_space<boost::spirit::char_encoding::ascii>
  >' to 'const boost::spirit::karma::any_space<boost::spirit::char_encoding::ascii>' for 3rd argument
result_type operator()(BOOST_FUNCTION_PARMS) const

以及 boost/spirit/home/karma/nonterminal/rule.hpp 中的相关注释:

// If you are seeing a compilation error here stating that the
// third parameter can't be converted to a karma::reference
// then you are probably trying to use a rule or a grammar with
// an incompatible delimiter type.

在我自己的项目中,我能够毫无问题地执行“no_delimit[a << b]”(使用 karma::space 分隔符)。

关于替代方案,我是否遗漏了什么?为什么 no_delimit 与“<<”一起使用,而不是“|”?

我使用的是 boost 1.48,是否有我需要修复的错误?

最佳答案

您需要修改规则声明以反射(reflect)它们未使用定界符这一事实。

假设您不想要任何定界,无论如何:

template <typename OuputIterator>
struct dump_ast
  : karma::grammar<OuputIterator, expression_ast()>
{
    dump_ast() : dump_ast::base_type(ast_node)
    {
        ast_node %= int_ | binary_node | unary_node;
        binary_node %= '(' << ast_node << char_ << ast_node << ')';
        unary_node %= '(' << char_ << ast_node << ')';
    }

    karma::rule<OuputIterator, expression_ast()> ast_node;
    karma::rule<OuputIterator, binary_op()> binary_node;
    karma::rule<OuputIterator, unary_op()> unary_node;
};

观看直播 http://liveworkspace.org/code/4edZlj$0

关于c++ - boost::spirit::karma:将 no_delimit 与替代项一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234616/

相关文章:

c++ - 库 C 头文件在 Linux 上放在哪里

c++ - 传统上如何在 C++ 中实现多类型容器(数组/vector/列表)? (当然没有元组)

c++ - 子解析器属性

c++ - 为什么我的代码一直打印 "i"而不是完整的句子?

c++ - 为什么成员函数调用没有段错误?

c++ - xiAPI : Failed to change thread scheduler, 检查实时优先级的用户限制

c++ - BOOST_PP_DEFINED 可以实现吗?

c++ - 使用 boost::serialization 大大增加了二进制大小

c++ - 使用 boost spirit x3 进行语义检查

c++ - boost::spirit 属性赋值:struct is_nullary:基类型不能是结构或类类型