semantic analysis - stanford university...semantic analysis ensure that the program has a...

222
Semantic Analysis

Upload: others

Post on 09-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Semantic Analysis

Page 2: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Announcements

● Programming Project 2 due Friday at 11:59PM.

● Written Assignment 2 due Wednesday at 5:00PM.

● Questions?● Stop by office hours!● Ask on Piazza!● Email the course staff!

Page 3: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Where We Are

Syntax Analysis

Lexical Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 4: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Where We Are

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Syntax-tic!Syntax-tic!

Syntax Analysis

Lexical Analysis

Page 5: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Where We Are

Lexical Analysis

Semantic Analysis

Syntax Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 6: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Not Symantec Analysis

Page 7: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Where We Are

● Program is lexically well-formed:● Identifiers have valid names.● Strings are properly terminated.● No stray characters.

● Program is syntactically well-formed:● Class declarations have the correct structure.● Expressions are syntactically valid.

● Does this mean that the program is legal?

Page 8: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

A Short Decaf Programclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }

Page 9: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

A Short Decaf Programclass MyClass implements MyInterface { string myInteger; void doSomething() { int[] x = new string; x[5] = myInteger * y; } void doSomething() { } int fibonacci(int n) { return doSomething() + fibonacci(n – 1); } }

Interface not declared

Wrong type

Variable not declared

Can't multiply strings

Can't redefine functions

Can't add voidNo main function

Page 10: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Semantic Analysis

● Ensure that the program has a well-defined meaning.

● Verify properties of the program that aren't caught during the earlier phases:● Variables are declared before they're used.● Expressions have the right types.● Arrays can only be instantiated with NewArray.● Classes don't inherit from nonexistent base classes● …

● Once we finish semantic analysis, we know that the user's input program is legal.

Page 11: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Challenges in Semantic Analysis

● Reject the largest number of incorrect programs.

● Accept the largest number of correct programs.

Page 12: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Validity versus Correctness

int main() { string x; if (false) { x = 137; }}

Page 13: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Validity versus Correctness

int main() { string x; if (false) { x = 137; }} Safe; can't

happen

Page 14: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Validity versus Correctness

int Fibonacci(int n) { if (n <= 1) return 0;

return Fibonacci(n – 1) + Fibonacci(n – 2);}

int main() { Print(Fibonacci(40));}

Page 15: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Validity versus Correctness

int Fibonacci(int n) { if (n <= 1) return 0;

return Fibonacci(n – 1) + Fibonacci(n – 2);}

int main() { Print(Fibonacci(40));}

Incorrect, should be “return n;”

Page 16: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Challenges in Semantic Analysis

● Reject the largest number of incorrect programs.

● Accept the largest number of correct programs.

● Do so quickly.

Page 17: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Challenges in Semantic Analysis

● Reject the largest number of incorrect programs.

● Accept the largest number of correct programs.

● Do so quickly.

Page 18: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Other Goals of Semantic Analysis

● Gather useful information about program for later phases:● Determine what variables are meant by each

identifier.● Build an internal representation of

inheritance hierarchies.● Count how many variables are in scope at

each point.

Page 19: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Why can't we just do this during parsing?

Page 20: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Limitations of CFGs

● Using CFGs:● How would you prevent duplicate class

definitions?● How would you differentiate variables of one type

from variables of another type?● How would you ensure classes implement all

interface methods?

For most programming languages, these are provably impossible.

Use the pumping lemma for context-free languages.

Page 21: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Limitations of CFGs

● Using CFGs:● How would you prevent duplicate class

definitions?● How would you differentiate variables of one type

from variables of another type?● How would you ensure classes implement all

interface methods?

● For most programming languages, these are provably impossible.● Use the pumping lemma for context-free

languages, or Ogden's lemma.

Page 22: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Implementing Semantic Analysis

● Attribute Grammars● Augment bison rules to do checking during

parsing.● Approach suggested in the Compilers book.● Has its limitations; more on that later.

● Recursive AST Walk● Construct the AST, then use virtual functions

and recursion to explore the tree.● The approach we'll take in this class.

Page 23: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Overview for this Week

● Scope-Checking (Today)● How can we tell what object a particular

identifier refers to?● How do we store this information?

● Type-Checking (Wednesday / Friday)● How can we tell whether expressions have

valid types?● How do we know all function calls have valid

arguments?

Page 24: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scope

Page 25: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Not This

Page 26: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

