1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.

Slides:



Advertisements
Similar presentations
Chapter 3:: Names, Scopes, and Bindings
Advertisements

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.
CSC 4181 Compiler Construction Scope and Symbol Table.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
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.
Chapter 8 :: Subroutines and Control Abstraction
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
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.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 8 - Control II: Procedures and Environments
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. 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.
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.
Runtime Organization.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Copyright © 2009 Elsevier Chapter 3:: Names, Scopes, and Bindings Programming Language Pragmatics Michael L. Scott.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
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.
Implementing Subprograms
Names, Scope, and Bindings Programming Languages and Paradigms.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Advanced Programming in C
Implementing Subprograms
Chapter 3:: Names, Scopes, and Bindings
Implementing Subprograms Chapter 10
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
CS 3304 Comparative Languages
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Implementing Subprograms
Names, Scopes, and Bindings: Scopes
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Scope, Visibility, and Lifetime
Implementing Subprograms
Binding Times Binding is an association between two things Examples:
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott

2 Binding Time (Review) A binding is an association between two thingsA binding is an association between two things –E.g. Name of an object and the object Binding time is the time at which a binding is createdBinding time is the time at which a binding is created –Language design time –Language implementation –Program writing time –Compile Time –Link Time –Load Time –Run Time

3 Binding Time Example Java programJava program What is the binding time ofWhat is the binding time of –Value of argument x? –Set of values of argument x? –Type of argument x? –Set of types of argument x? –Properties of operator +? public static int increment(int x) { return x + 1; }

4 Binding Time Example Perl programPerl program What is the binding time ofWhat is the binding time of –Value of –Set of values of –Type of –Set of types of –Properties of operator +? sub increment { return + 1; }

5 Scope Scope is the textual region of a program in which a binding is activeScope is the textual region of a program in which a binding is active The word 'scope' used as a noun is a program section of maximal size in which no bindings change.The word 'scope' used as a noun is a program section of maximal size in which no bindings change. –In most languages with subroutines, we OPEN a new scope on subroutine entry. Algol 68 introduced the term elaboration for the process of creating bindings when entering a scope. Elaboration time is a useful concept.Algol 68 introduced the term elaboration for the process of creating bindings when entering a scope. Elaboration time is a useful concept.

6 Referencing Environment The referencing environment (of a statement or expression) is the set of active bindings.The referencing environment (of a statement or expression) is the set of active bindings. A referencing environment corresponds to a collection of scopes that are examined (in order) to find a binding.A referencing environment corresponds to a collection of scopes that are examined (in order) to find a binding.

7 Scope Rules Programming languages implementProgramming languages implement –Static Scoping: active bindings are determined using the text of the program. The determination of scopes can be made by the compiler. »Most recent scan of the program from top to bottom »Closest nested subroutine rule »Most compiled languages employ static scope rules –Dynamic Scoping: active bindings are determined by the flow of execution at run time (i.e., the call sequence). »The determination of scopes can NOT be made by the compiler. »Dynamic scope rules are usually encountered in interpreted languages; in particular, early LISP dialects assumed dynamic scope rules.

8 Static Scope The classical example of static scope rules is the most closely nested rule used in block structured languages such as Algol 60 and Pascal.The classical example of static scope rules is the most closely nested rule used in block structured languages such as Algol 60 and Pascal. An identifier is known in the scope in which it is declared and in each enclosed scope, unless it is redeclared in an enclosed scope.An identifier is known in the scope in which it is declared and in each enclosed scope, unless it is redeclared in an enclosed scope. To resolve a reference to an identifier, we examine the local scope and statically enclosing scopes until a binding is found.To resolve a reference to an identifier, we examine the local scope and statically enclosing scopes until a binding is found.

9 Nested Subroutines Closest Nested Subroutine Rule

10 Storage Management (revisited) Static allocation for: Static allocation for: – code, globals, "own" variables, explicit constants – scalars may be stored in the instructions themselves Central stack for: Central stack for: – parameters, local variables, temporaries, bookkeeping information Why a stack? Why a stack? – allocate space for recursive routines » (not necessary in FORTRAN, etc) – reuse space » (useful in any language) Heap for:Heap for: – dynamic allocation

11 Stack-based Allocation

12 Static Links Local variables and arguments are assigned fixed offsets from the stack pointer or frame pointer at compile timeLocal variables and arguments are assigned fixed offsets from the stack pointer or frame pointer at compile time Access to non-local variables via static linksAccess to non-local variables via static links – Each frame points to the frame of the (correct instance of) the routine inside which it was declared. »In the absense of formal subroutines, "correct" means closest to the top of the stack. – you access a variable in a scope k levels out by following k static links and then using the known offset within the frame thus found.

