edited by
10,868 views
33 votes
33 votes

Consider the following translation scheme. 

  • $ S\rightarrow ER$
  • $ R\rightarrow *E\left \{ \text{print}(\text{‘}*\text{’}); \right \} R\mid \varepsilon $
  • $ E\rightarrow F+E\left \{ \text{print}(\text{‘}+\text{’}); \right \}\mid F $
  • $ F\rightarrow (S)\mid id \left \{ \text{print}(id.value); \right \} $

Here $id$ is a token that represents an integer and $id.value$ represents the corresponding integer value. For an input $\text{‘}2 * 3 + 4\text{’},$  this translation scheme prints 

  1. $2 * 3 + 4$
  2. $2 * +3 \ 4$
  3. $2 \ 3 * 4 +$
  4. $2 \ 3 \ 4+*$
edited by

4 Answers

Best answer
44 votes
44 votes

Correct Option: D

Make a tree and perform post order evaluation.

edited by
0 votes
0 votes
This grammer is actually left recursive removed grammer of S-->E*E|E  E-->F+E|F and so on. So + have highest precedence than * . So although the grammer give two parse tree for the given i/p string.. The correct ans should be which allow + have higher precedence than * .. i.e Option D.
0 votes
0 votes
clearly here {} is present in between the variables in r.h.s hence we will go for top down parsing as it is L-attributed. Now refer the best answer for the figure.
–2 votes
–2 votes

My Guesstimation: ..Usuallhy in this typ of question we perform top down left to right... but here  { } is given in between...so this is S-attributed grammer(use bottom up parsing)

Answer:

Related questions