in Compiler Design recategorized by
6,092 views
18 votes
18 votes

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 recategorized by
6.1k views

2 Comments

3
3
@kenzou, @Arjun Sir. Please format the code appropriately.
0
0

4 Answers

43 votes
43 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

4 Comments

@ankitgupta.1729 Is the indentation fine now?

1
1

@Arjun sir, yes fine now. Thank you.

1
1

@MiNiPanda Thanks for this explanation! cleared all the doubts 

0
0
11 votes
11 votes

Reference :- NPTEL

4 Comments

@ASNR1010 Question was different when I had written the answer. The pseudocode mentioned in this answer was the question previously. You can check the comments of the best answer..Indentation issue was there initially.. 

1
1

@ankitgupta.1729

Actually, I was confused by seeing two different answers before. but your nptel link made it clear thanks. 

1
1

@ankitgupta.1729 Sir, thank you for the NPTEL ref link. Very helpful.

1
1
5 votes
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

4 Comments

p==>s==>q==>r==>q i think this is the sequence
0
0
Can someone please provide "C" equivalent of this ..
0
0
@jatin function can not define in another function but it can declared in C
0
0
0 votes
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