GATE2005-83b

4.3k views

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$

edited
1
the question asks-  What precedence and associativity properties does the generated parser realize?

according to me + is having higher precedence than *.

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

@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.

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

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.

edited by
2
simple & good explanation..
0
0

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

2

@Prajwal,

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.

1

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!!

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

So the question says conflicts will be resolved by "yacc" does that mean ambiguity will be resolved?
1
yes.
4
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
0
What makes it right associative?
0
(Both shift and reduce possible but yacc prefers shift)  why do we say this
0
How is it right associative?
2
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 '*'?
3
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.
1
Got it thanx!!
0

@Arjun sir,

id*id+id

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

0
draw corresponding LALR(1) DFA, you will get exactly how is it working.
0
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 ?
0
it is going like this

3*2+1

3

E->3

E->3*

E->3*2

E->3*E->2

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

E->3*E->2+1

E->3*E->2+E->1

E->3*E

E
0
ok.I have one doubt here :-

3*2+1

3

E->3

E->3*

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?
0
these are not productions, instead these are steps of formation of parse tree,

E->3* means

E

/

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.
0
Can someone please explain how the preference of S over R is leading to right associativity?
3

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. 0 @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. 0 @Ayush Upadhyaya state 0 on num should be S1 not 0 on$

0

@tusharp-is it correct now?

0

0
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,

S->A/a,

A->a

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

edited
0

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

1
5.1k views
Consider the grammar: $S \rightarrow (S) \mid a$ Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be $n_1, n_2$ and $n_3$ respectively. The following relationship holds good: $n_1 < n_2 < n_3$ $n_1 = n_3 < n_2$ $n_1 = n_2 = n_3$ $n_1 \geq n_3 \geq n_2$
Consider the context-free grammar $E\rightarrow E+E$ $E\rightarrow (E *E)$ $E\rightarrow \text{id}$ where $E$ is the starting symbol, the set of terminals is $\{id, (,+,),*\}$, and the set of non-terminals is $\{E\}$. For the terminal string $id + id + id + id$, how many parse trees are possible? $5$ $4$ $3$ $2$
The grammar $A \rightarrow AA \mid (A) \mid \epsilon$ is not suitable for predictive-parsing because the grammar is: ambiguous left-recursive right-recursive an operator-grammar