Context Free Grammar
What is Context Free Grammar? & EXAMPLES:
A context-free grammar (CFG) consisting of a finite set of grammar (G) rules is a quadruple (V, T, P, S) whereV is a set of non-terminal symbols.
T is a set of terminal symbols
P is productions; Which are set of Rules;
S is start Symbol.
In a CFG rule must have a Have a L.H.S that is a Single Non-Terminal Have a R.H.S (it is a String of 0 or more terminals & Nonterminals
---------------------------------------------------------------------------------------------------------------
CFG Examples
--------------------------------------------------------------------------------------------------------------
Write a CFG for Balanced Paranetheses Langauge
G={{S,(,)},{(,)},R,S}
V={S,(,)} , T={(,)} S={S} where R=
S ->(S)
S->ε
S->SS
Write a CFG for anbn
G={{S,a,b},{a,b},R,S}
where R=
S ->aSb
S->ε
Write a CFG for Even Length Palindrome Where W∈{a,b}* G={{S,a,b}, {a,b},R,S}
S->aSa
S->bSb
S->ε
Write a CFG to accept set of all Palindrome over input {0,1} W∈{a,b}* G={{S,0,1}, {0,1},R,S}
S->0S0
S-> 0
S-> 1
S->1S1
S->ε
Write a CFG to accept set of odd Palindrome over input {0,1} W∈{a,b}*
G={{S,0,1}, {0,1},R,S}
R={
S->0S0
S-> 0
S-> 1
S->1S1
}
Write a CFG for Equal no.of a's and b's
S->aSb
S->bSa
S->SS
S->ε
----------------
OR
-----------------
S->aSbS
S->bSaS
S-> ε
Write a CFG for L={nb(w)=na(w)+1}
S->aSb
S->bSa
S->SS
S->b | ε
Write a CFG for L={na(w)=nb(w)+1}
S->aSb
S->bSa
S->SS
S->a | ε
Write a CFG for L={0n12n n>=0}
S-> 0S11
S->ε
Write a CFG for L={0n12n n>=0}
S-> 0S11
S->ε
Write a CFG for to accept string's of 0's and 1's consists of any no. of 0's &1's.
S->ε
S-> 0S
S->1S
Write a CFG to accept string consists of even no. of a's or for language L={a2n|n>=0}or L={#amod 2 =0}
S->ε
S->aaS
Construct a CFG for L={anbmck| m,n>=0 &k=m+n}
Let us rewrite the L ={anbmck }={anbmcm+n }={anbmcm cn}=
={anX cn} where X=bmcm
S-> a S c | X
X->b X c | ε
Construct a CFG for L={akbmcn| m,n>=1 & m+n=k}
Let us rewrite the L ={akbmcn}={am+nbmcn }={amanbm cn}={anambm cn}
={anX cn} where X=ambm
S-> a S c | aaXbc
X->a X b | ε
Obtain a CFG for L={anwwRbn| w𝛜{0,1}* n>=2}
S-> aaTbb
T->aTb|B
B->0B0|1B1|ε
Construct a CFG for L={anbncm :n,m>=0}
Let us divide the string into 2 regions: N=anbn & M=cm
S-> N M
N-> aNb |ε
M->cM | ε
Construct a CFG for L={anbmcm :n,m>=2}
Let us divide the string into 2 regions: N=an & M=bmcm
S-> N M
N-> aN | aa
M->bMc | bbcc
Construct a CFG for L={ambncm :n,m>=0}
S-> aSc | T
T-> bT | ε
Comments
Post a Comment