What's in a Name?

● The same name in a program may refer to fundamentally different things:

● This is perfectly legal Java code:

public class A { char A; A A(A A) { A.A = 'A'; return A((A) A); }}

Page 27: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

What's in a Name?

● The same name in a program may refer to fundamentally different things:

● This is perfectly legal Java code:

public class A { char A; A A(A A) { A.A = 'A'; return A((A) A); }}

Page 28: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

What's in a Name?

● The same name in a program may refer to completely different objects:

● This is perfectly legal C++ code:int Awful() { int x = 137; { string x = "Scope!" if (float x = 0) double x = x; } if (x == 137) cout << "Y";}

Page 29: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

What's in a Name?

● The same name in a program may refer to completely different objects:

● This is perfectly legal C++ code:int Awful() { int x = 137; { string x = "Scope!" if (float x = 0) double x = x; } if (x == 137) cout << "Y";}

Page 30: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scope

● The scope of an entity is the set of locations in a program where that entity's name refers to that entity.

● The introduction of new variables into scope may hide older variables.

● How do we keep track of what's visible?

Page 31: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables

● A symbol table is a mapping from a name to the thing that name refers to.

● As we run our semantic analysis, continuously update the symbol table with information about what is in scope.

● Questions:● What does this look like in practice?● What operations need to be defined on it?● How do we implement it?

Page 32: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Page 33: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

Page 34: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

Page 35: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0

Page 36: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0

Page 37: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

Page 38: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

Page 39: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

Page 40: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 41: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 42: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 43: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x, y, z); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 44: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 45: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 46: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 47: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 48: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 49: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z = y; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 50: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 51: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x = z; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 52: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 53: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 54: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 55: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 56: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 57: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 58: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 59: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 60: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x, y, z);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 61: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 62: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 63: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 64: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x, y, z);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 65: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 66: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

y 9

Page 67: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 68: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x, y, z);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 69: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 70: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }

Symbol Table

x 0z 1

x 2y 2

x 5z 5

Page 71: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 72: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 73: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }

Symbol Table

x 0z 1

x 2y 2

Page 74: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }

Symbol Table

x 0z 1

Page 75: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Tables: The Intuition

0: int x = 137; 1: int z = 42; 2: int MyFunction(int x, int y) { 3: printf("%d,%d,%d\n", x@2, y@2, z@1); 4: { 5: int x, z; 6: z@5 = y@2; 7: x@5 = z@5; 8: { 9: int y = x@5;10: {11: printf("%d,%d,%d\n", x@5, y@9, z@5);12: }13: printf("%d,%d,%d\n", x@5, y@9, z@5);14: }15: printf("%d,%d,%d\n", x@5, y@2, z@5);16: }17: }

Symbol Table

x 0z 1

Page 76: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Symbol Table Operations

● Typically implemented as a stack of maps.● Each map corresponds to a particular scope.● Stack allows for easy “enter” and “exit”

operations.● Symbol table operations are

● Push scope: Enter a new scope.● Pop scope: Leave a scope, discarding all

declarations in it.● Insert symbol: Add a new entry to the current

scope.● Lookup symbol: Find what a name corresponds to.

Page 77: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Using a Symbol Table

● To process a portion of the program that creates a scope (block statements, function calls, classes, etc.)● Enter a new scope.● Add all variable declarations to the symbol table.● Process the body of the block/function/class.● Exit the scope.

● Much of semantic analysis is defined in terms of recursive AST traversals like this.

Page 78: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

Page 79: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Page 80: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

Page 81: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

Page 82: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

Page 83: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

Page 84: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

Page 85: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

Page 86: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

Page 87: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

Page 88: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

Page 89: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

Page 90: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

Page 91: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

Page 92: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

Page 93: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

Page 94: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6

Page 95: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6

Page 96: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6

Page 97: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6

Page 98: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6

Page 99: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6 w 9

Page 100: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6 w 9

Page 101: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6 w 9

Page 102: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Another View of Symbol Tables

0: int x; 1: int y; 2: int MyFunction(int x, int y) 3: { 4: int w, z; 5: { 6: int y; 7: } 8: { 9: int w;10: }11: }

Root Scope

x 0

y 1

xy 2

2

wz 4

4

y 6 w 9

Page 103: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Spaghetti Stacks

● Treat the symbol table as a linked structure of scopes.

● Each scope stores a pointer to its parents, but not vice-versa.

