Lecture Slides on Type Checking - Compiler Design | CS 5300, Study notes of Computer Science

Material Type: Notes; Professor: Allan; Class: COMPILER DESIGN; Subject: Computer Science; University: Utah State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-h85
koofers-user-h85 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Type Checking
CS 5300 - SJAllan 2
Error Checking
๎šƒDynamic checking takes place while
program is running
๎šƒStatic checking takes place during
compilation
โ€“ Type checks
โ€“ Flow-of-control checks
โ€“ Uniqueness checks
โ€“ Name-related checks
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Lecture Slides on Type Checking - Compiler Design | CS 5300 and more Study notes Computer Science in PDF only on Docsity!

Type Checking

CS 5300 - SJAllan 2

Error Checking

ยƒ Dynamic checking takes place while

program is running

ยƒ Static checking takes place during

compilation

  • Type checks
  • Flow-of-control checks
  • Uniqueness checks
  • Name-related checks

CS 5300 - SJAllan 3

Types

ยƒ Every expression has an associated type

ยƒ Rules exist to assign a type to an

expression, e.g.:

  • If you add, subtract, or multiply two integer operands, the result is an integer
  • If you take the address of an operand of type T , the type of the result is pointer to T

ยƒ In Pascal and C, types are either basic or

constructed

CS 5300 - SJAllan 4

Type Checking

ยƒ Verifying that the type of a construct

matches that expect by its context

ยƒ Type information may be needed when code

is generated. Why?

ยƒ A symbol that can represent different

operations in different contexts is said to be

overloaded

ยƒ Overloading may be accompanied by

coercion of types

CS 5300 - SJAllan 7

Type Expressions

ยƒ Applying "type constructors" to type expressions forms new type expressions

  • Arrays
  • Records
  • Pointers

ยƒ A function maps elements from one set (domain) to another set (range)

  • mod has type " int x int ร† int "
  • function f(a, b: char) : โ†‘ integer; ( f has type " char x char ร† pointer(integer)" )

CS 5300 - SJAllan 8

Type Systems

ยƒ A type system is a collection of rules for

assigning type expressions

ยƒ A type checker implements a type system

ยƒ Different type systems may be used by

different compilers of the same language

ยƒ A type checker should adequately handle

error recovery

CS 5300 - SJAllan 9

Static vs. Dynamic Checks

ยƒ In principle, any check can be done dynamically if the target code carries type information

ยƒ A sound type system eliminates the need for dynamic checking

ยƒ A language is strongly typed of its compiler only accepts programs that have no type errors

ยƒ In practice, some checks can only be done dynamically (e.g., checking array indices)

CS 5300 - SJAllan 10

Simple Type Checker

ยƒ The simple language above allows a sequence of declarations followed by a single expression ยƒ A simple syntax-directed definition can specify a type checker for this 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 โ†‘

CS 5300 - SJAllan 13

Type Checking of Statements

S.type := if S 1 .type = void and S 2 .type = void then void else type_error

S ร† S 1 ; S 2

S.type := if E.type = boolean S ร† while E do S (^1) then S 1 .type else type_error

S.type := if E.type = boolean S ร† if E then S (^1) then S 1 .type else type_error

S.type := if id.type = E.type S ร† id := E then void else type_error

Production Statements

CS 5300 - SJAllan 14

Type Checking of Functions

E.type := if E 2 .type = s and E 1 .type = s ร† t then t else type_error

E ร† E 1 (E 2 )

id.type := arguments.type ร† standard_type.type

subprogam_head ร† function id arguments : standard_type ';'

Production Statements

CS 5300 - SJAllan 15

Type Systems

ยƒ A collection of rules for assigning type

expressions to the various parts of a

program

ยƒ A type checker implements a type system

CS 5300 - SJAllan 16

Type Checkers

ยƒ Static

  • Checking is done by the compiler ยƒ Dynamic
  • Checking is done at run-time ยƒ A sound type system eliminates the need for dynamic checking for type errors because it allows us to determine statically that these errors cannot occur at run-time ยƒ A langue is strongly typed if its compiler can guarantee that the programs it accepts will execute without errors

CS 5300 - SJAllan 19

Type Checking Scheme

T โ†’ array [ num ] of T 1 T.type = array( num .val, T 1 .type)

T โ†’ โ†‘T 1 T.type = pointer(T 1 .type)

T โ†’ integer T.type = integer

T โ†’ char T.type = char

D โ†’ id : T addtype( id .entry, T.type)

D โ†’ D ; D

P โ†’ D ; E

Production Semantic Rules

CS 5300 - SJAllan 20

Type System for Expressions

E.type = if E 1 .type = pointer(t) then t else type_error

E โ†’ E 1 โ†‘

E.type = if E 2 .type = integer and E 1 .type = array(s,t) then t else type_error

E โ†’ E 1 [E 2 ]

E.type = if E 1 .type = integer and E 2 .type = integer then integer else type_error

E โ†’ E 1 mod E 2

E โ†’ id E.type = lookup(id.entry)

E โ†’ num E.type = integer

E โ†’ literal E.type = char

Production Semantic Rules

CS 5300 - SJAllan 21

Type System for Statements

S.type = if S 1 .type = void and S 2 .type = void then void else type_error

S โ†’ S 1 ; S (^2)

S.type = if E.type = boolean then void else type_error

S โ†’ while E do S (^1)

S.type = if E.type = boolean then void else type_error

S โ†’ if E then S (^1)

S.type = if id.type = E.type then void else type_error

S โ†’ id = E

Production Semantic Rules

CS 5300 - SJAllan 22

Type System for Functions

E.type = if E 2 .type = s and E 1 .type = s โ†’ t then t else type_error

E โ†’ E 1 (E 2 )

T โ†’ T 1 โ€œโ†’ โ€œ T 2 T.type = T 1 .type โ†’ T 2 .type

Production Semantic Rules

CS 5300 - SJAllan 25

Coercion of Expressions

E.type = if E 1 .type = integer and E 2 .type = integer then integer else if E 1 .type = integer and E 2 .type = real then real else if E 1 .type = real and E 2. type = integer then real else if E 1 .type = real and E 2 .type = real then real else type_error

E โ†’ E 1 op E 2

E โ†’ id E.type = lookup( id .Entry)

E โ†’ num. num E.type = real

E โ†’ num E.type = integer

Production Semantic Rules