Names, Bindings, Type Checking, and Scopes PowerPoint PPT Presentation

presentation player overlay
1 / 59
About This Presentation
Transcript and Presenter's Notes

Title: Names, Bindings, Type Checking, and Scopes


1
Names, Bindings, Type Checking, and Scopes
  • Jianhua Yang
  • Department of Math and Computer Science
  • Bennett College

2
Outline
  • Names and variables
  • The concepts of binding
  • Type checking, strong type and type compatibility
  • Scope, and lifetime
  • Referencing Environments
  • Named Constants

3
5.1 Introduction
  • Von Neumann architecture
  • Memory
  • Processor

Store instructions and data
Execute the instructions and process the data
4
variable
  • Can be characterized by a collection of
    properties, or attributes.
  • Most of the important one is Type

5
The important issues about Variable
  • Scope
  • lifetime

6
5.2 Names
  • Variable names
  • Associate with labels, subprograms, formal
    parameters, and other program constructs.

7
1. Design issues
  • Case-sensitive?
  • Keywords or reserved words?

8
2. Name forms
  • A name is a string of characters used to identify
    some entity in a program
  • Length problem
  • The forms are almost the same
  • A letter followed by a string consisting of
    letters, digits, and underscore characters.

9
3. Special words
Is a word of a programming language that is
special only in certain contexts.
  • Keyword
  • Reserved word

Is a special word of a programming language that
cannot be used as a name.
Real Apple Real 3.4
Integer Real Real Integer
10
5.3 Variables
  • Variable is memory address.
  • Variable attributes
  • Name
  • Address
  • Value
  • Type
  • Lifetime
  • Scope

11
1. Address
  • Is the machine memory address with which it is
    associated.
  • Same name can be associated with different
    address.
  • Same variable can be associated with different
    address
  • Aliases
  • Pointer

12
2. Type
  • The variable of type determines
  • The range of values of the variable
  • Operations

char, int, float
13
4. Value
  • The content of the memory
  • Abstract cell
  • r-value l-value

14
5.4 The concept of binding
  • Binding is to associate sth. With sth.
  • Binding can take place at different times, phases.
  • Language design time
  • Implementation time
  • Compile time
  • Load time
  • Link time
  • Run time

15
Example of binding
  • count count 5
  • Compile time the type of count is bound
  • Compiler design time the set of possible values
    of count is bound
  • Compile time the meaning of
  • Compiler design time literal 5
  • Execution time the value of count is bound

16
1. Binding of attributes to variables
  • Static binding
  • Dynamic binding

A binding is static if it first occurs before run
time and remains unchanged throughout program
execution.
A binding is dynamic if the binding occurs during
run time or can change in the course of program
execution.
17
2. Type Bindings
  • Before a variable can be accessed in a program,
    it must be bound to a data type.

18
1) Variable Declarations
  • Explicit declaration
  • Implicit declaration

Static binding
Advantages, and disadvantages from Fortran and
Perl. C, C Declaration and definition.
19
2) Dynamic Type Binding
  • Binding time Occurs when it is assigned a value
    in an assignment statement.

Advantage programming flexibility
20
Example
  • JavaScript, PHP
  • List10.2, 3.5
  • List47

21
Disadvantages
  • Readability
  • Error detection
  • Cost (time, memory)

22
3) Type inference
  • Such as
  • fun square (x real) xx

23
3. Storage Bindings and Lifetime
  • Allocation
  • Deallocation
  • Lifetime
  • Categories
  • Static
  • Stack-dynamic
  • Explicit heap-dynamic
  • Implicit heap-dynamic

24
1) Static variables
  • Those are bound to memory cells before program
    execution begins and remain bound to those same
    memory cells until program execution terminates.

25
Advantages
  • Globally access
  • Efficiency
  • No run-time overhead for allocation and
    deallocation

26
Disadvantages
  • Reduced flexibility
  • Memory shareable problem

27
2) Stack-dynamic variables
  • Are those variables whose storage bindings are
    created when their declaration statements are
    elaborated.
  • They are allocated from the run-time stack.

28
Advantages
  • memory can be shared
  • Can work for recursive program

29
Disadvantages
  • Run-time overhead of allocation and deallocation
  • No history sensitive

30
3) Explicit heap-dynamic variables
  • Are nameless memory cells that are allocated and
    deallocated by explicit run-time instructions
    specified by the programmer.

