6 Type Checking - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

6 Type Checking

Description:

Title: CHAPTER 3 LEXICAL ANALYSIS Last modified by: zzz Created Date: 10/3/2004 2:56:43 PM Document presentation format: Company – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 24
Provided by: educ5312
Category:

less

Transcript and Presenter's Notes

Title: 6 Type Checking


1
6 Type Checking
  • Zhang Zhizheng
  • seu_zzz_at_seu.edu.cn

2
6. 0 Overview
  • 1.Static Checking
  • Check that the source program follows both the
    syntactic and semantic conventions of the source
    language

3
  • Examples of static check
  • Type checks (incompatible operand)
  • Flow-of-control checks (break statement)
  • Uniqueness checks (uniquely declared identifier)
  • Name-related checks

4
  • 2.Position of type checker

Intermediate code generator
Type checker
Syntax tree
Syntax tree
Token stream
Intermediate representation
parser
5
(No Transcript)
6
  • Notes
  • 1)A type checker verifies that the type of a
    construct matches that expected by its context.

7
  • 2)Type information gathered by a type checker may
    be needed when code is generated.
  • For example, arithmetic operators like usually
    apply to either integers or reals, perhaps to
    other types, and we have to look at the context
    of to determine the sense that is intended.

8
  • 3)A symbol that can represent different
    operations in different context is said to be
    overloaded, overloading may be accompanied by
    coercion of types, where a compiler supplies an
    operator to convert an operand into the type
    expected by the context.

9
  • 4) Polymorphism function

10
6. 1 Type Systems
  • The design of a type checker for a language is
    based on information about the syntactic
    constructs in the language, the notion of types,
    and the rules for assigning types to language
    constructs.
  • For example,

11
  • if both operands of the arithmetic of addition,
    subtraction, and multiplication are of type
    integer, then the result is of type integer
  • The result of the unary operator is a pointer
    to the object referred to by the operand, if the
    type of the operand is ??, then the type of the
    result is point to ??

12
  • 1. Type Expressions
  • Denoting 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.

13
  • Notes A convenient way to represent a type
    expression is to use a graph

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

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

16
  • 4. Error Recovery

17
6. 2 Specification of a Simple Type Checker
  • 1. A simple language
  • 1)Grammar for source language
  • P?DE
  • D?DD idT
  • T ?char integer array num of T T
  • E ?literal num id E mod E EE E

18
  • 2)The part of a translation scheme that saves the
    type of an identifier.
  • P?DE
  • D?DD
  • D? idT addtype(id.entry, T.type)
  • T ?char T.typechar
  • T ? integer T.typeinteger
  • T ? array num of T T.typearray(1..num,T1.typ
    e)
  • T ? T1 T.typepointer(T1.type)

19
  • 2. Type Checking of Expressions
  • E ?literal E.typechar
  • E ?num E.typeinteger
  • E ?id E.typelookup(id.entry)
  • E ?E1 mod E2 E.type if (E1.typeinteger)(E1.
    typeinteger) integer else type_error

20
  • E ?E1 E2 E.type if (E2.typeinteger)
    (E1.typearray(s,t) t else type_error
  • E ?E E.type if (E1.typepointer(t)) t else
    type_error

21
  • 3. Type Checking of Statements
  • S ?idE S.type if (id.typeE.type) void else
    type_error
  • S ?if E then S1 S.type if (E.typeboolean)
    S1.type else type_error

22
  • S ?while E do S1 S.type if (E.typeboolean)
    S1.type else type_error
  • S ? S1S2 S.type if (S1.typevoid)
    (S2.type void) void else type_error

23
  • 4. Type Checking of Functions
  • E ?E(E)
  • T ? T1 ? T2 T.type T1 .type? T2 .type
  • E ?E1 (E2) E.type if (E2.types)
    (E1.types?t) t else type_error
Write a Comment
User Comments (0)
About PowerShow.com