Scope.

Slides:



Advertisements
Similar presentations
Chapter 5 Names, Bindings, and Scopes
Advertisements

ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 10 Implementing Subprograms.
ISBN Lecture 05 Variable Attributes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Names, Bindings, Type Checking, and Scopes
Names, Bindings, and Scopes
Names, Bindings, and Scopes
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names, Bindings, Type Checking, and Scopes
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
1 CS Programming Languages Class 07 September 14, 2000.
Names, Bindings, Type Checking, and Scopes
COME Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
ISBN Chapter 5 Names, Bindings, and Scopes.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
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.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Names, Bindings, Type Checking, and Scopes
Type Checking, and Scopes
Structure of Programming Languages
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, and Scopes
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Scope, Visibility, and Lifetime
Scope.
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
Presentation transcript:

Scope

Scope of a variable The range of statements in which the variable is visible A variable is visible in a statement if it can be referenced in that statement A variable is local in a program unit (block) if it is declared there A variable is nonlocal if it is visible, but not declared in the program unit Scope rules of a language determine how references to names are associated with variables

Static Scope One method of binding names to nonlocal variables Determination of the scope of a variable can be accomplished prior to execution Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent

Static Scope continued Variables can be hidden from a unit by having a "closer" variable with the same name C++ and Ada allow access to these "hidden" variables In Ada: unit.name In C++: class_name::name

Static Scope Example Procedure Big is x : Integer; procedure Sub1 is begin … end; procedure Sub2 is … x …

Blocks Allow new static scope to be defined in the midst of executable code Allows for local variables with minimized scope { } of C-based languages void sub () { int count; … while ( … ) { int count = 0; count++; }

Declaration Order In some languages all data declarations in a function, except those in nested blocks, must appear at the beginning of the function C++ and Java, a declaration can appear anywhere a statement can appear Resulting scope is not strictly a compound statement or subprogram, since it begins at the declaration and ends at the end of the block in which it appears

Global Scope Definitions outside functions in a file create global variables Definitions and declarations Declarations specify types and other attributes, but do not cause allocation of storage Definitions specify attributes and cause allocation of storage Languages implement global scope in a variety of ways

PHP example local day is Wednesday global day is Monday global month is January $day = “Monday”; $month = “February”; function calendar() { $day = “Wednesday”; global $month; print “local day is $day <br />”; $gday = $GLOBALS[‘day’]; print “global day is $gday <br />”; print “global month is $month <br />”; } calendar();

Python Example day = “Monday” def tester(): print “The global day is:”, day tester(); The global day is: Monday

Python example (2) day = “Monday” def tester(): print “The global day is:”, day day = “Tuesday” print = “The new value of day is:”, day tester() UnboundLocalError

Python example (3) day = “Monday” def tester(): global day print “The global day is:”, day day = “Tuesday” print “The new value of day is :”, day tester() The global day is: Monday The new value of day is: Tuesday

Problems with Static Scope In many cases, allows more access to variables and subprograms than is necessary Program evolution may cause issues Restructuring changes Encourages global variables

Dynamic Scope Scope based on calling sequence, not spatial relationship Only determined at run time Procedure Big is x : Integer; procedure Sub1 is begin … end; procedure Sub2 is … x …

The trouble with dynamic scope All local variables of a subprogram are visible to any other executing subprogram Not possible to statically type check references to non-locals Decreased readability Efficiency concerns

Scope and Lifetime Can appear related … but are actually unrelated e.g. variable defined in a Java method that calls no other methods … but are actually unrelated e.g. use of static in C and C++ e.g. when subprograms are involved void printheader(){ … } void compute() { int sum; printheader();

Referencing Environment The collection of all names that are visible in the statement In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes

Ada Example 1 procedure Example is 2 A, B : Integer; 3 ... 4 procedure Sub1 is 5 X, Y : Integer; 6 begin 7 ... 8 end; 9 procedure Sub2 is 9 procedure Sub2 is 10 X : Integer; 11 ... 12 procedure Sub3 is 13 X: Integer; 14 begin 15 ... 16 end; 17 begin 18 ... 19 end; 20 begin 21 ... 22 end; “line” (Referencing Environment) “7” (X, Y of Sub1, A,B of Example) “15” (X of Sub3, A,B of Example) “18” (X of Sub2, A,B of Example)

Dynamic Referencing Environment In a dynamically-scoped language, it is the local variables plus all visible variables in all active subprograms A subprogram is active if its execution has begun but has not yet terminated

Dynamic scope example void sub1(){ int a, b; ... } void sub2(){ int b, c; sub1(); void main(){ int c, d; sub2(); Assume that the only functions calls are the following: main calls sub2, which calls sub1. “line” (Referencing Environment) “3” (a, b of sub1, c of sub2, d of main) “7” (b, c of sub2, d of main) “12” (c, d of main)

Named Constant A variable that is bound to a value only once Advantages: Readability e.g. PI instead of 3.141529 Program Reliability/Modifiability e.g. arrays and loops Binding of constants can be either static or dynamic e.g. Fortran95 requires that a named constant be initialized with a constant expression aka manifest constant e.g. Ada, C++ allow dynamic bindings of values to constants