Compiler Design - CSE 504: Type Checking
Compiler Design - CSE 504: Type Checking
Type Checking
Static Checking
Token Stream
Parser
Static Checker
Intermediate Code
Type checks: operator applied to incompatible operands? Flow of control checks: break (outside while?) Uniqueness checks: labels in case statements Name related checks: same name?
Type Checking
Problem: Verify that a type of a construct matches that expected by its context. Examples: mod requires integer operands (PASCAL) * (dereferencing) applied to a pointer a[i] indexing applied to an array f(a1, a2, , an) function applied to correct arguments.
Type Systems
A collection of rules for assigning type expressions to the various parts of a program. Based on: Syntactic constructs, notion of a type. Example: If both operators of +, -, * are of type integer then so is the result. Type Checker: An implementation of a type system.
Syntax Directed.
Sound Type System: eliminates the need for checking type errors during run time.
Type Expressions
Implicit Assumptions:
Expressions Statements
Type Constructors
Arrays Records Sets Pointers Functions
Boolean
Real Enumerations Void Variables
Character
Integer Sub-ranges Error Names
cell = record
x
x char char
pointer integer
x char
pointer integer
x ptr
Tree
DAG
Structured Types
{E.type = pointer(E1.tye)}
{E.type = if (E1.type = arrow(S, T) & E2.type = S, then T else err}
S -> while E do S1
S -> S1; S2