ISRO Scientist/Engineer CSE 2023Theory of Computation › Not yet assigned to a topic
ISRO Scientist/Engineer CSE 2023 PYQ: Consider the context-free grammar $G$ below for arithmetic expressions : $$\begin{array}{l} E \rightarrow…
Consider the context-free grammar $G$ below for arithmetic expressions :
$$\begin{array}{l}
E \rightarrow E+E|E \times E| \text { id }
\end{array}$$
Which of the following statements is TRUE:
- AThe string $"\mathrm{id}$ $+\mathrm{id} \times \mathrm{id}$ " has no parse tree according to $G$
- BThe string $"\mathrm{id}$ $+\mathrm{id} \times$ $\mathrm{id}$ " has only one parse tree according to $G$
- CThe string $"\mathrm{id}$ $+\mathrm{id} \times \mathrm{id}$ " has exactly two parse trees according to $G$Correct answer
- DThe string $"\mathrm{id}$ $+\mathrm{id} \times \mathrm{id}$ " has more than two parse trees according to $G$
Answer
(C) The string $"\mathrm{id}$ $+\mathrm{id} \times \mathrm{id}$ " has exactly two parse trees according to $G$
Approach
The grammar E → E+E | E×E | id is ambiguous. For the string id+id×id, we must count the number of distinct parse trees. Since the grammar has no precedence or associativity rules, the operators + and × can be grouped in different ways, leading to exactly two distinct parse trees.
Reviewed by ISRO IQ Editorial Team.