Pages

Sunday 23 June 2013

Chapter 10

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (5 numbers, quite randomly in Review Qustion and 4 numbers in Problem Set) in Chapter 10 in "Concept of Programming Language" book.

Review Question

4. What are the two steps in locating a nonlocal variable in a static-scoped language with stack-dynamic local variables and nested subprograms?
  • Find the correct activation record instance
  • Determine the correct offset within that activation record instance
6. What are the two potential problems with the static chain methods?
  • A nonlocal reference is slow if the number of scopes between the reference and the declaration of the referenced variable is large
  • Time-critical code is difficult, because the costs of nonlocal references are not equal, and can change with code upgrades and fixes
7. What is display?
One alternative to static chain is to use a display, for this approach, the static links are collected in a single array called a display. Display uses a pointer array to store the activation records along the static chain.

11. Describe the deep access method of implementing dynamic scoping?
Deep Access – nonlocal references are found by searching the activation record instances on the dynamic chain. Length of chain cannot be statically determined every activation record instance must have variable names

16. Describe the deep-access method of implementing dynamic scoping.
The dynamic chain links together all subprogram activation records instances in the reverse of the order in which they were activated. Therefore, the dynamic chain is exactly what is needed to reference nonlocal variables in a dynamic-scoped language.

Problem Set
6. Although local variables in Java methods are dynamically allocated at the beginning of each activation, under what circumstances could the value of a local variable in a particular activation retain the value of previous activation ?
If the variable is declared as static. Static modifier is a modifier that makes a variable history – sensitive.

8. Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access?
Following the hint stated with the question, the target of every goto in a program could be represented as an address and a nesting_depth, where the nesting_depth is the difference between the nesting level of the procedure that contains the goto and that of the procedure containing the target. Then, when a goto is executed, the static chain is followed by the number of links indicated in the nesting_depth of the goto target. The stack top pointer is reset to the top of the activation record at the end of the chain.

9. The static-chain method could be expanded slightly by using two static links in each activation record instance where the second points to the static grandparent activation record instance. How would this approach affect the time required for subprogram linkage and nonlocal references?
Including two static links would reduce the access time to nonlocals that are defined in scopes two steps away to be equal to that for nonlocals that are one step away. Overall, because most nonlocal references are relatively close, this could significantly increase the execution efficiency of many programs.

11. If a compiler uses the static chain approach to implementing blocks, which of the entries in the activation records for subprograms are needed in the activation records for blocks?
Blocks are treated as parameterless subprograms that are always called from the same place in the program.

No comments:

Post a Comment