Wednesday, June 26, 2013

Concepts of Programming Languages ---- Chapter 10 Implementing Subprograms

Concepts of Programming Languages ---- Chapter 10 Implementing Subprograms

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 10 Implementing Subprograms


Review Questions
4.   What is the task of a linker?
The task of a linker is to find the files that contain the translated subprograms referenced in that  and load them into memory
5.   What are the two reasons why implementing subprograms with stack-dynamic local variables is more difficult than implementing simple subprograms?
       ·         the compiler must generate code to cause the implicit allocation and deal-location of local 
       variables
       ·         recursion adds the possibility of multiple simultaneous activations of a subprogram which 
       means that there can be more than one instance at a given time.
10. Define static chain, static_depth, nesting depth and chain_offset
       ·         Static chain is a chain of static links that connect certain activation record instances in the 
       stack.
       ·         Static depth is an interger associated with a static scope that indicated how deeply it is 
       nested  in the outermost scope.
       ·         Nesting depth is the length of the static chain needed to reach the correct activation record 
       instance for a nonlocal reference to a variable X
       ·         Chain_offset is the length of static depth of the subprogram containing the reference to X 
       and  declaration for X.
11. What is an EP and what is its purpose?
EP(environment pointer) is used to access parameters and local variables during the execution of a subprogram. Its purpose is used as the base of the offset addressing of the data contents of the activation record instance - parameters and local variable or in other meaning, it only saved versions are stored in the activation record instances as the dynamic links.
15. Explain the two methods of implementing blocks
      ·         deep access
      ·         shallow access
16. Describe the deep-access method of implementing dynamic scoping
The dynamic chain links together all subprogram activation record instances in the reverse of the order in which they were activated. The dynamic chain is exactly what is needed to reference nonlocal variables in a dynamic-scoped language.
17. Describe the shallow-access method of implementing dynamic scoping
An alternative implementation method, not an alternative semantics. In the shallow-access method, variables declared in subprograms are not stored in the activation records of those subprograms.

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.
7.   It stated in this chapter that when nonlocal variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every nonlocal access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.
One very simple alternative is to assign integer values to all variable names used in the program. Then the integer values could be used in the activation records, and the comparisons would be between integer values, which are much faster than string comparisons.
8.   Pascal allows gotos with nonlocal targets. How could such statements be handled if static chains were used for nonlocal variable access? Hint: Consider the way the correct activation record instance of the static parent of a newly enacted procedure is found (see Section 10.4.2).
Following the hint stated with the question, the target of every goto in a program could be  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.
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?
There are two options for implementing blocks as parameterless subprograms: One way is to use the same activation record as a subprogram that has no parameters. This is the most simple way, because accesses to block variables will be exactly like accesses to local variables. Of course, the space for the static and dynamic links and the return address will be wasted. The alternative is to leave out the static and dynamic links and the return address, which saves space but makes accesses to block variables different from subprogram locals.


Concepts of Programming Languages ---- Chapter 9 Subprograms

Concepts of Programming Languages ---- Chapter 9 Subprograms

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 9 Subprograms



Review Questions
1.   What are the three general characteristics of subprograms?
       ·         each subprogram has a single entry point
       ·         the calling program unit is suspended during the execution of the called subprogram
       ·         control always returns to the caller when the subprogram execution terminates
2.   What does it mean for a subprogram to be active?
A subprogram is said to be active after having been called, it has begun execution but has not yet completed that execution.
6.   What is ruby array formal parameter?
ruby array formal parameter is the single parameter which followed by the hash item which preceded by an asterik. Ruby allows a variable number of parameters in a way similar to C# because ruby arrays can store different types.
7.   What is a parameter profile?  what is a subprogram protocol?
      Parameter profile is a subprogram contains the number, order , and types of its formal 
      parameters. Subprogram protocol is a subprogram which its parameter profile plus, if it is a  
      function , its return type. In languages in which subprogram have types, those types are 
      defined by the subprogram's protocol.
11. What are the design issues for subprograms?
       ·         are local variables statically or dynamically allocated?
       ·         can subprogram definitions appear in other subprogram definitions?
       ·         what parameter-passing method or methods are used?
       ·         are the types of the actual parameters checked against the types of the formal parameters?
       ·         if a subprograms can be passed as parameters and subprograms can be nested, what is the    
       referencing environment of a passed subprogram?
       ·          can subprograms be overloaded?
       ·          can subprograms be generic?
       ·         if the language allows nested subprograms , are closures supported?
12. What are the advantages and disadvantages of dynamic local variables?
Advantages
       ·         being flexibility they provide to the subprogram
       ·         the storage for local variables in an active subprogram can be shared with the local 
      variables in all inactive subprograms.
Disadvantages
       ·         there is the cost of time required to allocate, initialize and deallocate such variables for each 
       call to the subprogram.
       ·         accesses to stack- dynamic local variables must be indirect.
       ·         when all local variables are stack dynamic, subprograms cannot be history sensitive.
