in Compiler Design
6,644 views
20 votes
20 votes

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
in Compiler Design
6.6k views

2 Answers

42 votes
42 votes
Best answer

$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 $*$

So, answer is B

edited by

4 Comments

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.
13
13
But

S ------> T * P

So how can say that * is Left Associative???
0
0
edited by

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

TTU is a recursive production Hence defines left associativity.

5
5

For leading and trailing:

Ref: here and here

3
3
6 votes
6 votes
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. 
Answer:

Related questions