Chapter 5:Semantic Analysis

Cycles in representation of types

. Data structures like linked lists are defined recursively

. Implemented through structures which contain pointers to structures

. Consider following code

type link = ^ cell;

cell = record

info : integer;

next : link

end;

. The type name cell is defined in terms of link and link is defined in terms of cell (recursive definitions)

. Recursively defined type names can be substituted by definitions

. However, it introduces cycles into the type graph

. C uses structural equivalence for all types except records

. It uses the acyclic structure of the type graph

. Type names must be declared before they are used

- However, allow pointers to undeclared record types

- All potential cycles are due to pointers to records

. Name of a record is part of its type

- Testing for structural equivalence stops when a record constructor is reached

As we had seen earlier the definition of a structural type is recursive in nature, so it is likely that there is a cycle in the definition of a type as in the first example. This is generally the case with recursive data structures like Trees, Linked Lists etc.

Substituting the definitions in the type diagram will lead to cycles as seen in second example which is undesirable. Languages like C avoid cycles by using Structural equivalence for all types except records thereby using the acyclic representation. Records are same if they are name equivalent. Thus the row records,

struct t_rupee{

int val;

};

struct t_dollar{

int val;

};

though structurally same will be of different types in C as records are checked by name equivalence and these two are of different names. Thus the following code fragment is not consistent according to the grammar.

/*

*A code fragment demonstrating the name equivalence in C.

*/

struct t_dollar dol;

struct t_rupeee rs;

dol.val = 10;

rs = dol;// error "incompatible types"