static semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · scoping: general...

24
2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)

Upload: others

Post on 25-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Static Semantics

Lecture 15(Notes by P. N. Hilfinger and

R. Bodik)

Page 2: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 2

Current Status

• Lexical analysis– Produces tokens– Detects & eliminates illegal tokens

• Parsing– Produces trees– Detects & eliminates ill-formed parse trees

• Static semantic analysis we are here– Produces “decorated tree” with additional

information attached– Detects & eliminates remaining static errors

Page 3: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 3

Static vs. Dynamic

• We use the term static to describe propertiesthat the compiler can determine withoutconsidering any particular execution.– E.g., in def f(x) : x + 1

Both uses of x refer to same variable• Dynamic properties are those that depend on

particular executions in general. E.g., willx = x/y cause an arithmetic exception.

Page 4: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 4

Static vs. Dynamic, contd.

• Actually, distinction is not that simple. E.g.,after x = 3 y = x + 2

compiler could deduce that x and y are Ints• But languages often designed to require that

we treat variables only according to explicitlydeclared types, because deductions aredifficult or impossible in general.

Page 5: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 5

Typical Tasks of the Semantic Analyzer

• Find the declaration that defines eachidentifier instance

• Determine the static types of expressions• Perform re-organizations of the AST that

were inconvenient in parser, or requiredsemantic information

• Detect errors and fix to allow furtherprocessing

Page 6: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 6

Typical Semantic Errors: Java, C++

• Multiple declarations: a variable should be declared (inthe same region) at most once

• Undeclared variable: a variable should not be usedbefore being declared.

• Type mismatch: type of the left-hand side of anassignment should match the type of the right-handside.

• Wrong arguments: methods should be called with theright number and types of arguments.

• Definite-assignment check (Java): conservative checkthat simple variables assigned to before use.

Page 7: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 7

Output from Static Semantic Analysis

• Input is AST; output is an annotated tree.x = 3def f (x): return x+yy: Inty = 2

=

x

x

x

y

y yf

def =

return Int

type_decl

3 2

+

#1: x, Any, 0#2: f, Any->Any, 0#3: x, Any, 1#4: y, Int, 0

#1 #2

#3

#3 #4

#4 #4Int

Any

Ids decorated with declarations. Expressionsannotated with (static) types.

Int

Page 8: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 8

Output from Static Semantic Analysis (II)

• Analysis has added objects we’ll call “symbolentries” to hold information about instancesof identifiers.

• In this example, #1: x, Any, 0 denotes an entryfor something named x occurring at the outerlexical level (level 0) and having static typeAny.

• For other expressions, we annotate with statictype information.

Page 9: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 9

Output from Static Semantic Analysis (III)

• Symbol entries for classes (or modules, packages,namespaces and the like) must contain equivalent of adictionary mapping names of members to symbolentries.

• So given an expression such as x.clear (), we– find symbol entry for x– find type (class) of x from its entry, and– find clear in dictionary of entry associated with x’s type.

Page 10: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 10

Binding names to symbol entries: Scoping

• Scope of a declaration: section of text whereit applies

• Declarative region: section of text thatbounds scopes of declarations (we’ll say“region” for short)

• In most languages, the same name can bedeclared multiple times– if its declarations occur in different declarative

regions, and/or– involve different kinds of names.

Page 11: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 11

Scoping: example

• Java: can use same name for– a class,– field of the class,– a method of the class, and– a local variable of the method

• legal Java program:

class Test {int Test;Test( ) { double Test; }

}

Page 12: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 12

Scoping: general rules

• The scope rules of a language:– determine which declaration of a named object corresponds

to each use of the object.– Scoping rules map uses of objects to their declarations (or

symbol entries).• C++ and Java use static scoping:

– mapping from uses to declarations is made at compile time.– C++ uses “Algol scope rules”

• a use of variable x matches the declaration with the most closelyenclosing scope.

• a deeply nested variable x hides x declared in an outer region.– in Java:

• inner regions cannot define variables defined in outer regions

Page 13: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 13

Scoping Options: Declarative Regions

• In Java, each function has one or moredeclarative regions:– one for the function body,– and possibly additional regions in the function

