Docsity

5 Problems in Midterm Exam - Programming Languages - fall 2005 | ECS 140A, Exams of Programming Languages

Toggle sidebar
bg1
Midterm Examination
May 11, 2005
Name (Last, First): , Score: /100
Directions
•This exam consists of 5 questions on 7 pages; be sure that you have the entire exam before starting.
•Make sure that you write your name on every page of the exam.
•Answer each question in the space following the question. If you find it necessary to continue an answer
elsewhere, indicate clearly the location of its continutation and label its continuation with the question number.
•The point value of each question is indicated at its beginning. The entire exam has 100 points.
•Be concise and indicate clearly what your answer is. Presentation and simplicity of your answers may affect
your grade.
•This exam is open book open notes.
•You have 50 minutes.
ECS 140A -1- Spring 2005
pf3
pf4
pf5

Partial preview of the text

Download 5 Problems in Midterm Exam - Programming Languages - fall 2005 | ECS 140A and more Exams Programming Languages in PDF only on Docsity!

Midterm Examination May 11, 2005

Name (Last, First): , Score: /

Directions

  • This exam consists of 5 questions on 7 pages; be sure that you have the entire exam before starting.
  • Make sure that you write your name on every page of the exam.
  • Answer each question in the space following the question. If you find it necessary to continue an answer elsewhere, indicate clearly the location of its continutation and label its continuation with the question number.
  • The point value of each question is indicated at its beginning. The entire exam has 100 points.
  • Be concise and indicate clearly what your answer is. Presentation and simplicity of your answers may affect your grade.
  • This exam is open book open notes.
  • You have 50 minutes.

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.

1.b (4 Points) Draw the syntax diagram for the EBNF grammar.

The following BNF grammar defines a language L :

::= ::= a b | ε ::= c d | ε

In this grammar , , and are nonterminals, whereas a, b, c, and d are terminals. is the starting nonterminal.

2.a (3 Points) Compute the first sets of , , and .

2.b (17 Points) Construct a predictive parser for the language. Extend your parser such that it outputs the number of a’s, b’s, c’s, and d’s in an input string. You do not have to write the parser part separately from the part that performs counting. You can write one program that does both parsing and counting. Also, feel free to write the program in pseudo-code. However, make sure that you explicitly state any assumptions that you make.

This problem deals with a special two-dimensional N × N array. Assume that the row and column indices start at 1. Also, assume that N is even. As shown the figure 1(a), the two matrices of size ( N / 2 ) x ( N / 2 ) contain zero values.

N

N

(a) General Array Layout

(b) An example array

Figure 1: Problem 3 Matrix

In Figure 1(b) we show an example of such a 4 x 4 matrix. For this question, assume that the elements of arrays are laid out in the storage in the row-major form. Note that zero elements are not stored in the memory.

3.a) (4 Points) Show the memory layout of the elements of the array in Figure 1(b).

3.b) (16 Points) Give a general formula for the address of element A[i][j] where A is an N × N array.

This problem deals with static and dynamic scoping. Assume a language, L , that combines both static and dynamic scoping: all variables (global, parameters and local) that start with the prefix ’’static_’’ follow static scope; others starting with the prefix ’’dynamic_’’ follow dynamic scoping. Show the output of the following program.

int static_x; // global statically scoped variable int dynamic_y; // global dynamically scoped variable

void f(int static_x) { dynamic_y = dynamic_y - 1; printf (‘‘%d %d\n’’, static_x, dynamic_y); if (static_x <= 0) return; else g(static_x-1); printf (‘‘%d %d\n’’, static_x, dynamic_y); }

void g(int dynamic_y) { static_x = static_x -1; printf (‘‘%d %d\n’’, static_x, dynamic_y); if (dynamic_y <= 0) return; else f(dynamic_y-1); printf (‘‘%d %d\n’’, static_x, dynamic_y); } void main() { static_x := 3; dynamic_y := 2; f(static_x); printf (‘‘%d %d\n’’, static_x, dynamic_y); }

Output =