Docsity

Midterm Problems with Solution - Programming Languages | ECS 140A, Exams of Programming Languages

Toggle sidebar
bg1
Midterm Examination Solution
Problem 1 (20 Points)
This problem tests your familiarity with BNF, EBNF, parse trees etc.
1.a (6 Points) Convert the following BNF into EBNF:
<E> ::= <E> + <T> | <T>
<T> ::= <T> * <F> | <F>
<F> ::= id
In this grammar, <E>,<T>, and <F> are nonterminals, and ’+’, ’*’, and id are terminals. Also assume that <E> is
the starting nonterminal.
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, G1:
<UT> ::= <UT> ?<UV> |<UV>
<UV> ::= <UW> !<UV> |<UW>
<UW> ::= term
In this grammar, <UT>,<UV>, and <UW> are nonterminals, and ?, ! and t erm are terminals. Also assume that <UT>
is the starting nonterminal.
1.c (6 Points) Answer the following for the operators ? and !:
•Which of the operators are left-associative?
?
•Which of the operators are right-associative?
!
•List the operators in the order of precedence. If two operators have the same precedence, indicate so.
!, ?
ECS 140A -1- Spring 2005
pf3
pf4

Partial preview of the text

Download Midterm Problems with Solution - Programming Languages | ECS 140A and more Exams Programming Languages in PDF only on Docsity!

Midterm Examination Solution

Problem 1 (20 Points)

This problem tests your familiarity with BNF, EBNF, parse trees etc.

1.a (6 Points) Convert the following BNF into EBNF:

::= + | ::= * | ::= id

In this grammar, , , and are nonterminals, and ’+’, ’*’, and id are terminals. Also assume that is the starting nonterminal.

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 :

::= ? | ::= ! | ::= term

In this grammar, , , and are nonterminals, and ?,! and term are terminals. Also assume that is the starting nonterminal.

1.c (6 Points) Answer the following for the operators? and !:

  • Which of the operators are left-associative? ?
  • Which of the operators are right-associative? !
  • List the operators in the order of precedence. If two operators have the same precedence, indicate so. !,?

1.d (4 Points) Show the parse tree (if any) for the following strings: i! i? i? i! i

Problem 3 (20 Points)

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

Problem 4 (25 Points)

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 } }

Problem 5 (15 Points)

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