Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types –...

46
Ravi Sethi, CSC 453, Fall 2016 CSC 453, Fall 2016 Symbol Tables Ravi Sethi Version: 1 October 25, 2016

Transcript of Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types –...

Page 1: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

CSC 453, Fall 2016�� � �

Symbol Tables

Ravi Sethi

Version: 1 October 25, 2016

Page 2: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Each phase transforms a representation of the source code

2

A Sample Compiler

Lexical�Analyzer

Syntax�Analyzer

Intermediate�Code Gen

Code�Generator

Lexical�Analyzer

Characters Tokens Syntax�Tree

Intermediate�Code

Target �Code

Page 3: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Passes information from a declaration to uses of the name

3

The Role of the Symbol Table

Lexical�Analyzer

Syntax�Analyzer

Intermediate�Code Gen

Code�Generator

Lexical�Analyzer

Characters Tokens Syntax�Tree

Intermediate�Code

Target �Code

Symbol�Table

For example, type information collected incrementally during the analysis phases is used during the generation phases for storage layout.

Page 4: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Symbols

A symbol table associates information with names.

4

Page 5: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016 Ravi Sethi, CSC 453, Fall 2016

“It has been remarked to me …�that once a person has understood the way in which variables are used in programming, [he or she] has understood the quintessence of programming.”

— Edsger Dijkstra

5 Dijkstra [1972]

Page 6: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Class names

• Variable names

• Method names

• Parameter names

Reserve the term “identifier” for the grammar symbol

6

Some Uses of Names

Page 7: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

class Cloud {

public void rain(byte x, byte y) {

if (this.inBounds(x, y)) {

Meggy.setPixel(x, y, Meggy.Color.BLUE);

if (this.inBounds(x,(byte)(y+(byte)1))) {

Meggy.setPixel(x, (byte)(y+(byte)1), Meggy.Color.DARK);

} else {}

Meggy.delay(100);

this.rain(x, (byte)(y-(byte)1));

} else {}

}

public boolean inBounds(byte x, byte y) {

return ((byte)(0-1) < y) && (y < (byte)8);

}

}

How is x used in the following (from PA4raindrop.java)?

7

Symbols

Page 8: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

class C {

int x;

public int f(int x) { return x; }

public int g(int y) { return x; }

}

We’ll use pseudo-code to focus on the use of names like x

8

Symbols

Page 9: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

class D {

int x;

public int f(int y) {

C x = new C();

return x.f(1);

}

}

What about x in the following?

9

Symbols

Page 10: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

class D {

int x;

public int f(int y) {

C x = new C();

return x.f(1);

}

}

What about x in the following?

10

Symbols

How does this pseudo-code use f?

Page 11: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

class E {

C x;

public int f(int y) {

x = new C()

return x.x;

}

}

What do the occurrences of x denote?

11

Symbols

Q. Why would anyone write such a program? A. To test a compiler.

Page 12: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Scope Rules

12

Page 13: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

•  A declaration associates information with a name

• The scope rules of a language determine which declaration applies to an occurrence of a name

• The scope of a declaration is the portion of the program to which the declaration applies

Definitions

13

Scope of a Declaration

Page 14: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Shorthand: scope of a name x –  Short for “scope of a the declaration of the name x”

• Scope by itself – A portion of a program that is the scope of one or more declarations

Popular usage of the term scope

14

Scope

Page 15: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Static scope rules are based on the program text – The scope of a declaration can be determined at compile time – Otherwise, the language is said to have dynamic scope rules – Macro-expansion results in dynamic scope

• A block consists of declarations and statements –  Blocks are delimited by braces, {}, in C, Java, … –  Blocks can be nested – Does MeggyJava have blocks?

Most languages have static scope rules

15

Static Scope Rules

Page 16: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

class C

{

int x;

public int f(int x)

{

return x;

}

public int g(int y)

{

return x;

}

}

How many declarations of x?

