GATE CSE
First time here? Checkout the FAQ!
x
+4 votes
415 views

Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.

$\begin{align*}E \rightarrow & number \\ &| E \text{ '+' } E \\&| E \text{ '}\times\text{' } E \end{align*}$ $\begin {align*} &E.val = \text {number.val} \\ &E^{(1)}.val = E^{(2)}.val + E^{(3)}.val \\ &E^{(1)}.val = E^{(2)}.val \times E^{(3)}.val \end{align*}$

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

asked in Compiler Design by Veteran (77.8k points)  
reopened by | 415 views

1 Answer

+7 votes
Best answer

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

For 3×2+1

E->E * E (Both shift and reduce possible but yacc prefers shift)

  ->E * E + E

  ->E * E + 1

  ->E * 2 + 1

  ->E * 3

  ->3 * 3

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

answered by Veteran (11.6k points)  
selected by
@Arjun sir

So the question says conflicts will be resolved by "yacc" does that mean ambiguity will be resolved?
yes.
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


Top Users Jun 2017
  1. Bikram

    3704 Points

  2. Hemant Parihar

    1484 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1408 Points

  5. Niraj Singh 2

    1311 Points

  6. Rupendra Choudhary

    1194 Points

  7. rahul sharma 5

    1148 Points

  8. Debashish Deka

    1112 Points

  9. srestha

    932 Points

  10. Arjun

    930 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1960 Points

  2. Niraj Singh 2

    1306 Points

  3. junaid ahmad

    502 Points

  4. sudsho

    410 Points

  5. akankshadewangan24

    392 Points


23,361 questions
30,068 answers
67,376 comments
28,385 users