Type Checking: By:Vishnu Kumar Gehlot
Type Checking: By:Vishnu Kumar Gehlot
Examples:
mod requires integer operands (PASCAL)
too.
A type system is a collection of rules for assigning
type expressions to the parts of a program.
Type Systems
A collection of rules for assigning type expressions to the
various parts of a program.
x pointer x pointer x x
struct cell {
Tree DAG int info;
struct cell * next;
(char x char)-> pointer (integer) };
A Simple Type Checking
System
P → D;E
D → D;D
D → id:T { addtype(id.entry,T.type) }
T → char { T.type=char }
T → int { T.type=int }
T → real { T.type=real }
T → ↑T1 { T.type=pointer(T1.type) }
T → array[intnum] of T1 {
T.type=array(1..intnum.val,T1.type) }
A Simple Typed Language
Program -> Declaration; Statement
Declaration -> Declaration; Declaration
| id: Type
Statement -> Statement; Statement
| id := Expression
| if Expression then Statement
| while Expression do Statement
Expression -> literal | num | id
| Expression mod Expression
| E[E] | E ↑ | E (E)
Type Checking of
Expressions
E id
→ { E.type=lookup(id.entry) }
E charliteral
→ { E.type=char }
E intliteral
→ { E.type=int }
E realliteral { E.type=real }
→
E E1 + E2
→ { if (E1.type=int and E2.type=int) then E.type=int
else if (E1.type=int and E2.type=real) then E.type=real
else if (E1.type=real and E2.type=int) then E.type=real
else if (E1.type=real and E2.type=real) then
E.type=real else E.type=type-error }
E → E1 [E2] { if (E2.type=int and E1.type=array(s,t)) then E.type=t
else E.type=type-error }
E → E1 ↑ { if (E1.type=pointer(t)) then E.type=t
else E.type=type-error }
Type Checking of
Statements
S id = E { if (id.type=E.type then
S.type=void
else S.type=type-error }