Documente Academic
Documente Profesional
Documente Cultură
• Semantic Checks
Static – done during compilation
Dynamic – done during run-time
2
Process of designing a type checker
3
Static type checking
4
Type Checking
5
Dynamic type checking
6
Type Systems
• Collection of rules for assigning type expressions.
• A sound type system eliminates run-time type checking for
type errors.
7
Uses of type checking
8
• Polymorphism types: Some languages allow a function to be
poly-morphic, that is, to be defined over a large class of similar
types, e.g. over all arrays no matter what the types of the
elements are.
9
Type Expression
• The type of a language construct is denoted by a type
expression.
• A type expression can be:
– A basic type (also called primitive types)
• a primitive data type such as integer, real, char, boolean, …
– A type name
• a name can be used to denote a type expression.
10
Type constructors
11
(c) Record: Similar to Product but with names for different fields (used to
access the components of a record). Example of a C record type: struct
{ double r;
int i;
}
(d) Pointer: If T is a type expression, then pointer(T ) is the
type expression “pointer to an object of type T ”;
(e) Function: If D is the domain and R the range of the function then we
denote its type by the type expression: D : R.
The mod operator has type, int × int : int. The Pascal function:
function f(a, b: char): int
has type:
char × char : int
12
A Simple Type Checking System
Program → Declaration;
Statement →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)
13
Type Checking of Expressions
E → id { E.type:=lookup(id.entry) }
E → literal { E.type:=char }
E → int { E.type:=int }
E → real { E.type:=real }
14
Type Checking of Statements
15