● From any point in the program, symbol table appears to be a stack.

● This is called a spaghetti stack.

Page 104: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Why Two Interpretations?

● Spaghetti stack more accurately captures the scoping structure.

● Spaghetti stack is a static structure; explicit stack is a dynamic structure.

● Explicit stack is an optimization of a spaghetti stack; more on that later.

Page 105: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping in Object-Oriented Languages

Page 106: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

Page 107: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

Root Scope

Page 108: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

Root Scope

Base

publicBaseInt 1baseInt 2

Page 109: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Page 110: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

Page 111: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

>

Page 112: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

>

Page 113: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> doSomething

Page 114: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> doSomething

Page 115: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 doSomething

Page 116: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 doSomething

Page 117: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2

doSomething

Page 118: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2

doSomething

Page 119: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2 3

doSomething

Page 120: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2 3

doSomething

Page 121: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2 3

doSomething

publicBaseInt 6

Page 122: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2 3

doSomething

publicBaseInt 6

Page 123: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2 3 6

doSomething

publicBaseInt 6

Page 124: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2 3 6

doSomething

publicBaseInt 6

Page 125: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Inheritance

public class Base { public int publicBaseInt = 1; protected int baseInt = 2;}

public class Derived extends Base { public int derivedInt = 3; public int publicBaseInt = 4;

public void doSomething() { System.out.println(publicBaseInt); System.out.println(baseInt); System.out.println(derivedInt);

int publicBaseInt = 6; System.out.println(publicBaseInt); }}

Root Scope

Base

publicBaseInt 1baseInt 2

Derived

derivedInt 3publicBaseInt 4

> 4 2 3 6

doSomething

publicBaseInt 6

Typically the scope would also contain the names “Base” and

“Derived,” along with the function name “doSomething.” For

simplicity, I've left these out.

Typically the scope would also contain the names “Base” and

“Derived,” along with the function name “doSomething.” For

simplicity, I've left these out.

Page 126: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Inheritance and Scoping

● Typically, the scope for a derived class will store a link to the scope of its base class.

● Looking up a field of a class traverses the scope chain until that field is found or a semantic error is found.

Page 127: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Page 128: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Page 129: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

Page 130: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

Page 131: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

>

Page 132: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

>

Page 133: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

> 3

Page 134: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

> 3

Page 135: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

> 3 2

Page 136: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

> 3 2

Page 137: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

> 3 2 1

