You are on page 1of 4

CENTRAL STACK AND PARAMETERS Question 1. A BK06-PASCAL program is given as follows.

Provided parameters with keyword var will be passed by reference, others by value. var x: real; y: integer; z: real; s: real; procedure fact(x: real; y: integer; var z: real); var t: real; begin if (y = 1) then z := x; else begin fact(x, y 1, t); z := t * x; {**} end; s := s + z; {*} end; begin s := 0; readln(x, y); fact(x, y, z); writeln(x, ^, y, =, z, s); end. a. Draw the symbol table and scope stack when the statement {**} has just been compiled. Each entry in the symbol table should cover the following information: IDs lexeme, IDs kind, IDs type expression and offset. If a function has parameter(s) passed by reference, do not need to generate the type expression for this function. Supposed that the sizes of objects of types integer and real are 2 and 6 respectively. The IDs kinds are specified as follows: var: local variables and parameters passed by value proc: functions and procedures par_val: parameters passed by reference b. Illustrate the central stack when the statement {*} just has executed, provided that user has inputted 2 and 3. Question 2. What will be displayed in the screen after the following program executes, provided the formal parameter denoted with & will be transferred by:

a. value/result b. reference c. name


int a[5]; int i,X=1; void swap (int&a, int&b,int&c) int t; { c++; t = a+X+c; a = b; b = t; } void main() { for (i=0;i<5;i++) a[i] = 6 i; i = 2; swap(i, a[i],X); printf(%d%d%d%d%d%d,i,a[0], a[1], a[2], a[3], a[4]); }

Question 3. A BK06-PASCAL program is given as follows. Provided parameters with keyword var will be passed by reference, others by value.
program main; type PT = ^T; (* PT is a pointer type pointing to data objects of T *) T = record a: integer; p: PT end var x, y: T; procedure sub_1(var x: T; y: T); procedure sub_2(var x: PT; y: PT); begin y := x; x := nil; (* 1 *) end; procedure sub_3(var x: PT; var y: PT); begin sub_2(x,y); (* 2 *) end; begin x.a := x.a + 1; y.a := y.a + 1; sub_3(x.p, y.p) end; begin x.a = 4; x.p = addr(y); y.a = 6; y.p = nil; (* addr(v) returns address of v *) (* 3 *)

sub_1(x, y) end.

(* 4 *)

a. Draw the symbol table and scope stack when the statements (* 1 *), (* 2 *), (* 3 *) and (* 4 *) have just been compiled. Each entry in the symbol table should cover the following information: IDs lexeme, IDs kind, IDs type expression and offset. If an identifier is the name of a data type, IDs type expression will be the type expression of the type. If a function has parameter(s) passed by reference, do not need to generate the type expression for this function. Type expressions are specified as follows: - If a data type is named T, then T is the type expression of the data type. - If p is a pointer to a object data of type T, the type expression of p is ^Te where Te is type expression of T - If a is a record, the type expression of a is N1 N2 Nm where Ni is the type expression of the ith field of a. Supposed that the sizes of objects of types pointer, integer and real are 4, 2 and 6 respectively. The IDs kinds are specified as follows: type: this ID is a name of a data type var: local variables and parameters passed by value proc: functions and procedures par_val: parameters passed by reference b. Assumed that record type has no description, illustrate the central stack at the following points of execution: b1. Right after all of assignments in the main subprogram have executed. b2. Right after all of assignments in the sub_1 subprogram have executed. b3. Right after all of assignments in the sub_2 subprogram have executed. Values of variables should be evaluated in the stack. If a variable is a pointer, draw an arrow indicating the corresponding target of the pointer. Honor Programs students are required to filled SCP fields of all activations. POLYMORPHISM Question 4. Given a Java-like program as follows.
class A { public int n; public int f1() {

n+=4; int m= f2();return n + m + k; } public int f2() { n += 1; return n; } public int f3() { f1(); return n; } }; class B : A { public int f1() { n-=4; int m= f2(); return n - m; } public int f2() { n += n; return n; } } };

A b = new B(); b.n = 4; cout << b.f3(); //1

State the value displayed after the statement commented as //1 executes in the following cases: Case A B c d static binding f1,f2 f1 f2 dynamic binding f1,f2 f2 f1