Docsity

Semantic Analysis IV Static Semantics - Compiler Construction | EECS 483, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Semantic Analysis IV Static Semantics - Compiler Construction | EECS 483 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Semantic Analysis IV Static Semantics

EECS 483 – Lecture 12 University of Michigan Monday, October 20, 2003

  • 1 -

Static Semantics and Type Judgments^ Y

Static semantics = formal notation whichdescribes type judgments:^ » E : T^ » means “E is a well-typed expression of type T” Y Type judgment examples:^ » 2 : int^ » true : bool^ » 2 * (3 + 4) : int^ » “Hello” : string

  • 3 -

Deriving a Judgment^ Y

Consider the judgment^ » if (b) 2 else 3 : int Y What do we need to decide that this is awell-typed expression of type int?^ » b must be a bool (b : bool)^ » 2 must be an int (2 : int)^ » 3 must be an int (3 : int)

  • 4 -

Type Judgements^ Y

Type judgment notation: A

E : T

» Means “In the context A, the expression E is awell-typed expression with type T”

Y^ Type context is a set of type bindings: id : T

» (i.e. type context = symbol table)» b: bool, x: int

b: bool

» b: bool, x: int

if (b) 2 else x : int

»^

2 + 2 : int

  • 6 -

General Rule^ Y

For any environment A, expression E,statements S1 and S2, the judgement:^ » A

if (E) S1 else S2 : T

Y^ Is true if:

» A

E : bool » A

S1 : T

» A

S2 : T

  • 7 -

Inference Rules if-rule

premises

A^

E : bool

A^

S1 : T

A^

S2 : T

A^

if (E) S1 else S2 : T

conclusion

  • Holds for any choice of E, S1, S2, T
  • 9 -

Meaning of Inference Rule^ Y

Inference rule says:^ » Given the premises are true (with some

substitutions for A, E1, E2) » Then, the conclusion is true (with consistentsubstitution)

E

:int

E

:int

E^

  • E

A^

E1 : int A^ ⊥ ⊥E2 : int

(+)

A^ ⊥E1 + E2 : int

:int

  • 10 -

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

  • 12 -

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)

  • 13 -

If Statements^ • If statement as an expression: its value is the value of the clause^ that is executed

A^

E : bool A^

S1 : T

A

S2 : T

(if-then-else)

A^

if (E) S1 else S2 : T ⊥

  • If with no else clause, no value, why??

A^

E : bool A^

⊥^ ⊥S : T

(if-then)

A^

if (E) S : unit ⊥

  • 15 -

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

  • 16 -

Declarations

= unit if no E

A^

id : T [ = E ] : T A, id : T

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

⊥

(declaration)

A^

⊥(id : T [ = E ]; S2; .... ; Sn) : Tn Declarations add entries to the environment (e.g., the symbol table)

  • 18 -

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?

  • 19 -

Add Arguments to Environment^ Y

Let A be the context surrounding thefunction declaration.^ » The function declaration:

y^ Tr fun (T1 a1, ... , Tn an) = E » Is well-formed if

y^ A, a1 : T1 , ... , an : Tn

⊥E : Tr

Y^ What about recursion?

» Need: fun: T1 x T2 x ... x Tn

Æ

Tr

∈^

A