



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 May 11, 2005
Name (Last, First): , Score: /
This problem tests your familiarity with BNF, EBNF, parse trees etc.
1.a (6 Points) Convert the following BNF into EBNF:
In this grammar,
1.b (4 Points) Draw the syntax diagram for the EBNF grammar.
The following BNF grammar defines a language L :
::=
In this grammar , is the starting nonterminal.
2.a (3 Points) Compute the first sets of ,
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.
(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 =