Scope Rules Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 11.

Slides:



Advertisements
Similar presentations
Names, Scope, Memory, and Binding. Name, Scope, and Binding A name is exactly what you think it is – Usually think of identifiers but can be more general.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
(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.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
ISBN Chapter 10 Implementing Subprograms.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
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.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
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.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
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.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
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.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Programming Languages and Paradigms Imperative Programming.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 12 Support for Object-Oriented Programming.
Language Paradigms CS655.
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Chapter 3:: Names, Scopes, and Bindings
Run-Time Environments Chapter 7
Name Spaces: ALL versus OOL
Implementing Subprograms Chapter 10
Implementing Subprograms
Programming Language Concepts
CS 326 Programming Languages, Concepts and Implementation
Programming Language Principles
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
Structure of Programming Languages
Implementing Subprograms
Subprograms The basic abstraction mechanism.
Chapter 10: Implementing Subprograms Sangho Ha
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
PZ05A - Abstract data types
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
Abstract data types Programming Language Design and Implementation
Name Binding and Object Lifetimes
Programming Languages and Paradigms
Abstract data types Programming Language Design and Implementation
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Scope Rules Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 11

Definition Scope: the region of program text in which a binding holds. Static binding is prevalent in most modern PLs. New scope in each function. Activate bindings for parameters and locals. Deactivate bindings of globals obscured by locals. Deactivate bindings upon function return.

Example let Sqr x = x * x in let f a b = Sqr a + b in Print(f (let Sqr x = x+x in f 4 3)) ) In RPAL, this program prints = 38. If RPAL used dynamic scoping, the result would be = 30.

Terminology Referencing environment: the set of active bindings at a given point in a program's execution. Deep binding: the reference environment of a fcn/proc is determined when the function is created. Shallow binding: the reference environment of a fcn/proc is determined when the function is called.

Structure of Referencing Environments Normally, a stack suffices (C, C++, Java). If a procedure (not merely a pointer) can be passed as a parameter, the referencing environments are organized as a tree.

Example: Passing Procedures as Parameters in Pascal: program test(output); procedure dummy; begin end; procedure Q(procedure S; n:integer); procedure R; begin writeln(n) end; begin { Q } if n=1 then Q(R,0) else S; end; begin {main} Q(dummy,1) end. The output of this program is ???

Types of Static Scope Flat: (BASIC, Cobol) All name sin the same scope, all visible everywhere. Local/global (Fortan, C, Prolog) Only two referencing environments: current and global.

Types of Static Scope (cont’d) Nested procedures (Algol, Pascal, Ada, Modula, RPAL). Each procedure has its own scope, visible to itself and all procedures nested within it.

Types of Static Scope (cont’d) Modular scope (Modula, Ada, C++, Java). Scopes defined by modules, which explicitly export names to: The global scope (public) The scopes of subclasses (protected) Nowhere (private)

Nested Scoping Example (in pseudo-Pascal): proc A; var a; proc B; var b: proc C; var c; begin c:=b-1; if b=1 then B else a:=a-c; end;{ C } begin b:=a-4; if b<2 then C; else a:=0; end;{ B } begin a:=5; B; end;{ A } A can only see a. B can see a and b but not c. C can see a, b, and c. C can see many versions of b and a, depending on the calling sequence.

Access to non-locals Needed to handle nested scoping. Two methods: Static links Displays. Will cover displays (and contrast them) later.

Static Link Pointer to the most recent instance of the next statically enclosing procedure (see diagram). Each instance of B points to the most recent version of A Each instance of C points to the most recent version of B

Static Link (cont’d) Compiler keeps track of nesting levels: variable a is at level 1, variable b is at level 2, variable c is at level 3. To access a variable, subtract the variable's nesting depth from the current depth, and follow that many static links.

Examples For B to access a, depth[ B ]-depth[ a ] = 2-1= 1; follow 1 static link. For C to access b, depth[ C ]-depth[ b ] = 3-2= 1; follow 1 static link. For C to access a, depth[ C ]-depth[ a ] = 3-1= 2; follow 2 static links.

Block Scoping In the C language, execution blocks, delimited with {}, define scopes. { int t; t = a; a = b; { float t=3.14; printf("%f",t); } b = t; }

Block Scoping (cont’d) The variable's scope is limited to the {} block that contains its declaration. Handled like ordinary local variables: Space allocated in subroutine prologue (frame setup) Deallocated in subroutine epilogue (frame destruction). Compiler's symbol table keeps track of declarations.

