Scope.

Slides:



Advertisements
Similar presentations
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
ISBN Chapter 10 Implementing Subprograms.
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
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 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
Scope.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
More High-Level Concepts
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
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.
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.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Subprograms - implementation
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Names, Scope, and Bindings Programming Languages and Paradigms.
ISBN Chapter 10 Implementing Subprograms.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Advanced Programming in C
Implementing Subprograms
More High-Level Concepts
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Chapter 4 Variables & Binding
Type Checking, and Scopes
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Names, Scopes, and Bindings: Scopes
For Loops October 12, 2017.
Names and Binding Imperative programming has instructions that manipulate the “state” of the process the “state” is the collection of variables and their.
Scope of Variables.
Scope, Visibility, and Lifetime
An Introduction to Java – Part I, language basics
Scope (visibility) scope in C is simple:
Implementing Subprograms
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Implementing Subprograms
Names and Binding In Text: Chapter 5.
ECE 103 Engineering Programming Chapter 12 More C Statements
Names, Bindings, and Scopes
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
CSC 533: Organization of Programming Languages Spring 2007
Implementing Subprograms
Types and Related Issues
Implementing Subprograms
Presentation transcript:

Scope

Scope Variable scope defines the range of statements over which a variable is visible Local variables of a program unit X are variables that are declared within X and therefore visible within X Non-local variables of a program unit X are variables that are visible but not declared within X

Static Scope Static scope indicates that the scope of a variable can be determined before execution How? Search local declarations first, then search in increasingly larger enclosing scopes, until a valid declaration is found Variable x becomes hidden Not valid in Java or C# main() { /** C **/ int x = 5; printf("%d\n", x); { int x = 10; int y = 20; int x = 15; int z = 20; }

Static Scope Static scope indicates that the scope of a variable can be determined before execution JavaScript and PHP do not support nested static scopes But JavaScript and PHP do support function-level scope var abc = false; if ( s == "HELLO" ) { abc = true; var abc = 500; document.write("abc is " + abc) } document.write("abc is still " + abc)

Dynamic Scoping Dynamic scoping defines a variable’s scope based on the calling sequences of subprograms Used in APL, SNOBOL4, early versions of LISP procedure Main is X : Integer; procedure Sub1 is begin -- of Sub1 ... X ... end; -- of Sub1 procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Main end; -- of Main Call Main, which calls Sub1 Call Main, which calls Sub2, which then calls Sub1 scope of X can only be determined at run time

repeat using dynamic scoping Problem 8 Given this Ada code: Assume execution of this code is Main calls Sub1 Sub1 calls Sub2 Sub2 calls Sub3 Using static scoping, which X is referenced in Sub1? In Sub2? In Sub3? procedure Main is X : Integer; procedure Sub3; -- allows Sub1 to -- to call Sub3 procedure Sub1 is procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main repeat using dynamic scoping

SOLUTIONS Problem 8 Given this Ada code: Assume execution of this code is Main calls Sub1 Sub1 calls Sub2 Sub2 calls Sub3 Using static scoping Sub1 references Sub1.X Sub2 references Sub1.X Sub3 references Main.X procedure Main is X : Integer; procedure Sub3; -- allows Sub1 to -- to call Sub3 procedure Sub1 is procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main

SOLUTIONS Problem 8 Given this Ada code: Assume execution of this code is Main calls Sub1 Sub1 calls Sub2 Sub2 calls Sub3 Using dynamic scoping Sub1 references Sub1.X Sub2 references Sub1.X Sub3 references Sub1.X procedure Main is X : Integer; procedure Sub3; -- allows Sub1 to -- to call Sub3 procedure Sub1 is procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main

Problem 9 Given this Ada code: Assume program execution using static-scoping rules What is the output of this program? What is the output using dynamic-scoping rules? procedure Main is X : Integer; procedure SubA is begin -- of SubA Put(X); -- i.e. output X end; -- of SubA procedure SubB is begin -- of SubB X := 10; SubA end; -- of SubB begin -- of Main X := 5; SubB end; -- of Main

SOLUTIONS Problem 9 Given this Ada code: Assume program execution using static-scoping rules What is the output of this program? 5 What is the output using dynamic-scoping? 10 procedure Main is X : Integer; procedure SubA is begin -- of SubA Put(X); -- i.e. output X end; -- of SubA procedure SubB is begin -- of SubB X := 10; SubA end; -- of SubB begin -- of Main X := 5; SubB end; -- of Main

