FLAT Regular Expressions and Languages
FLAT Regular Expressions and Languages
4
a ii d L a n g u a g e s
e INSIDE THIS CHAPTER
S
sio n 4.2 . Co mp arative Stu dy of Regular Expression , Reg uIar ets
,....- Reg ular Exp res • fF f t .
ression 4·4• Co ns ruction
4 1· F'ni·te Automata 4.-3 . c onstruct1on o A .or Regular Exp ressions
·
and 1 . Algebraic Laws for Regular Exp
ula r Expression from DFA 4.5.
of Aeg
· ·
or 1.
So regular ex p!_e ssi on is
-tJ ) (0 +1 ) (0 ~ 1)
r =(0 + _1)* 1w
,:r 1) (0 ,:!: 1) (0 :t,.1) (0 t,.1) (0 -+',) ) (0
'
~+ » ~
♦
lI ~ 0
Fig 4.1. q : is the final state.
1
{e}
Fig. 4.2.
Start~0
Every a in 1: ~ ·~
!al
is a regular Fig. 4.3.
expression.
R u R2 (where R1
'2
If rI and are , 1
and R2 are regular
regular expression
sets corresponding
then (r1 + r2) or
to r and r2 __ _
r/r is regular 1 Where N is FA accepting R1 and N2 is a
2
respectively). 1
expression. finite automata accepting R2•
Fig. 4.4.
r r2 is a regular R R (where R1 and
1 2
1
expression. R are regular sets
2
corresponding to r Where N1 is finite automata for R1 and N2
1
and r respectively). is FA for R2.
2
Fig. 4.5.
t is a regular R* (where R is a
expression if r regular set corres-
is regular ponding to r).
expression. where N is finite automata accepting R.
1
Fig. 4.6.
4·3• CONSTRUCTION. OF FA FOR REGULA
R EXPRESSION
fact We can construct NF A with e transmission by cons
idering following four
S,
, 1. The
autom t· exp ressm
· n 1s
· r + s for some smal
ler expression rand s. The fol Iowin
· g.
'l'"'I~ a 10n serv h 5
'1.. . es, w ere R 1s · automalt·on for r and S 1s· automatton
· for ·
•u1,l!\;i'1artmg at the new start state, we can go to the start state of either lhe
one of tlion for r or the automation for S. We can reach the accep st
lese automata, follo . ting ate of
· g in
wing a path labelled by some strin · L(r ) and
J
~
AUTOMATA THEORY AND FORMAL LA~
GlJ,\Q
86 h the accepting state of the autornat·1 ~
we reac t· on
L(s), respectively. 0nee of the e-arcs to th~ ac~ep mg st_ate of the Ot i
f
or s, we can follow one the automation in Fig. 4.7 (a) is L(r) v L l\e\v
automation. The language for (s).
R
+ b).
fig. 4.S(a). Automation for (a
to take next,. we. construct autom a Ion fo
t·
. e closu•re isf req( uir ed
b) r
2:
S111C
au tom ati on or a + as sh ow n m Fig. 4.B(b).
,teP
b)t LJSiJlg .
e a
(a.,
(a + b)*
Fig. 4.S(b). Automation for
(c).
nstruct the au tom ati on for a. (a+ b)* as shown in Fig. 4.8
t we co
step 3. Nex E
a . (a + b)*.
Fig. 4.S(c). Automation for using
xt we co ns tru ct the au tom ati on for a . (a + b)* . b by
Ne
Step 4· * 1
automation for a . (a + b) .
a . (a + b)* . b. ·
Fig. 4.8(d). Automation for
ally we ca n co ns tru ct au tom ati on for a . (a + b)* . b . b by
. Step 5. Now fin
b.
usmg automation for a . (a + b)* .
(a + b)* . b . b.
It is . ig. 4.B(e). Automation for a .
F'
.
required aut omah.on for given reg ula r expression.
88
--- - - - - - - - - - - - -AU TO
y AND FORMAL LANGlJ~
~~
1 1
:~:-=M ATtarA~expres ·
'Example 4.13. Construct the e-NFA fOr the regu sion (0 + l)~
l
{0 + 1) . F
. . utomatton. irst we i.v·
. , d 1 is basic a
Solution. Here automation ror o_an 111
make automation for (0 + 1) as in 4 9 (a). hown in Fig
Fig .. · for (0 + 1)* as 5 4
· ·9(b).
Now we will convert it into automatio
n O
E 1 e
t mation for (0 + 1)"'.
. 4 9(b). Au o . p·
•
F"ig. 4.9(a). Automation for
(O + t). Fig, · l) as shown m 1g. 4.9(c).
ation for 1 . (0 +
Now we will con st ruc t autom .
o e
R == Q + RP
,,,,,... RfSSION AND LANGUAGES
e~P . _ _ _ _ _ _ 89
J!~i)~ oJution tha t 1s R = Qf, .
~ev 1·qi.le und s -
t.111 ers tan d as : R = Q +- RP --
~5 9 11 be R.H .S.
11 ft ,11 Jue of R in
i,e va
R = Q + (Q + RP)P = Q + QP +RP2
11t.1t t · d . - the follow · _
l fR aga m an aga m we got
we put the va ue o
2 mg equation.
wne 11 R = Q + QP + QP + QP3 ...
2
R = Q(l + p + p + p3 ...
2
R = Q( E + p + p + p3 + ...
R = QP,.
.
By the def ini tio n of closure ope rat ion for regul ar expression.)
(
... (n)
q,, + q2W2n + ... + q,,w,,,, '
= ql Win
ms of w;/s and it will be required
We solve these equ atio n for q; in ter
not ed tha t we add ~ (null _string) in
regular expression. ~e thin~ sho uld be out qi
te q and we sol ve equ atio n to fmd
the equation starts wit h sta rtm g sta 1
one of the reg ula r expression for given
(final state) in terms of W;/s, it is
deterministic finite aut om ata .
sion for transition diagram given
Example 4.14. Find the regular expres
in Fig. 4.11.
a
Start
b
'I
a
I
a b I
I
a,b
b
II
Fig, 4,U.
/j
ND
r
AUTOMATATHEOR~FO-
RMAL LANGUA~
- - ---!-~
~90~- - - - - - - - - : - -
Solution. Now let us from --
the equations : "'
q1 = q2b + q3a + E
q2 = qla
q3 = q1 b b + q a + q4b
q4 = q2a + q3 4
Put q2 and q3 in q1 as
bfl + E
q1 = q1ab + qi )
( b + ba
:= E + ql a
ql
= e(ab + ba)*
ql
So required regular expressi· 15 · (ab + ba)* ·
on co
tar expression rresponding to \he state
•
Ex ample 4.15. Construct a reg
u · · ·
diagram given in Fig. 4.1 2
,
Start
1
Fig. 4.12,
Solution. Let us form the
equations :
ql = q10 + q30 + E
q2 = ql 1 + q21 + q3l
q3 = q20
q = q 1 + q 1 ,+ (q 0) 1 = q 1
2 1 2 2 1 + qz(l + 01)
q2 = q1 1 (1 + 01)*
So q = q 0 + q 0 + e = q 0 + q 00
1 1 3 1 2 + E = q10 + (q 11 (1101)*)00
ql = ql(Q + 1(1 + 01)*00) +e
+E
q1 ·= e(O + 1(1 + 01)*00)*
ql = (0 + 1(1 + 01)* 00)*
So regular expression is (O·
+ 1(1 + 01)* 00)*.
Example 4.16. Find the re
gular expression co rre sp
on di ng to Fig. 4.13.
0
Fig. 4.13.
::: q31 = q201
~o_wq; equation, we get q2 =q11 + q21 + q20l 1 = q11 + q(l +
2
0s111g qz =q1l(l + 011)* =q1(1(l + 01f}*) 011)
and q4 in q1 equation qt = q10 + qz00 + qz0l0 + E
l't1 1q~ = ql0 + qz(00- + 010) + e
= qlO + q1l(l + 011)* (00 + 010) + E
6
~::L0 -
L con * .n this special case.
This rule is really the definiti(l !µ J~
opera~ 1
' · · ntitl.!s
for Re ~ular Ex pression
4.5,6. the
Ide . of above-d-iscussion we will see som
bas~s - -~ --- -:- 1 e identitites for the regular
()Ji . Let p, q, r are three reg~ ar expression . ~ - --i._ ,__ __
~press1on, ~ . Tw o reg ula r expression p
e are equ1
ay p = q) if p and q rep rese nt the same set of strings.
and q identities are foll owmg . '- - - - - - --------=
:
somIte ,,. -- ~__ r =__,.r
+ __
I
2 ~r = r~ = ~
p -"--'====-
- =_re_=r
er _,
t1 e* = e and f ~
5 r+ r= r
1
.6
J
r*r* = r*
..--- --- +
r7 _!r* =r*~ =.!,_.
t ~1*=~
r9 e +rr* =~
1
10 _s.(pqf*p= fiqp)*e .+ r*r
Ill (p + q)~ =(p_ *q~)* = (p* + q*)*
112 (p + q)r =pr+_qr and ~(p +_q) = rp +
rq
Example 4.17. Prove th~t (1 + 00*1) + (1
': 00::!,) (0 + lO* lt' (0 ~ 10*1.~
=o•l(o + 10*1)*
. ,.-. •h ,-
Solution. L.H.S. = (1 + OQ.*l), (e + (~ ±. 10*1 *(0 _+
•. , '~ . ~
1Q*l) using I12
9
I •
= (1 +.00~1) ~ ~ using I._
, ..
= ( E -~ _(9~)• ufir \ising 1 for (1 + QQ*l) .
12
__ __ __ _ = ~ •t..lil!.1) * ·using 9
1 --
.. .{ R.H.S.
enc~ it is ~
114" I EX ER CI SE 1
1. Give the English description for the
following regular expression :
(a) r = (aa)* (bb)* b
(b) r = (O + 1)*00 (0 + l)*
(c) r = ((0 + 1) (O + l)*)* 00(0 + 1)*
2
· Find the regular expression for the
following :
(a) L = la"b'", n ~ 1, m ~ 1, nm ~ 3\
(b) L = lab"w : 11 ~ 3, w e \ab\ +\
(c) L 0
-= la" : 11 ~ 0, n :1,: 31
: z ; z ,>
oRM
AL LANGU
94 AUTOMATAT EORY AND F A~e,~
H
. n for the lp h a b e t \0, 1\ :
3 Write regular exp toUowing la uage over a
· ress10 ng
(a) All strin · · 01
g endin g in . 01
b)
( All strings not
ending in
(c) All strin . ing an eve number of O's
gs contain n b s tr in g 00.
(d) All strin o f th
gs w i.th at most two ccurrences o e s u
(e) All stri . . the substrin 10
ngs not con g ·
4. Prove th taining
at ,. - (r +
(a) ( r t t = r{
" (b) r 1,.(r 1 + r2) == 1 r 2 t
(c) (r., + r
t
2 = (r1 •r2 •)*.
For all regu l
a r ress1on 11 an d r2. H e re = sta n ds f o r e quivalence in the se
of the langu e xp t nse
age genera _e d
5 G ive the English desc
." for the la r expre . .
· ription• b + following regu ( + ss1on .
(a) r = a(a + b• a b t (bb + a
b t ab (b.) r - a (c) r = aa )"'
~
6. Prove the fO11Owing 1dentity ·.
(a • ab + ba)•a• - (a + ab + ba)*
-
7. Construe t the trans1.t1.on sy t m for the f 11 wing re la r e x p re s s
,. se o o gu io n s :
(11) r = 1 + (b) r = (ab *)*b
00 + 010 1 +c
s. Construct a f10 . 1·te autom f
ata or the regular expressio
_ (a + b)*
n
in r - abb .
9. F d the regular express . h
ion for t e following transit. io
•
n d.ia g r a m.
a,b
\, , -l I
Fig. ·4.13.
10. Find th
e regular ex
the automati pression co
on given in rresponding
11. Find th Fig . 4.1 4. to
e regular exp
following se ression repre
ts : senting the
(i) The set
o f all string
most o n e p s over \0, 1
air of O's o \, n aving a t
l' s . r at most l
pair .of
(ii) The se
t o_f all strin 1
· th e r e · a r e gs over \0,
a tl e a s t tw o 1\ in whi~h
between a n o c c u rr e n c e b
y two occu of 1
(iii) Th e se rance of a.
t ·of all ~trin
gs over \a, , Fig. 4.14.
(iv) The se
t of all s tr in b\ e n d in g ~ it h
12. C o n s tr g s over \0, bb a n d b e g in
u c t a re g u 1\ not c o n ta in g w it h · a
la r in in g 1 .
th e a lp h a b e x p re ssion defin 0 1 a s s u b s tr in
e t I = {a, b in g each of th e g . .
(a) All s tr in \ fo ll o w in g
g s th a t d o la n g u a g e
(b) All s tr in not e n d in over
g s in w h ic a double le
h letter b n tt e r .
(c) All s tr in ever triples
g s in w h ic .
(d) All s tr in h a is tr ip le d o r b
g s th a t h a v is tr ip le d .
(e) All s tr in e a n even
g s th a t h a v n u m b e r of a's a n
e an odd n d an o dd
u m b e r of a n u m b e r of
's a n d a n o b's.
dd numbe
r of b's.
*****