16

Scope of a Declaration

Page 17: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Subscripts distinguish between roles of x

17

Scope of a Declaration

class C

{

int x1;

public int f(int x2)

{

return x2;

}

public int g(int y)

{

return x1;

}

}

Block B1

Block B2

Block B3

Page 18: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Block B2 is a hole in the scope of the declaration of x1

18

Hole in the Scope of a Declaration

Block B1

Block B2

Block B3

class C

{

int x1;

public int f(int x2)

{

return x2;

}

public int g(int y)

{

return x1;

}

}

Page 19: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Find the declaration of x by examining blocks inside out

19

Most Closely Nested Rule

Block B1

Block B2

Block B3

class C

{

int x1;

public int f(int x2)

{

return x2;

}

public int g(int y)

{

return x1;

}

}

Page 20: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Global scope – Top level declarations in C

• Named scopes –  For variable and method names in a class

• Package scopes –  Import a package in Java

• Unnamed scopes –  Blocks

In languages like C and Java

20

Examples of Scopes

Page 21: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Example: – Class C introduces a new scope for x, f, and g:

class C {

int x;

public int f(...) { ... }

public int g(...) { ... }

}

–  Now suppose y denotes an object of class C: y = new C()

– Then, y.x refers to variable x in the source text of class C

Classes introduce a new scope for their variables and methods

21

Explicit Access Control

Page 22: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• public – The scope rules just discussed for classes apply without restrictions

• private – Access to the declared variable is restricted to methods of the class

• protected – Access to the declared variable is restricted to methods of the class

and to the methods of any subclasses

The keywords public, private, protected control access

22

Explicit Access Control

Page 23: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Symbol Table Per Scope

23

Page 24: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Symbol Tables

• Kinds of information in a symbol table – Type information for static checking –  For named scopes, the identifiers in that named scope –  Layout information for storage at run time; e.g., for storage allocation – …

• Operations on symbol tables – Create a new table –  Put information in the current table – Get information from a chain of tables

24

Page 25: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Creating a new table object of class Env

public class Env {

private hashtable table;

protected Env previous;

public Env(Env p) {

table = new Hashtable() }

previous = p;

}

...

}

table is a chain of objects of class Env

25

Java Implementation of Symbol Tables

Page 26: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Get information from a chain of objects

public Symbol get(String s) {

for( Env e = this; e != null; e = e.previous ) {

Symbol found = (Symbol)(e.table.get(s));

if( found != null ) return found;

}

return null;

}

table is a chain of objects of class Env

26

Java Implementation of Symbol Tables

Page 27: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• How can we handle inheritance? – Use a symbol table per class – The symbol table for a subclass points to the�

table for the superclass

Create a new table object for a class

27

Handling Named Scopes

Page 28: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Type Checking�is a form of consistency checking

Ensures that the type of a construct matches the expected type. For example,

Meggy.setpixel

expects a triple of type

byte × byte × color

28

Page 29: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Consider the function inBounds public boolean inBounds(byte x, byte y) {

return ((byte)(0-1) < y) && (y < (byte)8);

}

•  It expects parameters and returns a value –  Parameter types (byte, byte) – Return value of type boolean

Extending type checking from variables to expressions

29

Type Checking

Page 30: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Function Signatures

• Consider function f–  Its parameter has type s, where s can be a tuple –  Its return type is t

• Then, the signature of f is s ! t

30

Page 31: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Basic Rule of Type Checking

•  If function f has signature s ! t and x has type s

• Then expression f(x) has type t

31

Page 32: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Basic Types –  boolean, byte, int, color –  void denotes the absence of a value

• Tuples –  If t1, t2, …, tn are types, then t1× t2× …× tn is a type representing a

tuple of values of types t1, t2, …, tn.

• Functions –  If s and t are types, then s ! t is a type expression – Thus, a function signature is a type expression

Type checking associates type expressions with expressions

