8 Qualifying Exam in Programming Languages and Compilers _U_n_i_v_e_r_s_i_t_y _o_f _W_i_s_c_o_n_s_i_n Fall 1988 9 _I_n_s_t_r_u_c_t_i_o_n_s This exam contains seven questions, divided into two parts. All students taking the exam should answer _a_l_l _f_o_u_r ques- tions in Part I. The answers to these questions will be collected after two hours. Students taking the exam to satisfy the depth requirement will have an additional two hours to answer _a_l_l _t_h_r_e_e questions in Part II. If you are unable to provide a complete solution to a ques- tion, you can earn partial credit by explaining techniques for solving such problems, relating these problems to known similar problems, _e_t_c, so please give your best answer for the required number of questions. 9 Part I _Q_u_e_s_t_i_o_n _1 (a) Explain what is meant by static and dynamic scoping of non-local variables. Give examples that illustrate the difference. (b) Outline how non-locals are accessed at run-time in statically scoped languages. (c) Outline how non-locals are accessed at run-time in dynamically scoped languages. (d) How can a programmer using a statically scoped language simulate dynamic scoping? Can this technique also be used by the compiler for a dynamically scoped language to implement access to non-local variables? If yes, compare this technique with the technique(s) you described in part (c). _Q_u_e_s_t_i_o_n _2 A context-free grammar is said to be in Chomsky Normal Form if it contains only productions of the form 9 - 6 - @A~ ->~ a@ @|1@@A@ a nonterminal, @a@ a terminal @B~ ->~ C~ D@ @/1@@B@, @C@, @D@ nonterminals 9 (a) Let G be any context-free grammar that contains no lambda-productions (also known as epsilon productions). Can G always be rewritten into an equivalent context- free grammar in Chomsky Normal Form? 9 (b) Let G be any context-free grammar in Chomsky Normal Form, and x any string of terminal symbols. Give a general parsing algorithm that can parse x under G. _Q_u_e_s_t_i_o_n _3 (a) Pascal allows the type constructor, set of T, where T is an enumeration or subrange type. Typically, the enumeration or subrange that T represents is restricted to a bounded number of values such as 32 or 64. Sug- gest how such sets might be represented in memory. How are in, + (union), and * (intersection) implemented? 9 (b) Assume we generalize the set constructor so that a set of _a_n_y enumeration or subrange is allowed. Thus set of integer would be legal. Suggest how these extended sets might be represented in memory. Now how are in, + (union), and * (intersection) now implemented? 9 (c) Assume we generalize the set constructor further so that sets of records and arrays are now allowed. Thus set of array[1..10] of integer would be legal. Suggest how these extended sets might be represented in memory. How are in, + (union), and * (intersection) imple- mented? _Q_u_e_s_t_i_o_n _4 (a) What is boolean short circuiting? Why is it desirable? 9 (b) Write a procedure that generates code for short-circuit evaluation of boolean expressions. The inputs to the procedure are: (1) an abstract-syntax tree representa- tion of the expression; (2) the label to which to branch if the expression evaluates to true; (3) the label to which to branch if the expression evaluates to 9 - 7 - false. Internal tree nodes contain boolean operators (_n_o_t, _a_n_d, _o_r); leaf nodes contain variable names. The instruction set is as follows: branch_true @|1@ @|2@