You are on page 1of 3

Name: Nguyễn Đức Phi Hồng

ID : ITITIU17022
Principles of Programming Languages 5

Develop static referencing environments for the following programs

Question 1. (Pascal-like language)

program main;
var A, B: real;
procedure Sub1 (Sub1,C: real);
var D: real;
procedure Sub2 (C: real);
var D: real;
begin
… C:= C+B; …
end;
begin
… Sub2(B); …
end;
begin
… Sub1(A); …
end.

Local Non-local Global


Main A,B,Sub1 A,B,sub1
Sub1 C,D,Sub2 B,C Sub1 B,C,Sub1
Sub2 C,D A,B,Sub1,Sub2 B,Sub1

Question 2. (Pascal-like language)

program main;
var A, B: real;
procedure Sub1 (Sub1,C: real);
var D: real;
begin
… Sub2(B); …
end;
procedure Sub2 (Sub1: real);
var D: real;
begin
… C:= C+B; …
end;

begin
… Sub1(A); …
end.

Local Non-local Global


Main A,B,Sub1,Sub2 A,B,sub1,Sub2
Sub1 D,Sub1 B,Sub2 B,Sub1
Sub2 Sub1,D A,Sub1 B,Sub1

Question 3 (C-like language)

int x =1;

int f()
{
int x = 2;
cout << x;
}
int f2()
{
cout << x;
}

void main() {
int x = 3;

//block 1
{
int x = 4;
cout << x;
int y;
}

//block 2

{
cout <<x;
cout << y;
while (...) //block 3
{
f();
cout << x;
}

for (int i =1;i<=3;i++)


//block 4
{
int f = 5;
f();
cout << x;
}

Local Non-local Global


Main program x,f,f2,main x,f,f2,main
f() x f,f2,main f,f2,main
f2() x(global), f,f2,main x, f,f2,main
Main x f,f2,main f,f2,main
Block 1 x,y f,f2,main f,f2,main
Block 2 x(main), f,f2,main,block 3 x,f,f2,main
Block 3 f, x(main), f,f2,main x,f,f2,main
Block 4 f x(main), f,f2,main x,f,f2,main

Question 4.

program main;
var A, B, C: real;
procedure Sub ;
var D: real;
procedure Sub;
var D: real;
begin
… C:= C+B; …
... Sub; ... /*1*/
end;
begin
… Sub; … /*2*/
end;
begin
… Sub; … /*3*/
end.

Which Sub will be called?

/*1*/, /*2*/, /*3*/

You might also like