Consider the grammar defined by the following production rules, with two operators ∗ and +
S --> T * P T --> U | T * U P --> Q + P | Q Q --> Id U --> Id
Which one of the following is TRUE?
(A) + is left associative, while ∗ is right associative
(B) + is right associative, while ∗ is left associative
(C) Both + and ∗ are right associative
(D) Both + and ∗ are left associative
Explanation: From the grammar we can find out associative by looking at grammar.
Let us consider the 2nd production T -> T * U T is generating T*U recursively (left recursive) so * is left associative. Similarly P -> Q + P Right recursion so + is right associative. So option B is correct.
NOTE: Above is the shortcut trick that can be observed after drawing
few parse trees.
One can also find out correct answer by drawing the parse tree.
Quiz of this Question