Search code examples
clanguage-designbisonparser-generator

heap handling in a bison push pure parser


Is there any way to specify my own allocator/deallocator functions for heap management instead of malloc()/free() for a pure push parser in bison?


Solution

  • Most of Bison's memory allocations can be redirected with macros - in the prologue (between %{ and %}) you can write

    #define YYMALLOC mymalloc
    #define YYFREE myfree
    

    and Bison will then call mymalloc and myfree instead of malloc and free. However, it expects whatever functions you provide to have exactly the same type signature as the standard malloc and free; there is no way to get it to pass extra/different arguments. And I wouldn't use function-like macros if I were you. Worse, in my copy (Bison 2.4.1) yypstate_new calls malloc directly, with no override possible -- this is arguably a bug.