CS 132 Midterm, Spring 2008: Solution ------------------------------------- Question 1: A ::= x B | C y B ::= y | x B C ::= A y | z -> A ::= x B | A y y | z y B ::= y | x B -> A ::= D | A y y B ::= y | x B D ::= x B | z y -> A ::= D (y y)* B ::= y | x B D ::= x B | z y -> A ::= D E B ::= y | x B D ::= x B | z y E ::= epsilon | y y E First Follow Nullable x y z A x z \$ no D E error error B x y y \$ no x B y error D x z y \$ no x B error z y E y \$ yes error y y E error LL(1) because: - A: only one choice. - B: not nullable and the two choices have different First sets. - D: not nullable and the two choices have different First sets. - E: nullable, and First(y y E) = {y} has an empty intersection with Follow(E) = {\$} Question 2: A ::= z B | C y B ::= x C | epsilon C ::= B A x | y First Follow Nullable x y z A x y z x no C y C y z B B x x y z yes C Not LL(1) because: B is nullable and First(x C) and Follow(B) have x in the intersection. Question 3: A ::= x B | C y B ::= A x C C ::= z B | epsilon proc eat(symbol a) { if (token==a) { token=next_token(); } else { throw new Exception(); } } proc main() { token=next_token(); A(); eat(EOF); } proc A() { if (token==x) { eat(x); B(); } else if (token==y or token==z) { C(); eat(y); } else throw new Exception(); } proc B() { A(); eat(x); C(); } proc C(): if (token==z) { eat(z); B(); } else // do nothing }