6.3 Type Equivalence

While type checking, we will have to check equivalence of types. For simple types, it is a matter of merely comparing type(np1) with type(np2), where np1 and np2 are pointers to nodes representing the two program components.

When we have complex data types, we need to ensure that the two type trees have exactly the same structure. We do this by a int tree_comp(Node *np1, Node *np2) function:

int tree_comp(Node *np1, Node *np2){
   if(type(np1) == type(np2)){
       if(type(np1) < TREE) return 1;
       retrun tree_comp_rec(ttree(np1), ttree(np2));
   }
   return 0;
}
int tree_comp_rec(Node *np1, Node *np2){
   return tree_comp(np1, np2);
}

Get Compilers: Principles and Practice now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.