Chapter 5 - Programming Languages
Blocks
A method of creating static scopes inside program units - from ALGOL 60 • Creates stack-dynamic variables
Named Constants
A named constant is a variable that is bound to a value only when it is bound to storage • Advantages: readability and modifiability • Used to parameterize programs • The binding of values to named constants can be either static (called manifest constants) or dynamic
Advantages and disadvantages of implicit type declaration?
Advantage: writability Disadvantage: reliability (less trouble with Perl) • Compiler fails to catch typos in variable names
What is Explicit heap-dynamic binding and what are the advantages and disadvantages of it?
Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution Ex. New allocators in C++ Advantages: provides for dynamic storage management Disadvantage: inefficient (indirect addressing) and unreliable (error prone); also complex storage management implementation
Storage Bindings & Lifetime
Allocation - getting a cell from some pool of available cells Deallocation - putting a cell back into the pool
What is Implicit heap-dynamic and what are the advantages and disadvantages of it?
Allocation and deallocation caused by assignment statements Advantage: flexibility (generic code) • Disadvantages: - Inefficient, because all attributes are dynamic - Loss of error detection by compiler
What's a disadvantage of case sensitivity?
Bad readability
Dynamic Scope
Based on calling sequences of program units, not their textual layout (temporal versus spatial) References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point
Static scope
Based on program text • To connect a name reference to a variable, you (or the compiler) must find the declaration Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name • Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
Binding
Between an attribute and an entity
Global Scope
C, C++, PHP, and Python support a program structure that consists of a collection of function definitions in a file
Declaration Order
C99, C++, Java, and C# allow variable declarations to appear anywhere a statement can appear In C++, Java, and C#, variables can be declared in for statements
Advantages and disadvantages of dynamic scoping
Evaluation of Dynamic Scoping: - Advantage: convenience (called subprogram is executed in the context of the caller) - Disadvantage: (1) poor readability (programs are difficult to reason about), (2) no static type checking*, and (3) variables are visible to all called subprogram.
What are the possible binding times?
Language design time - bind operator symbols to operations Implementation time - binding floating point type to a representation Compile time - bind a variable to a data type in C or Java Load time - bind a C or C++ static variable to a memory cell Run Time -bind a nonstatic local variable to a memory cell
Properties of variables
Name - not all variables have them Address - the memory address with which a variable is associated Type - determines the range of values of variables and the set of operations that are defined for values of that type Value - the contents of the location with which the variable is associated The L-value of a variable is its address The R-value of a variable is its stored value Lifetime - the time during which it is bound to a particular memory cell Scope - where the variable can be accessed from
Type Inferencing
Rather than by declaration statements, types are determined (by the compiler) from the context of the reference - Add some type rules to handle ambiguous cases - Get advantage of static typing without the programmer having to use explicit declarations
What is the difference between static and dynamic binding?
Static: occurs before runtime AND remains unchanged throughout the execution Dynamic: first occurs during execution OR can change during execution of the program
What is stack-dynamic binding and what are the advantages and disadvantages of it?
Storage bindings are created for variables when their declaration statements are elaborated. Ex: local variables in C subprograms and Java methods Advantage: allows recursion; conserves storage Disadvantages: - Overhead of allocation and deallocation (not that significant since allocated en masse) - Subprograms cannot be history sensitive - Inefficient references (indirect addressing)
Variable initialization
The binding of a variable to a value at the time it is bound to storage is called initialization
Referencing Environments
The referencing environment of a statement is the collection of all names that are visible at the statement > A variable may exist but not be visible if it is shadowed by another variable > In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes (or static ancestors) > In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms > A subprogram is active if its execution has begun but has not yet terminated
How can a variable be hidden from a unit?
Variables can be hidden from a unit by having a "closer" variable with the same name
Evaluation of Static Scoping
Works well in most situations - Programs are easy to reason about (critical) • Problems: - In many cases, too much access is possible - Want variables to be accessed only where needed - As a program evolves, the initial structure is destroyed and local variables often become global; subprograms also gravitate toward becoming global, rather than nested
Variable
abstraction of a memory cell
Advantages and Disadvantages of dynamic type binding
advantages: flexibility Disadvantages: • High cost (run-time type checking, and interpretation rather than compilation) • Type error detection by the compiler is difficult
What is binding?
association of attributes with program entities
What is static binding and what are the advantages and disadvantages of it?
bound to memory cells before execution begins and remains bound to the same memory cell throughout execution EX: static variables in C++ and C Advantages: efficiency (direct addressing), globally accessible, history-sensitive subprogram support Disadvantage: lack of flexibility (no recursion), no shared storage
What is the difference between explicit and implicit type declarations?
explicit type declarations: a program statement used for declaring the types of variables implicit type declarations: default mechanism for specifying types of variables
Named Constants
is a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability Used to parameterize programs
What is the difference between keywords and reserved words?
keywords: special only in certain contexts Reserved words: special word that can't be used in a user defined
What are the six things that a variable is categorized by?
name type address value lifetime scope
Scope
scope of a variable is the range of statements over which it is visible nonlocal variables of a program unit are those that are visible but not declared there The scope rules of a language determine how references to names are associated with variables
What is the difference between scope and lifetime
scope: who can access it lifetime:how long a variable lasts
How can scalar variables be categorized?
static stack dynamic explicit heap dynamic implicit heap dynamic