Concepts of Programming Languages - Chapter 5 (Names, Bindings, and Scopes) Part 2
What are the advantages and disadvantages of implicit declarations?
Advantage: writability. Disadvantages: reliability and readability.
What are the pros and cons of dynamic scoping?
Advantages: Writability and flexibility Disadvantages: Readability, reliability 1. Inability to statically check for references to nonlocal variables. 2. Dynamics scoping also makes program difficult to read because the calling sequence of subprograms must be known to determine the meaning of references to nonlocal variables. 3. There's no way to protect local variables from being accessed to by subprograms because local variables of subprogram are all visible to all other executing subprograms, regardless of textual proximity. 4. Accessing to nonlocal variables in dynamic scoping takes far longer than accesses to nonlocal variables when static scoping is used.
What are the advantages and disadvantages of dynamic type binding?
Advantages: flexibility and no definite types declared(PHP, JavaScript). Disadvantages: adds to the cost of implementation and type error detection by the compiler is difficult.
Dynamic type binding is closely related to implicit heap dynamic variables. Explain this relationship.
Implicit heap dynamic variables are bound to a type at runtime when a value is assigned to a variable. Dynamic type binding is the binding of a type to a variable at runtime or changing the type of a variable during runtime. Implicit heap dynamic variables use dynamic type binding.
Define lifetime, scope, static scope, and dynamic scope
Lifetime of variable is time during which the variable is bound to a specific men location. Scope of variable is the range of statements over which it is visible. Static scope is binding names to non-local variables. Dynamic scope is based on the calling sequence of subprograms, not on their spatial relationship to each other. Thus the scope can be determined only at run time.
What is the general problem with static scoping?
Programmer could mistakenly call subprogram that should not have been callable, which would not be detected as an error by the compiler. Too much data accesses and verifications. Encourages the use of more global than are necessary and not preferable to programming
What is a block?
Section of code to have its own local variables whose scope is minimized. Such variables are typically stack dynamic, so their storage is allocated when the section is entered and deallocated when the section is exited. Such a section of code is called a block.
Define static binding and dynamic binding?
Static binding: if it first occurs before run time and remains unchanged throughout program execution. Dynamic binding: if it first occurs during execution or can change during execution of the program.
Define static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic variables. What are their advantages and disadvantages?
Static: bound to memory cells before execution begins and remains bound to the same memory cell throughout the execution. Stack-dynamic: storage bindings are created for variables when their declaration statements are elaborated. Explicit heap-dynamic: allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution. Implicit heap-dynamic variables: Allocation and deallocation caused by assignment statements.
What is a static ancestor of a subprogram? What is a dynamic ancestor of a subprogram?
The static ancestors of a subprogram sub() are all the procedures in the program within which the procedure sub() is defined, i.e., the definition of the procedure sub() is nested. The definition of a procedure may be directly nested within only one procedure, called its static parent procedure. However, this static parent procedure may itself be nested within another procedure, and so on up to the main() program. All these procedures are considered to be static ancestors of the procedure sub(). Simply put, the static ancestors are those that strictly contain the subprogram in question. The dynamic ancestors of a subprogram sub() are all the procedures called before sub() during the execution of a program, that have not yet finished executing. These are the procedures that are waiting for procedure sub() to finish executing before they can terminate. Simply put, dynamic ancestors are those that are called to reach the subprogram in question.
What are the pros of named constants?
Useful as aids to readability and program reliability. Readability by using pi instead of the constant 3.14159265. Parameterize a program.
What is the referencing environment of a statement?
the collection of all variables that are visible in the statement