Semantic Analysis III Type Checking - Lecture Slides | EECS 483, Study notes of Electrical and Electronics Engineering

Material Type: Notes; Class: Compiler Constr; Subject: Electrical Engineering And Computer Science; University: University of Michigan - Ann Arbor; Term: Fall 2003;

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-ewq
koofers-user-ewq 🇺🇸

5

(1)

10 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Semantic Analysis III
Type Checking
EECS 483 – Lecture 11
University of Michigan
Wednesday, October 8, 2003
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Semantic Analysis III Type Checking - Lecture Slides | EECS 483 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Semantic Analysis III Type Checking

EECS 483 – Lecture 11 University of Michigan Wednesday, October 8, 2003

Type Checking

Y

What are types?

» They describe the values computed during the

execution of the program

» Essentially they are a predicate on values

y^

E.g., “int x” in C means –2^31 <= x < 2^

Y

Type Errors: improper or inconsistentoperations during program execution

Y

Type-safety: absence of type errors

Type Checking

Y

Semantic checks to enforce the type safety of theprogram

Y

Examples^ » Unary and binary operators (e.g. +, ==, [ ]) must

receive operands of the proper type » Functions must be invoked with the right number and

type of arguments » Return statements must agree with the return type » In assignments, assigned value must be compatible

with type of variable on LHS » Class members accessed appropriately

4 Concepts Related to Types/Languages

Static vs dynamic checking »^

When to check types

Static vs dynamic typing »^

When to define types

Strong vs weak typing »^

How many type errors

Sound type systems »^

Statically catch all type errors

Static vs Dynamic Typing

Y

Static and dynamic typing refer to typedefinitions (i.e., bindings of types to variables,expressions, etc.)

Y

Static typed language^ » Types defined at compile-time and do not change

during the execution of the program^ y

E.g., C

Y

Dynamically typed language^ » Types defined at run-time, as program executes

y^

E.g., Lisp

Strong vs Weak Typing

Y

Refer to how much type consistency isenforced

Y

Strongly typed languages

» Guarantee accepted programs are type-safe

Y

Weakly typed languages

» Allow programs which contain type errors

Y

These concepts refer to run-time

» Can achieve strong typing using either static

or dynamic typing

  • 9 -

Class Problem^ Classify the following languages: C, C++, Pascal, Java, Scheme^ ML, Postscript, Modula-3, Smalltalk, assembly code

Strong Typing

Weak Typing

Static Typing Dynamic Typing

Why Static Checking?

Y

Efficient code

» Dynamic checks slow down the program

Y

Guarantees that all executions will be safe

» Dynamic checking gives safety guarantees

only for some execution of the program

Y

But is conservative for sound systems

» Needs to be expressive: reject few type-safe

programs

Type Expressions

Y

Language type systems have basic types(aka: primitive types or ground types)

» E.g., int, char*, double

Y

Build type expressions using basic types:

» Type constructors

y^

Array types y^

Structure types y^

Pointer types

» Type aliases » Function types

Type Expressions: Arrays

Y

Various kinds of array types in differentprogramming languages

Y

Array(T): arrays without bounds^ » C, Java: T[ ]

Y

Array(T,S): array with size^ » C: T[S], may be indexed 0 .. S-

Y

Array(T,L,U): array with upper/lower bounds^ » Pascal: array[L .. U] of T

Y

Array(T, S1, …, Sn): multi-dimensional arrays^ » Fortran: T(L1, …, Ln)

Type Expressions: Aliases / Pointers

Y

Type aliases^ » C: typedef int int_array[ ];^ » Aliases are not type constructors

y^

Int_array is the same type as int [ ]

» Problem: Different type expressions may denote the

same type

Y

Pointers^ » Pointer types characterize values that are addresses of

variables of other types » C pointers: T* (e.g., int *x; )

Implementation

Y

Use a separate class hierarchy for types:

» class BaseType extends Type {String name;} » class IntType extends BaseType { ... } » class FloatType extends BaseType { ... } » class ArrayType extends BaseType { ... } » class FunctionType extends BaseType { ... }

Y

Semantic analysis translates all typeexpressions to type objects

Y

Symbol table binds name to type object

Creating Type Objects

Y

Build types while parsing – use a syntax-directed definition

Y

non terminal Type typetype : INTEGER Type objects = AST nodes for typeexpressions

{$$ = new IntType(id); } | ARRAY LBRACKET type RBRACKET

{$$ = new ArrayType($3); } ;

Processing Type Declarations

Y

Type declarations add new identifiers andtheir types in the symbol tables

Y

Class definitions must be added to symboltable:

» class_defn : CLASS ID {decls} ;

Y

Forward references require multiple passesover AST to collect legal names

» class A {B b; } » class B { ... }