Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 Intermedi ate code generator Syntax tree 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 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

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,T 1.type)} –T  ^T 1 {T.type=pointer(T 1.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  E 1 mod E 2 –{E.type= if (E 1.type==integer) && (E 1.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 1 [E 2 ] –{E.type= if (E 2.type==integer) && (E 1.type==array(s,t) t else type_error} –E  E 1 ^ –{E.type= if (E 1.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 S 1 –{S.type= if (E.type==boolean) S 1.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 S 1 –{S.type= if (E.type==boolean) S 1.type else type_error} –S  S 1 ;S 2 –{S.type= if (S 1.type==void) && (S 2.type== void) void else type_error}

12 Chapter 6 Type Checking Section 2 Specification of a Simple Type Checker 4. Type Checking of Functions –T  T 1 ‘  ’ T 2 {T.type= T 1.type  T 2. type} –E  E 1 (E 2 ) –{E.type= if (E 2.type==s) &&(E 1.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)


Download ppt "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."

Similar presentations


Ads by Google