• for each for loop and• each nested block (delimited by curly braces)

• In Pyth, each function has one per function(possibly plus more for nested functions)

Page 14: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 14

Example (assume C++ rules)

double y, k; // y and k global variablesvoid f(int y) { // y also used as a parameter

y = k; // Uses global k and parameter yint k = 0; // k is now local variablewhile (k) { // refers to 2nd decl of k

int k = 1; // another local var, in a loop (not ok in Java)}

}– the outermost region includes decls of "f”, “k” and “y”– function f itself has two (nested) regions:

1. The outer region for f includes parameter y and local variable k .2.The innermost region is for the body of the while loop, and

includes the variable k that is initialized to 1.

global k hiddenstarting here

Page 15: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 15

Scoping Options: overloading

• Java and C++ (but not in Pascal, C, or Pyth):– can use the same name for more than one method– as long as the number and/or types of parameters

are unique.

int add(int a, int b);float add(float a, float b);

Page 16: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 16

Scoping options: Use before declaration?

• Can names be used before they are defined?– Java, C++: a method or field name can be used

before the definition appears; not true for avariable, whose scope starts at its declaration

– In Pyth, almost anything can be used beforedeclaration, where syntactically possible

Page 17: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 17

Scoping Options: Dynamic scoping

• Not all languages use static scoping.• Original Lisp, APL, and Snobol use dynamic

scoping.• Dynamic scoping:

– A use of a variable that has no correspondingdeclaration in the same function corresponds to thedeclaration in the most-recently-called stillactive function.

• With this rule, difficult for compiler todetermine much about identifiers

Page 18: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 18

Example

• For example, consider the following code:void main() { f1(); f2(); }void f1() { int x = 10; g(); }void f2() { String x = "hello"; f3();g(); }void f3() { double x = 30.5; }void g() { print(x); }

• With static scoping, illegal.• With dynamic scoping, prints 10 and hello

Page 19: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 19

So How do we Annotate with Declarations?

• Idea is to recursively navigate the AST, ineffect executing the program in simplifiedfashion, extracting information that isn’t datadependent.

• You saw it in CS61A (sort of).

Page 20: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 20

Environment Diagrams and Symbol Entries

• In Scheme, a program like (set! x 7) (define (f x) (let ((y (+ x 39)) (+ x y))) (f 3)

when executed would eventually give yousomething like this when computing (+ x y)

X: 7f: … x: 3 y: 42 current

environment

globalenvironment

Page 21: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 21

Environment Diagrams to Symbol Entries

Abstracting from this particular execution,X: 7f: … x: 3 y: 42 current

environment

we take away the values and replace with staticinfo:

#1. X: Any#2. f: Any->Any #3. x: Any #4. y: Any

current environment

… and we’re left with symbol entries and a datastructure (a kind of symbol table) for looking them up.

Page 22: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 22

Annotating Pyth with Symbol Entries

• Process requires several passes (traversalsof AST). A rough outline:1. Create symbol entries for all classes and record in

global environment, plus entries for all classmembers stored in the class’s entries.

2. For each declarative region,A. Create a new environment box for each declaration

immediately inside the region.B. Use it to annotate all uses, and recursively do step 2 for

all inner declarative regions.

Page 23: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 23

Type Checking

• the job of the type-checking phase is to:– Determine the type of each expression in the program

• (each node in the AST that corresponds to an expression)– Find type errors

• The type rules of a language define– how to determine expression types, and– what is considered to be an error.

• The type rules specify, for every operator (includingassignment),– what types the operands can have, and– what is the type of the result.

Page 24: Static Semanticsgamescrafters.berkeley.edu/~cs164/sp08/lectures/lecture15.pdf · Scoping: general rules •The scope rules of a language: –determine which declaration of a named

2/29/08 Prof. Hilfinger, CS164 Lecture 15 24

Type Errors

• The type checker must also1. find type errors having to do with the context of

expressions,• e.g., the context of some operators must be boolean,

2. type errors having to do with method calls.• Examples of the context errors:

– the condition of an if not boolean (Java)– type of returned value not function’s return type

• Examples of method errors:– calling something that is not a method– calling a method with the wrong number of arguments– calling a method with arguments of the wrong types