Back to the 2025 paper

Module 2: Context-Free Languages (CFL) and PDA

20257m

Given the grammar
G=({S},{a,b},P,S)G = (\{S\}, \{a, b\}, P, S)
P:S→aSb∣abP: S \rightarrow aSb | ab

Derive the string aabb using leftmost derivation, rightmost derivation and also draw the parse tree.

Worked SolutionAI Assisted

Answer

Grammar: G=({S},{a,b},P,S)G = (\{S\}, \{a,b\}, P, S) with P:S→aSb∣abP: S \rightarrow aSb \mid ab

1. Leftmost Derivation (LMD)

At each step, replace the leftmost non-terminal.

S⇒lmaSb(using S→aSb)⇒lmaaSbb(using S→aSb on leftmost S)⇒lmaabbb(using S→ab on leftmost S)=aabb\begin{aligned} S &\Rightarrow_{\text{lm}} aSb & \text{(using $S \rightarrow aSb$)} \\ &\Rightarrow_{\text{lm}} aaSbb & \text{(using $S \rightarrow aSb$ on leftmost $S$)} \\ &\Rightarrow_{\text{lm}} aabbb & \text{(using $S \rightarrow ab$ on leftmost $S$)} \\ &= \mathbf{aabb} \end{aligned}

LMD Steps: S⇒lmaSb⇒lmaaSbb⇒lmaabbb=aabbS \xRightarrow{lm} aSb \xRightarrow{lm} aaSbb \xRightarrow{lm} aabbb = aabb

2. Rightmost Derivation (RMD)

At each step, replace the rightmost non-terminal.

S⇒rmaSb(using S→aSb)⇒rmaaSbb(using S→aSb on rightmost S)⇒rmaabbb(using S→ab on rightmost S)=aabb\begin{aligned} S &\Rightarrow_{\text{rm}} aSb & \text{(using $S \rightarrow aSb$)} \\ &\Rightarrow_{\text{rm}} aaSbb & \text{(using $S \rightarrow aSb$ on rightmost $S$)} \\ &\Rightarrow_{\text{rm}} aabbb & \text{(using $S \rightarrow ab$ on rightmost $S$)} \\ &= \mathbf{aabb} \end{aligned}

RMD Steps: S⇒rmaSb⇒rmaaSbb⇒rmaabbb=aabbS \xRightarrow{rm} aSb \xRightarrow{rm} aaSbb \xRightarrow{rm} aabbb = aabb

Note: Since this grammar is unambiguous and has only one non-terminal, LMD and RMD coincide in the sequence of sentential forms.

3. Parse Tree (Derivation Tree)

            S
           /|\
          a S b
            |
            S
           /|\
          a S b
            |
            ab

Text/Mermaid representation:

MermaidDiagram
Rendering diagram…

Tree structure:

  • Root: SS
  • Internal nodes: SS (non-terminals)
  • Leaves (yield): a,a,b,ba, a, b, b (reading left to right = aabb)
  • Height: 3 (excluding root)

Verification

  • String generated: aabb ✓
  • Language of grammar: L(G)={anbn∣n≥1}L(G) = \{a^n b^n \mid n \ge 1\}
  • aabb corresponds to n=2n=2 ✓

Final Answer

LMD: S⇒lmaSb⇒lmaaSbb⇒lmaabbbS \xRightarrow{lm} aSb \xRightarrow{lm} aaSbb \xRightarrow{lm} aabbb
RMD: S⇒rmaSb⇒rmaaSbb⇒rmaabbbS \xRightarrow{rm} aSb \xRightarrow{rm} aaSbb \xRightarrow{rm} aabbb
Parse Tree: As shown above (yield = aabb).

Similar questions

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.