The Gateway to Computer Science Excellence
+22 votes

Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.$$\begin{array}{l|l} E\rightarrow number & E.val = {number.val} \\\qquad \mid \ E \ \ ‘+\text{'} \ E & E^{(1)}.val = E^{(2)}.val + E^{(3)}.val     \\\qquad \mid \ E \ \ ‘\times\text{'} \  E & E^{(1)}.val = E^{(2)}.val \times E^{(3)}.val  \end{array}$$

Assume the conflicts of this question are resolved using yacc tool and an LALR(1) parser is generated for parsing arithmetic expressions as per the given grammar. Consider an expression $3 \times 2 + 1$. What precedence and associativity properties does the generated parser realize?

  1. Equal precedence and left associativity; expression is evaluated to $7$

  2. Equal precedence and right associativity; expression is evaluated to $9$

  3. Precedence of ‘$\times$’ is higher than that of ‘$+$’, and both operators are left associative; expression is evaluated to $7$

  4. Precedence of ‘$+$’ is higher than that of ‘$\times$’, and both operators are left associative; expression is evaluated to $9$

in Compiler Design by Veteran (105k points)
edited by | 3.3k views
the question asks-  What precedence and associativity properties does the generated parser realize?

according to me + is having higher precedence than *.

please someone explain ?

Since yaac prefers shift over reduce and it performs 2+1 first and then multiplied so how + and * have same precedence?

@sushmita  LALR parser is SR parser here we use right most derivation...that's why we use right associativity...because we  reduced from right ..

All the productions are in same level therefore all have same precedence.

If a grammar has same precedence for 2 different operator (+,*) then it is ambiguous Grammar.

2 Answers

+38 votes
Best answer

LALR Parser is type of Bottom up Parser which uses Right most Derivation

For $3×2+1$

$E \rightarrow E * E$ (Both shift and reduce possible but yacc prefers shift)

     $ \rightarrow E * E + E$ 

     $ \rightarrow E * E + 1$

     $ \rightarrow E * 2 + 1$

     $ \rightarrow E * 3$

     $ \rightarrow 3 * 3$

     $ \rightarrow 9$

All the productions are in same level therefore all have same precedence

Therefore Ans is B. Equal precedence and right associativity; expression is evaluated to 9.

by Boss (11.2k points)
edited by
simple & good explanation..

Rajesh Pradhan What happened ?

the link which you gave is part-a and this particular question is part-b both are linked.


Actually, both the Answers available in Link contain part-b also.


LALR Parser is type of Bottom up Parser which uses Right most Derivation

By this info. I think we may get result as 7 also.

(We know grammar is ambiguous here so same precedency )

But due to YACC tool(prefer shift over reduce) given as part of linked question here even if 3∗2 (E∗E) handle found on top of the stack at some point of time, it will shift on reading + instead of reducing with E→E∗E.

But as per ur given quotation it may compute 3*2 also.

Plz see Debashis's ans in given link there he have put the o/p with black screen.


Rajesh Pradhan 

Actually after writing this ans, Shobith had informed me about ambiguity in this grammar so i had hidden this answer but it was Arjun Sir who has reshown this answer which you can see in the status

He had removed all other ans in this particular question and just retained my answer.

It would be better if he only tell the reason for that!!

@Rajesh, question here assumes "yacc". I just corrected it.
@Arjun sir

So the question says conflicts will be resolved by "yacc" does that mean ambiguity will be resolved?
Ok. @Arjun Sir.

@Prajwal Shift-Reduce Conflict (NOT AMBIGUITY)will solve by yacc by preferring Shift operation over reduce operation.

So, here though it will find 3∗2 (E∗E) handle on top of the stack initially, it will shift on reading + instead of reducing with E→E∗E.

so it work like right associativity(and we know priority is same due to ambiguous grammar.)

so 3*(2+1) =9
What makes it right associative?
(Both shift and reduce possible but yacc prefers shift)  why do we say this
How is it right associative?
Why can't precedence change? As SR conflict resolved in favour of shift,  '+' is done before '*'. Why can't we say that '+' has higher precedence over '*'?
I think precedence depends upon the grammar we use.
Also, YACC just resolves the SR to choose Shift over Reduce if expression will be "1+2*3" then * is chosen over adding 1+2, it doesn't mean that precedence changes.

