Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

4

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

4.2 Regular Grammar


A regular gr;unm;u- is defined is

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.

" .2.1 Construction of Regular Grammar from Regular Expre•• lon


We Call convert the reguI;u expression inlo its equivalent regular grarnnur by
using fonowing method.
1. Construct ~ NFA with t from given regular expression.
2. E'liminiIle t Innsitions and C(lIwert it to equiv~lent DFA.
3. From constructed DFA. the collespooding stares bewDoe non terminal
symbols and transitiON tNde are equivaJent to production rules.

!..We will understand. thi5 method with th,oo, tu.lp of,..,...., ~ples.

EDmp6e 4..1 : ClltlSt7lld 4

Solution: We will first ronstruct II DFA in


rtguln rnmrmu for lhe rrglilar LtpilSSiml a"b fa + Ill'.
;I slru&htforw.ard IlUN'\CI' for given rqular
e"pi . ' ",,~
-3
TMory of Compcltalion

... '-'
,-
'-'
,

,
'-'
(a 'b)"
~
Context Free Grammar

Now we wiD amvert it to NFA with [ tfansitiOf\$.


,
... •
p, • ,
••
r:.
'-'
,
~
Now elimiruile E moves. """

,
We can write the regular grammar as
G " (V. T. p. A DI where
v • lA o. Al l
T • 13, bl
P • IA g -t .. A g
A . --t bAI
A . --t b
A I --t .. A I
A, -tbA I
A ] -t ..

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

'Ihi$ FA can be c:oosttudo:d u.sing Ie tu.nsitioru u


,..... )

, n , r,;- . r,;- • ~
~
"

n•
~
, , r,;- . r,;- • ~
"
• , •
...
U we elin\iIute t transitions then the NFA will be -

• •

The transition table can be -

~I
S ....
• •
" {Qo. ql' qJ

"• • •
...

• • •
,., CofttUt FI"M Grammar

We an convm this NFA to equivaJent DFA as·


6'('10''') .. 1'10' q,. '1,1 ,
.. 1'10' 'Il' 'It! -t new mte genented
~. (qa. 1:0) - Ii ('10 . 1:0)
.. ['I, J
We will find input trans.iticm.s on ('1 0' 'Il' 'I, J
6' (I'1o.q,.q,La) -li(quo.)v! (q, ••)vli(q, •• )
.. 1'10' 'I,. 'I,) u 1'1,1 u.
'" 1'10' 'I" qt. qd
"" ('10''1,.'1,.'1,] ..... new state
li'(!qo. 'I"q,Lb) • li (qo. b)vli ('b.b) v 6(q"b)
- 1'1 ,1 v 0 u 1'1 .1
- 1'10. 1 1.11

: . li'(tqo.'I,.q •• 'I, La) c Ii (qo••l v Ii (lIb ••' v Ii ('I" .) u Ii ('If' ill


- 1'1 0. '1, . '1,1 u ['1,1 vo uo
- 1'10, '1).'1,,'1,1
- Iqo, q"q.,q,]
li' {I'Io.q, ,'h.'I,I. b) .li ('10' b) u Ii ('I,. b) u Ii ('I , . bJ v Ii ('I" bJ
'" Iq,lu.v[qolu.
- !qu.,!,1
.. ('1 0' '1''
6'(['10' '1.1. -)'' li(qu.·luli('l,.a)
- 1'1 0.'1,.'1,1 u+
.. Iqa.'1,.q.!
li'O'lo,q,Lb) .. HIo.b)vli(q, . b)
.. Iq,iv O
= ('I,)
Theory of Computation '·6 Context F.... Gnarnrn.ar

The tru,::.ition ubJoe for thi, OFA will be- •

• •
[.,

, •
CQo , IQ,q"q,/

{Qo, <!J. q" q,l

'"

• •

.-.. -~"---;:---'
.'
.,
', __ • 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 DFA with renamed states will be .

~
. ~.~.
," ", •
,
The regular grammar em be
54 ...

5~ bD

54b

A ~ aB

B~ aB

B4.
B4 be
B4b
C4 ...
C 4.
C~ bD
C4b

4~2 Right-linnr and Left-llnear Graf!1mar


U the non terminal symbol appears 1$ iI rightmost symbol in each prodlKtion of
regWu grammar then it is called right linear gr.unnuor. The right linear grammar is of
following fonn
A -+ as
Theory of Computation .-.
A ~,

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.

- -The I&npagt is ailed ~Iar If il is

'.3 Equlval.nce between Regular Grammar and FA


~ted by tither Itft linen or righilinur

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.

ProbI.ms based on FA to regular grammar



TNotJ of Compubdlod ,.,
Solution : 1he pmtrW' eqwvalent to given OFA will be-
G '" (V, T, P,S)

V '" (Ao, AI '


T _ 1;0. b,

Ao --t bAI AI --t bAllb

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

The regu1u graznmu can be


A, --t bA J

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

4,3,1 Construction of FA from Regular Grammar


Let G .. (y, 1:, P, A o) be. regu1.u grammar. We can construct DfA M whose
i) Stalt$ CVIitspOfld to varUblcs.
ill InitiaJ :;tale COli cspollld to A ~
ill) Transitions in M Wiiespond to productions in P.
U there is iI production of the fol'fl'\ A. -+ ii, the corresponding trmsition
termiNtes al a new state. ThiJ is the unique fina] state. Thus the DFA M can be
of Computation 4 -11 Context Free Grammar

~ a A j induces a transition from q i to q j with label a.


Each production A k ~ a induces a transition from q k to q f with label a.
~:>tnlr .. L(G) can be given by corresponding FA M.

s based on regular grammar to FA

Construct a finite automata recognizing L(G) where G is the grammar

--- 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

: The DFA is denoted by,


M = ({ q 0 ' q 1 , q 2 t q 3 }, {a, b}, 0, q 0 ' q 3 )
Theory of Computation " -12 Context Free

Start

Now this NFA can be converted to equivalent DFA


0' ([qo), a) = (qo, qd = [qo, qd
D'(fqo), b) = (qoJ = [qo)
O'([qIl, a) = </>
0' (fqIl, b) = (q 2J = [q2)
0'([q2)' a) = (q 3J = [q3)
0' (fq 2), b) = </>
o'([qo, qIl, a) = o([qo], a) U o ([qIl, a)
= (qo, qd
= [qo, ql 1.
o'(fqo, qIl, b) = 0(fqo), b) U o (fqIl, b)
= Iqo, q 2J
o'(fqo, q2), b) = o([qo), a) U o (fqd, a)

= [qo, qIl U [q3]


= [qO,ql,q3)
0' (fq 0' q 2], b) = 0 (q 0 , b) U 0 (q 2 ' b)

= (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

.. lq" qll"' iqa,qd


S·Oqo.q,.qlLb) .. S lqo.blu!i(q, .b)u!i(qJ.b)
.. Iqa,q,lv Iq2l v~

= 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

'" Iq. ,ql.q l l


= [q. ,ql , ql]

S'(lqo. q"q 1Lb) • S(lq . l,b) v,s(l'hLb) vS(lq1L b)


.. Iq. 1 v IqlJ v9
.. i'b,ql!
.. [q, .qd
The transition tilble will be •

• b

fi<ol No. ca.) 1'100 q,]

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

4.4 Context Free Grammar


The context free grammar can be formally defined as a set denoted by
G = (V, T, P, S) where V and T are set of non terminals and terminals respectively. P is
set of production rules, where each production rule is in the form of
non terminal -7 non terminals

or non terminal -7 terminals


S is a start symbol.
For example,
P = {S-7S+S

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

NOUN -7 Rama / Seeta / Gopal

VERB -7 goes / writes / sings


Thus if want to derive a string "Rama sings" then we can follow the above rules.

4.5 Derivation and Languages


The production rules are used to derive certain strings. We will now formally
define the language generated by grammar G = (V, T, P, S). The generation of
language using specific rules is called derivation.
Definition :

Let G = (V, T, P, S) be the context free graminar. If A -7 P is a production of P


and a and y are any strings from non terminals or terminals i.e. in (VUT)* then
aAY-7apy.
of Computation 4·15 /" Context Free Grammar

Suppose al,a 2 ,a 3 , .. .. a m are strings in (VUT)*, m ~ 1.


al ~ a2

Then we can say that a1 ~ am


G

The language generated by G is denoted by L(G). The language L is called context


language CFL if L(G) is for CFG.
For example
G = ({5, B) {a, b}, (5 ~ a B b, 5 )
B ~ bbb}
is a grammar. Then we can derive a string abbbb as -
i) We will first start from start symbol S
5~ aBb
ii) Then we will replace B by bbb
5 ~ aBb ~ a bbbb
Thus we can obtain the desired language L by certain rules. The language L can
described as a language which starts with letter a and having 4 b's following. Let
solve some examples for derivation of CFG.

Problems on context free grammar

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

Solution: Since S -7 a S b I ab is a rule. I indicates the 'or' operator.


S-7aSb
If this rule can be recursively applied then,
5
aSb
1
aaSbb
1
aaaSbbb
and if finally we can put 5 -7 ab then it becomes aaaa bbbb. Thus we can have any
number of a's first then equal number of b's following it. Hence we can guess the
language as {L=a n b l1 where n 2: 1}. The only way to recognize the language is to
try out various strings from the given production rules. Simply by observing the
derived strings, one can find out the language getting generated from given CFG.
1II. Example 4.10: Construct the CFG for the regular expression (Q +1) *.

Solution : The CFG can be given by,


P = {S -7 OS I 15
5 -7 £
YUllDVuWltion 4-17 /' Context Free Grammar

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

The string abaaba can be derived as


5
a5a ~baab~

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

. d the production rules as


5 ~ ATA

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

G = ({5, A}, {a, b}, P, 5)


.:..erive the string

Derivation Input Productions

as lIabab S -t a S
a (j S II II b a b S -t a S

aab A Illl¥>ab S-tbA

aabaS lIa¥> (lb A-t a S

aabab 1l(l¥>Il¥> S-tb

&.: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

Construct CFC for the language containing atleast one occurrence of

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

Example 4.19: Construct the CFG for the language L = a n b 2n where n ~ 1.

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;>

where S is a start symbol.

".. 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

&::IftI:DIe 4.22: Build a eFG for generating the integers.

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

Build a eFG for the language L = {Oi I i 2k I j> i+ k}.

As the language L = Oi 1 i 2k such that j > i + k. .


w e can rewrite L as
i i k P k
L = 0 1 1 1 2

I
This will be
greater than
i+k

e can again rewrite L for simplification as -

L= Oi
Theory of Computation 4-24 /" Context Free Grammar

A ~ 0 A 11 £

B~lBI1

C~.lC21£

Hence the CFG can be


5 ~ ABC
A~OA11£

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 £

))... Example 4.25: Obtain CFGfor the language L = {Oi 1i 2k I j:5k}


Solution : Here j :s; k. That means the language L has two variations.
Ll = Oi 1i 2i

. 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£

4.6 Derivation Trees '


Derivation trees is a graphical representation for the derivation of the given
production rules for a given CFG. It is the simple way to show how the derivation
4 -25 Context Free Grammar

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.

is a production rule. The 5 is a start symbol.

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

p between Derivation and Derivation Tree


_ _ : Let G = (V, T, P, S) be a context free grammar. Then S,; a if and only
ation tree in grammar G which gives the string a.

is a non terminal S in V then S ~ {l if and only if there is as-tree


string Q. We can easily prove this using induction' theorem. We can
, ....1C!l1Oll on number of interior nodes.

_...-.....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

Thus this derivation tree will ultimately give the string a.

4.7.1 Leftmost Derivation and Rightmost Derivation


As we know, derivation for any string means replacement of non terminal by it~
appropriate definition. There may be a situation, in which there are many nOil
terminals. Then which non terminal should be replaced by its definition is sometime~
confusing to decide.
Hence we normally apply two methods of deriving. The leftmost derivation is a
derivation in which the leftmost non terminal is replaced first from the sentential
form.
The rightmost derivation is a derivation in which rightmost non terminal U
replaced first from the sentential form.
Let us see how it works.
For example

S~XYX S~XYX

S ~aYX S ~XYa

S ~abX S ~Xba

S ~aba S ~aba

Leftmost derivation Rightmost derivation

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

leftmost and rightmost derivations

_ _. . 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

~-===:.ar can be derived in either leftmost derivation or rightmost derivation.


iii derivation tree called as parse tree or syntax tree based on these
~ arse tree has to be unique even though the derivation is leftmost or

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.

_~~~ - the CFG given by G = (V, T, P, 5)


V = {E}

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

e grammar is an ambiguous grammar. Let us solve some exercise on


Theory of Computation 4 -32 Context Free Grammal

Problems on ambjguity of grammar

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

Fig. 4.2 (a) For Example 4.30

or
s
/I~
c

I
b

b a a

Fig. 4.2 (b) For Example 4.30


Thus we have got more than two parse trees. Hence the given grammar
ambiguous.
utation 4-33 /" Context Free Grammar

!SI~~ 4.31: Consider the grammar G = ( V , L, R, 5).

• 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)

Fig. 4.3 For Example 4.31


Theory of Computation Context Free Grammar

Thus there are more than one parse tree getting generated. Hence the grammar is
ambiguous.

4.9 Simplification of CFG


As we have seen various languages can effectively be represented by context tree
grammar. All the grammars are not always optimized. That means grammar may
consists of some extra symbols (non terminals). Having extra symbols unnecessary
increases the length of grammar. Simplification of grammar means reduction of
grammar by removing useless symbols. The properties of reduced grammar are given
below.
1. Each variable (i.e. non terminal) and each terminal of G appears in the
derivation of some word in L.
2. There should not be any production as X ~ Y where X and Yare non
terminals.
3. If E is not in the language L then there need not be the production X ~ E.

We see the reduction of grammar as below.

I Reduced grammar
I
I I
Removal of Elimination of Removal of
useless symbols E production unit production

Let us study the reduction process in detail.

4.9.1 Removal of Useless Symbols


Any symbol is useful when it appears .on the right hand side, in the production
rule and generates some terminal string. If no such derivation exists then it is
supposed to be an useless symbol.
A symbol P is useful if there exists some derivation in the following form.

S ~ app

and app ~ w
Then p is said to be useful symbol.
4·35 Context Free Grammar

13 may be some terminal or non terminal symbol and will help us to


~~ .suing w in combination with P. Let us see what exactly means the
;ith some example.
G = (V, T, P, S) where V = IS, T, X}, T = to, 1}
S ~ OT 11T I X I 0 11 rule 1

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

reach to certain string after following these rules.


en there is no further rule as a definition to X. That means there is
-oooie S ~ X. Hence we can declare that X is a useless symbol. And we
- after removal of this useless symbol CFG becomes
where V = IS, T}
P = {S ~ OTl1TI011
T ~ OO}
01.

moval of useless symbol

e V = IS, A, B} T = to, 1}
P = {S ~ A 11 BillA

S ~ IBI 11

B ~ BB} For removing useless symbols


the given CFG if we try to derive any string A gives some terminal
does not give any terminal string. By following the rules with B we
number of B's and no significant string. Hence we can declare B as
Theory of Computation 4 - 36 Context Free Grammal

useless symbol and can remove the rules associated with it. Hence after removal of useles:
symbols we get,
S~l1All1

lI'. Example 4.33: Find CFG with no useless symbols equivalent to

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

))J. Example 4.34: Consider the following CFG

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

recognize that there is no derivation for Y. Hence we can eliminate


cith Y, and the rules are
5 ~ 0

X~l

Remove useless symbols from the following grammar.

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

_ _ _160111 of £ Productions from Grammar


in finite automata and regular expression that £ or a null string
nth no value. We have also seen that even though some NFA
' e can convert that NFA without £ moves. Even in context free
there is £ production we can remove it, without changing the
g4i!:m.IIl.Crr. Thus £ productions are not necessary in a grammar.

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

II,. Example 4.38: Remove the


it.
£ production from following CFG by preserving meanin~

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

If Y and X are £ then,

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

Collectively we can rewrite the CFG with removed £ productions as

S -7 XYIYXIXXIXIY
4·41 ~ Context Free Grammar

~ OXIO

Y ~ lYll

: For the CFG given below remove the E production

S ~ aSa
S ~ bSb

S~E

aing to the replacement procedure, we will pla~e E at S .in the sentential

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

B -70B 11B 1011


Collectively, we can write

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

Finally the rules are

5 -7 alA blbla ala B a


A -7 b

B-7b
/
4 - 43 Context Free Grammar

: Consider the CFG given below.

-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

we can straightaway delete it.

-7b

- rewrite the rules


- -7 XY
'-7 b
-7b
• -7 a Alb Alalb
B -7 B alB blalb
_ .... symbol and it defines XY only whereas X and Y yields b as a
.-r:cod:L - ere is no chance for producing rules by A or B. Hence A and B
p::::bafs- and we can remove it. Finally rules are
./
Theory of Computation 4-44 Context Free Gram

TI
S ~ XY
X~b

Y~b

which ultimately

S ~ bb

lIJ. Example 4.43: Consider the CFe given below,

For eliminating E productions


S~POQIQQIORIRQP

P~ROIIRIRRIRQP

Q~QOIPQIE

R~OPIQQQ
Solution : As we can see that Q ~ E.

Similarly R ~QQQ ~E.

Hence we will replace Q and R by E and accordingly add the production rules.
Similarly P ~ RR ~E

Let us modify the rules if P or Q or R becomes E.

S ~ PO QIOIO RIQ QIR Q P


P ~ R 0 I 0 1111 R I RR I RQP
Q ~ Q OIOIPQ
R ~ 0 PIOIQ Q Q
Note that with P, Q, R other meanings are assoCiated, they are not purely giving
to us. Therefore we cannot remove P, Q and R symbols.

4.9.3 Removing Unit Productions


The unit productions are the productions in which one non terminal gives anothe
non terminal.
For example if
X~Y

Y~Z Z~X

Then X, Yand Z are unit productions. To optimize the grammar we need


remove the unit productions.
4 - 45 Context Free Grammar

..: a unit production and B -7 Xl X2 X3 ..... Xn then while removing


-.=::C:::;':;::;!l:l we should add a rule A -7 Xl X2 X3 ... . Xn .

: If the eFG is as below.

5 -7 OA 11BIC

A -7 05100

B-71IA

C-701

;...w....._~" the unit productions.


,~~~~~ S C is a unit production. But while removing S
-7 -7 C we have to
gives. So, we can add a rule to S.
5 -7 OA 11B I 01

"'~_.-:~ - -7 A is also a unit production so we can modify it as

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

Reducing a grammar we have to avoid all the above conditions.


Let S -7 A
i.e. A -7 B is a useless symbol because B is not defined further more.
S -7 01110
i.e. S -7 011101 OC1
But C -7 e
Hence ultimately S -7 01110
A -7 B but we can remove this production since B is a useless symbol.
Hence A -7 01110
But the start -symbol S -7 01110
There is no A in the derivation of A so by considering A also as ~ useless symb
we get -final CFG as
S -7 01110

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

t B, C and D are unit productions. As E ~ d IAb, we will replace

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

text free grammar to check well formedness of parenthesis.


guage generated by following grammar
511 OA.l
A 115 I 0 I1
-lA I 15 I 1
- the eFe for set of all strings over fa, b) consisting of equal number of a's and b's.
the following grammar is ambiguous.

~ab5b
_ - aAb
- ---. bS
- - aAAb

You might also like