qi::rule不解析输入字符串

发布于 2024-10-24 16:45:18 字数 1761 浏览 0 评论 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”。控制和限制是符号表。

我做错了什么?

upd

定义了 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>

Backtrace

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

I have a strange trouble:

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];

Input is "WELL name3 PROD OIL \n". control and limit are symbol tables.

What I'm doing wrong?

upd:

With BOOST_SPIRIT_QI_DEBUG defined and with BOOST_SPIRIT_DEBUG_NODE (param) before expression I got following output if str used:

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

In +alnum case I got:

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

Backtrace:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-10-31 16:45:18

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

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

The problem is that you defined the rule str on the stack (as a local variable inside the constructor). This variable goes out of scope when the constructor of your grammar is exited, leaving the param rule with a dangling reference to it. If you move str to be a member variable of the grammar everything works as expected.

Moreover, you seem to want to skip spaces in between your input elements. I'd suggest to look at the phrase_parse API and at how to use skippers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文