Type Checking
Type Checking
ERRORS
Compiler must check that the source program follows
both the syntactic and semantic conventions of the
source language.
end foo;
TYPE CHECKER
Errors, if an operator is applied to an incompatible operand
Which operation to use for overloaded names and
operators (polymorphism)
TYPE SYSTEM
The design of a type checker for a language is based on
the information about the syntactic constructs in the
language.
Each expression has a type associated with it.
P --> D; E
D--> D; D | id: T
T--> char | integer | array[num] of T | * T
E--> literal | num | id | E mod E | E[E] | E*
Base Types: char, integer, type-error
TRANSLATION SCHEME
P --> D; E
D--> D;D
D--> id :T { addtype(id.entry,T.type);}
T--> char {T.type= char;}
T--> integer {T.type=integer;}
T-->*T1 {T.type=pointer(T1.type);}
T--> array[num] of T1 { T.type =array(1..num.val,T1.type);
}
TYPE CHECKING OF EXPRESSIONS
E--> literal { E.type=char;}
E--> num { E.type =integer;}
E--> id { E.type =lookup(id.entry);}
E--> E1 mod E2 { E.type =If (E1.type ==integer) and
(E2. Type ==integer) integer; else
type-error;
E--> E1[E2] { E.type=if
((E2.type==integer)&&
(E1.type==array(s,t)) t; else type-
error;}
E--> *E1 { E.type = if (E1.type
==pointer(t)) t else type-error;
TYPE CHECKING FOR STATEMENTS
S--> id=E { if (id.type==E.type) void; else
type-error;}
S--> if E then S1 { if (E.type==boolean) S1.type;
else type-error;}
S--> While E do S1 { if (E.type==boolean) S1.type;
else type-error;
S--> S1; S2 { if (S1.type==void) if (S2.type
==void) void; else type-error;}
TYPE CHECKING OF FUNCTIONS
E--> E(E)