Scope, Visibility, and Lifetime

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Programming Languages and Paradigms
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
CSC 533: Organization of Programming Languages Spring 2005
Chapter 5 Basic Semantics
Names, Bindings, Type Checking, and Scopes
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 4: Names Fall 2009 Marco Valtorta.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Scope.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
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.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
Names. 2 Variables  binding is an association between an entity (such as a variable) and a property (such as its value). A binding is static if the association.
1 CS Programming Languages Class 15 October 17, 2000.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
COMP3190: Principle of Programming Languages
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
1 CSC 533: Programming Languages Spring 2014 Language features and issues  variables & bindings  data types primitive complex/structured  expressions.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
Advanced Programming in C
Name Spaces: ALL versus OOL
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
Chapter 4 Variables & Binding
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Run-Time Storage Organization
Names.
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
CSC 533: Programming Languages Spring 2015
Scope Rules and Storage Types
Scope.
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Scope Rules Of Variables
Names, Bindings, and Scopes
Abstract data types Programming Language Design and Implementation
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
CSC 533: Organization of Programming Languages Spring 2007
CSC 533: Programming Languages Spring 2019
Types and Related Issues
Presentation transcript:

Scope, Visibility, and Lifetime

Variables can be bound to a scope either statically or dynamically. Scope, Visibility, and Lifetime: To reuse the same Identifier within a program requires the use of scope, hiding, and visibility. The scope of a variable is the range of program statements that can access that variable. A variable is visible within its scope and invisible or hidden outside it. Variables can be bound to a scope either statically or dynamically.

Static Scope: Static Scope defines the scope of a variable in terms of the lexical structure of a program. Using Static Scope each reference to a variable is statically bound to a particular (implicit or explicit) variable declaration. All variable references can be resolved by looking at the program’s source code and is independent of execution. Static scope rules are used by most traditional imperative programming languages (examples?).

Dynamic Scope: Dynamic Scope defines the scope of a variable in terms of program execution. Each variable declaration extends its effect over all subsequent statement execution, until a new declaration for the identifier is encountered. Dynamic scope rules are easy to implement but have drawbacks (such as?). Dynamic scope is most often used by interpreted languages; APL, LISP, and SNOBOL are examples.

The action that acquires storage for a variable is called allocation. Lifetime: The lifetime of a variable is the interval of time in which storage is bound to the variable. The action that acquires storage for a variable is called allocation. Some languages allocate storage before run-time. This is called static allocation. Others allocate storage at run-time either using explicit requests (malloc, new) or automatically upon entering a variable’s scope. This is called dynamic allocation. Languages may use both methods.

The scope of variables can be global or local. Identifier reuse: The scope of variables can be global or local. A global variable’s scope includes all the statements in a program. The scope of a local variable includes only statements inside the function in which it is declared. The same identifier can be reused inside different functions to name different variables. A name is local if it is declared in the current scope, and it is global if declared in an outer scope.

A global identifier can be redeclared inside of a function. Identifier reuse: A global identifier can be redeclared inside of a function. The new, inner declaration is said to hide the old, outer declaration (what effect does this have?). Some languages provide a scope resolution mechanism for referencing hidden variables. Some languages provide explicit hiding and exporting of variables and functions/methods in order to control visibility (examples?).

Example Program with Functions and Parameters Figure 4.5 Variables i and j as well as parameters x and y are local to A. Variables declared in B and main are not visible in A. The variables h and i are global. h is visible in A but i is hidden by the local declaration of i.

What constitutes a scope varies among languages. What is a Scope? What constitutes a scope varies among languages. Some allow scopes to be nested; every block or statement can contain declarations. Some allow declarations anywhere within a block, some only prior to any executable statements. Some allow function declarations to be nested (Pascal does, C does not). Some are even more complicated with special rules for some types of statements (Ada).

C/C++ allows declarations to be present in statements: Scope Example: C/C++ allows declarations to be present in statements: … If (a[j] > a[k]) { int t = a[j]; a[j] = a[k]; a[k] = t; }

C/C++ allows declarations to be present in statements (2): Scope Example: C/C++ allows declarations to be present in statements (2): … for (int i=0; i<10; i++) { sum = a[i]; } j = i; Note that what to do with loop variables has been a topic of much debate in the past.

Object oriented languages rely heavily on overloading. Early languages required that a locally defined identifier be unique in its scope (Fortran, Pascal, and C). Later languages permitted names to be overloaded, allowing multiple definitions of the same identifier within the same scope (Ada, C++). This is allowed as long as references can be resolved from the context the identifier is used in. Object oriented languages rely heavily on overloading.

Overloading Example: Java allows both instance variables and methods to have the same name within a single class: class Overload { int name; // an instance variable int name () {…} // a method … name = name(); } Why is this possible?

New Abstract Syntax Rules for Jay with Methods and Globals Figure 4.6

Abstract Syntax Sketch for a Jay Program with Globals and Methods Figure 4.7

Next time… Memory Management