Names, Scopes, and Bindings - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Names, Scopes, and Bindings

Description:

Stack and Heap. Stack-based allocation. frame or activation record ... heap management. garbage collection. Static Scope. Single, global scope. early Basic ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 17
Provided by: web2
Category:
Tags: bindings | names | scopes

less

Transcript and Presenter's Notes

Title: Names, Scopes, and Bindings


1
Names, Scopes, and Bindings
  • Chen Ding
  • MIT 6.035, September 2007
  • PLP Book, Chapters 3 and 9

2
High-level Names
  • Assembly level
  • memory address and memory content
  • Language and program level
  • variables
  • types
  • procedures

3
Binding Time
  • Language design time
  • primitive types, control flows
  • Language implementation time
  • machine dependent parameters
  • Programming time
  • user defined functions and data
  • Compile time
  • the layout of statically defined data
  • Link time
  • intermodule references
  • Load time
  • physical memory allocation

4
Object lifetime
  • The life of an object
  • creation of the object
  • creation of the binding
  • references
  • deactivation and activation
  • destruction of bindings
  • destruction of objects

5
Allocation Types
  • Static
  • fixed size, live throughout the execution
  • global variables, strings, tables for debugging,
    run-time type checking, exception handling, etc
  • often read-only

6
Stack and Heap
  • Stack-based allocation
  • frame or activation record
  • parameters, local vars, temporaries
  • Heap-based allocation
  • dynamically sized objects
  • heap management
  • garbage collection

7
Static Scope
  • Single, global scope
  • early Basic
  • common blocks in Fortran
  • Nested subroutines
  • Algol style closest nested scope rule
  • check the innermost scope and then successively
    surrounding scopes

8
Pascal Example
  • procedure P1(A ...)
  • var X real
  • procedure P2(B ...)
  • var X integer
  • ...
  • procedure P3(C ...)
  • ....

9
Modules
  • Info. hiding through explicit import/export
  • fewer name conflicts, more robust system and
    better error reporting
  • A module is a collection of objects
  • objects inside are visible inside but not visible
    outside unless exported
  • Clusters in Clu, Modula, packages in Ada,
    namespaces in C

10
Module-as-Type
  • Object-oriented programming
  • a class can be viewed as a module augmented with
    inheritance
  • encapsulation
  • the this pointer
  • my_stack.push(x), instead of
  • push(my_stack, x)
  • inheritance

11
Dynamic Scope
  • int a
  • void first() a1
  • void second() int a first()
  • void test()
  • a 2
  • if (b) seond()
  • else first()
  • print(a)

12
Reference Environment
  • First-class values
  • can be passed as a parameter, returned from a
    subroutine, or assigned into a variable
  • subroutines in functional languages
  • Second-class values
  • can be passed as a parameter
  • subroutines in imperative languages
  • Third-class values
  • e.g. labels

13
Dynamic Binding
  • Alias
  • common blocks, equivalence statements
  • pass-by-reference in C
  • Overloading
  • subroutine names, operators
  • false polymorphism
  • Polymorphism
  • same name, different actions
  • generic routines

14
Example Polymorphism
  • class Animal void MakeNoise() ...
  • class Dog inherits Animal void MakeNoise()..
  • class Cat inherits Animal void MakeNoise()...
  • void Sing(Animal a) a.MakeNoise()

15
Implementing Scope
  • Symbol tables
  • name-to-object bindings
  • variables and types (classes)
  • forward references
  • The scope stack
  • search down from top to bottom

16
Summary
  • Binding times
  • Allocation schemes
  • Modules and classes
  • Reference environment
  • Scope rules
  • Dynamic binding
  • Symbol tables
Write a Comment
User Comments (0)
About PowerShow.com