LECTURE 14. Names, Scopes, and Bindings: Scopes

Similar documents
Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

6. Names, Scopes, and Bindings

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Names and Abstractions: What s in a Name?

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

G Programming Languages - Fall 2012

Lecture 16: Static Semantics Overview 1

Principles of Programming Languages

CSE 307: Principles of Programming Languages

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Syntax Errors; Static Semantics

MIDTERM REVIEW. Lectures 1-15

Names, Scopes, and Bindings II. Hwansoo Han

Programming Languages

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CSCI312 Principles of Programming Languages!

Chapter 5. Names, Bindings, and Scopes

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The role of semantic analysis in a compiler

Chapter 5 Names, Binding, Type Checking and Scopes

CS 314 Principles of Programming Languages

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

Concepts Introduced in Chapter 7

Programming Languages Third Edition. Chapter 7 Basic Semantics

Type Bindings. Static Type Binding

Principles of Programming Languages

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

NOTE: Answer ANY FOUR of the following 6 sections:

Run Time Environments

LECTURE 18. Control Flow

CS 314 Principles of Programming Languages. Lecture 13

Special Topics: Programming Languages

CSC 533: Organization of Programming Languages. Spring 2005

Imperative Programming

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Special Topics: Programming Languages

Lecture08: Scope and Lexical Address

Chapter 5 Names, Bindings, Type Checking, and Scopes

Memory Management and Run-Time Systems

Chapter 9 Subprograms

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Names, Bindings, Scopes

CS558 Programming Languages. Winter 2013 Lecture 3

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Today's Topics. CISC 458 Winter J.R. Cordy

G Programming Languages - Fall 2012

Programmiersprachen (Programming Languages)

Informatica 3 Marcello Restelli

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

MIDTERM EXAM (Solutions)

Informatica 3 Syntax and Semantics

Run-time Environments - 2

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 415 Midterm Exam Spring 2002

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

System Software Assignment 1 Runtime Support for Procedures

Implementing Subprograms

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

COP4020 Fall 2006 Final Exam

Chapter 10. Implementing Subprograms ISBN

Procedures and Run-Time Storage

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Implementing Subprograms

Weeks 6&7: Procedures and Parameter Passing

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Run-time Environments

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Run-time Environments

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Control Flow February 9, Lecture 7

Iterative Languages. Scoping

Chapter 10. Implementing Subprograms

Semantic Analysis. Compiler Architecture

Names, Scope, and Bindings

Procedures and Run-Time Storage

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CA Compiler Construction

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

CS 415 Midterm Exam Spring SOLUTION

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Chapter 3:: Names, Scopes, and Bindings

Run-Time Environments

Programming Languages

Scope, Functions, and Storage Management

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

CS 314 Principles of Programming Languages

Scope. Chapter Ten Modern Programming Languages 1

Transcription:

LECTURE 14 Names, Scopes, and Bindings: Scopes

SCOPE The scope of a binding is the textual region of a program in which a name-to-object binding is active. Nonspecifically, scope is a program region of maximal size in which no bindings change. Typically called a block the body of a module, class, subroutine, etc. In C and C++, we delimit a block with { }.

SCOPE Statically scoped language: the scope of bindings is determined at compile time. Used by almost all but a few programming languages. More intuitive than dynamic scoping. We can take a C program and know exactly which names refer to which objects at which points in the program solely by looking at the code. Dynamically scoped language: the scope of bindings is determined at run time. Used in Lisp (early versions), APL, Snobol, and Perl (selectively). Bindings depend on the flow of execution at runtime.

SCOPE The set of active bindings at any point in time is known as the referencing environment. Determined by scope rules. May also be determined by binding rules. There are two options for determining the reference environment: Deep binding: choice is made when the reference is first created. Shallow binding: choice is made when the reference is first used. Relevant for dynamically-scoped languages.

