0

我有一个奇怪的麻烦:

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);
4

1 回答 1

2

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

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

于 2011-03-20T00:01:05.163 回答