search
Log In
12 votes
1.9k views

Consider the following program skeleton and below figure which shows activation records of procedures involved in the calling sequence. $$p \rightarrow s \rightarrow q \rightarrow r \rightarrow q.$$Write the access links of the activation records to enable correct access and variables in the procedures from other procedures involved in the calling sequence

procedure p;
  procedure q;
    procedure r;
      begin
        q
      end r;
    begin
       r
    end q;
  procedure s;
    begin
       q
    end s;
  begin
    s
  end p;
in Compiler Design
edited by
1.9k views
2
0
@kenzou, @Arjun Sir. Please format the code appropriately.

4 Answers

21 votes
 
Best answer

When procedure $p$ begins, $s$ is called $[p\to s].$ $s$ is enclosed within $p.$ So, any undeclared variable found inside $s$ will be searched for inside the body within which it is enclosed i.e., function $p$ (static scoping rules). So access link is from s to p.

Now, $s$ is visited and $s$ calls $q$ $[p\to s \to q].$ $q$ is visited and begin execution. Any undeclared variable found inside $q$ will be searched inside the enclosing function i.e. $p$ again. So, access link is from q to p.

Now, $q$ calls $r$ $[p \to s \to q \to r].$ $r$ is visited. $r$ begins execution and any undeclared variable found is searched in the enclosing function i.e., $q$ here. So, access link is from r to q.

$r$ calls $q$ $[p\to s \to q \to r \to q]:$  This is the sequence of calls which is already given in the question. 

We have already seen access links for $q.$ 

So, it becomes


edited by
0
Nicely Explained..Thanks:)
0

@MiNiPanda 

procedure 'r' and procedure 's' are at the same level and both are enclosed in 'q'.

So, should not 's' points to 'q' ? How it directly points to 'P' ?

0

@ankitgupta.1729 r is enclosed within q but s is not.

0

@MiNiPanda

can you give some reference for your statement ?

if s in not enclosed within q then q and s should have to be at the same level which is not the case here.

Can you tell me, in which language this code is written ?

0
After end q,  definition/declaration of procedure s begins...

So i hope they are at same level.
0

@Shaik Masthan sir, I am assuming the given code is written in pseudocode. So, according to rules, the given code should follow indentation, So, q and s should not be at the same level. I had also written the answer when no correct answer was there based on the the nptel video link which I had attached. I can also think that they should have to be at the same level but I did not find anywhere that's why I asked. Also the same question was there before in the image form, so, there is no problem with editing. I think it is a old question, so there might be some error in the question.    

1

If there is no end q... Then we don't know upto where it's extended. But here not the case, so i strongly believe those are at same level. ( it's my opinion only ).

 I think it is a old question, so there might be some error in the question.    

It may be possible.

1

@ankitgupta.1729 Is the indentation fine now?

1

@Arjun sir, yes fine now. Thank you.

5 votes

An activation record has the following parts:-

  1. A control link from record A points to the previous record on the stack. The chain of control links traces the dynamic execution of the program.
  2. An access link from record A points to the record of the closest enclosing block in the program. The chain of access links traces the static structure (think: scopes) of the program.

Going by the definition of the access link we have the record of the closest enclosing block in the program as mentioned.

The access link for the above ques is as follows:

$$p \rightarrow q \rightarrow  r \rightarrow  q \rightarrow s$$

 


edited by
3
Why will P access Link point to Q?

P is not enclosed in any function.

Can you please clear the doubt?

My answer:-

P=Null

Q=P ,as Q is enclosed under P

R=Q,as R in enclosed under Q

S=P,as S in enclosed under P
0
can someone verify the answer here/?
0
yes, @rahul you are correct.
1
@rahul

yes correct.

Access_link(P)--->main()

Access_link(S)--->P();

Access_link(Q)--->P();

Access_link(R)---->Q();
0
p==>s==>q==>r==>q i think this is the sequence
0
Can someone please provide "C" equivalent of this ..
0
@jatin function can not define in another function but it can declared in C
5 votes

Reference :- NPTEL

0 votes

Activation records keep track of values as a program executes. More specificly, an activation record has a set of names that are bound to values. We link activation records together in two ways:

  • with a control link
  • with an access link

control link from record A points to the previous record on the stack. The chain of control links traces the dynamic execution of the program.

An access link from record A points to the record of the closest enclosing block in the program. The chain of access links traces the static structure (think: scopes) of the program.

https://www.cs.hmc.edu/~benw/teaching/notes/activation.html

now come to answer

P is  enclosed by  main function 

so Access_link(P)--->main()
similarly    S is  enclosed by  P
Access_link(S)--->P()

and so on
Access_link(Q)--->P()
Access_link(R)---->Q()  because R is enclosed by Q
 


edited by

Related questions

2 votes
1 answer
1
510 views
Consider the procedure declaration: Procedure P (k: integer) where the parameter passing mechanism is call-by-value-result. Is it correct if the call, P (A[i]), where A is an array and i an integer, is implemented as below. create a new local variable, say z; assign to ... the body of P using z for k; set A [i] to z; Explain your answer. If this is incorrect implementation, suggest a correct one.
asked Dec 19, 2016 in Compiler Design jothee 510 views
16 votes
1 answer
2
885 views
What is printed by following program, assuming call-by reference method of passing parameters for all variables in the parameter list of procedure P? program Main(inout, output); var a, b:integer; procedure P(x, y, z:integer); begin y:=y+1 z:=x+x end P; begin a:=2; b:=3; p(a+b, a, a); Write(a) end.
asked Dec 19, 2016 in Compiler Design jothee 885 views
0 votes
1 answer
3
367 views
Translate the executable statements of the following Pascal Program into quadruples. Assume that integer and real values require four words each. repeat flag[i]:=true; while turn !=i do begin while flag[j] do skip turn:=i; end critical section flag[i]:=false; until false Program Test; var i:integer; a: array [1...10] of real; begin i:=0; While i:<=10 do begin a[i]:=0; i:=i+1 end; end.
asked Dec 19, 2016 in Compiler Design jothee 367 views
4 votes
1 answer
4
812 views
Construct a DAG for the following set of quadruples: E:=A+B F:=E-C G:=F*D H:=A+B I:=I-C J:=I+G
asked Dec 19, 2016 in Compiler Design jothee 812 views
...