STATIC SCOPING The bindings between names and objects can be determined by examination of the program text. Scope rules of a program language define the scope of variables and subroutines, which is the region of program text in which a name-to-object binding is usable. Early Basic: all variables are global and visible everywhere Fortran 77: the scope of a local variable is limited to a subroutine; the scope of a global variable is the whole program text unless it is hidden by a local variable declaration with the same variable name. Algol 60, Pascal, and Ada: these languages allow nested subroutines definitions and adopt the closest nested scope rule bindings introduced in some scope are valid in all internally nested scopes unless hidden by some other binding to the same name.

CLOSEST NESTED SCOPE RULE To find the object referenced by a given name: Look for a declaration in the current innermost scope. If there is none, look for a declaration in the immediately surrounding scope, etc. In each of the function bodies to the right, what bindings are visible? def f1(a1): x = 1 def f2(a2): def f3(a3): print "x in f3: ", x def f4(a4): def f5(a5): x = 2

CLOSEST NESTED SCOPE RULE Some languages allow the outer binding to be accessed by using a scope resolution operator. In C++, ::x refers to the global declaration of x. In Python, global x refers to the global declaration of x and nonlocal x refers to nested, but non-global declarations. In Ada, a name may be prefixed by the scope in which it is declared. My_proc.x refers to the declaration of x inside of procedure My_proc.

STATIC SCOPING You can think of built-in and pre-defined objects as being defined in an invisible outer scope which surrounds the scope of global objects. The search for a binding will terminate at this outermost scope. Because of this, you can even create new global bindings for predefined objects, hiding their default binding (this creates a hole in the scope of the default binding).

STATIC SCOPING Using static scoping, what will this program print? a:integer procedure first a:=1 procedure second a:integer first() procedure main a:=2 second() write_integer(a)

STATIC SCOPING We ll get 1. The following are the statements we execute in order: a:integer main() a:=2 second() a:integer first() a:=1 write_integer(a) Bound to global a a:integer procedure first a:=1 procedure second a:integer first() procedure main a:=2 second() write_integer(a)

STATIC LINKS In the previous lecture, we saw how we can use offsets from the current frame pointer to access local objects in the current subroutine. What if I m referencing a local variable to an enclosing subroutine? How can I find the frame that holds this variable? The order of stack frames will not necessarily correspond to the lexical nesting. But the enclosing subroutine must appear somewhere on the stack as I couldn t have called the current subroutine without first calling the enclosing subroutine.

STATIC LINKS Consider this example. The sequence of function calls is: 1. f1 2. f2 3. f5 4. f4 5. f3 def f1(): x = 1 def f2(): def f3(): def f4(): f3() def f5(): f4() f5() f2() f1() # executes first!

STATIC LINKS The stack will look like this: f3 fp f4 f5 f2 def f1(): x = 1 def f2(): def f3(): def f4(): f3() def f5(): f4() f5() f2() f1() # executes first! f1

STATIC LINKS We will maintain information about the lexically surrounding subroutine by creating a static link between a frame and its parent. fp f3 f4 f5 f2 f1 def f1(): x = 1 def f2(): f2() f1() def f3(): def f4(): f3() def f5(): f4() f5() # executes first!

STATIC LINKS A parent is the most recent invocation of the lexically surrounding subroutine. The outermost subroutine has a null static link. fp f3 f4 f5 f2 f1 def f1(): x = 1 def f2(): f2() f1() def f3(): def f4(): f3() def f5(): f4() f5() # executes first!

STATIC LINKS A subroutine k levels deep can trace a static chain of length k. f3 fp f4 f5 f2 def f1(): x = 1 def f2(): def f3(): def f4(): f3() def f5(): f4() f5() f2() f1() # executes first! f1

STATIC LINKS So, to reference a variable in an enclosing subroutine, we can traverse the static chain until we find its definition. fp f3 f4 f5 f2 f1 def f1(): x = 1 def f2(): f2() f1() def f3(): def f4(): f3() def f5(): f4() f5() # executes first!

