Flex/Bison: Error recovery destructors?

本小妞迷上赌 提交于 2019-12-04 04:03:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!