示例代码有一个非常小的规则来解析一个字符串——竖线('|')分隔的字符串。字符串不能包含字符('|'、'!'、'('、')'、'*')。
当我在没有 -O3 标志的情况下编译程序( ex.cpp )时,它工作正常。 g++的版本是5.2
g++ -std=c++14 -I ${INC} -o ex.out ex.cpp
Note INC 指向 boost/1.77.0/include
./ex.out ABC
-----------------------------------------
processing string: "ABC"
<str_rule>
<try>ABC</try>
<success></success>
<attributes>[]</attributes>
</str_rule>
ABC: parse succeeded:
However, when I compile it using the -O3 flags it crashes ( put -g so I can run gdb )
g++ -std=c++14 -I ${INC} -O3 -g -o ex.out ex.cpp
./ex.out ABC
-----------------------------------------
processing string: "ABC"
Segmentation fault (core dumped)
// 下面的 ex.cpp
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/home/qi/char/char.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, Skipper> {
parser() : parser::base_type(str_rule) {
using namespace qi;
auto const str = *(char_ - '|' - '!' - '(' - ')' - '*') ;
str_rule = (str % char_('|'));
BOOST_SPIRIT_DEBUG_NODE( str_rule );
}
private:
qi::rule<It, std::string(), Skipper> str;
qi::rule<It, Skipper> str_rule;
};
int main(int argc, const char*argv[] )
{
if( argc != 2 ) {
std::cerr << "Usage: main.out <str rule>\n";
return -1;
}
std::string r = argv[1];
std::cout << "-----------------------------------------" << std::endl;
std::cout << "processing string: " << "\"" << r << "\"" << std::endl;
typedef std::string::const_iterator It;
It f(r.begin()), l(r.end());
parser<It> p;
try {
bool ok = qi::phrase_parse(f,l,p ,qi::space);
if (ok && f == l ) {
std::cout << r << ": parse succeeded: \n";
} else {
std::cout << r << " failed to parse \n";
}
} catch (const qi::expectation_failure<It>& e) {
std::cerr << "expectation_failure at '" << std::string(e.first, e.last) << "'\n";
}
return 0;
}
代码也应该与 -O3 优化一起工作。
我尝试使用 -O2 和 -O1,但它们都崩溃了。
尝试使用 g++ 12.2 版,它仍然崩溃。
尝试使用 boost 版本 1.53,但仍然崩溃。
gdb 回溯如下 - 我不太明白。
#0 0x0000000000401f29 在 function_base (this=0x7ffda94834c8) at /data/tools/boost/1.77.0/include/boost/function/function_base.hpp:603
603 function_base():vtable(0){}
缺少单独的 debuginfos,使用:debuginfo-install glibc-2.17-292.el7.x86_64
(gdb) 在哪里
#0 0x0000000000401f29 in function_base (this=0x7ffda94834c8) 在/data/tools/boost/1.77.0/include/boost/function/function_base.hpp:603
#1 function4 (this=0x7ffda94834c8) 在/data/tools/boost/1.77.0/include/boost/function/function_template.hpp:706
#2 函数 (this=0x7ffda94834c8) 在 /data/tools/boost/1.77.0/include/boost/function/function_template.hpp:1076
#3 规则 (name="", this=0x7ffda94834a0) 在 /data/tools/boost/1.77.0/include/boost/spirit/home/qi/nonterminal/rule.hpp:168
#4 解析器<__gnu_cxx::_normal_iterator
我做错了什么吗?