Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Similar documents
Programming Languages

Today's Topics. CISC 458 Winter J.R. Cordy

Names, Scopes, and Bindings II. Hwansoo Han

Run-time Environments - 2

CSE 307: Principles of Programming Languages

In Java we have the keyword null, which is the value of an uninitialized reference type

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

Computational Expression

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

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

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Chapter 5. Names, Bindings, and Scopes

G Programming Languages - Fall 2012

Chapter 5 Names, Binding, Type Checking and Scopes

Lecture08: Scope and Lexical Address

CSCI312 Principles of Programming Languages!

COMP 524 Spring 2018 Midterm Thursday, March 1

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

names names identifiers variables subroutines constants

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

Chapter 3:: Names, Scopes, and Bindings

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

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Run Time Environment

Programming Languages Third Edition. Chapter 7 Basic Semantics

Computational Expression

Names, Bindings, Scopes

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

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

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

Names, Scope, and Bindings

CS 230 Programming Languages

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Names, Scope, and Bindings

G Programming Languages - Fall 2012

Implementing Abstractions

CSE 307: Principles of Programming Languages

Creating Object Classes in True BASIC

What about Object-Oriented Languages?

CSC 533: Organization of Programming Languages. Spring 2005

CS 314 Principles of Programming Languages

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 11: Compiler II: Code Generation

Concepts Introduced in Chapter 7

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Names, Scope, and Bindings

Memory and C++ Pointers

Procedure and Object- Oriented Abstraction

Separate Compilation Model

Contents. 8-1 Copyright (c) N. Afshartous

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Chapter 11. Abstract Data Types and Encapsulation Concepts

G52CPP C++ Programming Lecture 13

6. Names, Scopes, and Bindings

LECTURE 14. Names, Scopes, and Bindings: Scopes

StackVsHeap SPL/2010 SPL/20

Run-Time Environments

Imperative Programming

a data type is Types

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

INF3110 Programming Languages Runtime Organization part II

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

CS 314 Principles of Programming Languages. Lecture 11

Names, Scope, and Bindings

Pointers II. Class 31

PL Recitation 9/21/2010

CS111: PROGRAMMING LANGUAGE II

CS110D: PROGRAMMING LANGUAGE I

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Run Time Environments

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

Stacks and Function Calls

Lecture 3: C Programm

Compilers and Code Optimization EDOARDO FUSELLA

Implementing Subroutines. Outline [1]

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Variables and Java vs C++

CS201- Introduction to Programming Current Quizzes

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

The Procedure Abstraction

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

Introduction to Computer Science I

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Type Bindings. Static Type Binding

Implementing Subprograms

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Exam 3 Chapters 7 & 9

Evolution of Programming Languages

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

OBJECT ORIENTED PROGRAMMING USING C++

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

Run-Time Environements

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

PSD1C SYSTEM SOFTWAE UNIT: I - V PSD1C SYSTEM SOFTWARE

Transcription:

Programming Language Concepts Scoping Janyl Jumadinova January 31, 2017

Scope Rules A scope is a program section of maximal size in which no bindings change, or at least in which no re-declarations are permitted. 2/22

Scope Rules A scope is a program section of maximal size in which no bindings change, or at least in which no re-declarations are permitted. In most languages with subroutines (functions, methods, procedures), we OPEN a new scope on subroutine entry: create bindings for new local variables, deactivate bindings for global variables that are re-declared (these variable are said to have a hole in their scope) make references to variables 2/22

Scope Rules On subroutine exit: destroy bindings for local variables reactivate bindings for global variables that were deactivated The book uses the term elaboration for the process of allocating memory and creating bindings associated with a declaration. 3/22

Elaboration Example 4/22

Elaboration Example 5/22

Example (Java): Heap Allocation (Dynamic allocation) int values[ ]; System.out.print("How big is the array? "); int n = scan.nextint(); values = new int[n]; 6/22

Example (Java): Heap Allocation (Dynamic allocation) int values[ ]; System.out.print("How big is the array? "); int n = scan.nextint(); values = new int[n]; No way to know at compile time how much space will be needed for the array values determined at run time. So... how can we know how much memory to save on the stack? 6/22

Example (Java): Heap Allocation (Dynamic allocation) int values[ ]; System.out.print("How big is the array? "); int n = scan.nextint(); values = new int[n]; No way to know at compile time how much space will be needed for the array values determined at run time. So... how can we know how much memory to save on the stack? We can t! We must allocate it dynamically from a special memory area called the heap. 6/22

Heap Allocation 7/22

Heap Allocation Stack grows and shrinks ( push and pop ); easy to generate code for this at compile time. Heap: no pattern no last-in, first-out or similar rule: 7/22

Heap Allocation Any time we use the new operator in Java, we allocate space from the heap. In C, use of the malloc function allocates from the heap. Harder to maintain than a stack; many techniques used. 8/22

Heap Allocation 9/22

Heap Allocation 10/22

Heap Allocation 11/22

Heap Allocation 12/22

Heap Allocation 13/22

Heap Allocation 14/22

Which Block to Use from Free List? 15/22

Summary: Names, Scopes and Bindings Binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol. static vs. dynamic 16/22

Summary: Names, Scopes and Bindings Binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol. static vs. dynamic Example: count = count + 5; 16/22

Summary: Names, Scopes and Bindings Example: count = count + 5; Some of the bindings and their binding times: The type of count is bound at compile time. The set of possible values of count is bound at compiler design time. The meaning of the operator symbol + is bound at compile time, when the types of its operands have been determined. The internal representation of the literal 5 is bound at compiler design time. The value of count is bound at execution time with this statement. 17/22

function big() { function sub1() { var x = 7; sub2(); } function sub2() { var y = x; } var x = 3; sub1(); } Example: Static vs. Dynamic Scoping Under static scoping, the reference to the variable x in sub2 is to the x declared in the procedure big. 18/22

function big() { function sub1() { var x = 7; } function sub2() { var y = x; var z = 3; } var x = 3; } Example: Static vs. Dynamic Scoping Under dynamic scoping, the meaning of x may reference the variable from either declaration of x, depending on the calling sequence. 19/22

Summary: Names, Scopes and Bindings How is scope implemented at execution time? Pointers on the activation record stack refer to surrounding scope. Lexical: static link. Dynamic: dynamic link. 20/22

Example: JavaScript Go to http://goo.gl/hcrqme for a working version of Figure 3.5 (in JavaScript). 21/22

Ways Around Hole in Scope Some languages allow access to scopes that are hidden by new declarations. E.g., Java: public class MyClass { private int x; // This creates a hole in the scope // of the instance variable x public void mymethod(int x) { x = 10; // Parameter x this.x = 20; // instance variable x... C++ has a similar construct. 22/22