Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 2 [18pt] Consider the following pseudo-code, assuming nested subroutines

ID: 3596863 • Letter: P

Question

Problem 2 [18pt] Consider the following pseudo-code, assuming nested subroutines and static scope: 1 mainO 3 int x-8; int gi 4 function B(int a) int xa3; 8 unct.ion A (int n) 10 funct.ion R(int m) print x; xx 2; if (x> 1) 12 13 14 15 16 17 18 19 II body of main 20 B(1) R (m 1); else A (m) print g 1/2 1. (5pt) Draw the symbol table tree with 4 tables, one table for each of the 4 scopes (namely main, B, A, R) in this program. Assume each table contains two columns: name and kind (e.g, id, fun, para). 2. (2pt) What does the program print? 3. (6pt) Draw a picture of the run-time stack when A has just been called. For cach frame, show the static and dynamic links. You do not have to show the storage for any other information. 4. (3pt) Refer to the run-time stack, briefly explain how function A finds variable g. 5. (2pt) What does the program print if the language uses dynamic scoping?

Explanation / Answer

Solution:

1)

Symbol table for main:

Symbol table for B:

Symbol table for A:

Symbol table for R:

2)

8 4 2 1 will be printed.

3)

First main()-> B(3)-> R(1)-> R(2)-> R(3) -> A(3)

Firstly main is called then In the main nothing happens till 2nd last line i.e. B(1). Only initializations happen. Now function B() is called and value of a=1 which is added with 1 and assigned to x so x=4 (locally). After that R(1) is called which makes m=1 and x is printed i.e. 8. Then it is reduced by half and again R(1+1) is called which makes m=2. Till now first call of R(1) has not completed. This R(2) again prints x which is 4 and make it half and calls R(2+1) which makes m=3 and prints x which is 2 and make it half. Now it calls A(3) as m was 3 which assigns g as 3. Once this much is done A(3) ends which ends R(3) which called it. This ends R(2) which called R(2+1) which ends R(1) which and returns to B(3). As B(3) ends print g executes.

4) I guess 4 is also explained above.

5)

In case of dynamic scoping when function R requires x it will go to the function which called it and checks for if x is available so x is available with R so 4 will be printed first then 2 and then from main 1 will be printed

si in case of Dynamic Scoping the output will b 4 2 1.

Please, please upvote and ask your doubts in the comments.

Name Kind g id x id B func
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote