Scope Chapter TenModern Programming Languages, 2nd ed.1.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

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:
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
5/15/2015IT 3271 Polymorphism (Ch 8) n A functions that is not fixed is polymorphic n Applies to a wide variety of language features n Most languages have.
Chapter TenModern Programming Languages1 Scope. Chapter TenModern Programming Languages2 Reusing Names n Scope is trivial if you have a unique name for.
Scope Chapter Ten Modern Programming Languages.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 9: Subprogram Control
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
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.
Scope.
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.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Array.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
CPS 506 Comparative Programming Languages Names, Scope, Expression.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
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 Twenty-ThreeModern Programming Languages1 Formal Semantics.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
ISBN Chapter 5 Names, Bindings, and Scopes.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Classes, Interfaces and Packages
Chapter TenModern Programming Languages1 Scope. Chapter TenModern Programming Languages2 Reusing Names Scope is trivial when all names are unique But.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Copyright © Curt Hill Flow of Control A Quick Overview.
ISBN Chapter 12 Support for Object-Oriented Programming.
Advanced Programming in C
Polymorphism (Ch 8) Scope (Ch 10) Who are you? What do you do now?
Data Types In Text: Chapter 6.
Names and Attributes Names are a key programming language feature
Type Checking, and Scopes
Scope, Visibility, and Lifetime
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Binding Times Binding is an association between two things Examples:
Scope Rules and Storage Types
Introduction to Data Structure
The Three Attributes of an Identifier
Presentation transcript:

Scope Chapter TenModern Programming Languages, 2nd ed.1

Reusing Names n Scope is trivial if you have a unique name for everything: n But in modern languages, we often use the same name over and over: n How can this work? Chapter TenModern Programming Languages, 2nd ed.2 fun square n = n * n; fun double n = n + n; fun square a = a * a; fun double b = b + b;

Outline n Definitions and scope n Scoping with blocks n Scoping with labeled namespaces n Scoping with primitive namespaces n Dynamic scoping n Separate compilation Chapter TenModern Programming Languages, 2nd ed.3

Definitions n When there are different variables with the same name, there are different possible bindings for that name n Not just variables: type names, constant names, function names, etc. n A definition is anything that establishes a possible binding for a name Chapter TenModern Programming Languages, 2nd ed.4

Examples Chapter TenModern Programming Languages, 2nd ed.5 fun square n = n * n; fun square square = square * square; const Low = 1; High = 10; type Ints = array [Low..High] of Integer; var X: Ints;

Scope n There may be more than one definition for a given name n Each occurrence of the name (other than a definition) has to be bound according to one of its definitions n An occurrence of a name is in the scope of a given definition of that name whenever that definition governs the binding for that occurrence Chapter TenModern Programming Languages, 2nd ed.6

Examples n Each occurrence must be bound using one of the definitions n Which one? n There are many different ways to solve this scoping problem Chapter TenModern Programming Languages, 2nd ed.7 - fun square square = square * square; val square = fn : int -> int - square 3; val it = 9 : int

Outline n Definitions and scope n Scoping with blocks n Scoping with labeled namespaces n Scoping with primitive namespaces n Dynamic scoping n Separate compilation Chapter TenModern Programming Languages, 2nd ed.8

Blocks n A block is any language construct that contains definitions, and also contains the region of the program where those definitions apply Chapter TenModern Programming Languages, 2nd ed.9 let val x = 1; val y = 2; in x+y end

Different ML Blocks The let is just a block: no other purpose A fun definition includes a block: n Multiple alternatives have multiple blocks: n Each rule in a match is a block: Chapter TenModern Programming Languages, 2nd ed.10 fun cube x = x*x*x; fun f (a::b::_) = a+b | f [a] = a | f [] = 0; case x of (a,0) => a | (_,b) => b

Java Blocks In Java and other C-like languages, you can combine statements into one compound statement using { and } n A compound statement also serves as a block: Chapter TenModern Programming Languages, 2nd ed.11 while (i < 0) { int c = i*i*i; p += c; q += c; i -= step; }

Nesting n What happens if a block contains another block, and both have definitions of the same name? n ML example: what is the value of this expression: Chapter TenModern Programming Languages, 2nd ed.12 let val n = 1 in let val n = 2 in n end end

Classic Block Scope Rule n The scope of a definition is the block containing that definition, from the point of definition to the end of the block, minus the scopes of any redefinitions of the same name in interior blocks n That is ML’s rule; most statically scoped, block-structured languages use this or some minor variation Chapter TenModern Programming Languages, 2nd ed.13

