150 likes | 268 Views
Chapter 6 Type Checking Section 0 Overview. 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)
E N D
Chapter 6 Type Checking Section 0 Overview 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
Intermediate code generator Type checker Syntax tree Syntax tree Token stream Intermediate representation parser Chapter 6 Type Checking Section 0 overview 2.Position of type checker 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 “overloaded”
Chapter 6 Type Checking Section 1 Type Systems 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 expression is to use a graph
Chapter 6 Type Checking Section 1 Type Systems 2. Type Systems • A type system is a collection of rules for assigning type expressions to the various parts of a program. Note: 1)A type checker implements a type system 2)The type systems are specified in a syntax-directed manner 3)Different type systems may be used by different compilers or processors of the same language
Chapter 6 Type Checking Section 1 Type Systems 3. Static and Dynamic Checking of Types • Static Checking (Done by a compiler) • Dynamic Checking (Checking after run) • Strongly typed language (if its compiler can guarantee that the programs it accepts will execute without type error) 4. Error Recovery
Chapter 6 Type Checking Section 2 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^
Chapter 6 Type Checking Section 2 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 T {T.type=array(1..num,T1.type)} • T ^T1 {T.type=pointer(T1.type)}
Chapter 6 Type Checking Section 2 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}
Chapter 6 Type Checking Section 2 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} • E E1^ • {E.type= if (E1.type==pointer(t)) t else type_error}
Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 3. Type Checking of Statements • S id:=E • {S.type= if (id.type==E.type) void else type_error} • S if E then S1 • {S.type= if (E.type==boolean) S1.type else type_error}
Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 3. Type Checking of Statements • S while E do S1 • {S.type= if (E.type==boolean) S1.type else type_error} • S S1;S2 • {S.type= if (S1.type==void) && (S2.type== void) void else type_error}
Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 4. Type Checking of Functions • T T1 ‘’ T2 {T.type= T1 .type T2 .type} • E E1 (E2) • {E.type= if (E2.type==s) &&(E1.type==st) t else type_error}
Chapter 6 Type Checking Section 3 Equivalence of type expressions 1.Structural Equivalence of type expression • The same basic type • Formed by applying the same constructor to structurally equivalent types
Chapter 6 Type Checking Section 3 Equivalence of type expressions 2.Names for type expression type link=^cell 3.Cycles in representations of types type link=^cell; cell=record info:integer; next :link end;
Chapter 6 Type Checking Section 4 Type conversion • Coercions • Implicit type conversions • Explicit conversions • Chr(55)