Vous êtes sur la page 1sur 34

AUTOMATA LANGUAGE AND COMPUTATION

UNIT-II
Regular Sets and Context Free Grammars: Properties of regular sets, Context-Free Grammars-
derivation trees, Chomsky Normal Forms and Greia!h Normal Forms, amiguous and
unamiguous grammars" minimi#ation of finite automata$
1. REGULAR SETS
Regular languages !an also e defined, from the empty set and from some finite numer of
singleton sets, y the operations of union, !omposition, and %leene !losure$ Spe!ifi!ally, !onsider
any alphaet$ &hen a regular set over is defined in the follo'ing 'ay$
&he empty set (, the set )* !ontaining only the empty string, and the set )a* for ea!h
symol a in +, are regular sets$
,f -. and -/ are regular sets, then so are the union -. -/, the !omposition -.-/, and the
%leene !losure -.0$
No other set is regular$
1.1 The Pumping Lemma for Reguar Se!"
Applications:
o &he pumping lemma is a po'erful tool for providing and proving !ertain
language is regular or not$
o ,t is also useful in the development of algorithms to ans'er !ertain 1uestions
!on!erning finite automata, su!h as 'hether the language a!!epted y a given F2
is finite or infinite$
Statement:
-et 3-4 e a regular set$ &hen there is a !onstant n su!h that if # is any 'ord in -, and 5#5
6n$ 7e may 'rite #8uv' in su!h a 'ay that 5uv59n, 5v56. and for all i6:, then uv
i
' is in -$
Proof:
,f a language is a!!epted y a ;F2 <8 =>, +, ?, 1:, F@ then it is regular 'ith some
parti!ular numer of states, say n$ Consider a input of n or more symols a.a/A$$am, m6n and for
i8.,/,A$$,m$
-et ? =1:, a.a/A$$ai@ 8 1i
,t is not possile for ea!h of the nB. states 1:,1.,AA1n to e distin!t, sin!e there are only
different states$ &his there are t'o integers C and k, :9CDk9n, su!h that 1C81k$ &he path laeled
a.a/A$am in the transition diagram of <$ Sin!e CDk, the string aCB.$$$$ak is of length .$

