qi::rule不解析输入字符串
我有一个奇怪的麻烦:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在堆栈上定义了规则
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 theparam
rule with a dangling reference to it. If you movestr
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.