40
Compiler Construction Compiler Construction Semantic Analysis I

Compiler Construction Compiler Construction Semantic Analysis I

Embed Size (px)

Citation preview

Page 1: Compiler Construction Compiler Construction Semantic Analysis I

Compiler ConstructionCompiler Construction

Semantic Analysis I

Page 2: Compiler Construction Compiler Construction Semantic Analysis I

PA 1

22

• Solution must compile• Ant Parser, Ant Build => Try outside Eclipse!• Turn in all files needed for project to compile (including

build.xml)• Fail program only on critical errors.

• User code having errors is not a reason to fail. • Output errors and gracefully return 0.

• Your compiler should not crash.• Your printouts should match given outputs. Use diff.• Pay attention to what goes on in the forum. Our

instructions in response to questions in the forum must be followed.

Page 3: Compiler Construction Compiler Construction Semantic Analysis I

PAs

GroupsMailsPA2

Syntax analysis & AST construction 3 weeks

33

Page 4: Compiler Construction Compiler Construction Semantic Analysis I

44

Compiler

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST

Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compiler

VisitorScopesSymbol table

Agenda:

Page 5: Compiler Construction Compiler Construction Semantic Analysis I

55

Separate operations on objects of a data structure from object representation

Each operation (pass) may be implemented as separate visitor

Use double-dispatch to find right method for object

Visitor Pattern

Page 6: Compiler Construction Compiler Construction Semantic Analysis I

Visitor pattern in Java

66

interface Visitor { visit(A a); visit(B b); visit(C c);}

class A { A x; accept(Visitor v) { v.visit(this); }}

class B { accept(Visitor v) { v.visit(this); }}

class op1 implements Visitor { visit(A a) {…} visit(B b) {…} visit(C c) {…}}×class op2 implements Visitor { visit(A a) {…} visit(B b) {…} visit(C c) {…}}

class op3 implements Visitor { visit(A a) {…} visit(B b) {…} visit(C c) {…}}

class C { accept(Visitor v) { v.visit(this); }}

Page 7: Compiler Construction Compiler Construction Semantic Analysis I

Double dispatch example

77

Visitor v = new op1(); // op1/2/3 x = ???? // x can be A/B/C x.accept(v);

class op1 implements Visitor { visit(A a) { }

visit(B b) { … }}

class B { accept(Visitor v) { // always calls visit(B b) v.visit(this); }}

1st dispatch

2nd dispatch

Page 8: Compiler Construction Compiler Construction Semantic Analysis I

Straight Line Program example

88

prog stmt_list

stmt_list stmtstmt_list stmt_list stmtstmt var = expr;stmt print(expr);

expr expr + exprexpr expr - exprexpr expr * exprexpr expr / exprexpr - exprexpr ( expr )expr numberexpr readi()expr var

ASTNode

Stmt Expr

PrintStmt

AssignStmt

BinaryOpExpr

UnaryOpExpr

NumberExpr

ReadIExpr

StmtList

VarExpr

(Code available on web site.Demonstrates scanning, parsing, AST + visitors)

Page 9: Compiler Construction Compiler Construction Semantic Analysis I

99

Visitor variationsinterface PropagatingVisitor {

/** Visits a statement node with a given * context object (book-keeping) * and returns the result * of the computation on this node. */ Object visit(Stmt st, Object context);Object visit(Expr e, Object context);Object visit(BinaryOpExpr e, Object context);...

}

Propagate values down the AST (and back) SLPEvaluator example

Page 10: Compiler Construction Compiler Construction Semantic Analysis I

Semantic Analysis

Page 11: Compiler Construction Compiler Construction Semantic Analysis I

1111

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 12: Compiler Construction Compiler Construction Semantic Analysis I

1212

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 Other specific rules

Guarantee partial correctness Runtime checks

pointer dereferencing array access …

Page 13: Compiler Construction Compiler Construction Semantic Analysis I

1313

Semantic rules: examples

A variable must be declared before being used A variable should not be declared multiple times A variable should be initialized before being

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 14: Compiler Construction Compiler Construction Semantic Analysis I

1414

Example of semantic rules

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

the same type The type of a conditional test expression must

be Boolean

Page 15: Compiler Construction Compiler Construction Semantic Analysis I

1515

Scope

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

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

Page 16: Compiler Construction Compiler Construction Semantic Analysis I

1616

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 { void setValue(int c) {

value = c; test(); }}

scope oflocal variable b

scope of formalparameter c

scope of c

scope of local variablein statement block d

scope ofmethod test

scope offield value

Page 17: Compiler Construction Compiler Construction Semantic Analysis I

1717

Scope nesting

Scopes may be enclosed in other scopes

void foo(){ int a; … {

int a; }}

same name but different

symbol

Page 18: Compiler Construction Compiler Construction Semantic Analysis I

1818

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 19: Compiler Construction Compiler Construction Semantic Analysis I

1919

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 20: Compiler Construction Compiler Construction Semantic Analysis I

2020

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

Code block scope Variables defined in block

Page 21: Compiler Construction Compiler Construction Semantic Analysis I

2121

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.”

Page 22: Compiler Construction Compiler Construction Semantic Analysis I

2222

Symbol table

An environment that stores information about identifiers

A data structure that captures scope information

Symbol Kind Type Properties

value field int …

test method -> int private

setValue method int -> void public

Page 23: Compiler Construction Compiler Construction Semantic Analysis I

2323

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

One symbol table for each scope

Page 24: Compiler Construction Compiler Construction Semantic Analysis I

2424

Scope nesting in IC

Symbol Kind Type Properties Global

Symbol Kind Type Properties Class

Symbol Kind Type Properties Method

Symbol Kind Type Properties Block

names of all classes

fields and methods

formals + locals

variables defined in block

Page 25: Compiler Construction Compiler Construction Semantic Analysis I

2525

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; } }}

