Title: Names, Bindings, Type Checking, and Scopes
1Names, Bindings, Type Checking, and Scopes
- Jianhua Yang
- Department of Math and Computer Science
- Bennett College
2Outline
- Names and variables
- The concepts of binding
- Type checking, strong type and type compatibility
- Scope, and lifetime
- Referencing Environments
- Named Constants
35.1 Introduction
- Von Neumann architecture
- Memory
- Processor
Store instructions and data
Execute the instructions and process the data
4variable
- Can be characterized by a collection of
properties, or attributes. - Most of the important one is Type
5The important issues about Variable
65.2 Names
- Variable names
- Associate with labels, subprograms, formal
parameters, and other program constructs.
71. Design issues
- Case-sensitive?
- Keywords or reserved words?
82. 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.
93. Special words
Is a word of a programming language that is
special only in certain contexts.
Is a special word of a programming language that
cannot be used as a name.
Real Apple Real 3.4
Integer Real Real Integer
105.3 Variables
- Variable is memory address.
- Variable attributes
- Name
- Address
- Value
- Type
- Lifetime
- Scope
111. 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
122. Type
- The variable of type determines
- The range of values of the variable
- Operations
char, int, float
134. Value
- The content of the memory
145.4 The concept of binding
- Binding is to associate sth. With sth.
- Binding can take place at different times, phases.
15Example of binding
- 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
161. 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.
172. Type Bindings
- Before a variable can be accessed in a program,
it must be bound to a data type.
181) Variable Declarations
- Explicit declaration
- Implicit declaration
Static binding
Advantages, and disadvantages from Fortran and
Perl. C, C Declaration and definition.
192) Dynamic Type Binding
- Binding time Occurs when it is assigned a value
in an assignment statement.
Advantage programming flexibility
20Example
- JavaScript, PHP
- List10.2, 3.5
- List47
21Disadvantages
- Readability
- Error detection
- Cost (time, memory)
223) Type inference
- Such as
- fun square (x real) xx
233. Storage Bindings and Lifetime
- Allocation
- Deallocation
- Lifetime
- Categories
- Static
- Stack-dynamic
- Explicit heap-dynamic
- Implicit heap-dynamic
241) Static variables
- Those are bound to memory cells before program
execution begins and remain bound to those same
memory cells until program execution terminates.
25Advantages
- Globally access
- Efficiency
- No run-time overhead for allocation and
deallocation
26Disadvantages
- Reduced flexibility
- Memory shareable problem
272) 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.
28Advantages
- memory can be shared
- Can work for recursive program
29Disadvantages
- Run-time overhead of allocation and deallocation
- No history sensitive
303) Explicit heap-dynamic variables
- Are nameless memory cells that are allocated and
deallocated by explicit run-time instructions
specified by the programmer.
31Further 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.
32example
- In C
- int intnode //create a pointer
-
- intnode new int //create the heap-dynamic
variable -
- Delete intnode // deallocate the heap-dynamic
variable
33example
- 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.
34Disadvantages
- Difficult to use
- Cost of references to the variables
- Complexity of storage management implementation
354) Implicit heap-dynamic variables
- Are bound to heap storage only when they are
assigned values.
36advantage
37Disadvantages
- Run-time overhead
- Difficult to detect error
- Storage management problems
385.5 Type Checking
- Type checking is the activity of ensuring that
the operands of an operator are of compatible
types.
39Static 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
40Dynamic type checking
- Dynamic type binding requires type checking at
run time, which is called dynamic type checking
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
425.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.
43Advantages and disadvantages
- Name types compatibility is easy to implement but
is highly restrictive. - Structure type compatibility is more flexible,
but difficult to implement.
445.7 Scope
- The scope of a variable is the range of
statements in which the variable is visible.
451. Static scope
- Which means the scope of a variable can be
statically determined, prior to execution.
46Example1
- 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
47Example 2
- void sub()
- int count
-
- while()
- int count
- count
-
482. Blocks
- Very like a function or a subprogram
49Example 1
- If (listi lt listj)
- int temp
- temp listi
- listi listj
- listj temp
50Some 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.
51Dynamic scope
- A variables scope is determined at running time,
rather than compile time.
52Example
- 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.
534. Evaluation of Static Scoping
A
C
main
D
B
E
545. Scope and lifetime
- They are different, but sometimes are related
55Example 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
56Example in C
- void printheader()
-
-
- void compute()
- int sum
-
- printheader()
-
576. Referencing Environments
- The referencing environment of a statement is the
collection of all variables that are visible in
the statement.
58Example 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
59Summary
- Names and variable
- Binding and type checking
- Type scope and lifetime