Example Chapter TenModern Programming Languages, 2nd ed.14 let val n = 1 in let val n = 2 in n end end Scope of this definition is A-B Scope of this definition is B A B

Outline n Definitions and scope n Scoping with blocks n Scoping with labeled namespaces n Scoping with primitive namespaces n Dynamic scoping n Separate compilation Chapter TenModern Programming Languages, 2nd ed.15

Labeled Namespaces n A labeled namespace is any language construct that contains definitions and a region of the program where those definitions apply, and also has a name that can be used to access those definitions from outside the construct n ML has one called a structure… Chapter TenModern Programming Languages, 2nd ed.16

ML Structures A little like a block: a can be used anywhere from definition to the end But the definitions are also available outside, using the structure name: Fred.a and Fred.f Chapter TenModern Programming Languages, 2nd ed.17 structure Fred = struct val a = 1; fun f x = x + a; end;

Other Labeled Namespaces n Namespaces that are just namespaces: – C++ namespace – Modula-3 module – Ada package – Java package n Namespaces that serve other purposes too: – Class definitions in class-based object-oriented languages Chapter TenModern Programming Languages, 2nd ed.18

Example The variables min and max would be visible within the rest of the class Also accessible from outside, as Month.min and Month.max n Classes serve a different purpose too Chapter TenModern Programming Languages, 2nd ed.19 public class Month { public static int min = 1; public static int max = 12; … }

Namespace Advantages n Two conflicting goals: – Use memorable, simple names like max – For globally accessible things, use uncommon names like maxSupplierBid, names that will not conflict with other parts of the program n With namespaces, you can accomplish both: – Within the namespace, you can use max – From outside, SupplierBid.max Chapter TenModern Programming Languages, 2nd ed.20

Namespace Refinement n Most namespace constructs have some way to allow part of the namespace to be kept private n Often a good information hiding technique n Programs are more maintainable when scopes are small n For example, abstract data types reveal a strict interface while hiding implementation details… Chapter TenModern Programming Languages, 2nd ed.21

Example: An Abstract Data Type Chapter TenModern Programming Languages, 2nd ed.22 namespace dictionary contains a constant definition for initialSize a type definition for hashTable a function definition for hash a function definition for reallocate a function definition for create a function definition for insert a function definition for search a function definition for delete end namespace Interface definitions should be visible Implementation definitions should be hidden

Two Approaches n In some languages, like C++, the namespace specifies the visibility of its components n In other languages, like ML, a separate construct defines the interface to a namespace (a signature in ML) n And some languages, like Ada and Java, combine the two approaches Chapter TenModern Programming Languages, 2nd ed.23

Namespace Specifies Visibility Chapter TenModern Programming Languages, 2nd ed.24 namespace dictionary contains private: a constant definition for initialSize a type definition for hashTable a function definition for hash a function definition for reallocate public: a function definition for create a function definition for insert a function definition for search a function definition for delete end namespace

Separate Interface Chapter TenModern Programming Languages, 2nd ed.25 interface dictionary contains a function type definition for create a function type definition for insert a function type definition for search a function type definition for delete end interface namespace myDictionary implements dictionary contains a constant definition for initialSize a type definition for hashTable a function definition for hash a function definition for reallocate a function definition for create a function definition for insert a function definition for search a function definition for delete end namespace

Outline n Definitions and scope n Scoping with blocks n Scoping with labeled namespaces n Scoping with primitive namespaces n Dynamic scoping n Separate compilation Chapter TenModern Programming Languages, 2nd ed.26

Do Not Try This At Home It is legal to have a variable named int n ML is not confused You can even do this (ML understands that int*int is not a type here): Chapter TenModern Programming Languages, 2nd ed.27 - val int = 3; val int = 3 : int - fun f int = int*int; val f = fn : int -> int - f 3; val it = 9 : int

Primitive Namespaces n ML’s syntax keeps types and expressions separated n ML always knows whether it is looking for a type or for something else n There is a separate namespace for types Chapter TenModern Programming Languages, 2nd ed.28 fun f(int:int) = (int:int)*(int:int); These are in the namespace for types These are in the ordinary namespace

Primitive Namespaces n Not explicitly created using the language (like primitive types) n They are part of the language definition n Some languages have several separate primitive namespaces n Java: packages, types, methods, variables, and statement labels are in separate namespaces Chapter TenModern Programming Languages, 2nd ed.29

Outline n Definitions and scope n Scoping with blocks n Scoping with labeled namespaces n Scoping with primitive namespaces n Dynamic scoping n Separate compilation Chapter TenModern Programming Languages, 2nd ed.30

