The type of each identifier must be declared
before the identifier is used. The type checker is a translation scheme that
synthesizes the type of each expression from the type of its sub-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
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-->*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) if (E_2. Type ==integer)
integer; else type-error;
E--> E_1[E_2] { E.type=if
((E_2.type==integer)&& (E_1.type==array(s,t)) t; else type-error;}
E--> *E_1 { E.type = if (E_1.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 S { if (E.type==boolean)
S_1.type; else type-error;}
S--> While E do S { if (E.type==boolean)
S_1.type; else type-error;
S--> S; S; { if (S_1.type==void) if
(S_2.type ==void) void; else type-error;}
Type
Checking of Functions
E--> E(E)
T--> T ‘->’ T { T.type = T1.type ->
T2.type}
E--> E(E) {E.type = I f ((E_2.type ==s)
&& (E_1.type == s--> t)) t; else type-error;
No comments:
Post a Comment