Professional Documents
Culture Documents
Unit 4 Type Checking
Unit 4 Type Checking
1.Static Checking
Check that the source program follows both
the syntactic and semantic conventions of the
source language
Examples of static check
Type checks (incompatible operand)
Flow-of-control checks (break statement)
Uniqueness checks (uniquely declared identifier)
Name-related checks
1
CHAPTER 6 TYPE CHECKING
Intermedi
Token Syntax Type Syntax
parser ate code Intermediate
stream tree tree
checker generator representation
Notes: 1)A type checker verifies that the type of
a construct matches that expected by its
context.
2)Type information gathered by a type
checker may be needed when code is generated.
3)A symbol that can represent different
operations in different context is said to be 2
“overloaded”
CHAPTER 6 TYPE CHECKING
1. Type Expressions
Denote the type of a language construct
1) A basic type is a type expression.
2) A type name is a type expression.
3) A type constructor applied to type
expression is a type expression. Constructors
include:
Arrays, products, records,pointers, functions
4) Type expressions may contain variables
whose values are type expressions.
Notes: A convenient way to represent a type 3
expression is to use a graph
CHAPTER 6 TYPE CHECKING
2. Type Systems
5
SPECIFICATION OF A SIMPLE TYPE CHECKER
1. A simple language
1)Grammar for source language
PD;E
DD;D | id:T
T char | integer | array [num] of T | T
E literal | num | id | E mod E | E[E] | E
6
SPECIFICATION OF A SIMPLE TYPE CHECKER
1. A simple language
2)The part of a translation scheme that saves the type of an
identifier.
PD;E
DD;D
D id:T {addtype(id.entry, T.type)}
T char {T.type=char}
T integer {T.type=integer}
T array [num] of T1 {T.type=array(1..num,T1.type)}
T T1 {T.type=pointer(T1.type)}
7
SPECIFICATION OF A SIMPLE TYPE CHECKER
2. Type Checking of Expressions
E literal {E.type=char}
E num {E.type=integer}
E id {E.type=lookup(id.entry)}
E E1 mod E2
{E.type= if (E1.type==integer) && (E1.type==integer)
integer
else
type_error}
8
SPECIFICATION OF A SIMPLE TYPE CHECKER
2. Type Checking of Expressions
E E1[E2] {E.type= if (E2.type==integer) &&
(E1.type==array(s,t) t else type_error}
9
SPECIFICATION OF A SIMPLE TYPE CHECKER
10
SPECIFICATION OF A SIMPLE TYPE CHECKER
11
PASCAL DECLARATIONS
type A = array [1..10] of integer;
type B = array [1..10] of integer;
var x,y: A;
var u,v: B;
var w: array of [1..10] of integer;
Functions: maps elements of one set (domain) to
another set
(range)
E.g., mod: int int int
E E1 (E2)
{E.type= if (E2.type==s) &&(E1.type==st)
t
else type_error}
13
TYPE EQUIVALENCE:1
15
TYPE EQUIVALENCE:3
There are two kinds of type equivalence
Name equivalence
Structural equivalence
STRUCTURAL EQUIVALENCE
says two types are the same if they are made up of
the same basic types and constructors.
(difficult to implement)
NAME EQUIVALENCE
says two types are the same if their constituents
have the SAME NAMES.
16
(used for almost all languages)
NAME EQUIVALENCE
18
EQUIVALENCE OF TYPE EXPRESSIONS
24
TYPE CONVERSION
Coercions
Implicit type conversions
Suppose we encounter an expression
26
TYPE CHECKING EXAMPLE WITH COERCION
Production Semantic Rule
E -> num E.type := integer
E -> num . num E.type := real
E -> id E.type := lookup( id.entry )
E -> E1 op E2 E.type := if E1.type == integer and E2.type == integer
then integer
else if E1.type == integer and E2.type == real
then real
else if E1.type == real and E2.type == integer
then real
else if E1.type == real and E2.type == real
then real
else type_error
27