1.5k views

Consider the grammar defined by the following production rules, with two operators $∗$ and $+$

• $S\:\to\:T∗P$
• $T\:\to\:U\mid T∗U$
• $P\:\to\:Q+P\mid Q$
• $Q\:\to Id$
• $U\:\to Id$

Which one of the following is TRUE?

1. $+$ is left associative, while $∗$ is right associative
2. $+$ is right associative, while $∗$ is left associative
3. Both $+$ and $∗$ are right associative
4. Both $+$ and $∗$ are left associative

$P \rightarrow Q+P$ here  $P$ is to right of $+$

So, $+$ is right associative

Similarly, for  $T \rightarrow T *U$    $*$ is left associative as $T$ is to left of $*$

edited by
+7
Systematic, way to check associativity and precedence for any operator precedence grammar is using "operator precedence table".
To construct operator precedence table, we first find out "leading" and "trailing" of every Nonterminal then we use these for table construction.
0
But

S ------> T * P

So how can say that * is Left Associative???
+1

S -> T * P isn't a recursive production, so it won't define the associativity.

TTU is a recursive production Hence defines left associativity.

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. 

1
2