13 Nested Subroutines Determining Scope StaticLinks

14 An Example Procedure A2 { Procedure A2 { int a = 0; int a = 0; Procedure C { Procedure D { print a; } Call D; } Procedure B { char a = 'b'; Call C; } Call B; }

15 Static Scope Variants The key idea in static scope rules is that bindings are defined by the physical (lexical) structure of the program.The key idea in static scope rules is that bindings are defined by the physical (lexical) structure of the program. A newer example of static scope rules is the import/export strategies of modular languages such as Modula-2.A newer example of static scope rules is the import/export strategies of modular languages such as Modula-2. The modules of Modula, Ada, etc. give you closed scopes (no identifier is automatically inherited from enclosing scopes) without the limited lifetime.The modules of Modula, Ada, etc. give you closed scopes (no identifier is automatically inherited from enclosing scopes) without the limited lifetime. –ALGOL 60 blocks implement OPEN scopes (identifiers which are not redeclared are automatically inherited from the enclosing scope). –Bindings to variables declared in a module are inactive outside the module, but not destroyed.

16 Other Static Scope Variants Euclid is an example of a language with lexically- nested scopes in which all scopes are closed.Euclid is an example of a language with lexically- nested scopes in which all scopes are closed. The Euclid rules were designed to avoid ALIASES, which complicate optimization and correctness arguments.The Euclid rules were designed to avoid ALIASES, which complicate optimization and correctness arguments. It forces you to document side effects by explicitly importing any external variables that are read or written.It forces you to document side effects by explicitly importing any external variables that are read or written. Euclid prevents you from passing a variable by reference to a procedure that imports the same variable.Euclid prevents you from passing a variable by reference to a procedure that imports the same variable.

17 Evolution of Data Abstraction Facilities subroutines, variables, arrays subroutines, variables, arrays Fortran, Basic Fortran, Basic subroutine nesting subroutine nesting Algol 60, Pascal, many others Algol 60, Pascal, many others own (static) variables own (static) variables Algol 68, Fortran ("save"), C, others Algol 68, Fortran ("save"), C, others module as manager module as manager Modula, C files (sorta) Modula, C files (sorta) module as type module as type Simula*, Euclid Simula*, Euclid classes, with inheritance classes, with inheritance Simula*, Smalltalk, C++, Eiffel, Java, others Simula*, Smalltalk, C++, Eiffel, Java, others *predates Modula; clearly a language ahead of its time time

18 Dynamic Scope Rules Bindings cannot always be resolved by examining the program because they are dependent on calling sequences.Bindings cannot always be resolved by examining the program because they are dependent on calling sequences. Dynamic scope rules are usually encountered in interpreted languages.Dynamic scope rules are usually encountered in interpreted languages. Such languages do not normally have type checking at compile time because type determination isn't always possible when dynamic scope rules are in effect.Such languages do not normally have type checking at compile time because type determination isn't always possible when dynamic scope rules are in effect.

19 Dynamic Scope Bindings between names and objects depend on the flow of control at run timeBindings between names and objects depend on the flow of control at run time –The current binding is the one found most recently during execution ExampleExample –If the scoping is static, the output of the program is 1 –If the scoping is dynamic, output is 1 or 2 depending on the value read at line 8 (>0 or 0 or <=0 respectively)

20 Why Dynamic Scope Perhaps the most common use of dynamic scope rules is to provide implicit parameters to subroutines.Perhaps the most common use of dynamic scope rules is to provide implicit parameters to subroutines. This is generally considered bad programming practice nowadays.This is generally considered bad programming practice nowadays. Ex: -Given a function print_integer() capable of printing in several bases. printing in several bases. -Want to use decimal notation most times -set variable print_base to 10 early in execution -to change base: begin--nested block print_base : integer := 16 print_integer(n)

21 Accessing Variables with Dynamic Scope Two approaches:Two approaches: –Keep a stack (*association list*) of all active variables. » When you need to find a variable, hunt down from top of stack. » This is equivalent to searching the activation records on the dynamic chain. –Keep a central table with one slot for every variable name. »If names cannot be created at run time, the table layout (and the location of every slot) can be fixed at compile time. Otherwise, you'll need a hash function or something to do lookup. »Every subroutine changes the table entries for its locals at entry and exit.

22 Implementation of Dynamic Scoping