Princess Nora Bint Abdul Rahman University College of Computer and Information Sciences Department of Computer Science 1434 - 1435 (Second semester) CS 444 - Programming Languages Concepts Assignment 2 Due date: Thursday 3/4/2014 (8 am - 1:30 pm) Question 1. Consider the following skeletal C program: void fun1(void); /* prototype */ void fun2(void); /* prototype */ void fun3(void); /* prototype */ void main() { int a, b, c; . . . } void fun1(void) { int b, c, d; . . . } void fun2(void) { int c, d, e; . . . } void fun3(void) { int d, e, f; . . . } Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible in each function after the execution of the last called function? Include with each visible variable the name of the function in which it was defined. a. main calls fun1; fun1 calls fun2; fun2 calls fun3. b. main calls fun2; fun2 calls fun3; fun3 calls fun1. CS 444 Assignment 2 Solution: a. main calls fun1; fun1 calls fun2; fun2 calls fun3. d, e, f fun3 c fun2 (d and e of fun2 are hidden) b fun1 (c and d of fun1 are hidden) a main (b and c of main are hidden) b. main calls fun2; fun2 calls fun3; fun3 calls fun1. b, c, d fun1 e, f fun3 (d of fun3 is hidden) a main (b and c of main are hidden) (c, d and e of fun2 are hidden) Page 2 of 6 CS 444 Assignment 2 Question 2. Consider the following JavaScript program: var x, y, z; function sub1() { var a, y, z; function sub2() { var a, b, z; . . . 1 } . . . 2 } function sub3() { var a, x, w; . . . 3 } List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, (Points 1, 2, and 3) assuming static scoping is used. Solution: Point 1 - In sub2: a, b, z in sub2. y in sub1 (a and z of sub1 are hidden). x the global variable (global variables y and z are hidden). Point 2 - In sub1: a, y, z in sub1. x the global variable (global variables y and z are hidden). Point 3 - In sub3: a, x, w in sub3. y, z the global variables (global variable x is hidden). Page 3 of 6 CS 444 Assignment 2 Question 3. Assume the following JavaScript program was interpreted using staticscoping rules. What value of x is displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1? var x; function sub1() { document.write("x = " + x + "<br />"); } function sub2() { var x; x = 10; sub1(); } x = 5; sub2(); Solution: Static scoping: x is 5 Dynamic scoping: x is 10 Page 4 of 6 CS 444 Assignment 2 Question 4. Given the following grammar: 1. S A B 2. A A a | a 3. BB b | b Parse the sentence a a a b b using Shift-reduce parsing in the form of the table below. Stack Input Sequence Action Stack Input Sequence Action () aaabb - (a) aabb Shift a (A) aabb Reduce (2) (A a) abb Shift a (A) abb Reduce (2) (A a) bb Shift a (A) bb Reduce (2) (A b) b Shift b (A B) b Reduce (3) (A B b) - Shift b (A B) - Reduce (3) (S) - Accept/Success (1) Solution: Page 5 of 6 CS 444 Assignment 2 Question 5. Given the following grammar: 1. 2. 3. 4. SaB B““ BA AbB And the following parsing table: S A B a 1 - b 4 3 $ 2 Parse the sentence a b b using LL(1) Parser in the form of the table below. Hint: Rule 2 means that you will add nothing instead of B. Stack Input Action Output stream Stack Input Action Output stream [S, $] abb$ - - [a, B, $] abb$ Rewrite S by rule 1 [1] [B, $] bb$ Remove a and a (matching) [1] [A, $] bb$ Rewrite B by rule 3 [1, 3] [b, B, $] bb$ Rewrite A by rule 4 [1, 3, 4] [B, $] b$ Remove b and b (matching) [1, 3, 4] [A, $] b$ Rewrite B by rule 3 [1, 3, 4, 3] [b, B, $] b$ Rewrite A by rule 4 [1, 3, 4, 3, 4] [B, $] $ [$] $ Solution: Remove b and b (matching) Rewrite B and Accept by rule 2 [1, 3, 4, 3, 4] [1, 3, 4, 3, 4, 2] Page 6 of 6