compiler construction semantic analysis i ran shaham and ohad shacham school of computer science...

41
Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

Post on 20-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

Compiler Construction

Semantic Analysis I

Ran Shaham and Ohad ShachamSchool of Computer Science

Tel-Aviv University

Page 2: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

22

TA1

Parsing Submission is not in groups Deadline January 5th 2009 Submission either in class or to פז גרימברג box

Page 3: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

33

Compiler

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compiler

ScopesSymbol tablesType checking

Page 4: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

44

Semantic analysis motivation

Syntax analysis is not enough

int a;a = “hello”;

int a;b = 1;

Assigning wrong type

Assigning undeclared variable

int a;int a;a = 1;

Variable re declaration

Page 5: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

55

Goals of semantic analysis

Check “correct” use of programming constructs

Context-sensitive Beyond context free grammars Deeper analysis than lexical and syntax analysis

Semantic rules for checking correctness Scope rules Type-checking rules Specific rules

Guarantee partial correctness only Runtime checks

pointer dereferencing array access

Page 6: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

66

Example of semantic rules

A variable must be declared before used A variable should not be declared multiple times A variable should be initialized before used Non-void method should contain return

statement along all execution paths break/continue statements allowed only in loops this keyword cannot be used in static method main method should have specific signature …

Page 7: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

77

Example of semantic rules

Type rules are important class of semantic rulesIn an assignment RHS and LHS must have

the same typeIn a condition test expression must have

boolean type

Page 8: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

88

Scope

Scope of identifier portion of program where identifier can be referred to

Lexical scope Statement block Method body Class body Module / package / file Whole program (multiple modules)

Page 9: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

99

Scope exampleclass Foo { int value; int test() { int b = 3; return value + b; } void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } }}

class Bar extends Foo { int value; void setValue(int c) {

value = c; test(); }}

scope offield value

scope oflocal variable b

scope of formalparameter c

scope of cscope of value

scope of local variablein statement block d scope of

method test

Page 10: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1010

Scope nesting

Scopes may be enclosed in other scopes

void foo(){ int a; … {

int a; }}

same name but different

symbol

Page 11: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1111

Scope tree

Generally scope hierarchy forms a tree

class Foo { int value; int test() { int b = 3; return value + b; }

void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } }}

Foovalue

testb

setValuec

block Id

block I

Page 12: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1212

Subclasses

Scope of subclass enclosed in scope of its superclass Subtype relation must be acyclic

Class Foo {int a;

}

Class Bar extends Foo {

}

Bar sees “a” as well

Page 13: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1313

Scope hierarchy in IC

Global scope The names of all classes defined in the program

Class scope Instance scope: all fields and methods of the class Static scope: all static methods Scope of subclass nested in scope of its superclass

Method scope Formal parameters and local variables Variables defined in block

Code block scope Variables defined in block

Page 14: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1414

Scope rules in IC

“When resolving an identifier at a certain point in the program, the enclosing scopes are searched for that identifier.”

“local variables and method parameters can only be used after they are defined in one of the enclosing block or method scopes.”

“Fields and virtual methods can be used in expressions of the form e.f or e.m() when e has class type C and the instance scope of C contains those fields and methods.”

“static methods can be used in expressions of the form C.m() if the static scope of C contains m.”

… (Section 10 in IC specification)

Page 15: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1515

Symbol table

An environment that stores information about identifiers

A data structure that captures scope information

SymbolKindTypeProperties

valuefieldint…

testmethod-> intprivate

setValuemethodint -> void public

Page 16: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1616

Symbol table

Each entry in symbol table contains name of an identifier kind (variable/method/field…) Type (int, string, myClass…) Additional properties, e.g., final, public

(not needed for IC)

One symbol table for each scope

Page 17: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1717

Scope nesting in IC

SymbolKindTypeProperties Global

SymbolKindTypeProperties Class

SymbolKindTypeProperties Method

SymbolKindTypeProperties Block

names of all classes

fields and methods

formals + locals

variables defined in block

Scope nesting mirrored in hierarchy of symbol tables

