GNU Bison 生成的解析器在给定非空文件时抛出分段错误 11

Parser generated by GNU Bison throws a segmentation fault 11 when given a non-empty file

每当我使用有效文件调用 yyparse() 时,我都会收到一个段错误 似乎 是由这行代码(大约第 1789 行)引起的:

if (yyss + yystacksize - 1 <= yyssp){

我是通过在这行代码前后打印调试信息得出这个结论的。此行之前的消息已打印,但此行之后的消息未打印。

奇怪的是,如果我用一个空文件调用 yyparse(),不会抛出错误,但如果文件中至少有一个字符,则会抛出错误。

解析器本身已编译,没有任何错误。此段错误背后的 reason/s 可能是什么?

解析文件:https://gist.github.com/SamTebbs33/bffb72517f174af679ef

调试消息代码:

cout << "before if" << endl;
if (yyss + yystacksize - 1 <= yyssp){
    cout << "after if" << endl;
    cout.flush();

第一条调试消息在抛出错误之前打印了 3 次。

编辑: 当 55 标记在 yyreduce 标签中匹配时,switch 语句中实际上会抛出错误:

case 55:
   #line 219 "grammar/grammar.y" /* yacc.c:1661  */
   {
    cout << "processing token 55" << endl;
    (yyval.id) = new TIdentifier(*(yyvsp[0].string));
    cout << "processed token 55" << endl;
  }
  #line 2228 "grammar/parser.cpp" /* yacc.c:1661  */
  break;

在到达switch语句之前,我打印了被切换变量的整数值,它的值为55,所以错误代码应该在上面的代码中,因为没有打印"processed token 55"而是"processing token 55" 被打印出来。下面是 TIdentifier 构造函数的代码:

TIdentifier(std::string name) : name(name) {
}

这意味着解引用时一定会产生错误(yyvsp[0].string)

(Answered by the OP in a question edit. Converted to a community wiki answer, which is more appropriate to the Q&A format of Whosebug).

OP 写道:

After further debugging, I have realised that my flex grammar wasn't saving the strings found in the file as it should and was attempting to access a non-existing element in yylval and the parser now works!

但是,正如@rici 指出的那样,将正确的 material 包含在问题中会更好:

It would be more useful (or at least easier) to see your bison input file (.y), rather than the produced parser.