When is% destructor used in bison?

When is% destructor used in bison? I have the following bison code:

%union{ char * sval; Variable * vval; } %token VARIABLE %token Literal %type <vval> Expression VARIABLE %type <sval> Literal %destructor { delete $$; } <vval> %destructor { delete $$; } Literal 

where Variable is the class. I thought that after processing the string, all Variable objects would be freed, but I do not see any destructor called. And this will directly lead to a memory leak ...

Edit: be clear; I allocate a new Variable object for the new token, and this token is pushed onto the BISON stack. I want to delete a variable when it is knocked out by a bison and removed from the stack. I thought that% destructor serves this purpose, but I'm not sure anymore.

+5
source share
2 answers

I realized that I have to release () it after the action, for example,

 ... | String CONCAT String { $$ = concat($1,$3); free($1); free($3); } ... 

It helps me.

+3
source

From the Bison manual:

Discarded Characters:

  • folded characters that appear during the first phase of error recovery,
  • incoming terminals during the second phase of error recovery,
  • the current view and the entire stack (except the current right partial characters), when the parser returns immediately, and
  • start symbol when the parser completes successfully.

So, if you don't push the error, %destructor will be called YYABORT stack if you return immediately ( call YYABORT or YYACCEPT ), or will be called on the start character if the parsing succeeds.

+6
source

Source: https://habr.com/ru/post/989908/


All Articles