CS 132 Fall 2003, Midterm Solution ================================== Question 1 ---------- Here follows two somewhat different solutions (labeled A and B). Solution A: E ::= ( E ) S | i S S ::= . i ( E ) S | ? E : E | epsilon First Follow E ( i \$ ) : S . ? eps \$ ) : . ? : ( ) i \$ E (E)S iS S .i(E)S ?E:E eps eps eps Solution B: E ::= ( E ) D Q | i D Q Q ::= epsilon | ? E : E D ::= epsilon | . i ( E ) D First Follow E ( i \$ ) : Q ? eps \$ ) : D . eps \$ ) : ? . ? : ( ) i \$ E (E)DQ iDQ Q ?E:E eps eps eps D .i(E)D eps eps eps eps Question 2 ---------- Fi(A) = Fi(B) union Fi(C) union {y} = {x,y} Fi(B) = {y} union {eps} = {y,eps} Fi(C) = Fi(A) union {x} = {x,y} Fo(A) = {y} union Fo(B) = {x,y} Fo(B) = Fi(C) = {x,y} Fo(C) = {x} Not LL(1) because the two productions for A having overlapping First sets. Question 3 ---------- Here is a sketch of the Java code: void E() { B(); A(); if ( token != EOF ) error(); } void A() { if ( token == '&' ) { get-next-token(); B(); A(); } } void B() { if ( token == "true" || token == "false" ) { get-next-token(); else error(); }