问题
I have the following grammar for a comma-separated list with at least one item:
column_expression_list:
column_expression {
$$ = LinkedList_New();
LinkedListItem *item = LinkedListItem_New($1);
LinkedList_add($$, item);
}
|
column_expression_list T_COMMA column_expression {
LinkedListItem *item = LinkedListItem_New($3);
LinkedList_add($1, item);
}
;
But consider this:
column_expression error
The $$ = LinkedList_New();
will leak. Is there a way I can set a destructor function for when this is popped of the stack?
回答1:
Supposing you destroy a list with all items in it using a function called "LinkedList_Del", use Bison's %destructor directive which is made especially for cleaning up allocated elements that end up not used because of error:
%destructor { LinkedList_Del($$); } column_expression
Good luck!
Reference: http://www.gnu.org/software/bison/manual/bison.html#Destructor-Decl
来源:https://stackoverflow.com/questions/14730804/flex-bison-error-recovery-destructors