


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Midterm Examination Solution
This problem tests your familiarity with BNF, EBNF, parse trees etc.
1.a (6 Points) Convert the following BNF into EBNF:
In this grammar,
E ::= T { + T } T ::= F { * F } F ::= id
1.b (4 Points) Draw the syntax diagram for the EBNF grammar.
Problems 1.c and 1.d are based on the following BNF grammar, G 1 :
In this grammar,
1.c (6 Points) Answer the following for the operators? and !:
1.d (4 Points) Show the parse tree (if any) for the following strings: i! i? i? i! i
3.a) (4 Points) Show the memory layout of the elements of the array in Figure ??. 4 6 9 11 15 5 9 2
3.b) (16 Points) Give a general formula for the address of element A[i][j] where A is an N × N array. Case (i) 1 ≤ i ≤ ( N / 2 ), ( N / 2 ) + 1 ≤ j ≤ N : base + (i-1)(N/2)w+ ((j-1)-(N/2))w Case (ii) N / 2 + 1 ≤ i ≤ N , 1 ≤ j ≤ ( N / 2 ): base + (i-1)(N/2)w+ ((j-1))w
Class Definition
class A { A val; public void Print1(A a) { val = a; Print2(); val.Print2(); System.out.println(); } public void Print2() { System.out.print("A "); } } class B extends A { public void Print2() { System.out.print("B "); } } class C extends B { public void Print2() { System.out.print("C "); } } class D extends C { public void Print2() { System.out.print("D "); } }
Test class
class test { public static void main(String argv[]) { A a1; B b1; C c1; D d1; a1 = new A(); b1 = new B(); c1 = new C(); d1 = new D(); a1.Print1(d1); a1 = d1; // error b1.Print1(c1); // stmt 2 a1 = b1; // type error b1 = d1; c1.Print1(a1); // stmt 3 a1 = new B(); b1 = c1; // type error c1 = d1; a1.Print1(b1); // stmt 4 a1 = d1; // type error b1 = c1; b1.Print1(d1); // stmt 5 } }
sx = 3, 2, 1 dy = 2, 1 (1)=== call to f === sx = 3 output sx, dy ==============> 3, 1 (2) === call to g === dy = 2, 1 output sx, dy ==============> 2, 2 (3) === call to f === sx = 1 output sx, dy ============> 1, 1 (4) === call to g== dy = 0 output sx,dy ===========> 1, 0 output sx, dy=============> 1, 1 output sx, dy ==============> 1, 1 output sx, dy ================> 3, 1 output sx, dy ================> 1, 1