Chapter 9 & chapter 10 solutions


Published on

Solutions of Concept of Programming Languages

Published in: Technology, Business
1 Comment
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chapter 9 & chapter 10 solutions

  1. 1. Theory of Programming LanguagesChapter 9 & 10Chapter 9Review Questions: 1. What are the three general characteristics of subprograms? Each subprogram has a single entry point, excluding co-routine. The calling program is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time. Control always returns to the caller when the subprogram execution terminates. 2. What does it mean for a subprogram to be active? A subprogram s said to be active if, after having been called, it has begun execution but has not yet completed that execution. 3. What is the parameter profile? What is subprogram protocol? The parameter profile of a subprogram is the number, order and types of its formal parameters. The protocol of subprogram is its parameter profile, if it is a function and its return type. 4. What are formal parameters? What are actual parameters? The parameters in the subprogram header are called formal parameters. Subprogram call statements must include the name of the subprogram and alist of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters. Double sales_tax (price) {return 0.05 * price;} Tax = sales_tax(10.0); 10.00 is actual parameter and “price” is formal parameter. 5. What are the advantages and disadvantages of keyword parameters? The advantage of keyword parameter is that they can appear in any order in the actual parameter list. The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters. 6. What are the design issues for subprograms? o What parameter-passing method or methods are used? o Are the types of the actual parameters checked against the types of the formal parameters? o Are local variable statically or dynamically allocated? o Can subprogram definitions appear in other subprogram definitions? o If subprograms can be passed as parameters and subprograms can be nested, what is the referencing environment of a passed subprogram? o Can a subprogram be overloaded? o Can subprograms be generic? Prepared By: Saeed Iqbal MSCS, FAST-NUCES, Peshawar P11-6501
  2. 2. Theory of Programming LanguagesChapter 9 & 10 7. What are the advantages and disadvantages of dynamic local variables? Advantages: o They provide flexibility to the subprogram o The storage of local variables in an active subprogram can be shared with the local variables in all inactive subprograms. o They efficiently used when computer has small memory (Faster Access). Disadvantages: o Cost of the time required to allocate o Access to dynamic local variable must be indirect o The stack dynamic local variables, subprograms cannot be history sensitive 8. What are the three semantic models of parameter passing? The three semantic models are in mode, out mode, and in-out mode; In mode: they can receive data from the corresponding actual parameter. Out mode: they can transmit data to the actual parameter In-out mode: they can do both (receive data and transmit data). 9. What are the modes, the conceptual modes of transfer, the advantages, and the disadvantages or pass-by-value, pass-by-result, pass-by-value-result, and pass-by-reference parameter-passing models? There are two conceptual models of how data transfers take place in parameter transmission. Either an actual value is copied or an access path is transmitted. The advantage of pass-by-value is its speed. The Disadvantages of pass-by-value are, when copies are used, additional storage is required. Storage and copy operations can be costly. Pass-by-result has all of the advantages and disadvantages of pass-by-value, but more disadvantages. An additional disadvantage is that there can be an actual parameter collision, because order of expressions matter. Pass-by-value-result has the same advantages and disadvantages as pass-by-value and pass-by- result with some more advantages. The largest extra advantage of pass-by-value-result is that it solves pass-by-references aliasing problems. An advantage of pass-by-reference is that it is efficient in both time and space. A disadvantage to pass-by-reference is the increase in time to access formal parameters because of the additional level of indirect addressing. Secondly, if only one way communication to the called subprogram is required, inadvertent and erroneous changes may be made to the actual parameter. Finally, aliasing should be expected with pass-by-reference. Since pass-by- reference makes access paths available to the called subprograms, it broadens their access to nonlocal variables. These aliasing problems lead to decreased readability and reliability. 10. In what ways can aliases occur with pass-by-reference parameters? Aliases can be occurring because pass-by-reference makes access paths available to the called subprograms. 11. What is the difference between the way original C and C89 deal with an actual parameter whose type is not identical to that of the corresponding formal parameter? In the Original C, neither the number of parameters not their types were checked. In C89, the formal parameters of functions can be defined in two ways. First they can be as in the original C; that is, the names of the parameters are listed in parentheses. Prepared By: Saeed Iqbal MSCS, FAST-NUCES, Peshawar P11-6501
  3. 3. Theory of Programming LanguagesChapter 9 & 10 12. What is the problem with Ada’s policy of following implementers to decide which parameters to pass-by-reference and which to pass-by-value-result.? 13. What are two fundamental design considerations for parameter-passing methods? Two important considerations are involved in choosing parameter-passing methods: efficiency and whether one-way or two-way data transfer is needed. In-mode parameters should be used whenever no data are to be returned through parameters to the caller. Out-mode parameters should be used when no data are transferred to the called subprogram. In-Out mode parameters should be used only when data must move in both directions between the caller and the called subprogram. 14. What are the two issues that arise when subprogram names are parameters? The first issue that arises is type checking the parameters of the activations of the subprogram that was passed as a parameter. The second complication appears in languages that allow nested subprograms. There is another issue related to subprogram names that are passed as parameters. The question is what referencing environment for executing the pass subprogram should be used. 15. Define Shallow and deep binding for referencing environment of subprograms that have been passed as parameters? The environment of the call statement that enacts the passed subprogram is the environment for the passing subprogram. This is called shallow binding. The environment of the definition of the passed subprogram is the environment of the passing subprogram. This is called deep binding. 16. What is overloaded subprogram? Overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment 17. What is parametric polymorphism? Parametric polymorphism is provided by a subprogram that takes a generic parameter that is used in a type expression that describes the types of the parameters of the subprogram. Both Ada and C++ provides a kind of compile-time parametric polymorphism. 18. What causes a C++ template function to be instantiated? C++ template functions are instantiated implicitly either when the function is named in a call or when its address is taken with the & processor. 19. In what fundamental way do the generic parameters to a Java 5.0 generic methoddiffer from those of C++ methods? Java does not use objects exclusively, java have no enumeration or record type. Whereas C++ Classes can be defined to have no parent, that is not possible in Java. All Java Classes must be subclass of the root class. 20. If a Java 5.0 method returns a generic type, what type of object is actually returned? In Java any type or class can be returned by methods. Because methods are not types, they cannot be returned. 21. If a Java5.0 generic method is called with three different generic parameters, how many versions of the method will be generated by the compiler? 22. What are the design issues for functions? Two design issues are functions. i. Are side effects allowed? ii. What types of values can be returned? 23. In what ways are coroutines different from conventional subprogram? Prepared By: Saeed Iqbal MSCS, FAST-NUCES, Peshawar P11-6501
  4. 4. Theory of Programming LanguagesChapter 9 & 10 Conventional subprograms are subordinate to their callers. When a routine calls a subprogram, execution suspends at that point in the program and resumes after the subprogram has run to completion. As a result, conventional subprogram invocation is atomic, much like a built-in statement in the programming language. Prepared By: Saeed Iqbal MSCS, FAST-NUCES, Peshawar P11-6501
  5. 5. Theory of Programming LanguagesChapter 9 & 10 Chapter 10 Review Questions: 1. What are the two reasons why implementing subprograms with stack-dynamic local variables is more difficult than implementing simple sub-programs? A stack-dynamic local variable is more complex activation records. The compiler must generate code to cause implicit allocation and de-allocation of local variables Recursion must be supported (adds the possibility of multiple simultaneous activations of a subprogram). 2. What is the difference between an activation record and activation record instance? The Format, or layout, of the non-code part of a subprogram is called an activation record. An activation record stores all the information about subprogram calls,activation records stores the following data (in the following order) Return address Static link – to the static parent (where the subprogram is declared). Dynamic link – to the caller of this subprogram. Parameters Local variables. 3. Why are the return address, dynamic link, and parameters placed in the bottom of the activation record? Ans: ? 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 5. Define static chain, static depth, nesting_depth, and chain offset. A static chain is a chain of static links that connects certain activation record instances Static_depth is an integer associated with a static scope representing the scope’s nesting depth The chain_offset or nesting_depth of a non-local reference is the difference between the static_depth of the reference and that of the scope where it is declared 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. Prepared By: Saeed Iqbal MSCS, FAST-NUCES, Peshawar P11-6501
  6. 6. Theory of Programming LanguagesChapter 9 & 10 8. Explain how areference to a nonlocal variable is found when a display is used? Access to nonlocal variables using a display requires two steps for every access. First the link to the correct activation record, which resides in the display, is found using a statically computed value called the display offset. The local offset within the activation record instance is computed and used exactly as with static chain implementations. A nonlocal reference is represented by an ordered pair of integer (display offset and local offset) 9. How are references to variables represented in the static chain method? This chain can obviously be used to implement the access to non-local variables in static- scoped languages. When a reference is made to a non-local variable, the ARI containing the variable can be found by searching the static chain until a static ancestor ARI is found that contains the variable. Because the nesting scope is known at compile time, the compiler can determine not only that a reference is non-local but also the length of the static chain must be followed to reach the ARI that contains the non-local object. 10. Explain the two methods of implementing block? Blocks are treated as parameter less subprograms that are always called from the same place in the program. Block can also be implemented in a different and somewhat simpler and more efficient way. The maximum amount of storage required for block variables at any time during the exaction of program can be statically determined, because block are entered and exited in strictly textual order. 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 12. Describe the shallow access method of implementing dynamic scoping? In case of shallow access names and values are stored in a global table. Using this method, space is allocated for every variable name that is in the program (one space for variable temp though there might be several declarations of temp in the different methods). When a sub-routine is called it saves the current value of the variable and replaces it with the value in its current scope and restores the value of the variable while exiting. 13. What are the two differences between the deep access method for non-local access in dynamic-scoped languages and the static chain method for static scoped languages? Deep Access method for nonlocal access is found bysearching the activation record instances of other subprograms that are currently active, beginning with the one most recently activated. In static Chain, Compiler simply passes the link to the Static parameter, along with the parameter. Prepared By: Saeed Iqbal MSCS, FAST-NUCES, Peshawar P11-6501
  7. 7. Theory of Programming LanguagesChapter 9 & 10 14. Compare the efficiency of the deep access method to that of the shallow access method, in term of both call and nonlocal access? The deep access methods provides fast subprogram linkage, but references to nonlocal, especially references to distant nonlocals (in term of the call chain), are costly. The shallow access methods provide much faster references to nonlocals, especially distant nonlocals, but are more costly in term of subprogram linkage. Prepared By: Saeed Iqbal MSCS, FAST-NUCES, Peshawar P11-6501