Compiler (Design) [Fall 2009] Test II NAME: ______________________________________________________ Instructions: 1) This test is 9 pages in length. 2) You have 75 minutes to complete and turn in this test. 3) Short answer questions include a guideline for how many sentences to write. Respond in complete English sentences. 4) This test is closed books, notes, papers, friends, neighbors, etc. 5) Use the backs of pages in this test packet for scratch work. If you write more than a final answer in the area next to a question, circle your final answer. 6) Write and sign the following: “I pledge my Honor that I have not cheated, and will not cheat, on this test.” _______________________________________________________________________ _______________________________________________________________________ Signed: ______________________________________________ 1 1. [5 points] How do strongly typed languages prevent buffer-overflow vulnerabilities? (You do not need to define strongly typed languages; just explain how they prevent buffer overflows.) [1-2 sentences] 2. [6 points] What is a symbol table? Why do we call classesST in dj2dism an “enhanced” symbol table? [2 sentences] 2 3. [16 points] Make a list of all the errors in the following DJ program (which has line numbers annotated on the left-hand side). For every error you find, indicate (1) the line number on which the error occurs, (2) whether the error is lexical, syntactic, or semantic, and (3) a brief description of the error if it is a lexical or semantic error. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class C1 extends class C2 extends class C3 extends class c6 extends class C4 extends Main { var nat n; n = 3.4; n = 7j; n = j7; n = (8/8); n+1 = !4; n.n = 5; } C2 {} Object {} C4 {} C4 {} C2 {} 3 4. [14 points] Write a lex/flex rule (i.e., regular expression) for tokenizing floating-point literals, which are defined as follows: A float may optionally begin with a + or – sign. A float may optionally end with an exponent portion. Exponent portions of floats must begin with either the character e or E, then may contain a + or – sign, and then must contain a nonempty sequence of digits. A float may contain any natural number of digits but must contain at least one digit. A float must contain at least one digit before the exponent portion, if one is present. A float may optionally contain a decimal point. Any natural number of digits may appear before a decimal point, but at least one digit must appear after a decimal point. If the float contains both a decimal point and an exponent portion, then at least one digit must appear between the decimal point and the exponent portion. Keep your response simple enough that it cannot be simplified in any significant ways. 4 5. G is: 0 1 2 3 4 S A A B B -> -> -> -> -> A$ ABA x y a) [15 points] Draw an LALR(1) DFA and parse table for G. (Recall that LALR(1) DFAs are constructed by merging states in LR(1) DFAs.) Hint: G is not in LALR(1). 5 G is: 0 1 2 3 4 S A A B B -> -> -> -> -> A$ ABA x y b) [14 points] Using your LALR(1) parse table from Part (a), complete the following parse trace. If you arrive at a point in the parse trace in which conflicting actions must be performed, indicate the conflicting actions and stop the parse trace at that point. Stack Input xyx$ Action c) [5 points] Is G ambiguous? If so, prove it. 6 G is: 0 1 2 3 4 S A A B B -> -> -> -> -> A$ ABA x y d) [10 points] Rewrite G into an equivalent CFG G’ that is in LALR(1). Demonstrate that G’ is in LALR(1) just by drawing its LALR(1) DFA (you need not draw a parse table for G’). 7 G is: 0 1 2 3 4 S A A B B -> -> -> -> -> A$ ABA x y e) [7 points] Draw the LL(1) parse table for G. It will have at least one conflict. f) [8 points] Rewrite G into an equivalent CFG G’’ that is in LL(1), and draw the LL(1) parse table for G’’. 8 Undergraduates stop here. The remaining question is for graduate students only. 6. [20 points] Construct a minimum-state DFA that accepts exactly those strings over {0,1,2} that, when interpreted as a ternary (base-3) number, are divisible by either 2 or 3. 9