Page 18: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1818

class Foo { int value; int test() { int b = 3; return value + b; } void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } }}

class Bar { int value; void setValue(int c) {

value = c; }}

scope of value

scope of b

scope of c

scope of cscope of value

scope of d

Symbol table example

block1

Page 19: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

1919

class Foo { int value; int test() { int b = 3; return value + b; } void setValue(int c) { value = c; { int d = c; c = c + d; value = c; } }}

class Bar { int value; void setValue(int c) {

value = c; }}

Symbol table example

SymbolKindTypeProperties

valuefieldint…

testmethod-> int

setValuemethodint -> void

(Foo)

SymbolKindTypeProperties

bvarint…

(test)

SymbolKindTypeProperties

cvarint…

(setValue)

SymbolKindTypeProperties

dvarint…

(block1)

Page 20: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2020

SymbolKindTypeProperties

valuefieldint…

testmethod-> int

setValuemethodint -> void

SymbolKindTypeProperties

bvarint…

SymbolKindTypeProperties

cvarint…

SymbolKindTypeProperties

dvarint…

(Foo)

(test)

…Symbol table example cont.

(setValue)

(block1)

Page 21: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2121

Checking scope rules

SymbolKindTypeProperties

valuefieldint…

testmethod-> int

setValuemethodint -> void

SymbolKindTypeProperties

bvarint…

SymbolKindTypeProperties

cvarint…

SymbolKindTypeProperties

dvarint…

(Foo)

(test) (setValue)

(block1)

void setValue(int c) { value = c; { int d = c; c = c + d; value = c; }}

lookup(value)

Page 22: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2222

SymbolKindTypeProperties

valuefieldint…

testmethod-> int

setValuemethodint -> void

SymbolKindTypeProperties

bvarint…

SymbolKindTypeProperties

cvarint…

SymbolKindTypeProperties

dvarint…

(Foo)

(test) (setValue)

(block1)

void setValue(int c) { value = c; { int d = c; c = c + d; myValue = c; }}

lookup(myValue)

Error !undefined

symbol

Catching semantic errors

Page 23: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2323

Symbol table operations

insert Insert new symbol

(to current scope) lookup

Try to find a symbol in the tableMay cause lookup in parent tablesReport an error when symbol not found

How do we check illegal re-definitions?

Page 24: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2424

class

MethodDecl

Stmt

MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbolkind

globals

Symbolkind

Foo

Symbol

testSymbol

setValue

Foo

test methodsetValue method

b var c var

Symbol

block1

d var

Symbol table construction via AST traversal

Page 25: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2525

class

MethodDecl

Stmt

MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbolkind

globals

Symbolkind

Foo

Symbol

testSymbol

setValue

Foo

test methodsetValue method

b var c var

Symbol

block1

d var

Linking AST nodes to enclosing table

Page 26: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2626

public abstract class ASTNode { /** line in source program **/ private int line;

/** reference to symbol table of enclosing scope **/ private SymbolTable enclosingScope;

/** accept visitor **/ public abstract void accept(Visitor v);

/** accept propagating visitor **/ public abstract <D,U> U accept(PropagatingVisitor<D,U> v,D context);

/** return line number of this AST node in program **/ public int getLine() {…}

/** returns symbol table of enclosing scope **/ public SymbolTable enclosingScope() {…}}

What’s in an AST node

Page 27: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2727

Symbol table implementation

Each table could be implemented using java.util.HashMap

Implement a hierarchy of symbol tables Can implement a Symbol class

HashMap keys should obey equals/hashcode contracts

Safe when key is symbol name (String)

Page 28: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2828

Symbol table implementation

public class SymbolTable { /** map from String to Symbol **/ private Map<String,Symbol> entries; private String id; private SymbolTable parentSymbolTable; public SymbolTable(String id) { this.id = id; entries = new HashMap<String,Symbol>(); } …}

public class Symbol { private String id; private Type type; private Kind kind; …}

Page 29: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

2929

Implementing table structureHierarchy of symbol tables

Pointer to enclosing tableCan also keep list of sub-tables

