Type Checking
Type Checking
TYPE CHECKING
Type Checking
syntax Intermediat
token syntax Type intermediate
Parser e code
stream tree Checker tree representation
generator
Static checking
• Type checks
• Flow of control checks
• Uniqueness checks
• Name related checks
Outline
Type systems
Specification of simple type checker
Equivalence of type expressions
Type conversions
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.
Types are either basic or constructed. Basic types are atomic
with no internal structure as far as the program is constructed.
Pointers, arrays, functions, records can be treated as
constructor types.
Type expression
A type expression is either a basic type or is formed by applying
an operator called type constructor to other expressions.
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 schemes
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-->*T_1 {T.type=pointer(T_1.type);}
T--> array[num] of T_1 { T.type =array(1..num.val,T_1.type); }
Type Checking of Expressions
E--> literal { E.type=char}
E--> num { E.type =integer}
E--> id { E.type =lookup(id.entry)}
E--> E_1 mod E_2 { E.type =if (E_1.type ==integer) and (E_2. Type ==integer)
then integer; else type-error
E--> E_1[E_2] { E.type=if ((E_2.type==integer) and (E_1.type==array(s,t)) then
t; else type-error;}
E--> *E_1 { E.type = if (E_1.type ==pointer(t)) then t else type-error;
Type Checking for Statements
S--> id:=E { S.type:= if id.type == E.type then void else type-error}