DYNAMIC SCOPING Scope rule: the "current" binding for a given name is the one encountered most recently during execution. Typically adopted in (early) functional languages that are interpreted. With dynamic scope: Name-to-object bindings cannot be determined by a compiler in general. Easy for interpreter to look up name-to-object binding in a stack of declarations. Generally considered to be a bad programming language feature. Hard to keep track of active bindings when reading a program text. Most languages are now compiled, or a compiler/interpreter mix.

DYNAMIC SCOPING Using dynamic scoping, what will this program print? a:integer procedure first a:=1 procedure second a:integer first() procedure main a:=2 second() write_integer(a)

DYNAMIC SCOPING Using dynamic scoping, what will this program print? a:integer main() a:=2 second() a:integer first() Bound to most recent a. a:=1 write_integer(a) a:integer procedure first a:=1 procedure second a:integer first() procedure main a:=2 second() write_integer(a) Output will be 2

DYNAMIC SCOPING IMPLEMENTATION Each time a subroutine is called, its local variables are pushed onto the stack with their name-to-object binding. When a reference to a variable is made, the stack is searched top-down for the variable's name-to-object binding. After the subroutine returns, the bindings of the local variables are popped. Different implementations of a binding stack are used in programming languages with dynamic scope, each with advantages and disadvantages. Difficulty: when to create the referencing environment for subroutines that can be treated as objects?

BINDING RULES FOR SUBROUTINES Deep binding: choice is made when the reference is first created. Shallow binding: choice is made when the call is made. What is the output if we use deep binding? int x = 10; function f(int a) { x = x + a; } function g(function h){ int x = 30; h(100); print(x); } function main(){ g(f); print(x); }

BINDING RULES Deep binding: choice is made when the reference is first created. Shallow binding: choice is made when the reference is first used. What is the output if we use deep binding? 30 110 What is the output if we use shallow binding? int x = 10; function f(int a) { x = x + a; } function g(function h){ int x = 30; h(100); print(x); } function main(){ g(f); print(x); }

BINDING RULES Deep binding: choice is made when the reference is first created. Shallow binding: choice is made when the reference is first used. What is the output if we use deep binding? 30 110 What is the output if we use shallow binding? 130 10 int x = 10; function f(int a) { x = x + a; } function g(function h){ int x = 30; h(100); print(x); } function main(){ g(f); print(x); }

STATIC AND DYNAMIC SCOPING EXAMPLE a:integer; procedure foo a = 10 goo() hoo() write(a) procedure goo a = 20 procedure hoo write(a) procedure main a:integer a = 30 foo() write(a) What is the output with static scoping? Dynamic scoping with deep bindings? Dynamic scoping with shallow bindings?

STATIC AND DYNAMIC SCOPING EXAMPLE a:integer; procedure foo a = 10 goo() hoo() write(a) procedure goo a = 20 procedure hoo write(a) procedure main a:integer a = 30 foo() write(a) Static 20 20 30 Dynamic Scoping 20 20 20

DYNAMIC SCOPING Deep binding: reference environment of older is established with the first reference to older, which is when it is passed as an argument to show. main(p) thres:=35 show(p, older) thres:integer thres:=20 older(p) return p.age>thres if <return value is true> write(p) thres:integer function older(p:person):boolean return p.age>thres procedure show(p:person, c:function) thres:integer thres:=20 if c(p) write(p) procedure main(p) thres:=35 show(p, older)

DYNAMIC SCOPING Shallow binding: reference environment of older is established with the call to older in show. main(p) thres:=35 show(p, older) thres:integer thres:=20 older(p) return p.age>thres if <return value is true> write(p) thres:integer function older(p:person):boolean return p.age>thres procedure show(p:person, c:function) thres:integer thres:=20 if c(p) write(p) procedure main(p) thres:=35 show(p, older)