Symbol table key should include id and kind Can implement using 2-level maps

(kind->id->entry)Separating table in advance according to kinds

also acceptable

Page 30: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3030

Implementation option 1public class SymbolTable { /** Map kind->(id->entry) Kind enum->(String->Symbol) **/ private Map<Kind, Map<String,Symbol> > entries; private SymbolTable parent; … public Symbol getMethod(String id) { Map<String,Symbol> methodEntries = entries.get(METHOD_KIND); return methodEntries.get(id); } public void insertMethod(String id, Type t) { Map<String,Symbol> methodEntries = entries.get(METHOD_KIND); if (methodEntries == null) { methodEntries = new HashMap<String,Symbol>(); entries.put(METHOD_KIND, methodEntries); } methodEntries.put(id,new Symbol(id, t)); } …}

Page 31: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3131

Implementation option 2

public class SymbolTable { /** Method Map id->entry **/ private Map<String,Symbol> methodEntries; … private Map<String,Symbol> variableEntries; … private SymbolTable parent; public Symbol getMethod(String id, Type t) { return methodEntries.get(id); }

public void insertMethod(String id, Type t) { methodEntries.put(new Symbol(id,METHOD_KIND,t); }

Page 32: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3232

Forward references

class A {

void foo() {

bar();

}

void bar() {

}

}

Program

root

ClassDecl

id=A

MethodDecl

id=fooretType=void

MethodDecl

id=barretType=void

Call

id=bar()

class

Symbolkind

globals

Symbolkind

A

A

foo methodbar method

Undefined identifier bar()

bar used before encountered declaration

How do we handle forward references?We will see two solutions

Page 33: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3333

Solution 1 – multiple phases

Building visitor A propagating visitor Propagates reference to the symbol table of the

current scope

Checking visitor On visit to node

perform check using symbol tables Resolve identifiers

Look for symbol in table hierarchy

Page 34: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3434

class

MethodDecl MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbolkind

globals

Symbolkind

Foo

SymbolKind

test

Symbolkind

setValue

Foo

test methodsetValue method

b var c var

Symbolkind

block1

d var

Building phase

unresolvedsymbol

Stmt

setValue()

Page 35: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3535

class

MethodDecl MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbolkind

globals

Symbolkind

Foo

SymbolKind

test

Symbolkind

setValue

Foo

test methodsetValue method

b var c var

Symbolkind

block1

d var

Checking phase

Stmt

setValue()

Page 36: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3636

Forward references – solution 2

Use forward reference marker Update symbol table when symbol defined

Remove forward-reference marker

Count unresolved symbols Upon exit check that #unresolved=0

Page 37: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3737

Forward reference flag example

class A {

void foo() {

bar();

}

void bar() {

}

}

Program

root

ClassDecl

id=A

MethodDecl

id=fooretType=void

MethodDecl

id=barretType=void

Call

id=bar()

class

Symbolkind

globals

SymbolkindFREF

A

A

foo methodbar method true

Page 38: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3838

Class hierarchy

extends relation should be acyclic

Can check acyclicity in separate phase

Build symbol tables for classes first and check absence of cycles

Page 39: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

3939

Next phase: type checking

First, record all pre-defined types(string, int, boolean, void, null)

Second, record all user-defined types(classes, methods, arrays)

Recording done by storing in type tableNow, run type-checking algorithm

Page 40: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

4040

Type tableKeeps a single copy for each type

Can compare types for equality by ==Records primitive types: int, bool, string, void, null

Initialize table with primitive typesUser-defined types: arrays, methods, classes

Used to record inheritance relationTypes should support subtypeOf(Type t1, Type t2)

For IC enough to keep one global tableStatic field of some class (e.g., Type)In C/Java associate type table with scope

Page 41: Compiler Construction Semantic Analysis I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University

4141

Possible type hierarchyType

IntType

RefType

BoolTypeVoidType

NullType StringType ClassTypeICClass : c

ArrayTypeType : elemType

RefType MethodType

type ::= int | boolean | … | type `[` `]`