- Asked 2/7
- 2073 Shrawan
CFG for aⁿb^(2n); parse tree
Short answer
Grammar: S → aSbb | abb. Derivation for aabbbb: S ⇒ aSbb ⇒ aabbbb. Parse tree: root S → aSbb (children a, S, b, b); inner S → abb (children a, b, b). Leaves: a a b b b b.
Construct a CFG for the language L = {aⁿb^(2n), n > 0} and use this grammar to generate the string aabbbb. Also construct the parse tree.
L = { aⁿb²ⁿ : n > 0 }. Each a is matched with two b's.
Grammar
G = ({S}, {a, b}, P, S) with
S → aSbb | abb
- S → aSbb adds one a on the left and two b's on the right.
- S → abb ends the derivation with the innermost a and its two b's, so n ≥ 1.
By induction on n, S ⇒* aⁿb²ⁿ, and every derived terminal string has this form.
Derivation of aabbbb (n = 2)
S ⇒ aSbb ⇒ a(abb)bb = aabbbb (S → aSbb, then S → abb)
Parse tree
S
/ / \ \
a S b b
/ | \
a b b
The root S uses S → aSbb (children a, S, b, b). The inner S uses S → abb (children a, b, b). Leaves from left to right: a, a, b, b, b, b = aabbbb.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- CFG for aᵐbⁿ with m ≥ n · Theory of Computation, chapter 3 · asked 2×
- Role of parse tree in CFG · Theory of Computation, chapter 3
- CFG for palindromes with parse trees · Theory of Computation, chapter 3
- CFG versus unrestricted grammar · Theory of Computation, chapter 4
- Unrestricted grammar; superset of CFG · Theory of Computation, chapter 4 · asked 2×
- Ambiguity; language of S→SS|a · Theory of Computation, chapter 3
- Define CFG with example · Theory of Computation, chapter 3 · asked 2×
- Language of grammar; grammar for strings not ending in ab · Discrete Structure, chapter 2
- Strictly binary tree has 2n-1 nodes · Data Structure and Algorithm, chapter 6
- Construct FSA accepting reverse language · Discrete Structure, chapter 2
- NFA and DFA from grammar σ→bσ|aC · Discrete Structure, chapter 2 · asked 4×