Slide 1

advertisement
Names and Bindings
Names
 A name is a string of characters used to identify some entity
in a program
 Several design choices are involved even in specifying
allowable names
 Case sensitivity
 Minimum or maximum length
 Connectors
 Treatment of special words
Length
 If too short, names cannot be connotative
 If too long, extra space required for the symbol table
 Some examples:
 FORTRAN I: maximum 6
 COBOL: maximum 30
 FORTRAN 90 and ANSI C: maximum 31
 Ada and Java: no limit, and all are significant
 C++: technically no limit, but implementers often impose
one
Names continued
 Most PLs have the same form for names: a letter followed by a
string of letter, digits and underscores
 Underscores very popular in 1970s and 1980s to represent
spaces
 Replaced in C-based languages by camel notation
 Prior to Fortran90, names could contain spaces
 C-based languages are case sensitive
 Detriment to readability?
 Writability questionable?
Special Words
 Aid in readability
 Used to delimit or separate statement clauses
 Keyword is special only in certain contexts
 Fortran is the only remaining widely used language whose
special words are keywords
 Reserved words cannot be used as names
 Cannot be redefined
 Potentially less confusing, but too many means user has trouble
making up new ones
 Predefined names are between special words and user-
defined names
 Can be redefined
Variables
 Abstraction of a computer memory cell or collection of cells
 Made up of six attributes:
 Name
 Address
 Value
 Type
 Lifetime
 Scope
Address of a Variable
 The machine memory address with which a variable is
associated
 Can change during the course of execution
 Different addresses at different times during execution
 Different addresses at different locations in a program
 Sometimes referred to as l-value
 left-hand side of an assignment
 Multiple variables can have the same address, in which case the
variables are called aliases
 Hindrance to readability
 Can be created in several ways
Variable Type
 Determines range of values the variable can store
 Determines the set of operations that are defined for values
of that type
 E.g. int of Java specifies:
 Range of -2147483648 to 2147483647
 Arithmetic operations +, -,*, /,%
Variable Value
 The contents of the memory cell or cells associated with the
variable
 Abstract memory cell has the size required by the variable
with which it is associated
 The value of each simple, non-structured type is considered to
occupy a single abstract memory cell
 E.g. Though a floating point number may occupy 4 physical
bytes, the value is thought of as occupying a single abstract
memory cell`
 R-value, since this is what is required when used on the RHS
of an assignment statement
Binding
 A binding is an association
 Attribute and an entity
 Operation and a symbol
 The time when the association takes place is the binding time
 Both binding and binding time are important for semantics of
programming languages
Possible Binding Times
 Language design time -- bind operator symbols to operations
 Language implementation time -- bind floating point type to
a representation
 Compile time -- bind a variable to a type in C or Java
 Load time -- bind a FORTRAN 77 variable to a memory cell
(or a C static variable)
 Runtime -- bind a nonstatic local variable to a memory cell
Java assignment example
 Count = count + 5;
 Type of count bound at compile time
 Set of possible values bound at compiler design time
 Meaning of + bound at compile time (when types of operands
have been determined)
 Internal representation of literal 5 bound at compiler design time
 Value of count is bound at execution time with this statement
Static vs. Dynamic Bindings
 Static binding
 First occurs before run time AND
 Remains unchanged throughout program execution
 Dynamic binding
 First occurs at run time OR
 Can change in the course of program execution
Type Binding
 Before a variable can be referenced in a program, it must be
bound to a data type
 Two questions:
 When does type binding take place?
 How is type specified?
Static Type Binding
 Explicit declaration lists variable names and specifies that
they are a particular type
 Implicit declaration associates variables with types though
default conventions
 First appearance constitutes its implicit declaration
 Both versions in use today
 Advantage of implicit: writability
 Disadvantage: reliability (though less so in Perl)
Dynamic Type Binding
 Type of variable is determined when it is assigned a value
 Advantage
 Programming flexibility
 Disadvantages
 Reduced reliability
 Cost
 Usually implemented in languages with interpreters rather
than compilers
Type Inference
 Not by assignment, but by context
 E.g ML function declaration:
 Fun circumf(r) = 3.14159 * r * r
 Floating point (real) type inferred from the constant
 Fun square(x) = x * x
 Arithmetic operator * indicates numeric type, so return type and
arguments are default numeric type int
Allocation and Deallocation
 Allocation
 Process by which an available memory cell is taken from a
pool of available memory and bound to a variable
 Deallocation
 Process of placing a memory cell that has been unbound from
a variable back to the pool of available memory
Lifetime
 The time during which the variable is bound to a specific
memory location
 Scalar variables fall into four categories based on their
lifetimes:
 Static
 Stack-dynamic
 Explicit heap-dynamic
 Implict heap-dynamic
Static Variables
 Bound to memory locations before program execution begins
 Remain bound throughout the program execution
 Advantages:
 Efficiency through direct accessing
 History sensitive subprogram support
 Disadvantages
 Reduced flexibility (no recursion, no shared storage)
Stack-Dynamic Variables
 Storage bindings are created when their declaration statements are
elaborated, but type is dynamically bound
 Elaboration refers to the storage allocations and binding process
indicated by the declaration
 Occurs during run-time
 If scalar, all attributes except address are statically bound
 Advantages
 Allows recursion, conserves storage
 Disadvantages
 Overhead of allocation and deallocation
 Subprograms cannot be history sensitive
 Inefficient references (indirect addressing)
Explicit Heap-Dynamic Variables
 Allocated and deallocated by explicit directives, specified by
the programmer, which take effect during execution
 Referenced only through pointers or references
 E.g. dynamic objects in C++ (via new and delete), all objects in
Java
 Advantage
 Provides for dynamic storage management
 Disadvantage
 Inefficient and unreliable
Implicit Heap-Dynamic Variables
 Bound to heap storage only when they are assigned values
(i.e. assignment statements)
 E.g. all strings and arrays in Perl and JavaScript
 Advantage
 Extremely flexible
 Disadvantage
 Inefficient, since all attributes are dynamic
 Loss of error detection
Download