Flex / Bison: error recovery destructors?

I have the following grammar for a comma separated list with at least one element:

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 the following:

column_expression error

      

$$ = LinkedList_New();

will leak. Is there a way I can set the destructor function when this pops up from the stack?

+3


source to share


1 answer


If you destroy a list with all the items in it using the "LinkedList_Del" function, use the Bison% destructor directive, which is specifically designed to clean up selected items that are ultimately unused due to an error:

%destructor { LinkedList_Del($$); } column_expression

      



Good luck!

Link: http://www.gnu.org/software/bison/manual/bison.html#Destructor-Decl

+1


source







All Articles