Pada kesempatan kali ini saya akan menjawab soal-soal yang ada dalam buku "CONCEPTS OF Programming Languages (TENTH EDITION)" - ROBERT W. SEBESTA. Chapter 5 Names, Bindings, and Scopes
REVIEW QUESTION
11. Question : What are the advantages and disadvantages of dynamic type binding ?
Answer :
Advantages = It is more easy to write generic code.
Disadvantages = High Cost to check type and interpretation
12. Question : Define static, stack-dynamic, explicit heap-dynamic, and implicit heapdynamic
variables. What are their advantages and disadvantages ?
Answer :
- Static -- memory alloc'd before execution. Stays throughout.
Adv: runtime efficient, history-sensitive subprograms. Disadv: no recursion.
Adv: runtime efficient, history-sensitive subprograms. Disadv: no recursion.
- Stack dynamic - memory alloc'd from system stack when decl elaborated.
Adv: recursion, conserves memory. Disadv: no history sensitive subpgms, less run-time efficient than static.
- Explicit heap-dynamic -- memory alloc'd/freed by pgmer from "heap".
Adv: flexible storage mgmt. Disadv: less effiicent than stack-dyn; Error prone in pairing new/delete stmts.
Adv: flexible storage mgmt. Disadv: less effiicent than stack-dyn; Error prone in pairing new/delete stmts.
- Implicit heap-dynamic -- heap memory alloc'd/freed by assignments.
Adv: highext flexibility. Disadv: run-time cost to maintain all the dynamic attrs; Reduced compiler error detection.
Adv: highext flexibility. Disadv: run-time cost to maintain all the dynamic attrs; Reduced compiler error detection.
13. Question: Define lifetime, scope, static scope, and dynamic scope.
Answer :
Lifetime -- when memory is alloc'd for a variable (when in existence).
Scope -- portions of pgm variable can be referenced or set.
Static scope -- scope based on source pgm text (static ancestors).
Dynamic scope -- scope based on run-time call sequence.
Scope -- portions of pgm variable can be referenced or set.
Static scope -- scope based on source pgm text (static ancestors).
Dynamic scope -- scope based on run-time call sequence.
14. Question : How is a reference to a nonlocal variable in a static-scoped program connected
to its definition ?
Answer :
A reference to a non-locally variable in a static-scoped language with nested subprograms requires a two step access process:
1. Find the correct activation record instance
2. Determine the correct offset within that activation record instance
1. Find the correct activation record instance
2. Determine the correct offset within that activation record instance
15. Question : What is the general problem with static scoping ?
Answer :
Usually too much access. Scope structure destroyed as pgm evolves.
Problem Set
1. Question : Which of the following identifier forms is most readable? Support your
decision.
SumOfSales
sum_of_sales
SUMOFSALES
Answer : Sum_of_sales is the most readable. It is because it’s doesn’t have any problem with case sensitive, because the following identifier doesn’t use any caps lock.
2. Question : Some programming languages are typeless. What are the obvious advantages
and disadvantages of having no types in a language?
Answer :
Advantages:- The only advantage is that is allows coders to write sloppy programs quickly.
Disadvantages:
- You are not in control of the data and variables, the compiler or interpreter is.
- If you mis-assign variables, there is no way for the compiler to catch any of your mistakes. It just “does what you said”, even if it is wrong.
- Supporting programs in a type-less language is much more difficult that in a strongly types one. It is often very difficult to determine what the original programmer wanted to do.
3. Question : Write a simple assignment statement with one arithmetic operator in some
language you know. For each component of the statement, list the various
bindings that are required to determine the semantics when the statement is
executed. For each binding, indicate the binding time used for the language.
Answer :
(C++)
int count;count = count + 5;
int count;count = count + 5;
Possible types for count: set at language design time. Type of count: bound at compile time.
Set of possible values of count: bound at compiler design time. Value of count: bound at execution time with this statement. Set of possible meanings for the operator symbol ““:*bound at language definition time.*Meaning of the operator symbol “” in this statement: bound at compile time.
Internal representation of the literal “5”: bound at compiler design time.
Set of possible values of count: bound at compiler design time. Value of count: bound at execution time with this statement. Set of possible meanings for the operator symbol ““:*bound at language definition time.*Meaning of the operator symbol “” in this statement: bound at compile time.
Internal representation of the literal “5”: bound at compiler design time.
4. Question : Dynamic type binding is closely related to implicit heap-dynamic variables.
Explain this relationship.
Answer : Implicit heap-dynamic variables acquire types only when assigned value, which must be at runtime. Therefore, this variable are always dynamically bound to types.
5. Question : Describe a situation when a history-sensitive variable in a subprogram is
useful.
Answer : To describe a situation when a history-sensitive variable in a subprogram is useful, suppose that a FORTRAN subroutine is used to implement a data structure as an abstraction. In this situation, it is essential that the structure persists between different calls to the managing subroutine.