Bài tập 3 Question 1. Given a program written by BK06-PASCAL as follows var a,b,c: integer; procedure sub1(c:boolean) procedure sub2() var d:real begin … end procedure sub3(b:real) var c: real; begin … end begin … end begin … end. a. State the static referencing environments of main, sub1, sub2 and sub3 b. Supposed the current dynamic chain is main sub1 sub2 sub3 sub2, state the dynamic referencing environments of the corresponding activations. c. State the (static) scopes of declarations in the above program d. State the (dynamic) scopes for each association given in question b. e. Provided the dynamic chain is as that in question b, if there is a referencing of c given in the last call of sub2 , what is the corresponding data object? Question 2. Given a program in BK06-PASCAL as follows: procedure sub(a:integer) procedure sub(a:integer) begin … sub(1) ; end; begin // sub-1 // sub-2 //1 … sub(1); end; begin … sub(1); end. //2 //3 What are the procedures to be called in statement //1, //2 and //3? Question 3. Given a program in BK06-PASCAL as follows: program EXER; var s, t: integer; procedure sub1; var s, w: integer; begin s := t*4 + 1; sub2; end; procedure sub2; var w, t: integer; begin w := s * 3; t := w – 2; sub3;//2 end; procedure sub3; var x, y: integer; begin x := t+3; y := s+2; write(x, y); end; begin s := 2; t := 4;//1 sub1; end. Illustrate the central stack right after statements commented as //1 and //2 execute. Note: IP field of activation record may be left blank. Student must fill SCP and EP fields. Question 4. Given a program written by BK06-PASCAL as follows: program main; var X,Y,Z: integer; function F(a, b: integer): integer; var t: integer; begin if (a= 1) then begin F := b; {1} end else begin a := a -1; Y := Y+X; F := F(a,b); end; end ; begin X:= 2; Y=4; Z := F(X, Y); writeln(Z+X); end. Illustrate the central stack right after statement {1} executes. Provided that the address of the call F(X,Y) in main is I1, address of the next statement after this call is I2, the address of the call F(a,b) in F is I3, address of the next statement after this call is I4. The activation record of main is allocated at I5 whereas F’s are I6 and I7 respectively (corresponding to 2 times F is called). Parameters a and b are transferred by value. Question 5. 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; {type pointer} 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: ID’s lexeme, ID’s kind, ID’s 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 pointer, integer and real are 4, 4 and 6 respectively. The ID’s 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 6. 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] = 7 – i; i = 3; swap(i, a[i],X); printf(“%d%d%d%d%d%d%d”,i,a[0], a[1], a[2], a[3], a[4],X); } Question 7. 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) (* 3 *) end; begin x.a = 4; x.p = addr(y); y.a = 6; y.p = nil; sub_1(x, y) (* addr(v) returns address of v *) (* 4 *) end. 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: ID’s lexeme, ID’s kind, ID’s type expression and offset. If an identifier is the name of a data type, ID’s 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 ID’s 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. Question 8:Consider the following program written in C syntax: voidswap(inta, intb) { int temp; temp = a; a = b; b = temp; } void main() { intvalue = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? a. Passed by value b. Passed by reference c. Passed by value-result d. Passed by name Question 9:A program is given as following. Provided parameters with keyword varwill 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; {pointer type } 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. Illustrate the run-time stack when the statement {**} just has executed, provided that user has inputted 2 and 3. b. Illustrate the run-time stack when the statement {*} just has executed. Question 10: Cho một đoạn chương trình trên một ngôn ngữ cấu trúc khối tương tự Pascal như sau Program main; Var a: array [1..5] of integer; I, j : integer; Procedure swap(a, b, c: integer); Var t: integer; Begin T := a; a: = b + 1; b:=c+I; c: = t; /*1*/ End; Begin For i:=1 to 5 do a[i] : = i; I : = 1; j := 2; Swap(I, a[i], j); Writeln(I, j, a[1], a[2], a[3], a[4], a[5]); End. a) Cho biết công thức tính địa chỉ truy xuất của phần tử a[i] bất kỳ. biết rằng dãy a được cấp phát tại địa chỉ nền và có độ dài phần mô tả là D, mỗi đối tượng dữ liệu thuộc kiểu integer cần 2 bytes để lưu trữ. b) Hãy vẽ chồng trung tâm khi chương trình vừa thực thi xong tất cả các lệnh gán có chú thích /*1*/ (vào lúc này chương trình con swap vẫn đang thực thi và chưa kết thúc), biết rằng tham số truyền bằng trị. c) Cho biết giá trị in ra màn hình sau trong các trường hợp sau: a. Các thông số truyền bằng trị - kết quả b. Các thông số truyền bằng tham khảo c. Các thông số truyền bằng tên. Question 11: Cho một đoạn chương trình trên một ngôn ngữ cấu trúc khối tương tự Pascal như sau: Var a: array[1..5] of integer; I: integer; N: integer; Procedure sub1(a: integer) Var x, y: integer; Procedure sub2(a:integer) Begin I:=I + x; A:=a + I; N:=n+1; If n = 1 then Sub2(a); Writeln(x, y, a); End; Begin X: = 1 + a; //a1 //a2 //a3 //1 //2 Y: = a -1; Sub2(x); Writeln(x, y, a); //sub2 End; Begin For i: = 1 to 5 do a[i] := I; I: = 1; N:= 0; Sub1(a[i]); Writeln(I, a[1], a[2], a[3], a[4], a[5]); //3 End. a) Cho biết biến a trong các lệnh được chú thích //1, //2 và //3 tương ứng với biến a trong các lệnh khai báo nào trong số các lệnh có chú thích //a1, //a2 và //a3 b) Cho biết kết quả xuất ra màn hình trong các trường hợp sau: a. Các thông số được truyền bằng trị b. Các thông số được truyền bằng tham khảo c. Các thông số được truyền bằng tên c) Giả sử chương trình được viết lại như minh họa bên dưới. Cho biết chương trình có báo lỗi khi dịch hay không khi đổi tên biết a ở khai báo //a3 thành sub2, vì sao? Var a: array[1..5] of integer; I: integer; N: integer; Procedure sub1(a: integer) Var x, y: integer; Procedure sub2(a:integer) Begin End; Begin End; Lời giải: //a1 //a2 //a3