in Compiler Design recategorized by
7,279 views
25 votes
25 votes

Study the following program written in a block-structured language:

Var x, y:interger; 
procedure P(n:interger);
begin
     x:=(n+2)/(n-3);
end;

procedure Q 
Var x, y:interger;
begin   
    x:=3;
    y:=4; 
    P(y);
    Write(x)                                __(1)
end;
 
begin
    x:=7;
    y:=8;
    Q; 
Write(x);                                   __(2) 
end.

What will be printed by the write statements marked $(1)$ and $(2)$ in the program if the variables are statically scoped?

  1. $3, 6$
  2. $6, 7$
  3. $3, 7$
  4. None of the above.
in Compiler Design recategorized by
7.3k views

2 Answers

44 votes
44 votes
Best answer

Using Static Scoping:
First, procedure Q is called from the main procedure. Q has local variables x and y with values 3 and 4 respectively. This local variable y (value 4) is being passed to procedure P during call, and received in local variable n inside procedure P. Now, as P does not have any local definition for variable x, it will assign the evaluated value of (n+2)/(n-3) i.e. (4+2)/(4-3)=6 to the global variable x, which was previously 7. After the call of procedure P, procedure Q writes the value of local variable x which is still 3. Lastly, the main procedure writes the value of global variable x which has been changed to 6 inside procedure P. So, the output will be 3, 6.

Using Dynamic Scoping:
The same sequence of statements will be executed using dynamic scoping. However, as there is no local definition of variable x in procedure P, it will consider the recent definition in the calling sequence; as P is being called from procedure Q, definition of x from Q will be used, and value of x will be changed to 6 from 3. Now, when Q writes local variable x, 6 will be printed. The write global variable x from main procedure will print 7 (as value of the global variable x has not been changed). So, the output will be 6, 7.

Correct Answer: $A$

edited by

4 Comments

nice explanation ,deserves to be best answer
1
1
nice explanation@sutanay3
1
1
Nice answer
1
1

P does not have any local definition for variable x,----it should be 

P does not have any local declaration for variable x.

1
1
18 votes
18 votes

 

In static scoping, the free variable is replaced by the global variable, which means if a variable is not defined locally it will take the value from a global variable.

So option (A) is correct.

edited by

4 Comments

@Vinil
Is the answer to your image 3,3 ?
Also for the above question my doubt is we can easily find x=7 in the same block, isn't it ?
So why are we printing x=6, because if we consider the memory model diagram, we can clearly see that, x = 6 is lost at line 1.
So why at lne 2 x is not considered at 7, why the answer is x=6 at line 2, can you explain with a memory model diagram, considering the memory stack.

PS : Thanks for replying

2
2
edited by
@Hira thakur
begin
    x:=7;
    y:=8;
    Q; 
Write(x);                                   __(2) 
end.

here we have the local value x=7 and local value should be given preference than global value .So, why Write(x) is printing 6 .

0
0

 
$x=7$ is updated to $x=6$ because in the $P$ function call when an expression is evaluated and the value is store in $x$, so $x$ is not defined there that’s why that value is updated here. Write(x) value can’t be 7 because before print there is a $Q$ function call which changes the value of $x$.

0
0
Answer:

Related questions