Professional Documents
Culture Documents
CFG and Its FORMS PDF
CFG and Its FORMS PDF
Objectly"
• ~ of MgUlar grammar.
• Conver.;ion of regu1u graDUlIiIr to FA.
• Concept oJ rontut frft! grammar.
• Derivation trees.
• Ambiguity.
• Simp1ilication of CFG.
4.1 Introduction
(In previous ch.:!ptcr) we hive l.eamt the eff",:t1ve use of regubr exprcsslons fOT
defining the regular iangwSC$- In fact we ha;"e l~t throe ways of chu.actmsing
regular ~ges : i) Regular exprcs.sions ill F"wle automata ~ ilil Construction of
nAtural 1&ngwgc:s using simple operations.. There is yet another way of expn::s:sing the
fCgIllar languag:s.; that is by something cAlJed yammar. The gr.unrnar is basic.illy the
set of I'Ules used to define the langw.ge. These I'I.Iks an be rewritten which .ue used
to ~Ie the desiTed .tnng. FOl" exampk>, eonsid.er the Iangu.:a~ rqJresmted by I'
which can be represented by a set U. 11, 111, M.I. To generate the strings o f this
Ianguagl! we will use one simple method ; Let 5 be a symbol to start the process with.
Now we an write a rule with symbol 5 as 5 -t I and 5 ..... 15. These ruIe5 mean tNt
5 is rewritten as 1 or 15. Now 10 gmer.. te a string Ill, start with 5 lhen rep~ 5 by
15, then Igain apply the serond rule to replace 5 by IS wlUch gives 115. Now apply
first rule 5 -+ I to replace 5 by 1. Ultill'l.ltd y we will get the string 111. Thus
grarrunill" is iU\ effective way to represent the regula.r language.
The use of context free pammat is in COOlpiJation of the program. Th;al is. when
we compile the program we san it completely and the syntax of progrilnuning
amstno.:b ill ~ For ~ the SynlD of the pf'Ot;raD'l o:crlain niles should be
ddintd. And this wit can be carried out by COI'Itext he gramm.:rr 01" CFG. In thill
dlapla' we will understand the ~ of CFG, then we will diJCUS5 the derh·~ tion of
grammar and ambiguity in grammaJ"S and languages.
(4 • 1)
Theoty of Comput.lUon .., Context F,... Grllmmar
C _ (V, T, P, 5) when:
V is set of symbols GlIled non tcnnin.tls which ... c: used 10 dc:finc: the: rul=.
T is .1. set of symbols eaJlcd tmninals.
r is .1. set of production rules.
5 is .1. start symbol which E v.
The produ~ rules r are of the form.
A --7 .1.6
A ~.
Where A and B are non lenninool symbols, ~nd .1. i5 termiNI ~bol.
For u.mple,
Consider c ,. (y, T, P, 5) with
V= IS,A)
T "" .(0,11
5 is ~ start symbol and production rules are as given below.
s ~ OS
S --7 IB
B ~ ,
This is oilled a regular l=guase and il can be represented by some DFA. Thus,
when II grammar am be represented by some finite ;lutomata ~ it, is a regul.l.r
grammar.
!..We will understand. thi5 method with th,oo, tu.lp of,..,...., ~ples.
... '-'
,-
'-'
,
,
'-'
(a 'b)"
~
Context Free Grammar
I
and A 0 is , stlort symbol.
Th... C i5 ~l>i=d ~ 5'"..............
. . . Exampt. ...2: Ccmstnld a ~ j.1II6"... Joy (ab + .)' (... + b).
Solution; We wUl ~ construct FA for given u .
,-, Contellll FI"III Gratl'llTlllf
, n , r,;- . r,;- • ~
~
"
•
n•
~
, , r,;- . r,;- • ~
"
• , •
...
U we elin\iIute t transitions then the NFA will be -
• •
~I
S ....
• •
" {Qo. ql' qJ
•
"• • •
...
•
• • •
,., CofttUt FI"M Grammar
• •
[.,
•
, •
CQo , IQ,q"q,/
'"
•
• •
.-.. -~"---;:---'
.'
.,
', __ • b
-'." ,
••
Wecan .......... te .,/
--~~~------
Now WII! nn rcnulloe thoe 1rtiIh5 ;l$ ;
(q o) = 5
(q • . qJ.qtl;: A
Iq".q).q).q,l . B
IqQ. qd" C
Iqd '" D
Theoty of Computlltic)n Cont:lIXt F... Gtarntnar
~
. ~.~.
," ", •
,
The regular grammar em be
54 ...
5~ bD
54b
A ~ aB
B~ aB
B4.
B4 be
B4b
C4 ...
C 4.
C~ bD
C4b
Where A and B an!! non terInirw.I tymbols and .... is a terminal symbol.
U the non tmninal symbol Ippsr5 IS 1 leftmost symbol in ead!. prexiuction of
regular grammar then II is ailed left linear grammar.
The left linear gt.IfNrIU is of following form.
A~ ..
A ~,
Where A and B an!! non tenninal symbols and '1' is a terminal symbol.
Let M .. (Iqo , ql' ..., q~Il:.~. qo' f) be a DFA. The equivalent g:ranunar G an. be
CONtructtd from this OFA such thai produdion:l ,hould (oue>poad 10 tnnsitiOI15. The
derivations o:an be tvminaled by a production rult pvlng lenninals. For such
production JUle, the lnnSitions terminating alsomt ~ stab! is encountntd.
.... G .. (IA o, A 1, ... A.I , l:.P,A o)
Where P the $tI of production ru!es can be defined by following roles.
1. AI -J aA j is a productic:ln rWe if Ii (q j .a) = q ,.whenq, If: F
2. A I -.. a AI and A I -.. a an!! production rules if li (q i' a) = a, where q l E F.
Thus the given gr~ is accepted by DFA M.
•
•
TNotJ of Compubdlod ,.,
Solution : 1he pmtrW' eqwvalent to given OFA will be-
G '" (V, T, P,S)
Ao --t b
ThUll the two stites CaO"I1"e,poop""~ 10 two non tenninab. The state ql is final ,tate.
He:nee the transition tennlnating 10 final state q l are giving the terminal iJYI!Ibok.
Note tNt we &re considering ou~ edges for production rule.
1" Exam,*, 4.A: CoItStnld . rqukr J11lIIlIIIiU for gir:>trI fA
~ ..b
A, --tIA,
A, --t bAc
A, ~b
A, --taA,
A, -+ bAl
A, --taA t
A, ~.
A. --tI A4
Theory of Comput.tion Cont.xt Free GnlmlM'
A . ... b
As state q. is ~ filal state the conesponding non termiJvJ A. is usN. Now;o.ll
the edges leading to q. gives lenniml productions as well
_ Example 4.5: Corutnlct. ,~IIU rwnlJlllr Jar
, ,
,
,
Solution : The equivaJalt reg\lJoor &"lINN, can be denoted by G ... (\I, T, P, 51 where
V '"' {A g , A],A 2 ,A l l
The production rules can be
A D ..... OA 1
AI ..... OA~
A~ ..... OA.
A~ 4 l A J
A . ... 1
--- as I bA I b
~ aA I bSI a
: We can have the FA M as
M =
({q Ot ql' qd, {a, b},o, qOt {qd)
q 0 and q 1 correspond to 5 and A and q f is a final state.
a a
15 basically NFA because from q 0 with b there are two next states q 1 and q f •
Construct a deterministic finite automaton equivalent to the grammar
- as I bS I aA
Start
= (qoJ u</>
= [qo)
O'([qo, ql,q3],a) = o (qo,a) uO(ql,a) UO(q3,a)
= Iqo,q 1 J u</> u</>
Conlut F.... Grammar
= Iq " ql,q11
.. Iq" q"ql]
,s'(!qO' q"ql La) ,. o(1qa!,a) v o(lqlL a) u o(1qlLa)
= Iq" ql! v~ v Iql l
• b
0,' • Iq,)
.,'
• . ,'
Iq,)
•
"
•
•,
.....
.....,
1tIo.ca.]
...
lob q,.q, l
""
• feb. q,.q,1
lei. q,.ql l
.. "
(q",q ..q,1
(q",~q,)
[<I. q,1
Theory of Computation 4 -14 Context Free Grammar
S-7S*S
S -7 (S)
S -74 }
If the language is 4 + 4 * 4 then we can use the production rules given by P. The
start symbol is S. The number of non terminals in the rules P is one and the only non
terminal i.e. S. The terminals are +, *, ( I ) and 4.
We are using following conventions.
1. The capital letters are used to denote the non terminals.
2. The lower case letters are used to denote the terminals.
Example : The formation of production rules for checking syntax of any English
statement is
SENTENCE -7 NOUN VERB
Example 4.8: Construct the CFG for the language having any number of a's over the set
L ={a}.
Solution : As we know the regular expression for above mentioned language is
r.e. = a*
Let us build the production rules for the' same,
rule 1
5 ~ e rule 1
Now if want "aaaaa" string to be derived we can start with start symbols.
1
Theory of Computation 4-16 ,"'" Context Free Grammar
5
as
aaS .. rule 1
aaaS rule 1
aaaaS rule 1
aaaaaS rule 1
aaaaa£ rule 2
=aaaaa
The r.e. = a* suggest a set of {E, a, aa, aaa, ... }. We can have a null string simp;'
because 5 is a start symbol, and rule 2 gives 5 -7 E.
))1. Example 4.9: Try to recognize the language L for given CFG.
G = [{S}, {a,b},p,{S}]
where P= {SS-7abaSb}
-7
are in combination of D's and l's with the start symbol. 5ince (0+1) *
I, 01, 10, 00, 11, ... } in this set e is a string. So in the rules we can set
&illCpie 4.11 Construct a grammar for the language containing strings of atleast two
G = (V, T, P, S)
V = {5, A}
T = {a, b}
P = {5 ~ Aa Aa A
aA I bA Ie}
A ~
: ~ AaAaA is something in which the two a's are maintained since at
d be there in the strings. And A ~ aA IbA IE gives any combination
e. this rules gives the strings of (a + b)* .
.c for this example will be
(any thing) a (any thing)
after a there could be any combination of a's and b's .
.12: Construct a grammar generating
here WE {a,b} *
strmgs which can be generated for given Lis {aacaa, bcb, abcba, bacab ... } -
!::i==~ could be
5 ~ a 5a
5 ~ b 5b
5 ~ c
guage L = w c W T where w E (a + b)*
5 a or 5 ~ b 5 b . The string abcba can be generated from given
I~ I~ I
~as
5
b Ic b
aSa I~ b Ic b I~ I
a b 5b
a bcba
I~ b I~ b I~ I
, this kind of string could be derived from the given production rules.
Theory of Computation 4-18 /'" Context Free Grammar
)'.. Example 4.13: Construct CFG for the language L which has all the strings which are u1J
palindrome over L={ a,b}.
Solution : As we know the strings are palindrome if they posses same alphabets from
forward as well as from backward.
For example, the string "madam" is a palindrome because
-
read
mad am
- read
a
It is the same!
Since the language L is over L = {a, b}. We want the production rules to be build
a's and b's. As E can be the palindrome, a can be palindrome even b can be
palindrome. So we can write the production rules as
G = ({5}, {a, b}, P, 5)
P can be
5 ~ b 5b
5~a
5~b
5 ~ E
ab5ba ~baab~
aba5aba ~b~~ba
a b nE ab n~ b ~ ~ b ~
abaaba ~b~~b~
which is a. palindrome.
).... Example 4.14: Construct CFG which consists of all the strings having at/east one
occurrence of 000.
Solution : The CFG for this language can be equivalent to r.e. (any thing) (000) (anything)
Thus r.e. = (0+1)*000 (O+l)~
Computation 4-19 /'" Context Free Grammar
A ~ OA I 1A IE
T ~ 000
&.:n;ple 4.15: Construct CFG for the language in which there are no consecutive b's,
gs may or may not have consecutive a's.
- -. .~ : : - H S I bA I a IblE
~a 5 la lE
e rules, there is no condition on occurence of a's. But no consecutive b's
_~=- .. Tote that in the rule 5 ~ bA, and A gives all the strings which are
as lIabab S -t a S
a (j S II II b a b S -t a S
&.:nple 4.16: Recognize the context free language for the given CFG.
5~aBlbA
A ~ alaSlbAA
B ~ blb51aBB
aiiiillixl ' 0 find the language denoted by given CFG we try to derive the rules and get
gs. Then after observing those strings we come to know, which language it is
~Ef~ :et us rewrite all those rules and number them
5 ~a B rule 1 ·
5~bA rule 2
rule~
Theory of Computation 4-20 ./'" Context Free Grammar
A ~aS rule 4
A ~bAA ruleS
B~b rule 6
B ~b S rule 7
B ~a BB rule 8
Now let us apply the rules randomly starting with start symbol.
S
aB rule 1
aa B B rule 8
aab S B rule 7
aabbAB rule 2
aabbaB rule 3
aabbabS rule 7
aabbabbA rule 2
aabbabba rule 3
We have got some string as "aabbabba". Let us try out something else.
S
bA rule 2
bb AA ruleS
bbaSA rule 4
bbaaBA rule 1
bbaabA rule 6
bbaaba rule 3
Now we have got "bbaaba" such a string !
Thus by obtaining more and more strings by applying more and more· rules
randomly will help us to find out what language it indicates. Observe the strings
generated carefully, we can draw a conclusion as these strings contain equal number
of a's and equal number of b's. Even you can try out various rules to obtain some
more strings. And see that it is a language L containing all the strings having equal
number of a's and b's.
4 - 21 Context Free Grammar
CFG can be built with a logic as : one rule we will built for double a i.e.
rule we will built for any number of a's and b's in any combination.
B'bB It which is always equivalent to (a + b)'" {you can note it as
.::'l:1:±~e both of these rules we can get the desired context free grammar.
one occurrence)
5 ~ BAB => (anything) ( of double a ( anything)
B ~ aBlbBI£
contains V = {A, B, S} T = {a, b}. The start symbol is S.
AB
AB (tbaab B ~a B
BAB ~baab B ~bB
AB ~baab B · ~£
B (ti?(t~b A ~aa
-:zbB ~i?~~i? B ~ bB
tt (ti?(t(tV> B~£
= ~baab ~i?~~V>
.1 8: Construct crc for the language containing all the strings of different
symbols over L={ 0, 1}.
the problem statement says as if the string starts willi 0 it should end
string starts with 1 it should end with O.
S~OA111AO
A~OAI1Alt
T in the above CFG different start and end symbols are maintained.
Eo::na1 A ~ OA 11A 1£ indicates (0 + 1).... Thus the given CFG is equivalent
~__- expression [0(0 + 1)'" 1 + 1(0 + 1) ... OJ
l.
Theory of Computation 4 -22 Context Free Grammar
Solution : As in some previous example we have seen the case of an b n • Now the number
of b's are doubled. So we can write
S ~ aSbb I abb
It is as simple as this !
)). Example 4.20: Construct the production rules for defining a language
L = {axb Y I x:;ty}.
Solution: This is the language in which all the a's must appear before all the b's. But total
number of a's must not be equal to total number of b's. Either number of a's must be
equal to number of b's or number of b's must be equal to number of a's. The rule
S ~ aSb
gives us equal number of a's must be followed by equal number of b's. But
according to problem statement we need more number of a's either or more number
of b's. Hence the required production rules can be -
s ~ aSb I R1 I R2
R1 ~ aR1 Ia
R2 ~ bR2 11;>
".. Example 4.21 : Find the CFG for the regular expression (110 + 11) . . (10)*.
Solution : Let
r.e. = (110+ 11)* ("10)*
Can be represented ~ T
by the non ~ ~
terminal
A B
If we assume S as start symbol then,
S ~ AB
Now for each non termmal A and B we can define production rules as
A ~ 1l0AIll AlE
B ~ 10 B IE
Hence, finally the CFG fOf given f.e. can be
S ~AB
A ~ 1l0A I llA I E
B ~ lOB I E
putation 4 -23 ' Context Free Grammar
integer is any numerical value without decimal place which can be either
.gned. Hence the CFG can be -
5 ~ Gl
G ~ + 1-
1 ~ DI 1 D
D ~ 0 111213 ... 19
G is for denoting sign, I denotes integer and D denotes digits from 0 to 9. To
_~sf;;lo:i the rules lets derive these rules for some integer.
can be derived as
5 ~ Gl
- I G~-
- DI 1 ~DI
- 21 D~2
- 20 I~D
- 21 D~l
I
This will be
greater than
i+k
L= Oi
Theory of Computation 4-24 /" Context Free Grammar
A ~ 0 A 11 £
B~lBI1
C~.lC21£
B ~ IB I1
C~lC21 £
)1,. Example 4.24: Build a CFG for the language L = {Oi 1i 2k I i=j}
Solution: As i = j and there is no condition on k. We can say that k is an arbitrary value.
Then we rewrite L as
. . k
0' 1J 2
L=
~~
A 8
5 ~ A B
A ~ 0 All £
B~2BI £
. i k
L2 = 0' 1 2 where k > j
Hence the required CFG can be
5 ~ A B
A~OAI£
B->lB2IC
C ~ 2 C 1£
obtain some string from given set of production rules. The derivation
ed parse tree.
s.1IO!..-:::g- are properties of any derivation tree -
node is always a node indicating start symbol.
ation is read from left to right.
: nodes are always terminal nodes .
.or nodes are always the non terminal nodes.
a
Fig. 4.1 Derivation tree
tree is a derivation tree drawn for deriving a string bbabb. By simply
. nodes we can obtain the desired string. The same tree can also be
derivation tree
&~I!»e 4.26: Draw a derivation tree for the string abaaba for the CFG given by,
P = {S ~ aSa
5 ~ bSb
S~alblE}
Theory of Computation 4 - 26 Context Free Gramm.
S -- E is chosen
l~ Example 4.27 ': Construct the derivation tree for the string aabbabba from the CFG give?
by
S ~ aBlbA
A ~ alaSlbAA
B ~ blbSlaBB
Solution : To draw a tree we will first try to obtain derivation for the string aabbabba
S
aB S ~ aB
I
a aBB I 5 ~ aBB
aaChSJ B B ~ bS
aab IbAIB S ~ bA
aabb ~B A ~a
aabba I bS I B ~ bS
aabbab [hAJ S ~ bA
aabbabb @] A ~ a
4 -27 Context Free Grammar
a
/1B
a
/1'"
B B
b
/1s ~S
b
I"" b~A
b A
I
I
a
a
_...-.....u,e S is the only one interior which gives the string a which can be
eriving S ~ al, Q 2 ' Q 3' . . , . an ' Then there exists a derivation tree which
and gives the string a.
....._a. step: We assume, that the derivation will enable us to draw the tree
'or nodes. We have to prove, that it is possible to draw the derivation
.or nodes which gives the string a. In the derivation tree not all the
es or interior nodes. Some are leaves where as others are interior nodes
them S is a parent node.
Theory of Computation 4-28 / " Context Free Grammal
,,\,,
@ a2
S~XYX S~XYX
S ~aYX S ~XYa
S ~abX S ~Xba
S ~aba S ~aba
Note that we have replaced first X from left to right in leftmost derivation and
XYX the last X i.e. the rightmost symbol.
Actually, we may use leftmost derivation or rightmost derivation we get the samE
string. The type of derivation does not affect on getting of a string.
Let us solve few problems based on this topic.
4-29 ~ Context Free Grammar
_ _. . 4.28 : Derive the string "aabbabba" for leftmost derivation and rightmost
rotg a CFG given by,
S -7 aB IbA
A -7 alaSlbAA
B -7 bibS IaBB
_ see the leftmost derivation first,
'3 S -7 aB
BB B -7 aBB
B B-7b
. bS B -7 bS
baB S -7 aB
bobS B -7 bS
babbA S -7 bA
• babba A -7 a
..._~:::.. _ ~ow let us solve using rightmost derivation.
'3 S -7 aB
BB B -7 aBB
BbS B -7bS
BbbA S -7 bA
Bbba A -7n
- bSbba B -7 bS
bbAbbn S -7 bA
bbabba A -7 a
.~PIe 4.29 : Derive the string 1000111 for leftmost and rightmost derivation using
G = (V, T, P, S) where
V = {S, T}
- Theory of Computation 4-30 / Context Free Grammar
T = {O, 1}
p = {S~TOOT
T ~ OTI1TIE
Solution : The leftmost derivation can be
5
TOOT 5 ~ TOOT
1TOOT T ~1T
10TOOT T ~OT
10 E OOT T~E
1000T
10001T T ~1T
100011T T ~1T
1000111T T~1T
1000111 E T~E
1000111
Right most derivation -
5
TOOT
T001T T~1T
TOO11T T~1T
T00111T T ~1T
TOOll1 E T~E
TOO111
1T00111 T~1T
10T00111 T ~OT
10 E 00111 T~E
1000111
4 - 31 Context Free Grammar
exists more than one parse trees for a given grammar, that means
~ r e than one leftmost or rightmost derivation possible and then that
~ be ambiguous grammar.
T = {id}
P = {E ---7 E + E
E---7E*E
E ---7 id }
5 = {E}
.rtring is id * id + id then we can draw the two different parse trees
·d + id.
E E
/I~E /I~E
= I +
Or
E * /I~
id I
id
E + E
I
id
I
id
id
I,. Example 4.30: Check whether the given grammar is ambiguous or not.
s ~ iC t S
S ~ iC t S e S
j.
S ~ a
C ~ b
Solution : To check whether given grammar is ambiguous or not we will have som
string for derivation tree such as ibtibtibtaea.
Let us draw the derivation tree.
ibtibtibtaea
b a
or
s
/I~
c
I
b
b a a
• ere V = {5, A}
L = {a, b} : (
R = {5~AA
A~AAA
A~· bA
A ~Ab}
.
is is an ambiguous grammar
..........""'"'"u,er a string booboo.
/
II 11
II
(b)
(a)
(c)
Thus there are more than one parse tree getting generated. Hence the grammar is
ambiguous.
I Reduced grammar
I
I I
Removal of Elimination of Removal of
useless symbols E production unit production
S ~ app
and app ~ w
Then p is said to be useful symbol.
4·35 Context Free Grammar
T ~ 00 rule 2
above CFG, the non terminals are S, T and X.
e string we have to start from start symbol S.
S ~OT
T ~OO
e V = IS, A, B} T = to, 1}
P = {S ~ A 11 BillA
S ~ IBI 11
useless symbol and can remove the rules associated with it. Hence after removal of useles:
symbols we get,
S~l1All1
S ~ AB I CA B ~ BC I AB
A ~ a C ~ aB Ib
Solution : Consider, the rule
5 ~ AB 1
5 ~ CA 2
In the rule 2, C and A can be replaced by some terminating string but in rule I, I
cannot be replaced by terminating string. It tends to form a never ending loop.
e.g.: 5 ~ AB ~ nAB ~ nnAB ~ nnnAB and so OU.
Thus we corne to know as B is an useless symbol. Removing B the rules are now,
5 ~ CA
A ~ n
C ~ b
G = (V,1:, R,S)
where V ·= {S, X, YJ
1: = {O, 1}
R = {S ~ XY I0
X ~1J
Remove the useless symbols from it.
Solution : As
5 ~ Xy
5 ~ 0
x ~ 1
4 -37 , , - Context Free Grammar
X~l
s ~ aA bB
A~aAla
B ~ bB
D ~ a~ I Ea
E ~ aC Id
first find all the productions which are giving terminal symbols.
A ~ a
D ~ ab
E~d
_ _ _ _ .=. start symbol
5 ~ aA I bB
~ bB we will be continuously in a loop with B productions. Thus B
. .~;::. c:'Ir.-'l,:-.nl. 50 we will eliminate it. Then the rules are
S -) aA
A -) alaA
D -) ablEa
(t;~~~I~)
_ ook at E productions there is a production E ~ aC. But there is no
e will remove the production E ~ aC.
/'S'::'~'A""'\
\..A -) aAla •..i
i3'~'i~i6iEa
E -)
- 5 and A indicates that there is no D and E in the derivation. Again
as useless symbols. 50 we will eliminate D and E. Finally after
- ess symbols the production rules are,
5 ~ aA
A~aAla
Theory of Computation 4 - 38 Context Free Gram.,
))11+ Example 4.36: Eliminate the useless symbols from the following grammar.
S ~ as I A I C
B ~ aa
C ~ aCb
Solution : We will first write all the productions that are giving terminal symbols.
A ~ a
B ~ aa
But if we look at start symbol S then,
S~aSIAIC
There is no production for B from start symbol. Thus B becomes a useless symbc
Similarly,
S ~ C ~ aCb ~ aaCbb ~ aaaCbbb ~ ...
There is no terminating symbol for C. Therefore we will eliminate C. Then set
rules are
S~aSIA
A ~ a
Thus we obtain the grammar having these two rules after removal of usele
symbols B, C.
)1. Example 4.37: Eliminate the useless symbols from following grammar.
S ~ aA I a I Bb I cC
A ~ aB
B~alAa
C ~ cCD
D ~ ddd
Solution : Consider all the productions that are giving terminal symbols.
S ~ a
B~a
D ~ ddd
4·39 ~ Context Free Grammar
S -7 cC
C -7 cCO
o -7 ddd.
derive the string using production rule for C we get,
- ccCO -7 ccCddd -7 cccCOddd -7 cccCdddddd -7 ...
any terminal symbol for C. Thus we get a useless symbol C. To
JT rule available is by using C. But as C gets eliminated, there is no
_ Hence 0 also will be removed.
S -7 aA Ia I Bb
A -7 aB
B -7 a Aa
S -7 05 1151£
ove £ production. But we have to take a care of meaning of CFG.
rG should not get changed if we place 5 -7 £ in other rules we get
Sand 5 -7 £
when 5 -7 1 5 and 5 -7 £
5 -7 05 115 I0 11
~X::::lC::lon is removed.
Theory of Computation 4-40 Context Free Gram
Th
Problems on Removal of £ Production
S -7 XYX
X -7 OXI£
Y -7 1 ~ 1£
Solution: Now, while removing production we are deleting the rules X -7 £ and Y-
£
To preserve the meaning of CFG we are actually placing E at right hand side whereev
and Y have appeared.
Let us take
S -7 XYX
if first X at right hand-side is £.
Then S -7 YX
Similarly if last X in R.H.S. = £.
Then S -7 XY
If Y=£ then
S -7 XX
S -7 X also
S -7 Y when both X are replaced by £
S -7 XYIYXIXXIXIY
Now let us consider
X -7 OX
If we place £ at right hand side for X then,
X-70
X -7 OXIO
Similarly Y-71Yll
S -7 XYIYXIXXIXIY
4·41 ~ Context Free Grammar
~ OXIO
Y ~ lYll
S ~ aSa
S ~ bSb
S~E
S ~ aSa ifS=E
S ~ aa
S ~ b Sb ifS=E
S ~ bb
S ~ aSalbSblaalbb
pie 4.40: Eliminate the E productions from the CFG given below.
A~OBII1Bl
B~OBI1BIE
_.......... : Now the E production is B ~ E we will delete this production. And then add
'uctions having B replaced by f-
A ~ 0B 1
B = E
A ~ 01
':rmilarly A~IBl~l1
o.
0
A ~ 0 B 111 B 11 01 111
imilarly, B ~ 0 B
if B = E
B ~O
Theory of Computation 4 -42 Context Free Gram"1
Th
as well as B -7 1
A -7 0 B 111 B 11 01111
B -7 0 Bl1 BIOI1
I'", Example 4.41 : Construct CFG without E production from the one which is given bel
5 -7 a IAb IaBa
A -7 blE
B -7 blA
Solution : If you observe carefully, then not only A have E production but even B
indicates E production i.e. A -7 E straight forward but B -7 A -7 E. Let us apply the meth
of replacement.
5 -7 A b
if A =E then
5 -7b
if B =E
5 -7 aa
5 -7 alA blbla ala B a
A -7b
B -7b
B-7b
/
4 - 43 Context Free Grammar
-7 BW
Z -7 AB
-7Z
_ -7 aA IbAIE
~ -7 Ba IBblE
• le only A and B shows £ productions.
A production then
-7 a Ib
D-7a lb
Z -7 A B Putting A = £, B= E
ars we place E
-7b
-7b
TI
S ~ XY
X~b
Y~b
which ultimately
S ~ bb
P~ROIIRIRRIRQP
Q~QOIPQIE
R~OPIQQQ
Solution : As we can see that Q ~ E.
Hence we will replace Q and R by E and accordingly add the production rules.
Similarly P ~ RR ~E
Y~Z Z~X
5 -7 OA 11BIC
A -7 05100
B-71IA
C-701
B -7 11051 00
; w e can write CFG without unit production as
5 -70A11B101
A -7 05100
B -7 11 051 00
C -7 01
:s;z::;ple 4.45: Optimize the eFG given below by reducing the grammar.
symbol.
5 -7 AIOC1
A -7 B\Ol\lO
C -7 £\CD
-7 A -7 B is a unit production
_ £ is a null production.
_ CD B and D are useless symbol.
Theory of Computation 4-46 \t '" Context Free Gramr
n,. Example 4.46: Eliminate the unit productions from following grammar
S -7 AB
B -7 Clb
C-7D
D -7 ElbC
E -7 dlAb
Solution : As,
S -7 AB and
A -7 a
There is only one rule with A and that too giving terminal symbol. Hence there il
no question of getting unit production with A. Now consider
n 4 -47 / Context Free Grammar
D ~ d I Ab I bC
C ~ D we can write
C ~ dlAblbC
B ~ dlAblbClb
~1tT\IT\ ar after removing unit productions
5 ~ AB
A ~ a
B ~ dlAblbClb
C ~ dlAblbC
D ~ dlAblbC
E ~ dlAb.
no path for D and E. From start state we can remove them by
ess symbols. The optimized grammar 'will be
5 ~ AB
B ~ dlAblbCjb
C ~ dlAblbC
~ab5b
_ - aAb
- ---. bS
- - aAAb