Skip to content
Official PYQ · 2006

GATE CS 2006 Q59 — Parsing (LL, LR)

Explain with AI

Consider the following translation scheme. 

  • S→ERS\rightarrow ER
  • R→∗E{print(‘∗’);}R∣εR\rightarrow *E\left \{ \text{print}(\text{‘}*\text{’}); \right \} R\mid \varepsilon
  • E→F+E{print(‘+’);}∣FE\rightarrow F+E\left \{ \text{print}(\text{‘}+\text{’}); \right \}\mid F
  • F→(S)∣id{print(id.value);}F\rightarrow (S)\mid id \left \{ \text{print}(id.value); \right \}

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

Choose one option
View worked solution

Answer: D

To find the output of the translation scheme for the input 2∗3+42 * 3 + 4, we construct the parse tree and execute the semantic actions during a depth-first (post-order) traversal.

The parse tree for the input string id2∗id3+id4id_2 * id_3 + id_4 is structured as follows:

  • S→E  RS \rightarrow E \; R
    • E→F→id2  {print(2);}E \rightarrow F \rightarrow id_2 \; \{\text{print}(2);\}
    • R→∗  E1  {print(‘∗’);}  R1R \rightarrow * \; E_1 \; \{\text{print}(‘*’);\} \; R_1
      • E1→F1+E2  {print(‘+’);}E_1 \rightarrow F_1 + E_2 \; \{\text{print}(‘+’);\}
        • F1→id3  {print(3);}F_1 \rightarrow id_3 \; \{\text{print}(3);\}
        • E2→F2→id4  {print(4);}E_2 \rightarrow F_2 \rightarrow id_4 \; \{\text{print}(4);\}
      • R1→εR_1 \rightarrow \varepsilon

Performing a depth-first traversal, the semantic actions are executed in the following order:

  1. Under EE, the token id2id_2 is matched: prints 2
  2. Under E1E_1, the token id3id_3 is matched: prints 3
  3. Under E2E_2, the token id4id_4 is matched: prints 4
  4. The action in E1→F1+E2{print(‘+’);}E_1 \rightarrow F_1 + E_2 \{\text{print}(‘+’);\} is executed: prints +
  5. The action in R→∗E1{print(‘∗’);}R1R \rightarrow * E_1 \{\text{print}(‘*’);\} R_1 is executed: prints *

Combining the printed values in order of execution gives: 2 3 4 + *.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions