Question:

A grammar is given:
$E \rightarrow E + T \mid T$
$T \rightarrow T * F \mid F$
$F \rightarrow (E) \mid id$
For the input: $id + id * id$, what is the correct interpretation?

Show Hint

This is a standard "Expression Grammar." To remember precedence, look at the depth: the deeper the operator is in the grammar tree, the higher its priority. Multiplication is "deeper" than addition here.
Updated On: Jul 4, 2026
  • $(id + id) * id$
  • $id + (id * id)$
  • $(id + id * id)$
  • Ambiguous expression
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Concept: In formal languages and compilers, grammars are designed to enforce operator precedence and associativity.
Precedence: Operators defined "lower" in the grammar (further from the start symbol $E$) have higher precedence.
Grammar analysis: Here, $F$ is defined by $T$, and $T$ is defined by $E$. This means multiplication ($*$) in the $T$ rule is evaluated before addition ($+$) in the $E$ rule.

Step 1:
Analyzing operator hierarchy.
In the given grammar:
• The addition operator ($+$) appears in the first level ($E$).
• The multiplication operator ($*$) appears in the second level ($T$). Because $T$ is a sub-component of $E$, the multiplication operation must be resolved to a single "term" ($T$) before the addition can be performed.

Step 2:
Tracing the derivation for $id + id * id$.
To derive this string, the parser would follow: $E \Rightarrow E + T$
$E \Rightarrow T + T$
$E \Rightarrow F + T$
$E \Rightarrow id + (T * F)$
$E \Rightarrow id + (F * F)$
$E \Rightarrow id + (id * id)$

Step 3:
Interpreting the structure.
The derivation shows that the $id * id$ part is grouped together as a single $T$ before being added to the first $id$. Thus, the multiplication happens first, which corresponds to the interpretation $id + (id * id)$.
Was this answer helpful?
0
0