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? $ +$ is left associative, while $∗$ is right associative $ +$ is right associative, while $∗$ is left associative Both $+$ and $∗$ are right associative Both $+$ and $∗$ are left associative Compiler Design gatecse-2014-set2 compiler-design grammar normal + – go_editor asked Sep 28, 2014 go_editor 6.6k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 42 votes 42 votes $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 Pooja Palod answered Oct 23, 2015 edited Dec 1, 2017 by kenzou Pooja Palod comment Share Follow See all 4 Comments See all 4 4 Comments reply Sachin Mittal 1 commented Jan 9, 2017 reply Follow Share 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 votes 13 votes Lakshman Bhaiya commented Jan 24, 2018 reply Follow Share But S ------> T * P So how can say that * is Left Associative??? 0 votes 0 votes vishalshrm539 commented Sep 22, 2018 i edited by Lakshman Bhaiya Jan 17, 2019 reply Follow Share S -> T * P isn't a recursive production, so it won't define the associativity. T→T∗U is a recursive production Hence defines left associativity. 5 votes 5 votes KUSHAGRA गुप्ता commented Dec 4, 2019 reply Follow Share For leading and trailing: Ref: here and here 3 votes 3 votes Please log in or register to add a comment.
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. Regina Phalange answered Apr 3, 2017 Regina Phalange comment Share Follow See all 0 reply Please log in or register to add a comment.