Examples — Cfg Solved

Derivation for abba : [ S \Rightarrow aSbS \Rightarrow a\varepsilon bS \Rightarrow abS \Rightarrow abbSaS \Rightarrow abb\varepsilon a\varepsilon = abba ] Language : Valid arithmetic expressions with a, b, +, *, (, )

: [ S \Rightarrow aSa \Rightarrow aba ] 7. Example 6 – ( a^i b^j c^k ) with i+j = k Language : ( a^i b^j c^i+j \mid i,j \ge 0 )

: [ S \to SS \mid (S) \mid \varepsilon ] cfg solved examples

S → aSbb → a(aSbb)bb → aa(ε)bbbb → aabbbb (wrong). So that’s 4 b’s, not 3.

S ⇒ aSbb (first a) Now replace S with aSbb again? That would add another a. We need total 2 a’s. So second S must be ε: S ⇒ aSbb ⇒ a(aSbb)bb — now we have 2 a’s so S → ε: ⇒ a(aεbb)bb = aa b b b b = 2 a, 4 b (m=4). Not 3. Derivation for abba : [ S \Rightarrow aSbS

Check: ( S \Rightarrow aA \Rightarrow abS \Rightarrow ab\varepsilon = ab ) (length 2). Works. Language : All strings of ( and ) that are balanced.

: [ S \to aSbS \mid bSaS \mid \varepsilon ] S ⇒ aSbb (first a) Now replace S with aSbb again

Better approach — known correct grammar: [ S \to aSb \mid aSbb \mid \varepsilon ] For m=3, n=2: S → aSbb → a(aSb)bb → aa(ε)bbbb? No — that’s 4 b’s. So maybe n=2, m=3 not possible? Actually it is: ( a^2 b^3 ) = a a b b b. Let’s test:

: [ S \to aS \mid bS \mid \varepsilon ] Wait — that gives any length. Let's fix:

[ S \to aA \mid bA \mid \varepsilon ] [ A \to aS \mid bS ]