Problem 10 Given this Ada code: procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; procedure Sub2 is A, B, Z : Integer; begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is A, X, W : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main Given this Ada code: Assume program execution using static-scoping rules For Sub1, Sub2, and Sub3, list the visible variables and the program units within which they are declared

SOLUTIONS Problem 10 Given this Ada code: procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; procedure Sub2 is A, B, Z : Integer; begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is A, X, W : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main Given this Ada code: Using static-scoping rules, list visible variables and the program units in which they are declared Sub1: A (Sub1), Y (Sub1), Z (Sub1), X (Main) Sub2: A (Sub2), B (Sub2), Z (Sub2), Y (Sub1), X (Main) Sub3: A (Sub3), X (Sub3), W (Sub3), Y (Main), Z (Main)

Problem 11 Given this Ada code: procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, X, W : Integer; procedure Sub3 is A, B, Z : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Sub2 end; -- of Sub2 begin -- of Main end; -- of Main Given this Ada code: Assume program execution using static-scoping rules For Sub1, Sub2, and Sub3, list the visible variables and the program units within which they are declared

SOLUTIONS Problem 11 Given this Ada code: procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, X, W : Integer; procedure Sub3 is A, B, Z : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Sub2 end; -- of Sub2 begin -- of Main end; -- of Main Given this Ada code: Using static-scoping rules, list visible variables and the program units in which they are declared Sub1: A (Sub1), Y (Sub1), Z (Sub1), X(Main) Sub2: A (Sub2), X (Sub2), W (Sub2), Z (Main), Y (Main) Sub3: A (Sub3), B (Sub3), Z (Sub3), X (Sub2), W (Sub2), Y (Main)

Problem 12 Given this C code: For each of the marked points A, B, C, and D, list each visible variable and the number of the declaration statement void doSomething() { int a, b, c; /** 1 **/ ... while ( ... ) int b, c, d; /** 2 **/ ... Point A int c, d, e; /** 3 **/ ... Point B } ... Point C ... Point D

SOLUTIONS Problem 12 Given this C code: For points A, B, C, and D, list each visible variable and the number of the declaration statement A: a (1), b (2), c (2), d (2) B: a (1), b (2), c (3), d (3), e (3) C: a (1), b (2), c (2), d (2) D: a (1), b (1), c (1) void doSomething() { int a, b, c; /** 1 **/ ... while ( ... ) int b, c, d; /** 2 **/ ... Point A int c, d, e; /** 3 **/ ... Point B } ... Point C ... Point D

Example Given this C-like code: Assume dynamic-scoping rules Given the following call sequences, what variables are visible during execution of the last function called ? Also specify the name of the function in which each variable was defined main() calls f1(), which calls f3() main() calls f2(), which calls f3(), which calls f1() main() calls f3(), which calls f1() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

SOLUTIONS Example Given this C-like code: Assume dynamic-scoping rules Given the following call sequences, what variables are visible during execution of the last function called ? main() calls f1(), which calls f3() In f3(): d, e, f declared in f3() b, c declared in f1() a declared in main() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

SOLUTIONS Example Given this C-like code: Assume dynamic-scoping rules Given the following call sequences, what variables are visible during execution of the last function called ? main() calls f2(), which calls f3(), which calls f1() In f1(): b, c, d declared in f1() e, f declared in f3() a declared in main() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

SOLUTIONS Example Given this C-like code: Assume dynamic-scoping rules Given the following call sequences, what variables are visible during execution of the last function called ? main() calls f3(), which calls f1() In f1(): b, c, d declared in f1() e, f declared in f3() a declared in main() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

Problem 14 Given this Ada code: Assume dynamic-scoping rules procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, B, Z : Integer; begin -- of Sub2 end; -- of Sub2 procedure Sub3 is A, X, W : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main Given this Ada code: Assume dynamic-scoping rules Given the following call sequences, what variables are visible during execution of the last function called ? Also specify the name of the function in which each variable was defined main() calls Sub1(), which calls Sub2(), which calls Sub3() main() calls Sub1() , which calls Sub3() main() calls Sub2(), which calls Sub3() , which calls Sub3()