何时在 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 可以达到这个目的,但我不确定了..
最佳答案
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/