Docsity

Static Semantics: Inference Rules and Type Derivations in EECS 483, University of Michigan, Exams of Electrical and Electronics Engineering

Partial preview of the text

Download Static Semantics: Inference Rules and Type Derivations in EECS 483, University of Michigan and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Semantic Analysis IV Static Semantics

EECS 483 – Lecture 12 University of Michigan Wednesday, October 20, 2004

  • 1 -

Announcements^ Y

Exam 1 review next Monday (Oct 25) inclass^ » Topics: Lexical, syntax, and semantic analysis Y Exam 1 next Wednes (Oct 27) in class^ » Room TBA Y Project 2 due date extended to Monday,Oct 25, 11:59pm

  • 3 -

Review: Proof Tree^ Y

Expression is well-typed if there exists atype derivation for a type judgment Y Type derivation is a proof tree Y Example: if A1 = b : bool, x : int, then:

A^

2 : int ⊥

A^

3 : int ⊥

A^

b : bool ⊥ A^

!b : bool ⊥

A^

2 + 3 : int ⊥

A^

x : int ⊥

b : bool, x : int

⊥if (!b) 2 + 3 else x : int

  • 4 -

Assignment Statements

id : T

∈^

A

A^

E : T

(variable-assign)

A^

id = E : T ⊥ A

E3 : T

A^

E2 : int A^

⊥ E1 : array[T] A^

E1[E2] = E3 : T

(array-assign)

  • 6 -

Class Problem 1.^

Show the inference rule for a while statement, while (E) S

2.^

Show the inference rule for a variable declaration with initializer, Type id = E

3.^

Show the inference rule for a question mark/colon operator, E1? S1 : S

  • 7 -

Sequence Statements^ Y

Rule: A sequence of statements is well-typed if the first statement is well-typed,and the remaining are well-typed as well:

A^

S1 : T

A^

⊥ ⊥(S2; .... ; Sn) : Tn

(sequence)

A^

⊥(S1; S2; .... ; Sn) : Tn

  • 9 -

Function Calls^ Y

If expression E is a function value, it has atype T1 x T2 x ... x Tn

Æ

Tr

Y^ Ti are argument types; Tr is the return type Y^ How to type-check a function call?

» E(E1, ..., En)

A^

E : T1 x T2 x ... Tn

Æ

Tr

A^

Ei : Ti

(i^ ∈

1 ... n)

(function-call)

A^

⊥E(E1, ..., En) : Tr

  • 10 -

Function Declarations^ Y

Consider a function declaration of the form:^ » Tr fun (T1 a1, ... , Tn an) = E^ » Equivalent to:

y^ Tr fun (T1 a1, ..., Tn an) {return E;}

Y^ Type of function body S must matchdeclared return type of function, i.e., E : Tr Y^ But, in what type context?

  • 12 -

Class Problem Recursive function – factorial int fact(int x) = if (x == 0) 1 else x * fact(x-1); Is this well-formed?, if so construct the type derivation

  • 13 -

Mutual Recursion^ Y

Example^ » int f(int x) = g(x) + 1;^ » int g(int x) = f(x) – 1; Y Need environment containing at least

y^ f: int

Æ^

int, g: int

Æ^

int

y^ when checking both f and g

Y^ Two-pass approach:

» Scan top level of AST picking up all function signaturesand creating an environment binding all global identifiers » Type-check each function individually using this globalenvironment

  • 15 -

Put Return in the Symbol Table^ Y^

Add a special entry {return_fun : T} when westart checking the function “fun”, look up thisentry when we hit a return statement Y To check Tr fun (T1 a1, ... , Tn an) { S } inenvironment A, need to check:^ A, a1 : T1, ..., an : Tn, return_fun : Tr

⊥A : Tr

A^

E : T

return_fun : T

∈^ A

A^

return E : unit

⊥

(return)

  • 16 -

Static Semantics Summary^ Y

Static semantics = formal specification oftype-checking rules Y Concise form of static semantics: typingrules expressed as inference rules Y Expression and statements are well-formed(or well-typed) if a typing derivation (prooftree) can be constructed using the inferencerules

  • 18 -

Other Forms of Semantic Analysis^ Y

One more category that we have notdiscussed Y Control flow errors^ » Must verify that a break or continue statements

are always encosed by a while (or for) stmt » Java: must verify that a break X statement isenclosed by a for loop with label X » Goto labels exist in the proper function » Can easily check control-flow errors byrecursively traversing the AST

  • 19 -

Where We Are... Source code (character stream)

regular expressions

Lexical Analysis

token stream

grammars

Syntax Analysis

abstract syntax

tree

static semantics

Semantic Analysis

abstract syntax^ tree + symbol^ tables, types

Intermediate Code Gen Intermediate code