semantic analysis ii

23
Semantic Analysis II

Upload: dillon

Post on 16-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Semantic Analysis II. x86 executable. exe. IC Program. ic. IC compiler. Compiler. We saw: Scope Symbol tables. Lexical Analysis. Syntax Analysis Parsing. AST. Symbol Table etc. Inter. Rep. (IR). Code Generation. Today: Type checking. assigned type doesn’t match declared type. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Semantic Analysis II

Semantic Analysis II

Page 2: Semantic Analysis II

22

Compiler

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compiler

We saw: Scope Symbol tables

Today: Type checking

Page 3: Semantic Analysis II

33

Examples of type errors

int a; a = true;

void foo(int x) { int y; foo(5,7);}

1 < true

class A {…}class B extends A { void foo() { A a; B b; b = a; }}

argument list doesn’t match

formal parameters

a is not a subtype of b

assigned type doesn’t match declared type

relational operator applied to non-int

type

Page 4: Semantic Analysis II

44

Types

Type Set of possible values (and operations)

boolean = {true,false} int = {-231..231-1} void = {}

Type safety Type usage adheres to formally defined typing rules

Page 5: Semantic Analysis II

55

Type judgments

e : T e is a well-typed expression of type T

Examples 2 : int 2 * (3 + 4) : int true : bool “Hello” : string

Page 6: Semantic Analysis II

66

Type judgments

E e : T In the context E, e is a well-typed expression of T

Examples: b:bool, x:int b:bool x:int 1 + x < 4:bool foo:int->string, x:int foo(x) : string

Page 7: Semantic Analysis II

77

Typing rules

Premise

Conclusion[Name]

Conclusion[Name]

Page 8: Semantic Analysis II

88

Typing rules for expressions

E e1 : int E e2 : int

E e1+e2 : int[+]

Page 9: Semantic Analysis II

99

Expression rules

E true : bool

E e1 : int E e2 : int

E e1 op e2 : int

E false : bool

E int-literal : int E string-literal : string

op { +, -, /, *, %}

E e1 : int E e2 : int

E e1 rop e2 : boolrop { <=,<, >, >=}

Page 10: Semantic Analysis II

1010

More expression rules

E e1 : bool E e2 : bool

E e1 lop e2 : boollop { &&,|| }

E e1 : int

E - e1 : int

E e1 : bool

E ! e1 : bool

E e1 : T[]

E e1.length : int

E e1 : T[] E e2 : int

E e1[e2] : T

E e1 : int

E new T[e1] : T[]

E new T() : T

E e:C (id : T) C

E e.id : T

Page 11: Semantic Analysis II

1111

Subtyping

Inheritance induces subtyping relation ≤

S ≤ T values(S) values(T)

“A value of type S may be used wherever a value of type T is expected”

Page 12: Semantic Analysis II

1212

Subtyping

For all types:

For reference types:

A ≤ A

A extends B {…}

A ≤ B

A ≤ B B ≤ C

A ≤ C null ≤ A

Page 13: Semantic Analysis II

Examples1. int ≤ int ?

2. null ≤ A ?

3. null ≤ string ?

4. string ≤ null ?

5. null ≤ boolean ?

6. null ≤ boolean[] ?

7. A[] ≤ B[] ?

1313

Page 14: Semantic Analysis II

Examples1. int ≤ int ?

2. null ≤ A ?

3. null ≤ string ?

4. string ≤ null ?

5. null ≤ boolean ?

6. null ≤ boolean[] ?

7. A[] ≤ B[] ?“Subtyping is not covariant for array types: if A is a subtype of B then A[ ] is not a

subtype of B[ ]. Instead, array subtyping is type invariant, which means that each array type is only a subtype of itself.”

1414

Page 15: Semantic Analysis II

1515

Expression rules with subtyping

E e1 : T1 E e2 : T2 T1 ≤ T2 or T2 ≤ T1

op {==,!=}

E e1 op e2 : bool

Page 16: Semantic Analysis II

1616

Rules for method invocations

E e0 : T1 … Tn Tr

E ei : T’i T’

i ≤ Ti for all i=1..n

E e0(e1, … ,en): Tr

(m : static T1 … Tn Tr) CE ei : T’

i T’i ≤ Ti for all

i=1..nE C.m(e1, … ,en): Tr

Page 17: Semantic Analysis II

1717

Statement rules

Statements have type voidJudgments of the form

E S In environment E, S is well typed

E e:bool E S

E while (e) S

E e:bool E S

E if (e) S

E e:bool E S1 E S2

E if (e) S1 else S2

E break E continue

Page 18: Semantic Analysis II

1818

Return statements

ret:Tr represents return type of current method

ret:void E

E return;

ret:T’E T≤T’

E return e;

E e:T

Page 19: Semantic Analysis II

More IC Rules

DeclarationsMethodClassProgram…

1919

Page 20: Semantic Analysis II

2020

Type-checking algorithm

1. Construct types1. Add basic types to a “type table”

2. Traverse AST looking for user-defined types (classes,methods,arrays) and store in table

3. Bind all symbols to types

Page 21: Semantic Analysis II

2121

Type-checking algorithm

2. Traverse AST bottom-up (using visitor)1. For each AST node find corresponding rule

(there is only one for each kind of node)

2. Check if rule holds1. Yes: assign type to node according to consequent

2. No: report error

Page 22: Semantic Analysis II

222245 > 32 && !false

BinopExpr UnopExpr

BinopExpr

op=AND

op=NEGop=GT

intLiteral

val=45

intLiteral

val=32

boolLiteral

val=false

: int : int

: bool

: bool

: bool

: bool

E false : bool

E int-literal : int

E e1 : int E e2 : int

E e1 > e2 : bool

E e1 : bool E e2 : bool

E e1 && e2 : bool

E e1 : bool

E !e1 : bool

Algorithm example

Page 23: Semantic Analysis II

2323

Semantic analysis flow

Parsing and AST construction Combine library AST with IC program AST

Construct and initialize global type table Construct class hierarchy and verify the hierarchy is

tree Phase 1: Symbol table construction

Assign enclosing-scope for each AST node Phase 2: Scope checking

Resolve names Check scope rules using symbol table

Phase 3: Type checking Assign type for each AST node

Phase 4: Remaining semantic checks