Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Type Checking Section 0 Overview

Similar presentations


Presentation on theme: "Chapter 6 Type Checking Section 0 Overview"— Presentation transcript:

1 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

2 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” parser Token stream Type checker Syntax tree Intermediate code generator Intermediate representation

3 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

4 Chapter 6 Type Checking Section 1 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

5 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

6 Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker
1. A simple language 1)Grammar for source language PD;E DD;D | id:T T char | integer | array [num] of T | ^T E literal | num | id | E mod E | E[E] | E^

7 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. PD;E DD;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  ^T {T.type=pointer(T1.type)}

8 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}

9 Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker
2. Type Checking of Expressions E E[E] {E.type= if (E2.type==integer) && (E1.type==array(s,t) t else type_error} E E^ {E.type= if (E1.type==pointer(t)) t else type_error}

10 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}

11 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}

12 Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker
4. Type Checking of Functions E E(E) T  T1 ‘’ T2 {T.type= T1 .type T2 .type} E E1 (E2) {E.type= if (E2.type==s) &&(E1.type==st) t else type_error}

13 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

14 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;

15 Chapter 6 Type Checking Section 4 Type conversion
Coercions Implicit type conversions Explicit conversions Chr(55)

16 Chapter 6 Type Checking Section 5 Overloading of functions and operators
1.Overloaded symbol A symbol that has different meanings depending on its context 2.Set of possible types for a sub-expression 3.Narrowing the set of possible types

17 Chapter 6 Type Checking Section 6 Polymorphic functions
1.Why polymorphic functions? 2.Type variables

18 Chapter 6 Type Checking Section 6 Polymorphic functions
template <class Type> class Queue { public: queue (int=10); Type EnQueue(const Type &item); } template <class Type> void Queue <Type> :: EnQueue(const Type &item) {}


Download ppt "Chapter 6 Type Checking Section 0 Overview"

Similar presentations


Ads by Google