【问题标题】:qi::rule<It, std::string ()> doesn't parse input stringqi::rule<It, std::string ()> 不解析输入字符串
【发布时间】:2011-03-18 11:29:34
【问题描述】:

我有一个奇怪的麻烦:

qi::rule <Iterator, std::string ()> str = +alnum;
// will not parse given input
//param = "WELL" >> space >> str >> ((space >> no_case[control]) || (space >> no_case[limit])) >> space >> qi::eol; 

// will parse given input ok
param = "WELL" >> space >> +alnum >> ((space >> no_case[control]) || (space >> no_case[limit])) >> space >> qi::eol; 

expression = qi::no_skip[param];

输入为“WELL name3 PROD OIL \n”。 control 和 limit 是符号表。

我做错了什么?

更新

定义了 BOOST_SPIRIT_QI_DEBUG 并在表达式之前使用 BOOST_SPIRIT_DEBUG_NODE (param) 如果使用 str,我得到以下输出:

<param>
  <try>WELL name3 PROD OIL </try>
Segmentation fault

在 +alnum 的情况下,我得到了:

<param>
  <try>WELL name3 PROD OIL </try>
  <success></success>
  <attributes>[]</attributes>
</param>

回溯

<param>
  <try>WELL name3 PROD OIL </try>

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff64db290 in boost::function4<bool, char*&, char* const&, boost::spirit::context<boost::fusion::cons<std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::fusion::nil>, boost::fusion::vector0<void> >&, boost::spirit::unused_type const&>::operator() (this=
    0x7fffffffd700, a0=@0x7fffffffd2d0, a1=@0x7fffffffda20, a2=..., a3=...)
    at /opt/libs/boost/boost/function/function_template.hpp:1013
1013                 (this->functor BOOST_FUNCTION_COMMA BOOST_FUNCTION_ARGS);

【问题讨论】:

  • 看这里发生了什么有点困难——你能提供更多代码吗——最好是一个小的可编译测试用例?
  • 是的,请提供一段我们可以尝试的最小但完整的代码。比如param是怎么定义的?
  • 抱歉延迟回答。示例代码 - gist.github.com/877743.
  • @hkaiser, param 定义为 qi::rule param;

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


【解决方案1】:

问题是您在堆栈上定义了规则str(作为构造函数内的局部变量)。当退出语法的构造函数时,此变量超出范围,从而使 param 规则带有对它的悬空引用。如果您将str 移动为语法的成员变量,一切都会按预期工作。

此外,您似乎想跳过输入元素之间的空格。我建议查看phrase_parse API 以及如何使用skippers。

【讨论】:

  • 哦,我的错!它有效,谢谢!现在我尝试使用 lex 来标记输入:)
  • hkaiser 射门,他进球了! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多