c++ - 在 BISON 中什么时候调用 %destructor?

标签 c++ bison

何时在 BISON 中调用 %destructor?我有以下 Bison 代码:

%union{
    char * sval; 
    Variable * vval; 
} 

%token VARIABLE 
%token Literal 
%type <vval> Expression VARIABLE 
%type <sval> Literal 

%destructor { delete $$; } <vval> 
%destructor { delete $$; } Literal 

Variable 是一个类。我以为在处理一行之后,所有 Variable 对象都将被释放,但我看不到任何析构函数被调用。这将直接导致内存泄漏...

编辑:要清楚;我为新 token 分配了一个新的 Variable 对象,并将此 token 推送到 BISON 堆栈。当 Variable 被 bison 弹出并从堆栈中丢弃时,我想删除它。我认为 %destructor 可以达到这个目的,但我不确定了..

最佳答案

From the Bison Manual:

Discarded symbols are the following:

  • stacked symbols popped during the first phase of error recovery,
  • incoming terminals during the second phase of error recovery,
  • the current lookahead and the entire stack (except the current right-hand side symbols) when the parser returns immediately, and
  • the start symbol, when the parser succeeds.

因此,如果您没有遇到错误,如果您立即返回 (call YYABORT or YYACCEPT),%destructor 将在堆栈上调用,或者如果解析成功,它将在开始符号上调用它.

关于c++ - 在 BISON 中什么时候调用 %destructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401286/

相关文章:

macos - 架构 x86_64 :"_yylval"的 undefined symbol ,引用自 Mac OS 上的 _yylex

c++ - 如何将 native (NT) 路径名转换为 Win32 路径名?

c++ - 如何排除部分输入被解析?

c++ - Xcode 中的 Flex、Bison、C++

c++ - valgrind:无效写入

c - 如何从 lex/yacc 生成不同的 yyparse 函数以用于同一程序?

c - 来自 flex 的 Bison 打印变量错误

c++ - <mutex> 和 <condition_variable> 的异常处理

c++ - 动态捕获 CPU 和内存使用情况

c++ - 如何在 Qt Creator 项目向导中添加自定义构建步骤?