32

Type Expressions

Page 33: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Examples: constructs and their type expressions

33

Type Expressions

int int × int ! int byte × byte ! boolean int × int ! boolean boolean × boolean ! boolean int ! byte

8

-

<

<

&&

(byte)

A function with more than one signature is said to be overloaded.

Page 34: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Expression ((byte)(0-1) < y) && (y < (byte)8)

34

An Expression Tree

&&  

<   <  

y   y  (byte)   (byte)  

8  -  

1  0  

Page 35: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Associate a type expression with each subexpression

35

Type Checking

&& : boolean  

< : boolean   < : boolean  

y : byte   y : byte  (byte) : byte   (byte) : byte  

8 : int  - : int  

1 : int  0 : int  

Page 36: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Treat while as a function with signature –  boolean × void ! void

• Similar treatment for other statement nodes

Allows uniform treatment of nodes in a syntax tree

36

Type Expressions for Statement Nodes

Page 37: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Lifetime

A consecutive sequence of steps at run time

37

Page 38: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Two-Stage Mapping of Names to Values

• The lifetime of a declaration – The consecutive sequence of steps during which the declared name

has�storage and a value

–  In other words, the state mapping is defined

• Lifetime does not equate to accessibility –  Example: a nested block may have another declaration of the name –  In other words, the environment may change

38

name storage value

environment state

Page 39: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

The value of x1 is inaccessible during the lifetime of x2

39

Scope and Lifetime of a Declaration

Block B1

Block B2

Block B3

class C

{

int x1;

public int f(int x2)

{

return x2;

}

public int g(int y)

{

return x1;

}

}

Page 40: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Activation Trees

Handling of local variables in recursive activations

40

Page 41: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• To sort array elements in the range m:n –  Pick a pivot element i –  Partition the elements into two groups: smaller and larger than the

pivot – Recursively quicksort the ranges m:i–1 and i+1:n –  Sort the whole array by calling quicksort with the lower and upper

bounds of the array

Function quicksort has parameters m and n and a local var i

41

A recursive function

smaller larger

m n i

Page 42: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Activations

• An activation of a function is an execution of the function body

• Activations can be nested –  If an activation of f initiates an activation of g, then that activation of

g is nested in that activation of f

42

Page 43: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

enter quicksort(1,9)

enter partition(1,9)

leave partition(1,9)

enter quicksort(1,3)

...

leave quicksort(1,3)

enter quicksort(5,9)

...

leave quicksort(5,9)

leave quicksort(1,9)

Parameters are in parentheses

43

Trace from an activation of quicksort

Page 44: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

Abbreviations: q for quicksort, p for partition

44

Activation Tree

p(5,9)   q(5,5)  

q(7,7)  p(7,9)   q(9,9)  

q(7,9)  

q(2,1)  p(2,3)   q(3,3)  

p(1,3)   q(1,0)   q(2,3)  

p(1,9)   q(1,3)   q(5,9)  

q(1,9)  

Page 45: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• We can use a stack to keep track of live activations – Called a run-time stack

• What does a local variable i in q denote? – What is its scope? – What is its lifetime?

Live activations when control reaches q(2,3)

45

Live Activations are Nested

q(2,3)  

q(1,3)  

q(1,9)  

Page 46: Symbol Tables - University of Arizona · Ravi Sethi, CSC 453, Fall 2016 • Basic Types – boolean, byte, int, color – void denotes the absence of a value • Tuples – If t 1,

Ravi Sethi, CSC 453, Fall 2016

• Static scope rules can be applied at compile time – We deal with the scope of a declaration of a name in the source text

• Symbol table per scope – Holds information that a declaration associates with a name –  Information collected in one phase can be used in another

• Type Checking – Associate a type expression with nodes in a syntax tree

• Lifetime is a run-time concept – We deal with the lifetime of an activation of a local variable

Key Points

46

Symbol Tables