Names, Binding, and Scope

Slides:



Advertisements
Similar presentations
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Advertisements

CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
Chapter 5 Basic Semantics
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
Tutorial 6 & 7 Symbol Table
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Run time vs. Compile time
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.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 Binding Time and Storage Allocation (Section ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
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 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
COMP3190: Principle of Programming Languages
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Object-Oriented Programming Chapter Chapter
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Names, Scope, and Bindings Programming Languages and Paradigms.
ISBN Chapter 12 Support for Object-Oriented Programming.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Advanced Programming in C
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Implementing Subprograms Chapter 10
Names and Attributes Names are a key programming language feature
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Semantic Analysis with Emphasis on Name Analysis
CSE 3302 Programming Languages
11.1 The Concept of Abstraction
Names, Bindings, and Scopes
Names.
Interfaces and Inheritance
Chapter 10: Implementing Subprograms Sangho Ha
Names, Scopes, and Bindings: Scopes
Scope, Visibility, and Lifetime
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
More Object-Oriented Programming
Binding Times Binding is an association between two things Examples:
Subject : T0152 – Programming Language Concept
Programming Languages
Names and Binding In Text: Chapter 5.
Name Binding and Object Lifetimes
Programming Languages and Paradigms
Names, Bindings, and Scopes
COMPILERS Semantic Analysis
Lecture 10 Concepts of Programming Languages
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Lecture 6: Names (Revised based on the Tucker’s slides) 5/27/2019
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Types and Related Issues
11.1 The Concept of Abstraction
Presentation transcript:

Names, Binding, and Scope http://flic.kr/p/LDPxN Names, Binding, and Scope

Key Idea in Computing: Abstraction Represent thing in form closer to its meaning (semantics), hiding away implementation details Example: Separate language features from details of computer architecture Factor out details so person can focus on a few concepts at a time Capture only details relevant to current perspective

Names: One way to abstract Create name to represent more complex thing Enable focusing on “what” rather than “how” Examples: Variables Functions Classes

Notion of Binding Mapping of name to thing it names Examples: Variable name bound to int data item Function name bound to function definition Class name bound to class definition

Notion of Binding Time Time at which a name is bound to thing it names Examples: Language-design time (e.g., primitive types) Program-writing time (e.g., class names) Run time (e.g., object references) Two categories: Static: Before run time Dynamic: At run time

Design Tension: Early versus Late Binding Early binding: Greater efficiency (of compiler/interpreter) Easier to comprehend Late binding: Greater flexibility

Binding Terminology Key events: Creation of object Creation of binding Use of binding Destruction of binding Destruction of object Binding lifetime: From binding creation to destruction Dangling reference: Referent object destroyed before binding

Storage Allocation Mechanisms: Creating/Destroying Objects Static: Objects get absolute address Retained throughout program execution Example: Global variable Stack: Objects allocated/deallocated in LIFO order Tied to subroutine calls/returns Heap: Objects allocated/deallocated at arbitrary times Garbage collection: Implicit/automagic deallocation

Notion of Scope Textual region in program where binding is active Static scope: Scope determined at compile time Example: C Language “block scoping” Dynamic scope: Scope determined at run time Example: Lisp Language Referencing environment: At any point in program execution, set of active bindings

Pascal Example: Scoping and Nested Subroutines

Language Design Issue: Declaration Order and Scoping In Java (or other C-like language of choice) Where is define-before-use required? Where is define-before-use not required? Local variables in a method definition must be defined before they can be used Class methods may be called before they are defined in a class declaration (e.g., by another method of the class)

Language Design Issue: Modularization and Information Hiding Info Hiding: Make objects and algorithms invisible to parts of system that don’t “need to know” Modularization: Break system into modules with interfaces and hidden implementations Modules may be hidden as well In Java, give example of hidden modules being made visible and name bindings added to current scope Hint: Think coarser granularity than a class Importing a package adds that package’s bindings to the current scope

Language Design Issue: Dynamic Scoping Binding for given name is one encountered most recently during execution and not yet destroyed by returning from its scope Con: Difficult to understand Example: If static scoping, prints 1 If dynamic, depend on read_integer: If pos., prints 2 otherwise, prints 1

Language Design Issue: Name Aliases Multiple names map to same object Give a Java example of an alias MyObject myO = new MyObject(); MyObject myAlias = myO;

Language Design Issue: Name Overloading One name maps to multiple objects Give a Java example of overloading Couple examples: Two methods in same class with different parameters Instance variables and method parameters can have same names (shadowing)

Related but different from overloading: Coercion Convert value of one type to value of another when second type is required by surrounding context Typically done by compiler C Example: double min(double x, double y) { … } … int myX = 5, myY = 6; … = min(myX, myY);

Related but different from overloading: Polymorphism From the Greek: “having multiple forms” Can apply to data structures or subroutines Two main kinds: Parametric polymorphism Subtype polymorphism

Parametric Polymorphism Code takes types as parameters May be implicit Ada Example:

Subtype Polymorphism Code works with one type T, but programmer can create subtypes of T that also work Give a Java example of subtype polymorphism public class Employee { public void printInfo() { … } … } public class SalariedEmployee extends Employee { Employee bob = getAnyTypeOfEmployee(); bob.printInfo();

Polymorphic solution more general than overloaded vs

Language Design Issue: Subroutine Binding to Reference Environment Figure 3.14, p152 One might argue that deep binding is appropriate for the environment of function older_than_threshold (for access to threshold), while shallow binding is appropriate for the environment of procedure print_person (for access to line_length).

Two Approaches to Subroutine Referencing Environments Shallow Binding: Referencing environment not created until subroutine called Deep Binding: Subroutine carries referencing environment with it Bundle known as a closure JavaScript has closures

Activity: How to implement name checking for HW3? Goal: Have parser give error if an identifier is undeclared Basic pieces: Treewalker Symbol table Scope stack