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.

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Scope and Symbol Table.
Advertisements

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.
Programming Languages and Paradigms
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Scope Chapter Ten Modern Programming Languages.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
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
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.
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 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 4 Names The first step toward wisdom is calling.
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.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
1 MT258 Computer Programming and Problem Solving Unit 7.
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.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
Chapter 3 :: Names, Scopes, and Bindings Michael L. Scott School of Computer & Information Engineering, Sangji University Kwangman.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Subprograms - implementation
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
7. Runtime Environments Zhang Zhizheng
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.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
ISBN Chapter 10 Implementing Subprograms.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
CS314 – Section 5 Recitation 9
Advanced Programming in C
Implementing Subprograms
Run-Time Environments Chapter 7
Functions.
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
Implementing Subprograms
Names, Scopes, and Bindings: Scopes
Names, Binding, and Scope
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Binding Times Binding is an association between two things Examples:
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
CSE 3302 Programming Languages
Recursive Procedures and Scopes
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Presentation transcript:

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 A { class C { void f (bool b) { if (b) { int i = 0; } } }; }

Binding Time Compile Time: The compiler chooses how to map the high level code statically. Link Time: Most compilers allows different modules to be compiled independantly. Thus if we include a package or library, the binding of an object does not happen until the linker resolves the external depndencies. Run Time: Binding occurs when we go to run our program. Values associated with variables are bound. Static infers something that happens anytime before the program is executed while dynamic refers to anything from run-time on.

Binding Examples static final int MAX = 100; public static void main (String args[]) { String name = args[0]; } if ( a instanceof Integer ) Integer I = (Integer) a;

Scope Rules The region in which a binding takes place is known as a Program Scope. We can analyse a C program and determine every binding. When we enter a new block of code, denoted by the curly braces { and }, we can pick out which names refer to which objects. This is known as static scoping. Anything between the { and } is the current scope of local scope. for (int i = 0; i < 10; i++) { … } To avoid variables losing their binding outside of the local scope, we can use the static keyword.

Dynamic Scope It is easy to figure out static scope, we can simple look in-side the curly braces. Dynamic Scope is different and depends on the flow of control at runtime and the order of the method calls. The binding of a name is dependant on the most recent execution of that variable so long as it has not been destroyed by leaving it’s scope!!. A compiler cannot tell in advance what the binding will be. Confused?

proc main(); int x := 5; proc q(); { x := x + 1;} proc r(); {int x := 0; q(); } { r(); print(x); }. Scoping : Static scoping x -> x output: 6 Dynamic scoping x -> x output: 1

Implementing Dynamic Scoping The value of variable x is the last value bound to x. => stack discipline Deep binding Use a global stack for all variables Shallow binding Use a separate stack for each variable Implementing recursion is trivial. First LISP evaluator used dynamic scoping by accident!! Meaning of a call depends on the context.

Application of Dynamic Scoping Exception Handling –provide a separate construct in statically scoped language Setting Local Formatting Parameters Input-Output Redirection –avoids passing parameters explicitly through “intermediate” procedures

Applications of Dynamic Scope var base = 10;// decimal function print(int n) print n;// decimal. base = 16;// switch to hex print (8);// in hex not decimal base = 10;// back to decimal