Page 138: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base { public int value = 2;

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

value 2

doSomething

value 3

Locals

this

super

> 3 2 1

Page 139: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

Page 140: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

>

Page 141: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

>

Page 142: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

> 3

Page 143: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

> 3

Page 144: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

> 3 1

Page 145: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

> 3 1

Page 146: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

> 3 1 1

Page 147: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Explicit Disambiguation

public class Base { public int value = 1;}

public class Derived extends Base {

public void doSomething() { int value = 3; System.out.println(value); System.out.println(this.value); System.out.println(super.value); }}

Root Scope

Base

value 1

Derived

doSomething

value 3

Locals

this

super

> 3 1 1

Page 148: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Disambiguating Scopes

● Maintain a second table of pointers into the scope stack.

● When looking up a value in a specific scope, begin the search from that scope.

● Some languages allow you to jump up to any arbitrary base class (for example,C++).

Page 149: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping in Practice

Page 150: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping in C++ and Java

class A {public: /* … */

private: B* myB};

class B {public: /* … */

private: A* myA;};

class A { private B myB;};

class B { private A myA;};

Page 151: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping in C++ and Java

class A {public: /* … */

private: B* myB};

class B {public: /* … */

private: A* myA;};

class A { private B myB;};

class B { private A myA;};

Error: B not declared

Perfectly fine!

Page 152: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Single- and Multi-Pass Compilers

● Our predictive parsing methods always scan the input from left-to-right.● LL(1), LR(0), LALR(1), etc.

● Since we only need one token of lookahead, we can do scanning and parsing simultaneously in one pass over the file.

● Some compilers can combine scanning, parsing, semantic analysis, and code generation into the same pass.● These are called single-pass compilers.

● Other compilers rescan the input multiple times.● These are called multi-pass compilers.

Page 153: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Single- and Multi-Pass Compilers

● Some languages are designed to support single-pass compilers.● e.g. C, C++.

● Some languages require multiple passes.● e.g. Java, Decaf.

● Most modern compilers use a huge number of passes over the input.

Page 154: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping in Multi-Pass Compilers

● Completely parse the input file into an abstract syntax tree (first pass).

● Walk the AST, gathering information about classes (second pass).

● Walk the AST checking other properties (third pass).

● Could combine some of these, though they are logically distinct.

Page 155: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

Page 156: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

Root Scope

Page 157: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

Root Scope

Page 158: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

Root Scope

Page 159: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

Root Scope

Page 160: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

Root Scope

Page 161: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {public: int x;};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

Root Scope

Page 162: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {public: int x;};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

x

Root Scope

Page 163: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {public: int x;};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

x

Root Scope

Page 164: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {public: int x;};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

x

Ambiguous – which x?

Root Scope

Page 165: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {public: int x;};

class C: public A, public B {public: void doSomething() { cout << A::x << endl; }}

A

x

B

C

x

Root Scope

Page 166: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {public: int x;};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

x

Root Scope

Page 167: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

x

Root Scope

Page 168: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

Root Scope

Page 169: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

int x;

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

Root Scope

Page 170: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

int x;

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

Root Scope

x

Page 171: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Scoping with Multiple Inheritance

int x;

class A {public: int x;};

class B {

};

class C: public A, public B {public: void doSomething() { cout << x << endl; }}

A

x

B

C

Root Scope

x

Page 172: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

(Simplified) C++ Scoping Rules

● Inside of a class, search the entire class hierarchy to see the set of names that can be found.● This uses the standard scoping lookup.

● If only one name is found, the lookup succeeds unambiguously.

● If more than one name is found, the lookup is ambiguous and requires disambiguation.

● Otherwise, restart the search from outside the class.

Page 173: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

Page 174: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Static and Dynamic Scoping

● The scoping we've seen so far is called static scoping and is done at compile-time.● Names refer to lexically related variables.

● Some languages use dynamic scoping, which is done at runtime.● Names refer to the variable with that name

that is most closely nested at runtime.

Page 175: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Page 176: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

>

Page 177: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

>

Page 178: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

>

Page 179: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

>

Page 180: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

>

Page 181: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

Page 182: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

Page 183: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

Page 184: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

Page 185: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

Page 186: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

Page 187: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

Page 188: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

x 0

Page 189: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

x 0

Page 190: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

x 0

Page 191: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

x 0

Page 192: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 >

x 0

Page 193: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

x 0

Page 194: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

x 0

Page 195: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

x 0

Page 196: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

x 0

Page 197: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

Page 198: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

Page 199: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

Page 200: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

Page 201: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

Page 202: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

Page 203: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

Page 204: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

Page 205: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

Page 206: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

Page 207: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

x 0

Page 208: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

x 0

Page 209: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

x 0

Page 210: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

x 0

Page 211: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 >

y 0

x 0

Page 212: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

y 0

x 0

Page 213: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

y 0

x 0

Page 214: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

y 0

x 0

Page 215: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

y 0

x 0

Page 216: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

y 0

Page 217: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

y 0

Page 218: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

Page 219: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping

int x = 137;int y = 42;void Function1() { Print(x + y);}void Function2() { int x = 0; Function1();}void Function3() { int y = 0; Function2();}Function1();Function2();Function3();

Symbol Table

x 137y 42

> 179 > 42 > 0 >

Page 220: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Dynamic Scoping in Practice

● Examples: Perl, Common LISP.● Often implemented by preserving symbol

table at runtime.● Often less efficient than static scoping.

● Compiler cannot “hardcode” locations of variables.

● Names must be resolved at runtime.

Page 221: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Summary

● Semantic analysis verifies that a syntactically valid program is correctly-formed and computes additional information about the meaning of the program.

● Scope checking determines what objects or classes are referred to by each name in the program.

● Scope checking is usually done with a symbol table implemented either as a stack or spaghetti stack.

● In object-oriented programs, the scope for a derived class is often placed inside of the scope of a base class.

● Some semantic analyzers operate in multiple passes in order to gain more information about the program.

● In dynamic scoping, the actual execution of a program determines what each name refers to.

● With multiple inheritance, a name may need to be searched for along multiple paths.

Page 222: Semantic Analysis - Stanford University...Semantic Analysis Ensure that the program has a well-defined meaning. Verify properties of the program that aren't caught during the earlier

Next Time

● Type Checking● Types as a proof system.● Static and dynamic types.● Types as a partial order.