2.6k 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 | 2.6k views
0
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.

by Boss (11.1k points)
edited by
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?
+2

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

by Loyal (7.5k points)
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.

1
2
3
4
5
6