31
Further more
  • Those variables, which are allocated from and
    deallocated to the heap, can only be referenced
    through pointer or reference variables.
  • The heap is a collection of storage cells whose
    organization is highly disorganized because of
    the unpredictability.

32
example
  • In C
  • int intnode //create a pointer
  • intnode new int //create the heap-dynamic
    variable
  • Delete intnode // deallocate the heap-dynamic
    variable

33
example
  • In Java, implicit garbage collection
  • In C, implicitly deallocated
  • Explicit heap-dynamic variables are useful in
    constructing dynamic structures, such as linked
    list and trees.

34
Disadvantages
  • Difficult to use
  • Cost of references to the variables
  • Complexity of storage management implementation

35
4) Implicit heap-dynamic variables
  • Are bound to heap storage only when they are
    assigned values.

36
advantage
  • flexibility

37
Disadvantages
  • Run-time overhead
  • Difficult to detect error
  • Storage management problems

38
5.5 Type Checking
  • Type checking is the activity of ensuring that
    the operands of an operator are of compatible
    types.

39
Static type checking
  • If all bindings of variables to types are static
    in a language, then type checking can nearly
    always be done statically.
  • Flexibility but less cost

40
Dynamic type checking
  • Dynamic type binding requires type checking at
    run time, which is called dynamic type checking
  • Flexible but more cost

41
Strong Typing
  • A strongly typed language is one in which each
    name in a program in the language has a single
    type associated with it, and that type is known
    at compile time.
  • Fortran 95 is not strongly typed
  • Ada is nearly strongly typed
  • C, C are nearly strongly typed
  • ML is strongly typed
  • Java, C nearly strongly typed

42
5.6 Type Compatibility (skip this)
  • Name type compatibility
  • Structure type compatibility

Two variables have compatible types only if they
are defined in either the same declaration or in
declarations that use the same type name.
Means that two variables have compatible types if
their types have identical structures.
43
Advantages and disadvantages
  • Name types compatibility is easy to implement but
    is highly restrictive.
  • Structure type compatibility is more flexible,
    but difficult to implement.

44
5.7 Scope
  • The scope of a variable is the range of
    statements in which the variable is visible.

45
1. Static scope
  • Which means the scope of a variable can be
    statically determined, prior to execution.

46
Example1
  • procedure Big is
  • x Integer
  • procedure Sub1 is
  • begin --of sub1
  • x
  • end --of sub1
  • procedure Sub2 is
  • x Integer
  • begin --of Sub2
  • end --of Sub2
  • begin -- of Big
  • end --of Big

47
Example 2
  • void sub()
  • int count
  • while()
  • int count
  • count

48
2. Blocks
  • Very like a function or a subprogram

49
Example 1
  • If (listi lt listj)
  • int temp
  • temp listi
  • listi listj
  • listj temp

50
Some points
  • 1. if a variable is defined in a block, its scope
    is limited within that block
  • 2. if a variable is not defined in a block, so
    its scope is from its definition statement to the
    end of the function.

51
Dynamic scope
  • A variables scope is determined at running time,
    rather than compile time.

52
Example
  • procedure Big is
  • x Integer
  • procedure Sub1 is
  • begin --of sub1
  • x
  • end --of sub1
  • procedure Sub2 is
  • x Integer
  • begin --of Sub2
  • end --of Sub2
  • begin -- of Big
  • end --of Big

1. Call Sub2, and then call Sub1 in Sub2 2.
Call Sub1 by Big, and then call Sub2.
53
4. Evaluation of Static Scoping
A
C
main
D
B
E
54
5. Scope and lifetime
  • They are different, but sometimes are related

55
Example in Java
  • Consider a variable that is declared in a Java
    method that contains no method call.
  • In this case, its scope is its lifetime

56
Example in C
  • void printheader()
  • void compute()
  • int sum
  • printheader()

57
6. Referencing Environments
  • The referencing environment of a statement is the
    collection of all variables that are visible in
    the statement.

58
Example in C
  • Void sub1()
  • int a, b
  • Void sub2()
  • int b, c
  • sub1
  • Void main()
  • int c, d
  • sub2()

a and b of sub1, c of sub2, d of main
1
2
b and c of sub2, d of main
3
c and d of main
59
Summary
  • Names and variable
  • Binding and type checking
  • Type scope and lifetime
Write a Comment
User Comments (0)
About PowerShow.com