- Asked 2/7
- 2071 Chaitra
CFG for aᵐbⁿ with m ≥ n
Short answer
Grammar: S → aSb | aS | ab. Derivation for aaab: S ⇒ aS ⇒ aaS ⇒ aaab. Parse tree: root S with children a, S; that S with a, S; that S with a, b (leaves a a a b).
Construct a CFG for the language L(G) = {aᵐbⁿ : m, n > 0, m ≥ n}. Use the grammar to generate the string "aaab". Also draw the parse tree.
L = { aᵐbⁿ : m ≥ n ≥ 1 }. Each b is matched with an a, and the extra a's (m − n of them) can be placed anywhere on the left, because all the a's come first.
Grammar
G = ({S}, {a, b}, P, S) with
S → aSb | aS | ab
- S → aSb adds one matched a…b pair.
- S → aS adds an extra a (so m can exceed n).
- S → ab ends the derivation with the last pair, ensuring n ≥ 1.
Every string is of the form aᵐbⁿ with m ≥ n ≥ 1: only a's are added on the left and b's on the right, and each b is created together with an a.
Derivation of aaab (m = 3, n = 1)
S ⇒ aS ⇒ aaS ⇒ aaab
The rules used are S → aS twice, then S → ab.
Parse tree
S
/ |
a S
/ |
a S
/ \
a b
Reading the leaves from left to right gives a, a, a, b = aaab. Each interior node is labelled S, and the bottom S uses S → ab.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- CFG for aⁿb^(2n); parse tree · 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
- Language of grammar; grammar for strings not ending in ab · Discrete Structure, chapter 2
- Construct FSA accepting reverse language · Discrete Structure, chapter 2