Compiler Construction Compiler Construction Semantic Analysis I.

Post on 19-Jan-2016

282 views 2 download

Transcript of Compiler Construction Compiler Construction Semantic Analysis I.

Compiler ConstructionCompiler 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.

PAs

GroupsMailsPA2

Syntax analysis & AST construction 3 weeks

33

44

Compiler

ICProgram

ic

x86 executable

exeLexicalAnalysi

s

Syntax Analysi

s

Parsing

AST

Symbol

Tableetc.

Inter.Rep.(IR)

CodeGeneration

IC compiler

VisitorScopesSymbol table

Agenda:

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

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

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

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)

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

Semantic Analysis

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

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 …

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

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

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)

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

1717

Scope nesting

Scopes may be enclosed in other scopes

void foo(){ int a; … {

int a; }}

same name but different

symbol

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

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

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

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

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

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

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

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

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)

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)

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

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?

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

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

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

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

3434

Symbol table implementation

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

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?

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

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()

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()

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

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