c++ - 将规则生成绑定(bind)到我的结构成员时出现编译错误

标签 c++ boost-spirit boost-spirit-qi boost-phoenix

用 Phoenix bind 写 Qi 语法我得到了类似这样的编译错误

boost/spirit/home/support/context.hpp(180): error C2338: index_is_out_of_bounds

这里

>> ruleHandId_[phx::bind(&parseContext::handId_, qi::_r1) = qi::_1];

我只是对 phoenix 绑定(bind)没有太多经验,但是 perv 绑定(bind)在行中

ruleStart_ = ruleEncoding_[phx::bind(&parseContext::encoding_, qi::_r1) = qi::_1]

运行良好,没有编译错误

这一切都在 VS2013 的 MSVC 下,提升了 1.56 x86

我在编译错误的代码下做错了什么?

源代码

#include <boost/spirit/include/qi.hpp>
#include <boost/phoenix/phoenix.hpp>
#include <boost/shared_ptr.hpp>
#include <sstream>

namespace sp = boost::spirit;
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

struct parseContext {
  std::string encoding_;
  uint64_t    handId_;
};

typedef boost::shared_ptr<parseContext> parseContextShPtr;

template <typename Iterator>
struct parseGrammar : qi::grammar<Iterator, void(parseContext&)> {
  parseGrammar() : parseGrammar::base_type(ruleStart_)
  {
    ruleStart_ = ruleEncoding_[phx::bind(&parseContext::encoding_, qi::_r1) = qi::_1]
      >> ruleHandHeader_;
    ruleEncoding_ = qi::lit("ABC");

    ruleHandHeader_ = qi::lit("DEF") >> qi::space
      >> qi::lit("XYZ #")
      >> ruleHandId_[phx::bind(&parseContext::handId_, qi::_r1) = qi::_1];
    ruleHandId_ = qi::long_long;
  }

  // Rules
  qi::rule<Iterator, void(parseContext&)> ruleStart_;
  qi::rule<Iterator, std::string()> ruleEncoding_;
  qi::rule<Iterator> ruleHandHeader_;
  qi::rule<Iterator, uint64_t> ruleHandId_;
};

void test()
{
  std::string s("ABCDEF XYZ #555: PQI #777");
  std::stringstream sb;
  sb.unsetf(std::ios::skipws);
  sb << s;
  const parseGrammar<sp::istream_iterator> p;
  sp::istream_iterator b(sb);
  sp::istream_iterator e;

  parseContextShPtr ctx(new parseContext);
  bool r = qi::parse(b, e, p(phx::ref(*ctx.get())));
  if (r) {
    std::cout << "Success" << std::endl;
  }
  else {
    std::cout << "Failure" << std::endl;
  }

  std::cout << std::string(b, e).substr(0, 32) << std::endl;
}

最佳答案

部分占位符无法绑定(bind)。

这可能是因为 ruleEncoding_ 没有公开属性(对于 _1)(不太可能)或者 ruleStart_ 没有继承属性(_r1)。

这就是我现在能告诉你的全部内容。

编辑 是后者。 ruleHandHeader 没有声明任何属性,更不用说要绑定(bind)到 _r1

的继承属性了

更新评论。

这里有一些建议。与我经常重复的避免语义操作的建议 (Boost Spirit: "Semantic actions are evil"?) 的脉络一样,我将结构调整为融合序列:

并使用更简单的语法规则:

    ruleStart_      = ruleEncoding_ >> ruleHandHeader_;
    ruleEncoding_   = "ABC";
    ruleHandId_     = qi::long_long;
    ruleHandHeader_ = "DEF XYZ #" >> ruleHandId_;

现在,在规则定义中添加 BOOST_SPIRIT_DEBUG 宏并将 uint64_t 固定为 uint64_t():

Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/shared_ptr.hpp>
#include <sstream>

namespace qi = boost::spirit::qi;

struct parseContext {
    std::string encoding_;
    uint64_t    handId_;
};

BOOST_FUSION_ADAPT_STRUCT(parseContext, (std::string, encoding_)(uint64_t,handId_))

typedef boost::shared_ptr<parseContext> parseContextShPtr;

template <typename Iterator>
struct parseGrammar : qi::grammar<Iterator, parseContext()> {
    parseGrammar() : parseGrammar::base_type(ruleStart_)
    {
        ruleStart_      = ruleEncoding_ >> ruleHandHeader_;
        ruleEncoding_   = "ABC";
        ruleHandId_     = qi::long_long;
        ruleHandHeader_ = "DEF XYZ #" >> ruleHandId_;

        BOOST_SPIRIT_DEBUG_NODES((ruleStart_)(ruleEncoding_)(ruleHandId_)(ruleHandHeader_))
    }

    // Rules
    qi::rule<Iterator, parseContext()> ruleStart_;
    qi::rule<Iterator, std::string()> ruleEncoding_;
    qi::rule<Iterator, uint64_t()> ruleHandId_, ruleHandHeader_;
};

void test()
{
    std::stringstream sb("ABCDEF XYZ #555: PQI #777");
    sb.unsetf(std::ios::skipws);

    typedef boost::spirit::istream_iterator It;
    const parseGrammar<It> p;
    It b(sb), e;

    parseContextShPtr ctx(new parseContext);
    bool r = qi::parse(b, e, p, *ctx);
    if (r) {
        std::cout << "Success: " << ctx->encoding_ << ", " << ctx->handId_ << std::endl;
    }

    else {
        std::cout << "Failure" << std::endl;
    }

    if (b!=e)
        std::cout << "Remaining: '" << std::string(b, e).substr(0, 32) << "'...\n";
}

int main()
{
    test();
}

打印

Success: ABC, 555
Remaining: ': PQI #777'...

关于c++ - 将规则生成绑定(bind)到我的结构成员时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003803/

相关文章:

c++ - 使用 boost::spirit 解析为复杂结构

c++ - 使用 Boost Spirit 解析带有数组的结构

c++ - 灵气 : Completely ignoring output of some rules

c++ - 如何概括作用于不同类型 vector 的函数?

c++ - 分析一个 dll 插件

c++ - 使用 `alias()` 解析精神规则时发生访问冲突

c++ - boost::spirit::qi 对输出进行重复解析

c++ - qi::rule 以继承属性作为继承属性

C++:如何设置超时(不读取输入,不线程化)?

c++ - 清除具有已删除赋值运算符的成员的类实例