One Ring to rule them all, One Ring to bring them all,

Similar documents
Chapter 3:: Names, Scopes, and Bindings

Review for the Third Exam COMP163

Programming Languages

Name, Scope, and Binding. Outline [1]

G Programming Languages - Fall 2012

Chapter 5. Names, Bindings, and Scopes

CSC 533: Organization of Programming Languages. Spring 2005

Chapter 8 :: Composite Types

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

G Programming Languages - Fall 2012

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Short Notes of CS201

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CS201 - Introduction to Programming Glossary By

Chapter 9 :: Subroutines and Control Abstraction

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Types II. Hwansoo Han

Concepts Introduced in Chapter 7

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Control Abstraction. Hwansoo Han

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

References and pointers

Chap. 8 :: Subroutines and Control Abstraction

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Run-time Environments

Run-time Environments

Imperative Programming

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Methods & Classes COMP163

Variables and Java vs C++

CS201 Some Important Definitions

6. Names, Scopes, and Bindings

CS113: Lecture 4. Topics: Functions. Function Activation Records

NOTE: Answer ANY FOUR of the following 6 sections:

Module 27 Switch-case statements and Run-time storage management

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Implementing Subprograms

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Principles of Programming Languages

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS 415 Midterm Exam Spring 2002

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Chapter 5 Names, Binding, Type Checking and Scopes

class objects instances Fields Constructors Methods static

COMP 524 Spring 2018 Midterm Thursday, March 1

CSCI Compiler Design

Chapter 10 :: Data Abstraction and Object Orientation

Run-time Environments - 2

Programming Languages

Names and Abstractions: What s in a Name?

Informatica 3 Syntax and Semantics

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Scope, Functions, and Storage Management

Lecture 3: C Programm

Run-time Environments -Part 1

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Semantic Analysis and Type Checking

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

CS201 Latest Solved MCQs

9. Subprograms. 9.2 Fundamentals of Subprograms

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Run Time Environments

The Procedure Abstraction


CS 415 Midterm Exam Spring SOLUTION

Java Internals. Frank Yellin Tim Lindholm JavaSoft

Chapter 9 Subprograms

Runtime Environments I. Basilio B. Fraguela

JAVA An overview for C++ programmers

CSE 504: Compiler Design. Runtime Environments

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Computer Architecture COMP360

Programming Languages Third Edition. Chapter 7 Basic Semantics

Lexical Considerations

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

CSE 230 Intermediate Programming in C and C++ Functions

// the current object. functioninvocation expression. identifier (expressionlist ) // call of an inner function

Qualifying Exam in Programming Languages and Compilers

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Advances in Programming Languages: Regions

Binding and Variables

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Field Analysis. Last time Exploit encapsulation to improve memory system performance

1 Lexical Considerations

Advanced Programming & C++ Language

Chapter 6 Introduction to Defining Classes

Lecture 7: Binding Time and Storage

Transcription:

Binding COMP360

One Ring to rule them all, One Ring to find them, One Ring to bring them all, And in the darkness bind them. inscription on the One Ring J. R. R. Tolkien

Reading Read chapter 10 of the textbook

Binding A binding is an association between two things, such as a name and the thing it names A name is exactly what you think it is Most names are identifiers symbols (like '+') can also be names The scope of a binding is the part of the program in which the binding is active from Programming Language Pragmatics, 4 th ed, by Michael Scott

What are we binding? At some point a variable name is bound to a memory location In Java, the keyword this means this object. It is bound to a specific object when the method is executed Keywords (such as final, synchronized or long) are bound to a specific concept at language design

Binding Time Binding Time is the point at which a binding is created or, more generally, the point at which any implementation decision is made Some bindings are made before the program is executed Other bindings are made during program execution from Programming Language Pragmatics, 4 th ed, by Michael Scott

Binding Time Examples Language design time meaning of keywords Language implementation time implementation of types Program writing time names to concepts Compile time variable names to relative addresses, program statements to machine language Link time - layout of whole program in memory

Run Time Binding Examples Program load variables and methods assigned to physical addresses Method Entry actual parameter values bound to formal parameters, local variables bound to memory locations

Static and Dynamic The terms STATIC and DYNAMIC are generally used to refer to things bound before run time and at run time, respectively The period of time from creation to destruction is called the LIFETIME of a binding If an object outlives binding, it's garbage If a binding outlives an object, it's a dangling reference from Programming Language Pragmatics, 4 th ed, by Michael Scott

Pros and Cons In general, early binding times are associated with greater efficiency Later binding times are associated with greater flexibility Compiled languages tend to have early binding times Interpreted languages tend to have later binding times We generally talk about the binding of identifiers to the variables they name from Programming Language Pragmatics, 4 th ed, by Michael Scott

When is a value bound to dog? int mymethod( double dog ) A. Writing of the program B. Compile time C. Program linking D. Program load E. Method entry

