본문 바로가기

Cfg Solved Examples -

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

So the sequence of rules: aSbb then aSb then ε. Good. So grammar works. Language : ( w \in a,b^* \mid w = w^R ) cfg solved examples

: [ E \to E + T \mid T ] [ T \to T \times F \mid F ] [ F \to (E) \mid a \mid b ] Check: ( S \Rightarrow aA \Rightarrow abS \Rightarrow

: [ S \to aS \mid bS \mid \varepsilon ] Wait — that gives any length. Let's fix: So the sequence of rules: aSbb then aSb then ε

That means m=3 not reachable for n=2 in this grammar? Correct — known property: this grammar gives m = n + k where k is number of times you used aSbb. For n=2, k can be 0 or 1 or 2 → m=2,3,4 possible. Yes, so m=3 possible: n=2,k=1 → S → aSbb → a(aεbb)bb? Let’s do stepwise:

: [ S \Rightarrow SS \Rightarrow (S)S \Rightarrow ((S))S \Rightarrow (())S \Rightarrow (())(S) \Rightarrow (())() ] 4. Example 3 – ( a^n b^n ) (equal number of a’s and b’s) Language : ( a^n b^n \mid n \ge 0 )

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: