Programming Languages
Assignment
Q1) For the program below show the output. (for space you can place the three values printed by P1 on the same line.
Assume static scoping
PROGRAM EX1;
int i; // global
int A[3]; // global
PROCEDURE P1( int x, int y)
Begin
END;
y := 2; PRINT(x); i := 3; PRINT(x); i := 3; PRINT(x); PRINT(y)
BEGIN //main
A[1]:= 7; A[2]:= 13; A[3]:= 11;
i := 1;
P1(A[i], i); // first call
P1(i, A[i]); // second call
END.
a) x is passed by value and y is passed by value.
b) x is passed by value and y is passed by name.
c) x is passed by name and y is passed by value.
d) x is passed by name and y is passed by name.
Q2. For the program below show the output
Assume static scoping
PROGRAM EX2;
int N; int M; // global
PROCEDURE P1(int x, int y, int z) BEGIN
END;
x := 1;
z := x + y;
BEGIN
N := 2; M := 0
P1 (N, N, M)
PRINT (M);
END.
a) What is printed for M if parameters passed by reference?
b) What is printed for M if parameters passed by value-results?
Q3. For the program below show the output
Assume static scoping
PROGRAM EX3;
int k; // global
PROCEDURE P1(int x)
BEGIN
x := 2;
x := x + k;
END;
BEGIN
END.
k := 1; P1(k);
PRINT (k);
a) What is printed for k if pass by value ?
b) What is printed for k if pass by results ?
Q4. Many programming languages allow unrestricted GOTO statements which allow a programmer to use a GOTO to jump to any visible LABEL. Thinking about the run-stack and activation record management,
a) Though looking very efficient in terms of a quick jump to distant code why may it cause some efficiency problems during execution? Explain.
Q5) Show (draw) the runtime-stack with AR(s) for the following. Include static and dynamic link information (dashed arrows) as used in fig. 10.9 Assume the execution is at the ground condition when it becomes true!!!!
PROGRAM EX5;
PROCEDURE P2(int y)
PROCEDURE P1(int x) // P1 is nested in P2 BEGIN
if (x < 0 ) // ground condition
return;
else P1 (x-1);
END;
BEGIN
P1 (y);
END;
// main driver
BEGIN
P2(3) //assume P2 will be first AR on stack!
END.