1 / 15

Chapter 6 Type Checking Section 0 Overview

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)

rimona
Télécharger la présentation

Chapter 6 Type Checking Section 0 Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 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”

  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,T1.type)} • T  ^T1 {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 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}

  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 • 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)

More Related