Storage Management Storage Allocation mechanisms Static Stack Heap Static allocation for code globals static or own variables explicit constants (including strings, sets, etc) scalars may be stored in the instructions from Programming Language Pragmatics, 4 th ed, by Michael Scott

Call stack for parameters local variables temporaries Stack Why a stack? allocate space for recursive routines (not necessary in FORTRAN no recursion) reuse space (in all programming languages) from Programming Language Pragmatics, 4 th ed, by Michael Scott

Stack Contents Contents of a stack frame arguments and returns local variables temporaries bookkeeping (saved registers, line number static link, etc.) Local variables and arguments are assigned fixed OFFSETS from the stack pointer or frame pointer at compile time from Programming Language Pragmatics, 4 th ed, by Michael Scott

Binding an Array Size Binding when program is written in myprog.c int rabbit[123];

Binding an Array Size Binding based on a configuration file in config.h #define SIZE 123 in myprog.c #include <config.h> int rabbit[size];

Binding an Array Size Binding at execution time in myprog.c int arraysize; // size of the array cin >> arraysize; // read size int *rabbit; // pointer to array rabbit = new int[arraysize];

Binding an Array Size Dynamic size of array-like data structure in myprog.java java.util.arraylist<int> rabbit = new java.util.arraylist<>();

Variable Scope The Scope of a variable refers to where you can use a variable Where you declare a variable determines where it can be used The scope of a binding is the part of the program in which the binding is active

Variable Range Variables defined in a block can only be used in that block following the variable declaration { // you cannot use the variable moth here double moth = 72.5; // you may use the variable moth here // this is the scope of moth // The variable moth is not allowed here

Out of Scope int cat; if (whatever == 0) { int dog = 1; else { int dog = 2; cat = dog * 3; // Error, dog is out of scope

This works int cat, dog; if (whatever == 0) { dog = 1; else { dog = 2; cat = dog * 3; // Only one dog variable

Method Parameter Scope The parameters defined in a method header can be used throughout that method Method parameter variables cannot be used in another method void amethod( int dog, double cat ) { You can use dog and cat throughout this method

Declarations First Java and C++ allow you to put variable declarations anywhere in a program It is usually advantageous to declare a variable at the beginning of a block Some older programming languages required you to put all variable declarations at the beginning of a block Fortran All declarations are before executable statements Cobol declarations are in the data division

for Loop Scope A loop counter variable only has scope in the loop for (int cow = 0; cow < 28; cow++) { // loop body // the loop counter, cow, can be used here // You cannot use cow after the loop

Local Variables public static void main( ) { double cat = 5, bird = 47; Scope of cat = myfunc( bird ); cat and bird System.out.print(cat); double myfunc(double cow) { double bull; bull = cow * 2.0; return bull; Scope of cow and bull

Multiple Variable with the Same Name In different parts of a program, you can use the same variable name, but it will mean a different variable This can be confusing and should be avoided

Local Variables (Tricky) public static void main( ) { double cat = 5, bird = 47; Scope of cat = myfunc( bird ); cat and bird System.out.print(cat); int myfunc(double cow) { int bird; bird = (int)cow * 2.0; return bird; Scope of cow and bird (different bird)

Variables A C++ or Java method can use three different types of variables local variables defined in the method parameter variables object instance variables Object instance variables can be used in any of the methods of the class

Name Collision public class Collide { int rat = 3; public int square( int rat ) { return rat * rat; Collide thing = new Collide(); int turtle = thing.square( 2 ); turtle is set to 4

Variable Priority A method cannot have a local variable the same name as a parameter variable If a class instance variable has the same name as a local variable or parameter, the method will always use the local variable or parameter

More Name Collisions public class Collide { int rat = 3; public int square( int mouse) { int rat = mouse * mouse; return rat; The class instance variable rat is a different memory location from the local variable rat

Name Collision public class Collide { int rat = 3; public int square( int rat ) { return this.rat * rat; Collide thing = new Collide(); int turtle = thing.square( 2 ); turtle gets the value 6

Separate Compilation Java, C++ and many languages allow modules to be compiled separately Rules for how variables work with separate compilation are messy In C++, static on a function or variable outside a function or private in Java means it is usable only in the current source file This static is a different notion from the static variables inside a function

What is printed by this program? public class Click { static int crow= 5, raven= 7; static void myfunc(int parrot) { int crow; crow = parrot + 1; System.out.print( crow ); public static void main( ) { Click ques = new Click(); ques.myfunc( 3 ); System.out.print( crow ); A. 4 4 B. 4 5 C. 5 5 D. none of above

What is printed by this program? public class Click { static int crow= 5, raven= 7; static void myfunc(int parrot) { // changed here crow = parrot + 1; System.out.print( crow ); public static void main( ) { Click ques = new Click(); ques.myfunc( 3 ); System.out.print(crow ); A. 4 4 B. 4 5 C. 5 5 D. none of above

Created Elsewhere extern on a C++ variable or function means that it is declared in another source file In Java and C++, method headers without bodies are extern by default

No class next week Spring break is March 5 to March 11, 2017