Theory of Programming LanguagePosted by Winnie Melda on October 4th, 2018 Chapter 3 Question 3.1 i. The number of built-in functions is specifically bound at language design time. However, it can increase by specific implementations. ii. The variable declaration that corresponds to a particular reference is bound at compile time iii. The maximum length of the character string in case there is a limit is bound at a language implementation time iv. The referencing environment for a subroutine which is passed as a parameter is same as the environment at the declaration of subroutine v. The address of a particular library function is bound by the linker in most of the systems. However, it may not be easily known until load time and run time in the systems performing dynamic linking. vi. The total amount of space occupied by program code and data is bound at run time. Additionally, the amount of stack and heap space that is required will also depend on the output. Question 3.2 i. Fortran 77 does not have a recursion and therefore, there is no more than one live instance of the local variables of a certain subroutine. Algol together with its descendants needs a stack to enable accommodation of multiple copies. The following is not going to work correctly with statically allocated local variables: function sum(f, low, high) if low=high return f(low) else return f(low) + sum(f, low + 1, high) ii. Algol and its descendants have a limitation in extent for local variables while lisp and its descendants should be allocated in the heap thus accommodating unlimited extent. The following is not likely to work well with stack-allocated variables: function add_n(n) return {function (k) return n+k} Question 3.3 i. Some different aspects of code generation might be delayed until link time to help in facilitating the improvement of whole code ii. Local variables on Fortran 77 might be allocated on the stack instead of static memory. This will minimize imprint of memory and facilitate interoperability with standard tools. Question 3.4 i. If procedure P declares a local variable name x in Ada, then a global variable with similar name x will be live but not in scope in the execution of P ii. In C, a static variable declared within a function becomes live but not in scope when executed outside the function. iii. In C++, the non-public fields of an object of the class C are live although not in scope when they are executed outside a method of C Question 3.5 a) 94214 b) c) A finds g by dereferencing its static link thus finding the sack frame of B. It finds B’s static link at an offset known statically within this frame. It then dereferences that to find the stack frame of main where it finds g again at an offset known statically. Question 3.6 a) The reverse_list routine lead to the production of a new list comprising of the nodes. Upon assigning of the return value back into L by Brad, he is bound to lose track of the nodes from the old list and will never reclaim them. The program has a memory leak. After several iterations of the main loop, Brad will exhaust the heap thus discontinuing the program. b) Although the call delete_list reclaims the old list of nodes successfully, it also reclaims widgets. The resultant reversed list comprises of dangling references. The dangling references refer to the locations within the heap that are likely to be used for the newly allocated data. The newly allocated data may also be corrupted because of the use of elements within the reversed list. Brad is lucky because he has not corrupted the heap. However, his widgets might be of the same size as list nodes. Without the help from Jane, Brad might have a lot of challenges as he tries to figure out why widgets have a spontaneous changing of value. Question 3.7 i. The module-as-abstraction which is equivalent of the Figure 3.7 with header file stack.h #define STACK_SIZE 100 Typedef in element; extern void push (element); extern element pop(); Implementation file stack.c; #include #include #include “stack.h” Typedef short stack_index; Element s[STACK_SIZE]; Stack_index top = 0; static void error (const char* err){ fprintf (stderr, “stack error: %s\n”, err); exit(1); } void push (element elem) { if (top ==STACK_SIZE) error (“overflow”); else s[top++] = elem; } Element pop (){ If (top == 0) error (“underflow”); Else return s[--top]; } Client file: #include #include “stack.h” Int main (){ Element x, y; x = 3; push (x); y = pop (); printf (“y==%d\n”, y); } ii. The module-as-manager which is equivalent to Figure 3.8 with header file stack_manager.h #define STACK_SIZE 100 typedef int element; typedef short stack_index; typedef struct { element s (STACK_SIZE); stack_index top; }stack extern void init_stack (stack*); extern void push (stack*, element); extern element pop (stack*); Implementation of file stack_manager.c: #include #include #include “stack_manager.h” Static void error (const char* err) { Fprintf (stderr, “stack error: %s\n”, err); exit (1); } extern void init_stack(stack* stk){ stk->top = 0; } extern void push (stack* stk, element elem){ if (stk ->top== STACK_SIZE) error (“overflow”); else stk ->s[stk->top++] = elem; } extern element pop (stack* stk) { if (stk - >top == 0) error (“underflow”); else return stk - >s[--stk->top]; } Client file #include #include “stack_manager.h” Int main (){ Element x, y; stack A, B; init_stack (&A); init_stack (&B); x = 3; push (&A, x); push (&B, pop (&A)); y = pop (&B); printf (“y == %d\n”, y); Question 3.8
Question 3.9 a) In the given code, variables a, b, and c are live when the outer block is being executed. Variables d, e, and f are needed in the first nested block only. They can also overlap the space for g, h, and i. it requires a 24 bytes. b) During the compiling of a subroutine, the compiler is able to construct a tree where each of the nodes represents a block and is a child of the node representing the surrounding block. All the variables declared in the outermost blocks are then assigned locations when the subroutine space is beginning. All the variables in a nested block should be assigned locations after the variables of the surrounding block. Additionally, variable siblings also overlap. Question 3.10
Question 3.11
Question 3.12 (let ((a (lambda (n) 1)) (b(lambda (n) 2))) (let ((a (lambda (n) (if (zero? n) 3 (b (- n 1))))) (b (lambda (n) (if (zero? n) 4 (a (- n 1))))) (list (a 5) (b 5)))
Chapter 7 Question 7.1
Question 7.2
Question 7.3
Question 7.4
name: string (1…10); age: integer; end record; P, q : person A, B : array (1…10) of integer; Question 7.5 subtype lc_letter is character range ’a’..’z’; Question 7.6
C : a+b; Question 7.9
Question 7.10 UCASE CSECT USING UCASE, R 15 MVC UC, PG MVC LC, PG OC UC, = 16C’ ‘ NC LC, = 16X’BF’ XPRINT PG, L’ PG XPRINT UC, L, UC XPRINT LC, L’ LC BR R14 PG DC CL9’ alphaBETA UC DS CL (L’PG) LC DS CL (L’ PG) YREGS END UCASE Question 7.11
Question 7.13
Question 7.19
y: pointer to A y: real type A = record x: pointer to B y: real Question 7.20 double *a[n]; //array of n pointers to doubles double (*b)[n]; //pointer to array of n doubles double (*c[n])(); //array of n pointers to functions returning doubles double (*())[n]; //function returning pointer to array of n doubles Question 7.21
Question 7.24 a) What I understand is that when one encounters the suggestion that a certain garbage collected language needs to provide operations such as delete as a choice for optimization. This is enabled by the user deleting some of the objects that are not needed. Therefore, this action enables the programmer to save the garbage collector the need of having to find and reclaim the objects automatically and thus he improves the performance. b) Yes, it is a good idea that the programmer tenure the object so that the object will never be used again in being a candidate for reclamation. Question 7.25
Chapter 8 Question 8.1
Question 8.5
Question 8.7
Question 8.11 Public static void swap (Object a, Object b){ Object t = b; b = a; a = t; } Question 8.13
Question 8.16
Question 8.19
score (q, doc 1) = 0.8974, score (q, doc2) = 0.6883, score (q, doc3) = 1.3015 Ranking: doc3, doc1, doc2
score (q, doc1) = 0.6883, score (q, doc2) = 0.7975, score (q, doc3) = 0.7823 Ranking: doc2, doc3, doc1 Question 8.20 { ... Integer x = new Integer(1); System.out.println("x is " + x); ... } void addOne(Integer originalValue) { value = new Integer(originalValue.intValue() + 1); } Question 8.22
Question 8.24
Sherry Roberts is the author of this paper. A senior editor at Melda Research in https://www.meldaresearch.com">already written essay if you need a similar paper you can place your order for https://researchpapers247.com/nursing-paper/">nursing writing services Like it? Share it!More by this author |