Skip to content
Official PYQ · 2006

GATE CS 2006 Q85 — Parsing (LL, LR)

Explain with AI

The grammar

  • S→AC∣CBS\rightarrow AC\mid CB
  • C→aCb∣ϵC\rightarrow aCb\mid \epsilon
  • A→aA∣aA\rightarrow aA\mid a
  • B→Bb∣bB\rightarrow Bb\mid b

generates the language L={aibj∣i≠j}L=\left \{ a^{i}b^{j}\mid i\neq j \right \}. In this grammar what is the length of the derivation (number of steps starting from SS) to generate the string albma^{l}b^{m} with l≠ml\neq m

Choose one option
View worked solution

Answer: A

To find the length of the derivation for the string albma^l b^m (where l≠ml \neq m), we analyze the two possible cases based on the grammar rules:

Case 1: l>ml > m

Since the number of aa's is greater than bb's, we must start with the production S→ACS \rightarrow AC.

  • Step 1: S→ACS \rightarrow AC (1 step)
  • Derivation of C→ambmC \rightarrow a^m b^m: We apply C→aCbC \rightarrow aCb exactly mm times, followed by C→ϵC \rightarrow \epsilon once. This takes m+1m + 1 steps.
  • Derivation of A→al−mA \rightarrow a^{l-m}: We apply A→aAA \rightarrow aA exactly (l−m−1)(l - m - 1) times, followed by A→aA \rightarrow a once. This takes l−ml - m steps.

Total steps for l>ml > m is: Total=1+(m+1)+(l−m)=l+2\text{Total} = 1 + (m + 1) + (l - m) = l + 2 Since l>ml > m, this is equal to max⁡(l,m)+2\max(l, m) + 2.

Case 2: l<ml < m

Since the number of bb's is greater than aa's, we must start with the production S→CBS \rightarrow CB.

  • Step 1: S→CBS \rightarrow CB (1 step)
  • Derivation of C→alblC \rightarrow a^l b^l: We apply C→aCbC \rightarrow aCb exactly ll times, followed by C→ϵC \rightarrow \epsilon once. This takes l+1l + 1 steps.
  • Derivation of B→bm−lB \rightarrow b^{m-l}: We apply B→BbB \rightarrow Bb exactly (m−l−1)(m - l - 1) times, followed by B→bB \rightarrow b once. This takes m−lm - l steps.

Total steps for l<ml < m is: Total=1+(l+1)+(m−l)=m+2\text{Total} = 1 + (l + 1) + (m - l) = m + 2 Since l<ml < m, this is equal to max⁡(l,m)+2\max(l, m) + 2.

In both cases, the total number of derivation steps is max⁡(l,m)+2\max(l, m) + 2.

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

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