Precedence will be same of both + and * as they are on same level in tree.
Got it thanx!!

@Arjun sir,


Whenever we are at first id.We push to stack. Now in stack we have id which is handle.And next input is *.Now on + also i can either result E->id or shift *.So Shift is not preferred over reduce here at first step in the answer

i think First of all everything will goto stack and then reduce in reverse order.Please clear

draw corresponding LALR(1) DFA, you will get exactly how is it working.
yes i checked with that.Because on reading "num" ,we will go to the state that will reduce E->num. and there are no conflicts,is this correct ?
it is going like this







E->3*E->2+ // see here it is not reducing E->E*E, instead shifting +, because S will be favoured over R




ok.I have one doubt here :-





Doubt:- 3 is look ahead,then you applied E->3 and reduce it to E,after that why is it E->3*?Here stack contains only E* as E->3 is reduced?

Also this E->3 is reduced because there is no SR conflict on reading num?
these are not productions, instead these are steps of formation of parse tree,

E->3* means



                     3   *

and yes E->3, this reduction took place because there was no SR conflict in that state where this reduction is done, otherwise, shift move would be taken over reduce by YACC.
Can someone please explain how the preference of S over R is leading to right associativity?

I have made the parsing table from DFA, using which input can be parsed and checked what happens when shift is favoured over reduce.

Consider production as

$1:E \Rightarrow num$

$2:E \Rightarrow E+E$

$3:E \Rightarrow E \times E$

  + X $ num E
0       S1 2
1 R1 R1 R1    
2 S4 S3 Accept    
3       S1 5
4       S1 6
5 S4/R3 S3/R3 R3    
6 S4/R2 S5/R2 R2    

Now, when you'll parse input $3 \times 2+1$, it is resolved like $3 \times 3\Rightarrow 9$

Equal precedence and Right associativity observed.


@Ayush Upadhyaya I am at state 0 and looking at 3... what shall I do according to your table? There is no reduce move from E-> num. 


@Ayush Upadhyaya state 0 on num should be S1 not 0 on $


@tusharp-is it correct now?


@Ayush Upadhyaya yeah perfect :)

All the productions are at the same level, therefore, all have the same precedence, still + is evaluated first before * so this grammar is Right Associative we can also say this because bottom-up parsers follow rightmost derivation so right most operators will be evaluated first.
+1 vote

None of the Options is correct here ... The answer has to be "precedence of + is higher than * " and "both * and + are right associative"

Here we will never come across an RR conflict because we dont have 2 productions with the same RHS but different LHS ... 

EX : In the grammar, 



we have 2 productions with the same RHS (which is a) but different LHS (S and A) ... Now while parsing a string I might come across a single state with productions as A->a. and S->a. Now this state will create a conflict on whether should I reduce string "a" to S or A ... So clearly there is an RR conflict here .... 

But in the given grammar it is not the case ... 

While parsing a string say "num+num*num" from the above grammar,I will come across an SR conflict ... When ?? after scanning num+num , I have a choice on whether should I shift on * (as good as giving higher precedence to * over +) or reduce "num+num" to E (as good as giving higher precedence to + over *) ... So here there is an SR conflict ... 

YACC tool always goes in-favour of SHIFT incase of SR conflict (and first reduce incase of RR conflict) ...

So,since we are using YACC to resolve conflicts, here + will be given higher precedence over * but incase if we come across a string like 2+3+5 , it will be right associative ... 

None of the Options is correct here ...

by Loyal (8k points)
edited by

although your answer explains that in certain cases when the grammar poses an RR conflict, the grammar rule which comes before will get a priority, it still is misleading as far as this grammar and question is concerned.


Since the case that you are mentioning here (RR conflict priority decision) doesn't concern the grammar given in question, I think the correct option, according to you, that " precedence of + is higher than * " is simply wrong.


Also after reading your answer, I feel that you don't clearly understand the precedence and associativity concepts. Always, remember, precedence is established first. Once the precedence rules are established, and there comes a case where two operators come one after another and have equal precedence (the operators may be same or different), only then, we apply the associativity rule.


In the last part of your answer, you are using the associativity rule to derive the precedence of + and x which I find to be a reason in concluding the possibility of confusion that you might have regarding associativity and precedence.


Nice catch though regarding RR conflict.


Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,309 answers
105,022 users