aCB.AA$ak
a.AA$aC akB.AAAam
,f 1m is in F, that is a.a/AA$$am is in -=n@, then a.a/AA$$aCakB.akB/AAam is also in -=n@$
?=1:,a.AA$$aCakB.AAam@8 ?=?=1:,a.AA$$aC@ akB.AAam@
8?=1C, akB.AAam@ =sin!e ?=1:,a.AA$$aC@8 1C@
8 ?=1k, akB.AAam@ =sin!e 1C8 1k@
81m
'hi!h is the final state of the automata to e a!!epted$
Een!e proved$
1.# Proper!ie" of Reguar Se!"
Proper!$ 1% The reguar "e!" are &o"e' un'er union( &on&a!ena!ion( an' )eene &o"ure.
Proof: -et + e a finite set of symols and let -, -. and -/ e sets of strings from +
0
$ &he
!on!atenation of -. and -/, denoted -.-/, is the set )xy5 x is in -.and y is in -/*$ &hat is, the
strings in -.-/ are formed y !hoosing a string -. and follo'ing it y a string in -/, in all possile
!ominations$ ;efine -
:
8 )F* and -
i
8--
i-.
for i6.$ &he %leene !losure of - denoted -
0
, is the set
-
0
8Gi8:-
i
and the positive !losure of -, denoted -
B
, is the set
-
B
8Gi8.-
i
&hat is, -
0
denotes 'ords !onstru!ted y !on!atenating any numer of 'ords from -$ -
B
is the
same, ut the !ase of #ero 'ords, 'hose !on!atenation is defined to e F, is ex!luded$ Note that
-
B
!ontains F if and only if - does$
Proper!$ #% The &a"" of reguar "e!" i" &o"e' un'er &ompemen!a!ion. Tha! i"( if L i" a
reguar "e! an' L*+
,
( an' !hen +
,
-L i" a reguar "e!.
Proof: -et - e - =<@ for ;F2 <8 =>, +., ?, 1:, F@ and -H+
0
$ First, 'e may assume +.8+, for if
there are symols in +. not in +, 'e may delete all transitions of < on symols not in +$ &he fa!t
that -H+
0
assures us that 'e shall not therey !hange the language of <$ ,f there are symols in
+ not in +., then none of these symols appear in 'ords of -$ 7e may therefore introdu!e a
3dead state4 d into < 'ith ? =d, a@ 8 d for all a in + and ? =1, a@ 8 d for all 1 in > and a in +-+.$
No', to a!!ept +
0
--, !omplement the final states of <$ &hat is, let <
I
8 =>, +., ?, 1:, >-F@$ &hen
<
I
a!!epts a 'ord w if and only if ? =1:, '@ is in >-F, that is, w is in +
0
--$ Note that it is essential
to the proof that < is deterministi! and 'ithout F moves$
Proper!$ -% The reguar "e!" are &o"e' un'er in!er"e&!ion.
q
0
q
j
=q
k
q
m
q
m
Proof: -.J-/8 =-.
I
K -/
I
@
I
, 'here the overar denotes !omplementation 'ith respe!t to an
alphaet in!luding the alphaets of -. and -/$Closure under interse!tion then follo's from
!losure under union and !omplementation$
,t is 'orth noting that a dire!t !onstru!tion of a ;F2 for the interse!tion of t'o regular
sets exists$ &he !onstru!tion involves taking the Cartesian produ!t of states, and 'e sket!h the
!onstru!tion as follo's:
-et <.8 =>., +, ?., 1., F.@ and </8 =>/, +, ?/, 1/, F/@ e t'o deterministi! finite automata$
-et
<8 =>.0 >/, +, ?, L1., 1/M, F.0 F/@
'here for all p. in >., p/ in >/, and a in +,
? =Lp., p/M, a@ 8 L?. =p., a@, ?/ =p/, a@M
Proper!$ .% The &a"" of reguar "e!" i" &o"e' un'er "u/"!i!u!ion.
Proof: -et R H+
0
e a regular set and for ea!h a in + let RaHN
0
e a regular set$ -et f: +-ON
0
e the
sustitutions denoting R and Ra$ Repla!e ea!h o!!urren!e of the symol a in the regular
expression R y the regular expression for Ra$ &o prove that the resulting regular expression
denotes f=R@, oserve that the sustitution of a union, produ!t or !losure is the union, produ!t or
!losure of the sustitution$ L&hus for example, f =-. -/@ 8 f =-.@K f =-/@$M 2 simple indu!tion on
the numer of operators in the regular expression !ompletes the proof$
2 type of sustitution that is of spe!ial interest is the homomorphism$ 2 homomorphism
h is a sustitution su!h that h(a) !ontains a single string for ea!h a$ 7e generally take h(a) to e
the string itself, rather than the set !ontaining that string$ ,t is useful to define the inverse
homomorphic image of a language - to e
h
-.
=-@8)P5 h=x@ is in -*
7e also use, for string w"
h
-.
=w@8)P5 h=w@ 8 w*
Proper!$ 0% The &a"" of reguar "e!" i" &o"e' un'er homomorphi"m an' in1er"e
homomorphi"m.
Proof: Closure under homomorphism follo's immediately from !losure under sustitution, sin!e
every homomorphism is a sustitution, in 'hi!h h=a@ has one memer$
&o sho' !losure under inverse homomorphism, let <8 =>, +, ?, 1:, F@ e a ;F2
a!!epting -, and let h e a homomorphism from N to +
0
$ 7e !onstru!t a ;F2 <
I
that a!!epts
h
-.
=-@ y reading symol a in N and simulating < on h=a@$ Formally, let <
I
8 =>, +, ?
I
, 1:, F@ and
define ?
I
=1, a@ for 1 in > and a in N to e ?=1, h=a@@$ Note that h=a@ may e long string, or F, ut ?
is defined on all strings y extension$ ,t is easy to sho' y indu!tion on 5x5 that
?
I
=1:,x@8?=1,h=x@@$ &herefore <
I
a!!epts x if and only if < a!!epts h=x@$ &hat is,
-=<
I
@8 h
-.
=-=<@@$
Proper!$ 2% The &a"" of reguar "e!" i" &o"e' un'er 3uo!ien! 4i!h ar/i!rar$ "e!".
Proof: -et <8 =>, +, ?, 1:, F@ e a finite automaton a!!epting some regular set R, and let - e an
aritrary language$ &he 1uotient RQ- is a!!epted a finite automaton <
I
8 =>, +, ?, 1:, F
I
@, 'hi!h
ehaves like < ex!ept that the final states of <
I
are all states q of < su!h that ? =1:, xy@ is in F$
&hus <
I
a!!epts RQ-$
#. CONTE5T 6REE GRAMMARS
Context-Free Grammars are a most po'erful method of des!riing languages$ Su!h grammars
!an des!rie !ertain features that have a re!ursive stru!ture, 'hi!h makes them useful in a variety of
appli!ations$
2 grammar !onsists of a !olle!tion of sustitution rules, also !alled produ!tions$ Ra!h rule
appears as a line in the grammar, !omprising a symol and a string separated y an arro'$ &he symol is
!alled a variable$ &he string !onsists of variales and other symols !alled terminals$ &he variale
symols often are represented y !apital letters$ &he terminals are analogous to the input alphaet and
often are represented y lo'er!ase letters, numers, or spe!ial symols$ Sne variale is designed as the
start variable$ ,t usually o!!urs on the left-hand side of the topmost rule$ For example, grammar G.
!ontains three rules$ G.
I
s variales are 2 and T, 'here 2 is the start variale$ ,ts terminals are :, . and U$
Vou use a grammar to des!rie a language y generating ea!h string of that language in the
follo'ing manner$
.$ 7rite do'n the start variale$ ,t is the variale on the left-hand side of the top rule, unless
spe!ified other'ise$
/$ Find a variale that is 'ritten do'n and a rule that starts 'ith that variale$ Repla!e the 'ritten
do'n variale 'ith the right-hand side of that rule$
W$ Repeat step / until no variales remain$
For example, grammar G. generates the string :::U...$ &he se1uen!e of sustitutions to otain a
string is !alled a derivation$ 2 derivation of string :::U... in grammar G. is
28O:2.8O::2..8O:::2...8O:::T...8O:::U...
Vou may also represent the same information pi!torially 'ith a parse tree. 2n example of a parse tree is
sho'n elo'
2
2
2
2
T
: : : U . . .
Fig: Parse tree for :::U... in Grammar G.
2ll strings generated in this 'ay !onstitute the language of the grammar$ 7e 'rite - =G.@ for the
language of grammar G.$ Some experimentation 'ith the grammar G. sho's us that - =G.@ is
):
n
U.
n
5 n6:*$ 2ny language that !an e generated y some !ontext-free grammar is !alled a context-free
language =CF-@$
FSR<2- ;RF,N,&,SN SF 2 CSN&RX&-FRRR GR2<<2R
-. DERI7ATION TREES
&he derivations in CFG !an e represented using trees$ Su!h trees representing derivations are
!alled derivation trees$
2 derivation tree for a CFG G8 =Y, &, P, S@ is a tree satisfying the follo'ing !onditions:
=a@ Rvery vertex has a lael 'hi!h is a variale or terminal or F
=@ &he root has lael S$
=!@ &he lael of an internal vertex is a variale$
=d@ ,f the verti!es n.,n/AAnk 'ritten 'ith laels x.,x/AAxk are the sons of vertex n
'ith lael 2, then 2-O x.,x/AAxk is a produ!tion in P$
=e@ 2 vertex n is a leaf if its lael is aH+ or F$
For example:
-et G8 =)S, 2*, )a, *, P, S@ 'here P !onsists of S-Oa2SQaQSS
2-OS2Qa
&he derivation tree is given for the string aaaa as elo':
2 context-free grammar is a Z-tuple =Y, +, R, S@, 'here
.$ Y is a finite set !alled the variables,
/$ + is a finite set, disCoint from Y, !alled the terminals,
W$ R is a finite set of rules, 'ith ea!h rule eing a variale and a
string of variales and terminals, and
Z$ S F Y is the start variale$
a a


/ a a
#.1 LE6TMOST AND RIG8TMOST DERI7ATION
-et G e a grammar S-O:TQ.2
2-O:Q:SQ.22
T-O.Q.SQ:TT
For the string ::..:.:.
-eftmost derivation:
S-O:T
-O::TT
-O::.T
-O::..S
-O::..:T
-O::..:.S
-O::..:.:T
-O::..:.:.
Tree:
S
S S
A

S

S
0 B
B B 0
Rightmost derivation:
S-O:T
-O::TT
-O::T.
-O::.S.
-O::..2.
-O::..:S.
-O::..:.2.
-O::..:.:.
1 1
S
0 B
1 S
0 B
1

S
0 B
E9ampe% Consider the follo'ing produ!tion
S-OaTQ2
2-OaSQ22Qa
T-OSQaTTQ
for string aaaaa find -<; and R<;
-<;:
S-OaT
-OaaTT
-OaaaTTT
-OaaaTT
B B
1 1
S
0
0 B
1 S
0 B
1
-OaaaT
-OaaaaTT
-OaaaaT
-OaaaaS
-Oaaaa2
-Oaaaaa
R<;:
S-OaT
-OaaTT
-OaaTS
-OaaT2
-OaaTa
-OaaaTTa
-OaaaTa
-OaaaSa
-Oaaa2a
-Oaaaaa
.. C8OMS): NORMAL 6ORM ;CN6<
2 Context-Free Grammar [GI is in Chomsky Normal Form if every produ!tion is of the
form
2-Oa =Non &erminal-O&erminal@
2-OTC =Non &erminal-O Non &erminal0Non &erminal@
is in G$
For Rxample:
Consider 3G4 'hose produ!tions are S-O2T, 2-Oa, T-O$ &hen G is in Chomsky Normal
Form$
Re'u&!ion !o Chom"=$ Norma 6orm
&he steps involved in redu!tion of CFG to CNF are
Step 1: Rlimination of null produ!tions and unit produ!tions$
-et the resultant grammar e G8 =YN, +, P, S@
Step 2: Rlimination of terminals on RES$
7e define G8 =YN
I
, +, P, SI@ 'here P and YN
I
are !onstru!ted as follo's:
=i@2ll the produ!tions in P of the form 2-Oa or 2-OTC are in!luded in P.$ 2ll the
variales in YN are in!luded in YN
I
$
=ii@ Consider 2-OX.X/AA$Xn 'ith some terminal on RES of Xi is a terminal say ai, add
a ne' variale Cai to YN
I
and Ca-O\i to P.$ ,n produ!tion 2-OX.X/AAXn, every terminal on
RES is repla!ed y the !orresponding ne' variales on the RES are retained$ &he resulting
produ!tion is added to P.$ &hus 'e get G.8 =YN
I
, +, P., S@$
Step 3: Restri!ting the numer of variales on RES$
For any produ!tion in P., the RES !onsists of either a single terminal or t'o or more
variales$ 7e define G8 =YN
I
, +, P/, S@ as follo's:
=i@2ll produ!tions in P. are added to P/ if they are in the re1uired form$ 2ll the variales
in YN
II
$
=ii@ Consider 2-O2.!., C.-O2/C/, !m-/-O2m-.2m and ne' variales are !., !/, A$$, !m-/$
E9ampe 1%
6in' !he grammar in CN6 e3ui1aen! !o S->aA/?( A->aA@a( ?->/?@/
Step 1: 2s there are no unit produ!tions or null produ!tions, 'e need not !arry out step .$ 7e
pro!eed to step /$
Step 2: -et G.8 =YN
I
, )a, *, P, S@ 'here P. and YN
I
are !onstru!ted$
2dd produ!tions 2-Oa, T-O to P.$
Een!e S-Oa2T ] =.@
repla!ed as
a) S-OCa2CT
2dd ne' produ!tions Ca-Oa and C -O
@ 2-Oa2 e!omes
2-OCa2
!@ T-OT e!omes
T-OCT
Step 3:
a@ S-OCa2CT is !onverted as
S-OCa;. 'here
;.-O2CT
2gain !onverted as ;.-O2;/
'here ;/-OCT
@ 2-OCa2
&hey are already in CNF
!@ T-OCT
&hey are already in CNF
Een!e the resultant CNF are
E9ampe #%
Find the CNF e1uivalent to the grammar S-O]SQLSJSMQpQ1
'here ], L, J,M, p, 1 are terminals$
Step 1: Consider the Grammar 3G4$
S-O]SQLSJSMQpQ1
Sin!e there are no null produ!tions and unit produ!tions 'e !an go for redu!tion$
Step 2: Consider the produ!tion =.@,
S-O]S
2dd ne' produ!tion C.-O]
&he produ!tion S-O]S e!omes S-OC.S
Step 3: Consider the produ!tion =/@,
S-O LSJSM
2dd ne' produ!tions C/-O L
CW-OJ
CZ-OM
No' S-OC/SCWSCZ
S-O C/;.
'here ;.-O SCWSCZ
and ;/-O CWSCZ
S-OCa;.
;.-O2;/
;/-OCT
2-OCa2
T-OCT
2-Oa
T-O
So ;.-O S;/
2gain ;/-O CWSCZ
-et ;W-O SCZ
2nd hen!e ;/-O CW;W
&he resultant CNF produ!tions are
Een!e derived$
So1e' Pro/em"%
No!e% NT-Termina T-Termina
1. Con1er! !he foo4ing grammar !o Chom"=$ Norma 6orm;CN6<
G%
S->aAD
A->a?@/A?
?->/
D->'
Solution:
Consider S-Oa2;
&he produ!tion is of the form N&-O&0N&0N&
Repla!e a y Ca$ 7e get, S-OCa2;
-et ;.-O2;$ &hen the produ!tion e!omes S-OCa ;. 'hi!h is in CNF$
S-OC.S
C.-O]
S-O C/;.
;.-O S;/
;/-O CW;W
;W-O SCZ
S-Op
S-O1
No' !onsider 2-OaT
&his is of the form N&-O&0N&
Sin!e Ca-Oa, 2-OaT e!omes 2-O CaT 'hi!h is in CNF$
-et us no' !onsider 2-O2T$
&his produ!tion is of the form N&-O&0N&0N&
Repla!e y C and 2T y ;/$ 7e get, 2-O C ;/$
&he other t'o produ!tions T-O and ;-Od are already in CNF$
Een!e the resultant CNF produ!tions are
#. Con1er! !he foo4ing grammar !o Chom"=$ Norma 6orm ;CN6<
G%
S->a/S/@a@aA/
A->/S@aAA/
Sou!ion%
Consider S-OaS
&his produ!tion is of the form N&-O&0N&0&0N&
Repla!ing a y Ca and y C 'e get, S-OCaCSC
&his is of the form N&-ON&0N&0N&0N&$
S-OCa ;.
;.-O2;
2-O CaT
2-O C ;/$
;/$-O2T
T-O
;-Od
So 'e repla!e CSC y ;.$ Een!e the produ!tion e!omes, S-OCa ;. 'hi!h is of the CNF$
=Repla!e SC y ; to !onvert ;. into CNF$ So ;-O SC@
Sin!e S-Oa is already in CNF 'e no' !onsider S-Oa2T
&he aove produ!tion is of the form N&-O&0N&0&$ Repla!e a y Ca and y C$
So no', S-O Ca2C$ Repla!e 2C y ;/ to !onvert the produ!tion to CNF$
&he produ!tion no' e!omes S-OCa ;/$
-et us no' !onsider the produ!tion 2-OS$ Repla!ing y C the grammar gets !onverted
into CNF, 2-OCS$
&he last produ!tion to e !onverted to CNF is 2-Oa22$
Repla!ing Ca and y C 'e get 2-O Ca22C$ -et us no' !onsider ;W-O2;/ 'here
;/-O2C$ So the produ!tion e!omes 2-OCa ;W 'hi!h is in CNF$
Een!e the resultant CNF produ!tions are
-. Con1er! !he foo4ing grammar !o Chom"=$ Norma 6orm ;CN6<
G%
S->ASA@/A
A->?QS
?->C
Sou!ion%
;-O SC
;.-O C;
S-OCa ;.
;/-O2C
S-OCa ;/
2-O Ca;W
;W-O2;/
;/-O2C
S-Oa
Consider S-O2S2$
&he aove produ!tion is of the form N&-ON&0N&0N&
-et ;.-OS2$ Repla!ing S2 the produ!tion e!omes S-O2;. 'hi!h is in CNF$
No' !onsider S-O2$ Repla!e y C in order to !onvert the produ!tion to CNF$
S-O2 e!omes S-O C2$
Consider the produ!tions 2-OT, 2-OS and T-OC$ &hey !an also e 'ritten as 2-OFT, 2-OFS,
T-OFC$ ,f 'e assume Ca-OF then the produ!tions e!ome 2-O CaT, 2-O CaS, T-O CaC 'hi!h
are in CNF$
Een!e the resultant CNF produ!tions are
.. Con1er! !he foo4ing grammar !o Chom"=$ Norma 6orm ;CN6<
G%
S->1A@A?
A->1AA@AS@A
?->A??@1S@1
Sou!ion%
Consider S-O.2$ &he aove produ!tion is of the form N&-O&0N&$ ,n order to !onvert it to
CNF, 'e repla!e . y Ca$ Kpon repla!ing 'e get, S-OCa2 'hi!h is in CNF$
No' !onsider S-O:T$ &he aove produ!tion is of the form N&-O&0N&$ ,n order to !onvert it
to CNF, 'e repla!e : y C$ Kpon repla!ing 'e get, S-OCT 'hi!h is in CNF$
S-O2;.
;.-OS2
2-O CaT
S-O C 2
2-O CaS
T-O CaC
&he next produ!tion to e !onsidered is 2-O.22 'hi!h is of the form N&-O&0N&0N&$ -et us
assume that ;.-O22$ 2lso 'e kno' Ca-O.$ &he produ!tion e!omes 2-OCa;. 'hi!h is in
CNF$
&he next produ!tion 2-O:S is of the form N&-O&0N&$ 7e kno' C-O:$ Kpon repla!ing 'e
get, 2-OCS 'hi!h is in CNF$ &he other produ!tion 2-O: is already in CNF$
No' take into a!!ount the produ!tion T-O:TT 'hi!h is of the form N&-O&0N&0N&$ -et us
assume that ;/-OTT$ 2lso 'e kno' C-O:$ &he produ!tion e!omes T-OC;/ 'hi!h is in
CNF$
&he next produ!tion T-O.S is of the form N&-O&0N&$ 7e kno' Ca-O.$ Kpon repla!ing 'e
get, T-OCaS 'hi!h is in CNF$ &he other produ!tion T-O. is already in CNF$
Een!e the resultant CNF produ!tions are

0. GREI?AC8 NORMAL 6ORM ;GN6<
2 Context-Free Grammar [GI is in Greia!h Normal Form if every produ!tion is of the form
2-Oa\ 'here \ H YN
0
and a H +
=Non &erminal-O&erminal0any numer of Non &erminals@
2-Oa =Non &erminal-O&erminal@
For Rxample:
S-Oa2T
2-OC
T-O
C-O!
are in GNF$
S-OCa2
S-OCT
;.-O22
2-O Ca;.
2-O CS
2-O:
T-O CaS
T-O:
Grei/a&h Norma
6orm Agori!hm%
begin
for k:=1 to m do
begin
for j=1 to k-1 do
for each production of the form A
k
-
>A
j
do
begin
for all productions A
j
-> do
add production A
k
->;
remove production A
k
->A
j

end;
for each production of the form A
k
-
>A
k
do
begin
add production
k
-> and
k
-
>
k
;
remove production A
k
->A
k

end;
for each production A
k
->! "here
does not begin "ith A
k
do
Re'u&!ion !o Grei/a&h Norma 6orm
Lemma ;1<%
-et G8 =Y, +, P, S@ e a CFG$ -et 2-OT^ e an 2-produ!tion in P$ -et the T-produ!tion
e T-OT.5T/5A$$5Tn
Lemma ;#<%
-et G8 =Y, +, P, S@ e a CFG$ -et 2-OT^ e an 2-produ!tion e
2-O2\.5A$$ 2\^5 T.5A$$5Tn
then # e a ne' variale 'hen P. is defined as:
=i@&he set of 2-produ!tions in P. are
2-O_.5_/5A$5_n
2-O _.#5 _/#5AA$5 _n#
=ii@&he set of #-produ!tions in P. are
#-O\.5\/5A5\n
#-O\.#5\/#5A5\n#
E9ampe%
Constru!t R1uivalent GNF for the CFG S-O22Qa,2-OSSQ
Step 1: &he given grammar is in CNF$ S and 2 are renamed as 2. and 2/$
Een!e the produ!tions e!ome 2.-O 2/ 2/Qa, 2/-O 2. 2.Q
&here is no need for null produ!tion or unit produ!tion elimination e!ause the produ!tion is
already in CNF$
Step 2:&he 2. produ!tions are in the re1uired form$ &hey are 2.-O 2/ 2/Qa$
&he produ!tion 2/-O is also in the re1uired form 'hereas 'e have to apply lemma . to
!onvert the produ!tion 2/-O 2. 2. to the re1uired form$ 2pplying lemma . 'e get,
2/-O 2/ 2/ 2. 2/-O a2.
Step 3: 7e apply lemma / to 2/ produ!tions as 'e have 2/-O 2/ 2/ 2.
-et #/ e a ne' variale$ &he resulting produ!tions are
2/-O a2. 2/-O
2/-O a2.#/ 2/-O#/
#/ -O 2/ 2. #/ -O 2/ 2. #/
Step 4: =i@ &he 2/ produ!tions are 2/-O a2.Q Q a2.#/
=ii@2mong the 2. produ!tions 'e retain 2.-Oa and eliminate 2.-O 2/ 2/ using lemma .$
&he resulting produ!tions are 2.-O a2. 2/Q2/ , 2.-O a2.# 2/Q#/2/$
&he set of all 2.-produ!tions is 2.-OaQ a2. 2/Q2/Q a2.# 2/Q#/2/
Step 5: &he #/ produ!tions to e modified are #/ -O 2/ 2. , #/ -O 2/ 2. #/$
2pplying lemma . 'e get #/ -O a2.2.Q2. Qa2.#/ 2.Q #/2.
#/-O a2.2.#/Q2. #/ Qa2.#/ 2. #/Q #/2. #/
Een!e the e1uivalent grammar is
2.-OaQ a2. 2/Q2/Q a2.# 2/Q#/2/
2/-O a2.Q Q a2.#/Q #/
#/ -O a2.2.Q2. Qa2.#/ 2.Q #/2. #/
#/-O a2.2.#/Q2. #/ Qa2.#/ 2. #/Q #/2. #/
So1e' Pro/em"%
No!e% NT-Termina T-Termina
1. Le! u" &on1er! !o Grei/a&h norma form !he grammar
GB ;CA1( A#( A-D( Ca( /D( P( A1<( 4here P &on"i"!" of !he foo4ing%
A1->A#A-
A#->A-A1@/
A-->A1A#@a
Sou!ion%
S!ep 1% Sin!e the right-hand side of the produ!tions for 2. and 2/ start 'ith terminals or
highest-numered variales, 'e egin 'ith the produ!tion 2W-O2/2W is the only
produ!tion 'ith 2. on the left$
&he resultant set of produ!tions is: 2.-O2/2W
2/-O2W2.Q
2W-O2/2W2/Qa
Sin!e the right side of the produ!tion 2W-O2/2W2/ egins 'ith a lo'er-numered
variale, 'e sustitute for the first o!!urren!e of 2/ oth 2W2. and $ &hus 2W-O2/2W2/
is repla!ed y 2W-O2W2.2W2/ and 2W-O2W2/$ &he ne' set is
2.-O2/2W
2/-O2W2.Q
2W-O 2W2.2W2/ Q 2W2/Qa
7e no' apply lemma / to the produ!tions
2W-O 2W2.2W2/ Q 2W2/Qa
-et #/ e a ne' variale$ &he resulting produ!tions are
2.-O2/2W
2/-O2W2.Q
2W-O2W2/#/Qa#/Q 2W2/Qa
#/-O 2.2W2/ Q 2.2W2/#/
S!ep #% No' all the produ!tions 'ith 2W on the left have right-hand sides that start 'ith
terminals$ &hese are used to repla!e 2W in the produ!tions 2/-O2W2. and then the
produ!tions 'ith 2/ on the left are used to repla!e 2/ in the produ!tion 2.-O2/2W$ &he
result is the follo'ing$
2W-O2W2/#/ 2W-O2W2/
2W-Oa#/ 2W-Oa
2/-O2W2/#/2. 2/-O2W2/2.
2/-Oa#/2. 2/-Oa2.
2/-O
2.-O2W2/#/2.2W 2.-O2W2/2.2W
2.-O #/2.2W 2.-O 2.2W
2.-O2W
#/-O 2.2W2/ #/-O 2.2W2/#/
S!ep -% &he t'o #/ produ!tions are !onverted to proper form, resulting in .: more
produ!tions$ &hat is, the produ!tions
#/-O 2.2W2/ #/-O 2.2W2/#/
are altered y sustituting the right side of ea!h of the five produ!tions 'ith 2. on the left
for the first o!!urren!es of 2.$ &hus #/-O 2.2W2/ e!omes
#/-O 2W2/#/2.2W 2W2/ #/-O a#/2.2W2W2/
#/-O 2W2W2/ #/-O 2W2/#/2.2W 2W2/
#/-O a2.2W2W2/
&he other produ!tion for #/ is repla!ed similarly$ &he final set of produ!tions is
0. AM?IGUOUS AND UNAM?IGUOUS GRAMMARS
Am/iguou" Grammar% &he produ!tion should e in GNF and if the produ!tion !ontains more
than one derivation or derivation tree then it is termed as an amiguous grammar$
For example !onsider the produ!tion, S-OSBSQS0SQaQ
7e have to derive the string aBa0
&he derivation is as follo's:
S-OSBS
-OSBS0S
-OaBa0
Een!e derived$
2W-O2W2/#/ 2W-O2W2/
2W-Oa#/ 2W-Oa
2/-O2W2/#/2. 2/-O2W2/2.
2/-Oa#/2. 2/-Oa2.
2/-O
2.-O2W2/#/2.2W 2.-O2W2/2.2W
2.-O #/2.2W 2.-O 2.2W
2.-O2W
#/-O 2W2/#/2.2W 2W2/ #/-O 2W2/#/2.2W 2W2/
#/-O a#/2.2W2W2/#/ #/-O a#/2.2W2W2/
#/-O 2W2W2/#/ #/-O 2W2W2/
#/-O 2W2/2.2W 2W2/#/ #/-O 2W2/2.2W 2W2/
#/-O a2.2W2W2/#/ #/-O a2.2W2W2/
&he same string !an also e derived in the follo'ing manner
S-OS0S
-OSBS0S
-OaBa0
Een!e from the aove t'o derivations it is ovious that the string !an e derived from more than
one derivation$ &herefore it is kno'n as amiguous grammar$
Unam/iguou" Grammar% ,f the produ!tion !ontains exa!tly one derivation or one derivation
tree then it is termed as an unamiguous grammar$
Consider an example:
S-OaQaSQa2
2-OSQa22, the 'ord to e derived is 78aa
;erivation:
S-OaS
-Oaa
Een!e derived$
No' !onsider
S-Oa2
-OaS
-OaaS
-Oaa
S-OaS
-O aaS
-Oaa
2-Oa22
-OaSS
-Oaaa
2-OS
-Oa
From the aove derivations it is ovious that the string aa !an have only one derivation$ Een!e
the grammar !an e termed as an unamiguous grammar$
2. T8E
M:8ILL-NERODE T8EOREM
egin
for p in # and $ in %-# do mark &p!$';
for each pair of distinct states &p!$'
in #(# or &%-#'(&%-#' do
if for some input a!&)&p!a'! )&$!a'' is
marked then
begin
*ark&p!$'
recursivel+ mark all unmarked pairs on
the list for &p!$' and on the lists of the
other pairs that are marked at
this step,
end
else
for all input s+mbols a do
-ut&p!$' on the list for !&)&p!a'!
)&$!a'' unless )&p!a'= )&$!a'
end
Con"!ru&!ion of Minimum Au!oma!on%
Consider the follo'ing transition diagram
1
:
1
.
1
W
1
/
1
`
1
a
1
Z
1
b
:
:
:
.
:
:
:
.
.
.
:
.
.
.
:
No' !onstru!t the transition tale:
? : .
1: 1. 1`
1. 1a 1/
1/ 1: 1/
1W 1/ 1a
1Z 1b 1`
1` 1/ 1a
1a 1a 1Z
1b 1a 1/
T8E M:8ILL-NERODE T8EOREM
S!ep 1% Constru!tion of :, y def :-e1uivalen!e
c: 8 )>.
:
, >/
:
* 'here
>.
:
is the set of final states and
egin
for p in # and $ in %-# do mark &p!$';
for each pair of distinct states &p!$'
in #(# or &%-#'(&%-#' do
if for some input a!&)&p!a'! )&$!a'' is
marked then
begin
*ark&p!$'
recursivel+ mark all unmarked pairs on
the list for &p!$' and on the lists of the
other pairs that are marked at
this step,
end
else
for all input s+mbols a do
-ut&p!$' on the list for !&)&p!a'!
)&$!a'' unless )&p!a'= )&$!a'
end
>/
:
8 > - >.
:
hen!e
>.
:
8 F 8 )1/*
>/
:
8 > - >.
:
So
c: 8 ))1/*, )1:, 1., 1W, 1Z, 1`, 1a, 1b **
&he )1/* in d: !annot e partitioned$
So >.8 )1/*
Consider 1: and 1. e >/
:
$
&he entries under the : f !olumn !orresponding to 1: and 1. are 1. and 1a" they lie in >/
:
$ &he
entries in .- !olumn are 1` and 1/, 1/ e >.
:
and 1` e >/
:
$ &herefore 1: and 1. are not .-
R1uivalent$ Similarly 1: is . f R1uivalent to 1W, 1` and 1b$
No' !onsider 1: and 1Z$ &he entries under the :-!olumn are 1. and 1b oth are in >/
:
$ &he
entries under .-!olumn are 1`, so 1Z and 1: are .- R1uivalent$
Similarly 1: is .- R1uivalent to 1a,
So, >/8 )1:, 1Z, 1a*
Repeat the !onstru!tion y !onsidering 1, as one of the states 1W, 1`, 1b$ No' 1. is not .
f R1uivalent to 1W and 1` ut . f R1uivalent to 1b hen!e >W8 )1., 1b*$ &he left over elements in
>/
:
are 1W and 1`$
&herefore,
c. 8 ))1/* , ) 1:,1Z,1a * ,)1.,1b *, )1W,1`**
&he )1/* is also in c/ as it !annot e further partitioned$ No' the entries under : f !olumn to 1:
and 1Z are 1. and 1b and these lie in the same e1uivalen!e !lass in c.$ &he entries under .-
!olumn are 1`$ So 1: and 1Z are /- R1uivalent$ Tut 1: and 1a are not /-R1uivalent$ Een!e
)1:,1Z, 1a * is partitioned into )1:,1Z* and )1a*$ 1. and 1b are /-R1uivalent$
c/ 8 ))1/*, )1:, 1Z*, )1a*, )1., 1b *, )1W,1`**
1: and 1Z are W f R1uivalent
&herefore
cW 8 ))1/*, )1:, 1Z*, )1a*, )1., 1b*, )1W, 1`**
&he minimum state automaton is
<8 =>, ):, .*, ?, 1:, F@
'here
> 8 )L 1/M ,L1:,1ZM, L1a M ,L1.,1b M, L1W,1`M*
1:8 [1:, 1ZM
F= L1/M
and ? is defined y
? : .
L1:, 1ZM L1., 1bM L1W, 1`M
L1., 1bM L1aM L1/M
L1/M L1:, 1ZM L1/M
L1W, 1`M L1/M L1aM
L1aM L1aM L1:, 1ZM
E9ampe #% MinimiEe !he fini!e au!oma!a gi1en /eo4
L1
:,
1
Z
M
L1
/
M
L1
.,
1
b
M
L1
W,
1
:
M
L1
a
M
:
:
.
:
:
.
.
.
.
:

&he transition tale !an e given as
? : .
a f
g !
! f !
d ! g
e h f
f ! g
g g e
h g !
g : 8 ))!*, )a, , d, e, f, g, h**
a b c d
e f g h
0
0 1 0
1
0
0
0
0 1
1
1
1
1
1
0
7here !lass 2 8 )!* and Class T 8 )a, , d, e, f, g, h*
Class T:
Case =i@ : !omparing a h
?=a,:@ 8 e T
?=,:@ 8 g e T
?=a,.@ 8 f e T
?=,.@ 8 ! e 2
So !an e partitioned from a
Case =ii@ : !omparing a h d
?=a,:@ 8 e T
?=d,:@ 8 ! e 2
?=a,.@ 8 f e T
?=d,.@ 8 g e T
So d !an e partitioned from a
Case =iii@: !omparing a h e
?=a,:@ 8 e T
?=e,:@ 8 h e T
?=a,.@ 8 f e T
?=e,.@ 8 f e T
So e !anIt e partitioned from a
Case =iv@ : !omparing a h f
?=a,:@ 8 e T
?=f,:@ 8 ! e 2
?=a,.@ 8 f e T
?=f,.@ 8 g e T
So f !an e partitioned from a
Case =v@: !omparing a h g
? =a,:@ 8 e T
? =g,:@ 8 g e T
? =a,.@ 8 f e T
? =g,.@ 8 e e T
So g !anIt e partitioned from a
Case =vi@: !omparing a h h
? =a,:@ 8 e T
? =h,:@ 8 g e T
? =a,.@ 8 f e T
? =h,.@ 8 ! e 2
So h !an e partitioned from a
g. 8 ))!*, )a, e, g*, ), d, f, h**
Class T:
Case =i@: !omparing a h e
? =a,:@ 8 e C
? =e,:@ 8 h e C
? =a,.@ 8 f e C
? =e,.@ 8 f e C
So e !anIt e partitioned from a
Case =i@: !omparing a h g
? =a,:@ 8 e C
? =g,:@ 8 g e T
? =a,.@ 8 f e C
? =g,.@ 8 e e T
So g !anIt e partitioned from a
Class C
Case =i@: !omparing h d
? =,:@ 8 g e T
? =d,:@ 8 ! e 2
? =,.@ 8 ! e 2
? =d,.@ 8 g e T
So d !an e partitioned from $
Case =i@: !omparing h f
? =,:@ 8 g e T
? =f,:@ 8 ! e 2
? =,.@ 8 ! e 2
? =f,.@ 8 g e T
So f !an e partitioned from $
Case =iii@: !omparing h h
? =,:@ 8 g e C
? =h,:@ 8 g e C
? =,.@ 8 ! e C
? =h,.@ 8 ! e C
So h !anIt e partitioned from $
g/ 8 ))!*, )a, e*, )g* ), h*, )d, f**
1
1

{c
}
{a,
e}

{g
}
{b,
h}
{d,
f}
1 0
0
0
1 1

Vous aimerez peut-être aussi