24. What is an overloaded subprogram?
Overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment
25. What is ad hoc binding?
Ad hoc binding is the environment of the call statement that passed the subprogram as an actual parameter
26. What is multicast delegate?
Multicast delegate is all of the methods stored in a delegate instance are called in the order in which they were placed in the instance.

Problem Set
3.   Argue in support of the template functions of C++. How is it different from the template functions in other languages?
      C++ templated classes are instantiated to become typed classes at compile time. For example, an instance of the templated Stack class, as well as an instance of the typed class, can be created with the following declaration: Stack<int> myIntStack; However, if an instance of the templated Stack class has already been created for the int type, the typed class need not be created.
4.   Suppose you want to write a method that prints a heading on a new output page, along with a page number that is 1 in the first activation and that increases by 1 with each subsequent activation. Can this be done without parameters and without reference to nonlocal variables in Java? Can it be done in C#?
      This can be done in both Java and C#, using a static (or class) data member for the page number.
11. Compare the use of closures by programming languages.
Nearly all functional programming languages, most scripting languages, and at least one primarily imperative language, C#, support closures. These languages are static-scoped, allow nested subprograms, and allow subprograms to be passed as parameters.
12. Research Jensen’s Device, which was a widely known use of pass-by-name parameters, and write a short description of what it is and how it can be used.
      Implementing a pass-by-name parameter requires a subprogram to be passed to the called subprogram to evaluate the address or value of the formal parameter. The referencing environment of the passed subprogram must also be passed. This subprogram/referencing environment is a closure. Pass-by-name parameters are both complex to implement and inefficient. They also add significant complexity to the program, thereby lowering its readability and reliability. Because pass-by-name is not part of any widely used language, it is not discussed further here. However, it is used at compile time by the macros in assembly languages and for the generic parameters of the generic subprograms in C++, Java 5.0, and C# 2005.
15. How is the problem of passing multidimensional arrays handled by Ada?
Ada compilers are able to determine the defined size of the dimensions of all arrays that are used as parameters at the time subprograms are compiled.

Concepts of Programming Languages ---- Chapter 8 Statement-Level Control Structures

Concepts of Programming Languages ---- Chapter 8 Statement-Level Control Structures

Name : Fandy Limardi
NIM : 1601210713
Lecture : Tri Djoko Wahjono, Ir., M.Sc. (D0206)
Assignment : Concept of programming languages ---- Chapter 8 Statement-Level Control Structures


Review Questions
1.   What is the definition of control structure?
  structure is a control statement and the collection of statements whose execution it controls.
2.   What did Bohm and Jocopini prove about flowcharts?
      they proved that all algorithms that can be expressed by flowcharts can be coded in a programming languages with only two control statements: one for choosing between two control flow paths and one for logically controlled iterations.
3.   What is the definition of block?
      Block is a sequence of code delimited by either braces or the do and end reserved words.
7.   Under what circumstances must an F# selector have an else clause?
      An F# selector have an “else” clause if  the “if” expression does return a value.
12. On what previous language was C’s switch statement based?
The ability to have control flow from one selectable segment to another is rarely used. C’s switch is modeled on the multiple-selection statement in ALGOL
68, which also does not have implicit branches from selectable segments
14. What are the design issues for all iterative control statements?
  • How is the iteration controlled?
  • Where should the control mechanism appear in the loop statement?
21. What are the design issues for all iterative control statements?
  • How is the iteration controlled?
  • Where should the control mechanism appear in the loop statement?
22. What is the main reason user-located loop control were invented?
It is convenient for a programmer to choose a location for loop control other than the top or bottom of the loop body. As a result, some languages provide this capability. A syntactic mechanism for user-located loop control can be relatively simple, so its design is not difficult. Such loops have the structure of infinite loops but include user-located loop exits.
23. What are the design issues for user-located loop control mechanisms?
The design issues for such a mechanism are the following:
• Should the conditional mechanism be an integral part of the exit?
• Should only one loop body be exited, or can enclosing loops also be exited?
26. What is a user-defined iteration control?
A user-defined iteration control is the one that issues a special call to the iterator, in which the iterator is called at the beginning of each iteration, and each time it is called, the iterator returns an element from a particular data structure in some specific order.

Problem Set
1.   What design issues should ne considered for two-way selection statements?
      The design issues for two-way selectors can be summarized as follows:
       ·         What is the form and type of the expression that controls the selection?
       ·         How are the then and else clauses specified?
       ·         How should the meaning of nested selectors be specified?
4.   What are the limitations of implementing a multiple selector from two-way selectors and 
      gotos?
A multiple selector can be built from two-way selectors and gotos, but the resulting structures are cumbersome, unreliable, and difficult to write and read.
5.   What are the arguments , pro and con, for Java’s approach to specify compound statements in control statements?
       ·         Compound statements are required in control statements when the body of the if or else 
             clause requires multiple statements.
       ·         Java uses braces to form compound statements, which serve as the bodies of if and else 
             clauses.
11. Explain the advantages and disadvantages of the Java switch statement, compared to C++’s switch statement.
The Java variable in the argument of a switch statement can be of type integral ( byte, short etc.), char and String( JDK 1.7 onwards), whereas in C++ the argument can be int or char.