When Is Scoping Resolved? n All scoping tools we have seen so far are static n They answer the question (whether a given occurrence of a name is in the scope of a given definition) at compile time n Some languages postpone the decision until runtime: dynamic scoping Chapter TenModern Programming Languages, 2nd ed.31

Dynamic Scoping n Each function has an environment of definitions n If a name that occurs in a function is not found in its environment, its caller’s environment is searched n And if not found there, the search continues back through the chain of callers n This generates a rather odd scope rule… Chapter TenModern Programming Languages, 2nd ed.32

Classic Dynamic Scope Rule n The scope of a definition is the function containing that definition, from the point of definition to the end of the function, along with any functions when they are called (even indirectly) from within that scope— minus the scopes of any redefinitions of the same name in those called functions Chapter TenModern Programming Languages, 2nd ed.33

Static Vs. Dynamic n The scope rules are similar n Both talk about scope holes—places where a scope does not reach because of redefinitions n But the static rule talks only about regions of program text, so it can be applied at compile time n The dynamic rule talks about runtime events: “functions when they are called…” Chapter TenModern Programming Languages, 2nd ed.34

Example Chapter TenModern Programming Languages, 2nd ed.35 fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end; What is the value of g 5 using ML’s classic block scope rule?

Block Scope (Static) Chapter TenModern Programming Languages, 2nd ed.36 fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end; With block scope, the reference to inc is bound to the previous definition in the same block. The definition in f ’s caller’s environment is inaccessible. g 5 = 6 in ML

Dynamic Scope Chapter TenModern Programming Languages, 2nd ed.37 fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end; With dynamic scope, the reference to inc is bound to the definition in the caller’s environment. g 5 = 7 if ML used dynamic scope

Where It Arises n Only in a few languages: some dialects of Lisp and APL n Available as an option in Common Lisp n Drawbacks: – Difficult to implement efficiently – Creates large and complicated scopes, since scopes extend into called functions – Choice of variable name in caller can affect behavior of called function Chapter TenModern Programming Languages, 2nd ed.38

Outline n Definitions and scope n Scoping with blocks n Scoping with labeled namespaces n Scoping with primitive namespaces n Dynamic scoping n Separate compilation Chapter TenModern Programming Languages, 2nd ed.39

Separate Compilation n We saw this in the classical sequence of language system steps n Parts are compiled separately, then linked together n Scope issues extend to the linker: it needs to connect references to definitions across separate compilations n Many languages have special support for this Chapter TenModern Programming Languages, 2nd ed.40

C Approach, Compiler Side n Two different kinds of definitions: – Full definition – Name and type only: a declaration in C-talk If several separate compilations want to use the same integer variable x : – Only one will have the full definition, int x = 3; – All others have the declaration extern int x; Chapter TenModern Programming Languages, 2nd ed.41

C Approach, Linker Side n When the linker runs, it treats a declaration as a reference to a name defined in some other file n It expects to see exactly one full definition of that name n Note that the declaration does not say where to find the definition—it just requires the linker to find it somewhere Chapter TenModern Programming Languages, 2nd ed.42

Older Fortran Approach, Compiler Side Older Fortran dialects used COMMON blocks n All separate compilations define variables in the normal way All separate compilations give the same COMMON declaration: COMMON A,B,C Chapter TenModern Programming Languages, 2nd ed.43

Older Fortran Approach, Linker Side The linker allocates just one block of memory for the COMMON variables: those from one compilation start at the same address as those from other compilations n The linker does not use the local names If there is a COMMON A,B,C in one compilation and a COMMON X,Y,Z in another, A will be identified with X, B with Y, and C with Z Chapter TenModern Programming Languages, 2nd ed.44

Modern Fortran Approach A MODULE can define data in one separate compilation A USE statement can import those definitions into another compilation USE says what module to use, but does not say what the definitions are n So unlike the C approach, the Fortran compiler must at least look at the result of that separate compilation Chapter TenModern Programming Languages, 2nd ed.45

Trends in Separate Compilation n In recent languages, separate compilation is less separate than it used to be – Java classes can depend on each other circularly, so the Java compiler must be able to compile separate classes simultaneously – ML is not really suitable for separate compilation at all, though CM (a separate tool in the SML system, the Compilation Manager) can do it for most ML programs Chapter TenModern Programming Languages, 2nd ed.46

Conclusion n Today: four approaches for scoping n There are many variations, and most languages employ several at once n Remember: names do not have scopes, definitions do! Chapter TenModern Programming Languages, 2nd ed.47