BNFC / bnfc

BNF Converter
http://bnfc.digitalgrammars.com/
586 stars 165 forks source link

C backend lacks destructor for ASTs #348

Closed andreasabel closed 3 years ago

andreasabel commented 3 years ago

The C backend defines constructors, but no destructors for ASTs.

Too add a recursive deallocation procedure e.g. for grammar Calc.cf we need to:

Add to Absyn.h

void freeExp(Exp p);

Add to Absyn.c

void freeExp (Exp p)
{
  switch (p->kind)
  {
   case is_EAdd:
     freeExp(p->u.eadd_.exp_1);
     freeExp(p->u.eadd_.exp_2);
     break;

   case is_ESub:
     freeExp(p->u.esub_.exp_1);
     freeExp(p->u.esub_.exp_2);
     break;

   case is_EMul:
     freeExp(p->u.emul_.exp_1);
     freeExp(p->u.emul_.exp_2);
     break;

   case is_EDiv:
     freeExp(p->u.ediv_.exp_1);
     freeExp(p->u.ediv_.exp_2);
     break;

   case is_EInt:
     break;
  }
  free(p);
}

The code could be adapted from the printer generator.

andreasabel commented 3 years ago

Similarly, we want clone_XXX methods to recursively copy trees.