Modular Scoping First languages to incorporate information hiding. Reduces "cognitive load" on programmers. CIA principle: if a code segment doesn't need to know about an object, it shouldn't. Tends to narrow (simplify) interfaces. Aids in abstraction.

Modular Scoping (cont’d) Modules allow encapsulation of objects. Objects inside the module are visible to each other. Objects inside are only visible outside if explicitly exported. Objects outside not visible inside unless imported.

Modular Scoping (cont’d) Various terms for modules: Clu: clusters Modula, Ada, Java: packages C++: namespaces

Example (Pseudo Modula) module Zorch; export A,b; var b:integer; var c:real: proc A();... end end Zorch; module Zip; import A; export D; var t:real; proc D();... end; end Zip; module Zap; import b,D; export Q; proc Q();... end; proc R();... end; end Zap; Procedure A can see b and c. Procedure D can see A and t, but not b or c. Procedures Q and R can see b and D, but not A, c, or t.

Module Types and Classes In Modula-2, Turing, and Ada83, a module can define only one instance of the object (module as manager). In Simula, Euclid, and ML, programmer can declare an arbitrary number of module objects (module as type).

Module Types and Classes (cont’d) Historically, this led to classes (Smalltalk, Eiffel, C++, Java), and object-orientation. Classes define types (abstraction). Classes encapsulate object data and methods (encapsulation). Classes hide information (information hiding). Classes facilitate polymorphism and inheritance (polymorphism).

Symbol Tables Compiler must keep track of declared names (type, variables, constants, etc.) Table stores mapping of names to attributes. Must enforce scope rules. Typical operations: Enter a new name, with relevant information (type, components, etc.) Lookup a name. Must return correct instance, in accordance with scope rules.

Symbol Tables (cont’d) Open_scope. New scope must allow visibility of names in outer scopes, and redeclaration of names in new scope. Close_scope. Probably non- destructively, without reclaiming space (need debugging info). Restore mapping of previous scope.

Symbol Tables (cont’d) Must allow forward declarations (name used before it is declared) Hash coded table, each entry a linked list of hash synonyms.

Example LeBlanc-Cook symbol table (see text book) Intrinsics added first (integer, real). Situation depicts environment in with statement. A2, F2 and T are synonyms.

Example (cont’d) Other mechanisms: Central Reference Tables. Used to keep track of run-time environments. Equivalent to RPAL's environment trees.

Binding of Reference Environments Needed when passing a function/proc as a parameter. Need subroutine closures: 4 items. 1.The fact that it is a function. 2.Functionality (prototype, parameters, etc.) 3.Pointer to body of fcn/proc. 4.Referencing environment (environment link in RPAL).

Binding of Reference Environments (cont’d) In C, functions are second-class. Can only pass function pointer (item 3) as a parameter. Fcns/procs are first class in Lisp, ML, Haskell, RPAL, but not in Modula, Ada, C, C++, or Java. Some would argue they are, but...

Overloading Multiple meanings for the same name, context dependent. Most languages allow it in some limited fashion, e.g. arithmetic operators ( a+b for ints and reals). In C++, one can overload both functions and operators. Can only overload fcns in Java (may change, more later).

Pitfalls in Language Design In Pascal, functions return values by assigning to the function name (no return statement). If the function name is redeclared, it becomes impossible to return a value. Example: function f: integer; var f:integer; begin f := 10; // won't return 10 end;

Pitfalls in Language Design (cont’d) Using an outer scope name, and then declaring it (maybe much later), is illegal in Pascal. Very expensive to check. const m = 0; procedure f; const n = m; { illegal use of m, } { m not yet declared.}... m = 0; { scope of m is all } { of f } end;

Pitfalls in Language Design (cont’d) The same applies to functions: procedure f;... end; procedure A; procedure B; begin f; {intent is to call } {outer f. } end; {error: f used before} {it's declared }... procedure f;... end;

Pitfalls in Language Design (cont’d) Some Pascal compilers (and most Pascal successors) specify that the scope of a declaration is limited to the remainder of (not the entire) block. Confusingly, Pascal allows use of pointer types before they are declared: type aptr = ^a; a = record data:...; next: aptr; end;

Pitfalls in Language Design (cont’d) Modula-3 changed this: scope of name is still the entire block, but names need not be declared before they are used. C, C++, Java have a mixed strategy: variables must be declared before they are used, but not type (class) names. Class names are visible over the entire containing module (package). Separate compilation (read in book).

Scope Rules Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 11