Names, Scopes, and Bindings: Scopes

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Scope and Symbol Table.
Advertisements

COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Programming Languages and Paradigms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
CSC321: Programming Languages Names Chapter 4: Names 4.1 Syntactic Issues 4.2 Variables 4.3 Scope 4.4 Symbol Table 4.5 Resolving References 4.6 Dynamic.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
Scope.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos.
Basic Semantics Associating meaning with language entities.
Runtime Organization.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
COMP3190: Principle of Programming Languages
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
ISBN Chapter 10 Implementing Subprograms.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Advanced Programming in C
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Names.
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Names, Binding, and Scope
CSE 3302 Programming Languages
Scope of Variables.
Scope, Visibility, and Lifetime
Implementing Subprograms
COP4020 Programming Languages
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
Implementing Subprograms
Scope.
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Run Time Environments 薛智文
CSE 3302 Programming Languages
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
CSC 533: Programming Languages Spring 2018
Implementing Subprograms
CSC 533: Programming Languages Spring 2019
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Names, Scopes, and Bindings: Scopes 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 def f1(a1): x = 1 def f2(a2): def f3(a3): print "x in f3: ", x def f4(a4): def f5(a5): x = 2 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?

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 a:integer procedure first   a:=1 procedure second   a:integer   first() procedure main   a:=2   second()   write_integer(a) Using static scoping, what will this program print?

Static scoping a:integer procedure first   a:=1 procedure second   a:integer   first() procedure main   a:=2   second()   write_integer(a) 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

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: def f1(): x = 1 def f2(): print x def f3(): print x def f4(): f3() def f5(): f4() f5() f2() f1() # executes first! Consider this example. The sequence of function calls is: f1 f2 f5 f4 f3

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

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

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

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

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

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 a:integer procedure first   a:=1 procedure second   a:integer   first() procedure main   a:=2   second()   write_integer(a) Using dynamic scoping, what will this program print?

Dynamic scoping a:integer procedure first   a:=1 procedure second   a:integer   first() procedure main   a:=2   second()   write_integer(a) Using dynamic scoping, what will this program print? a:integer main()             a:=2             second()           a:integer         first()            a:=1   write_integer(a) Bound to most recent 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 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); } 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?

Binding rules 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); } 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?

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

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 Dynamic Scoping 20 20 30 20

Dynamic scoping 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) 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)

Dynamic scoping 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) 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)