Parsing & Syntax Analysis



Answer & solution
Answer: E→E+T | T; T→T*F | F; F→id. (no official key was published)
One replacement grammar is
E → E + T | T
T → T * F | F
F → id
It accepts exactly the original token language id ((+|*) id)*, without introducing parentheses or changing which token strings are allowed. The E/T levels make * bind more tightly than +, and the left-recursive productions make repeated uses of either operator associate left. Thus i+j*k has only the i+(j*k) parse. This standard expression grammar is SLR(1), and hence LALR(1); separating the levels resolves the shift/reduce ambiguity. The original grammar specified no precedence, so multiplication-before-addition and left associativity are a legitimate choice of an unambiguous interpretation, not properties already forced by that grammar.