Skip to content
Official PYQ · 2000

GATE CS 2000 Q2.21 — Parsing (LL, LR)

Explain with AI

Given the following expression grammar:

E→E∗F∣F+E∣FF→F−F∣id\begin{align} E &\to E * F \mid F + E \mid F \\[1em] F &\to F - F \mid id \end{align}

Which of the following is true?

Choose one option
View worked solution

Answer: B

In a context-free grammar representing expressions, the precedence of operators is determined by their position in the derivation hierarchy:

  • Operators defined in non-terminals closer to the start symbol (higher in the grammar) have lower precedence.
  • Operators defined in non-terminals further from the start symbol (lower in the grammar, closer to the operands) have higher precedence because they group more tightly.

Let's analyze the given grammar:

E→E∗F∣F+E∣FF→F−F∣id\begin{align*}E &\to E * F \mid F + E \mid F \\F &\to F - F \mid id\end{align*}

Here, EE is the start symbol, which defines the operators ∗* and ++. The non-terminal FF is lower in the hierarchy (since EE derives FF) and defines the operator −-.

Since FF is at a lower level in the grammar than EE, the operator −- has higher precedence than both ∗* and ++.

Example: Consider the expression id∗id−idid * id - id. The parse tree for this expression is:

      E
/|\
E * F
| /|\
F F - F
| | |
id id id

In this tree, the subtraction (id−idid - id) is grouped as a sub-tree under FF and must be evaluated before the multiplication (∗*) at EE. This structurally demonstrates that −- has higher precedence than ∗*.

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

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