Chapter 5:Semantic Analysis

Type checking for expressions

E num E.type = int
E num.num E.type = real
E id E.type = lookup( id.entry
E E 1 op E 2 E.type = if E 1 .type == int && E 2 .type == int
 
then int
 

elseif E 1 .type == int && E 2 .type == real

 
then real
 

elseif E 1 .type == real && E 2 .type == int

 
then real
 

elseif E 1 .type == real && E 2 .type== real

 
then real
   

The language has to perform implicit type conversions where ever it is possible. As a thumb rule compiler performs a type conversion if it doesn't lead to any loss of information. For example, int can be converted into real, single precision to a double precision etc. under this rule. Compilers also support explicit type conversions. This conversion translates into many cases in the type assignment of a Left Hand side of a expression. For example, x = y + z ; Then the type of x depends on the type of y, z as described above.