scope of value

scope of b

scope of cscope of d

Symbol table example

block1

Page 26: Compiler Construction Compiler Construction Semantic Analysis I

2626

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; } }}

Symbol table example

Symbol Kind Type Properties

value field int …

test method -> int

setValue method int -> void

(Foo)

Symbol Kind Type Properties

b var int …

(test)

Symbol Kind Type Properties

c var int …

(setValue)

Symbol Kind Type Properties

d var int …

(block1)

Page 27: Compiler Construction Compiler Construction Semantic Analysis I

2727

Checking scope rules

Symbol Kind Type Properties

value field int …

test method -> int

setValue method int -> void

Symbol Kind Type Properties

b var int …

Symbol Kind Type Properties

c var int …

Symbol Kind Type Properties

d var int …

(Foo)

(test) (setValue)

(block1)

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

lookup(value)

Page 28: Compiler Construction Compiler Construction Semantic Analysis I

2828

Symbol Kind Type Properties

value field int …

test method -> int

setValue method int -> void

Symbol Kind Type Properties

b var int …

Symbol Kind Type Properties

c var int …

Symbol Kind Type Properties

d var int …

(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 29: Compiler Construction Compiler Construction Semantic Analysis I

2929

Symbol table operations

insert Insert new symbol

(to current scope) lookup

Try to find a symbol in the table May cause lookup in parent tables Report an error when symbol not found

How do we check illegal re-definitions?

Page 30: Compiler Construction Compiler Construction Semantic Analysis I

3030

class

MethodDecl

Stmt

MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbol kind

globals

Symbol kind

Foo

Symbol

testSymbol

setValue

Foo

test methodsetValue method

b var c var

Symbol

block1

d var

Symbol table construction via AST traversal

Page 31: Compiler Construction Compiler Construction Semantic Analysis I

3131

class

MethodDecl

Stmt

MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbol kind

globals

Symbol kind

Foo

Symbol

testSymbol

setValue

Foo

test methodsetValue method

b var c var

Symbol

block1

d var

Linking AST nodes to enclosing table

Page 32: Compiler Construction Compiler Construction Semantic Analysis I

3232

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 33: Compiler Construction Compiler Construction Semantic Analysis I

3333

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 34: Compiler Construction Compiler Construction Semantic Analysis I

3434

Symbol table implementation

Using java.util.HashMap HashMap keys should obey equals/hashcode contracts Safe when key is symbol name (String)

Page 35: Compiler Construction Compiler Construction Semantic Analysis I

3535

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

Symbol kind

globals

Symbol kind

A

A

foo methodbar method

Undefined identifier bar()

bar used before encountered declaration

How do we handle forward references?

Page 36: Compiler Construction Compiler Construction Semantic Analysis I

3636

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 37: Compiler Construction Compiler Construction Semantic Analysis I

3737

class

MethodDecl MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbol kind

globals

Symbol kind

Foo

Symbol Kind

test

Symbol kind

setValue

Foo

test methodsetValue method

b var c var

Symbol kind

block1

d var

Building phase

unresolvedsymbol

Stmt

setValue()

Page 38: Compiler Construction Compiler Construction Semantic Analysis I

3838

class

MethodDecl MethodDecl

ClassDecl

root

name=Foo

name=setValuename=test

VarDecl

id=b

Stmt Block

Stmt StmtVarDecl

id=d

Symbol kind

globals

Symbol kind

Foo

Symbol Kind

test

Symbol kind

setValue

Foo

test methodsetValue method

b var c var

Symbol kind

block1

d var

Checking phase

Stmt

setValue()

Page 39: Compiler Construction Compiler Construction Semantic Analysis I

3939

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 40: Compiler Construction Compiler Construction Semantic Analysis I

4040

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

Symbol kind

globals

Symbol kind FREF

A

A

foo methodbar method true