CS 132 Fall 2005, Midterm Solution ================================== Question 1 ---------- A ::= x C D C ::= x | y D ::= y C D | epsilon parsing table First Follow Nullable x y $ A {x} {$} No xCD - - C {x,y} {$,y} No x y - D {y} {$} Yes - yCD - Question 2 ---------- First Follow Nullable A {x,y,z} {$,x} No B {x,y,z} {$,x,z} No C {y} {$,x,z} Yes The grammar is LL(1) because (i) the two options for A have disjoint First sets and (ii) for C, the First set for (y B) is disjoint from Follow(C). Question 3 ---------- Here is a sketch of the Java code: void A() { if ( token == 'x' ) { get-next-token(); C(); } else { if ( token == 'z' ) { get-next-token(); } else { error(); } } } void B() { if ( token == 'y' ) { get-next-token(); } else { error(); } A(); } void C() { if ( token == 'y' ) { B(); if ( token == 'x' ) { get-next-token(); } else { error(); } } else { A(); if ( token == 'y' ) { get-next-token(); } else { error(); } B(); } }