GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
377 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 (76.3k points)  
reopened by | 377 views

1 Answer

+6 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.4k points)  
selected 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.

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

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


Top Users Apr 2017
  1. akash.dinkar12

    3752 Points

  2. Divya Bharti

    2618 Points

  3. Deepthi_ts

    2162 Points

  4. rude

    1966 Points

  5. Tesla!

    1768 Points

  6. Sanjay Sharma

    1646 Points

  7. Debashish Deka

    1614 Points

  8. Shubham Sharma 2

    1610 Points

  9. Prashant.

    1554 Points

  10. Kapil

    1528 Points

Monthly Topper: Rs. 500 gift card

22,100 questions
28,082 answers
63,368 comments
24,203 users