琐碎的精神解析器语法的分段错误
Segmentation fault with trivial Spirit Parser grammar
我的灵气解析器经常遇到段错误。
在花了几天时间调试问题(我发现堆栈跟踪无法摸索)后,我决定将其缩减为一个最小的示例。谁能说出我做错了什么,如果有的话?
将代码保存为 bug.cpp,使用 g++ -Wall -o bug bug.cpp
编译,您应该很高兴。
//#define BOOST_SPIRIT_DEBUG_PRINT_SOME 80
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/version.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
namespace /*anon*/
{
using namespace boost::spirit::qi;
template <typename Iterator, typename
Skipper> struct bug_demo :
public grammar<Iterator, Skipper>
{
bug_demo() :
grammar<Iterator, Skipper>(story, "bug"),
story(the),
the("the")
{
// BOOST_SPIRIT_DEBUG_NODE(story);
// BOOST_SPIRIT_DEBUG_NODE(the);
}
rule<Iterator, Skipper> story, the;
};
template <typename It>
bool do_parse(It begin, It end)
{
bug_demo<It, space_type> grammar;
return phrase_parse(begin, end, grammar, space);
}
}
int main()
{
std::cout << "Spirit version: " << std::hex << SPIRIT_VERSION << std::endl;
try
{
std::string contents = "the lazy cow";
if (do_parse(contents.begin(), contents.end()))
return 0;
} catch (std::exception e)
{
std::cerr << "exception: " << e.what() << std::endl;
}
return 255;
}
我已经测试过了
- g++ 4.4
- 、4.5、4.6 和
- Boost 版本 1.42 (Ubuntu Meerkat) 和 1.46.1.1 (natty)
输出为
sehe@meerkat:/tmp$ ./bug
Spirit version: 2020
Segmentation fault
或者,在 boost 1.46.1 中,它将报告Spirit version: 2042
按照答案中的建议更改初始化顺序只会隐藏问题。实际的问题是,rule<>
有适当的C++复制语义。您可以通过将语法初始化重写为:
bug_demo() :
grammar<Iterator, Skipper>(story, "bug"),
story(the.alias()),
the("the")
{}
有关基本原理和更详细的解释,请参阅此处。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 分段故障(堆芯转储)矢量
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- 数组的指针从不分段故障
- QMetaObject invokeMethod的基于函数指针的语法
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 放置/分段语法是否可能出现懒惰"operator or"重载?
- 琐碎的精神解析器语法的分段错误