0% found this document useful (0 votes)
12 views14 pages

FLAT Regular Expressions and Languages

Uploaded by

Akshit Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

FLAT Regular Expressions and Languages

Uploaded by

Akshit Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

R e g u la r E x p re ss io n

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

_1. REGULAR EXPRESSION


4
ea dy stu die d Fin ite Au tom ata . No w we -switch ou r attention
We have alr to an algebraic description : "th
e
lik e de scr ipt ion of lan gu ag e
f machine ula r ex pressions can defin e exa- ctly
ssi on ." We sh all fin d tha t, reg .c:: ---. ,~ -
romlar expre r
regu ag es tha t the va
.
no us f
_ orm s of au tom ata de sc rw e: the regula
the same lan gu .
·
languages.
ex pre ssi on off er so me thi ng tha t au tom ata do not : a
-tto-we ver,r eg ula r
ativ e wa y to- ex pr es s the · str ing s, we wa nt to ac ce pt. Th u~
declar
lan gu ag e t or ma ny sy ste m. Fo r exa~ le, lexica l:
expression as the inp u t
or Fler. -
analyser gen era1ors, su ch as Lex
l~r gP -ression
4.1.1. fh eo pe ra to rs of Regu
the reg ula r no tat ion , we ne ed to lea rn the three operations
Before describing ssi on rep res en t. Th ese op era tio ns
era tor s of reg ula r ex pre
on languages tha t op
are: of
of tw o lan gu ag es L an d L2, de no ted L 1 u L2, is the set
1. The un ion 1
, or bo th.
string tha t are in eit he r L 1 or L2
d L 2 = {e , 001} the n L1 u L 2 = {e
,
For ex am ple , if L 1 = (00 1, 10 , 111 } an
10, 001 , 111 }.
t ca n be
C<?n ca ten ati on of lan gu ag es L 1 an d L2 is a se t of str ing s tha
2. Th e s
by tak ing an y str ing in L ap d co nc ate na tin g it wi th an y str ing
!ormed 1
of lan gu ag es wi th a do t.
m L2• We de no te co nc ate na tio n L = {001,
ex am ple , if L = (00 1, 10 , 111 } an d L 2 = (e, 001}, the n L 1 . 2
For 1 ,
111 , 00 10 01 , 10 00 1, 11 10 01 }.
10, by
3 clo sur e (or sta r, or kle en e clo su re) of a lan gu ag e Li s de no ted
· T~e tak ing
a rep res en ts tfie se t of tlio se str ing s tha t ca n be for me d by
L an
of str ing s fro m L, po ssi bly wi th rep eti tio ns (th at is sam e
an f nu mb er
be rep ea ted mo re the n on ce ) an d co nc ate na tin g all of them.
;tn ng can s of O's an d 1's inc lud ing
= (0, 1 I, the n L * is all str ing
norJJ exa:°1ple, L
u stn ng . .
. .
More formally, L * 1s . th . f.1n1
_e m
. un ion
te U; ~ L', wh ere z ~ 0.
r 4. The positive do
f
AUTOMATA THEORY
language L is deno
AND FORMAL '-AliQ
ted by u~~
1
s r~ os :hat can be L+ and re
the se o t ose str~ formed by taking any nu!~es
Yy with rep
strings from L, po~s1 etitions and concate e~\
. nating all of eht ~!
excluding null string i.e. t el't\ \
__ :: ., ,, .- -- L + = L * -E '
.
More forma 11y, L+ is the infinite umo
_ _ _ _ n Ui ~ 1 C, where i
> 0.
4.1.2. Definition o
f ·Regular ~xpr~s
The sel of regtrlar sion .
otpressien 1s dean
1. E~ eti by the fo_)}Gwlng_ry
s anJ ie ~ de into a les :
.~ lar expression) regular expressio~
U\
2. If r1 and r2 are re g u la r g,
expres sion,_!h en
(i) (r )
1 .-- (ii) r 1r 2
(iii) r + r
1 2 (iv) r1*
(v) r; are also. re g
u la r expressio~.
3. Nothing else is
re g u la r expressi
4.1.3. Building R,--- - - - - - - - - -
on.

.... e gular Expression


The algebra oi re
g u la r expression
variables th at d en s follows this p at
o te la n g u ag es , an h u si n g constant
dot an d st ar . W e d operators for th s and
ca n describe the ree o p er at io n s of
regular expression union,
lJ\ this definition, w recursively, as fo
e n o t only describe llows :
b u t for ea ch re g what are the lega
u la r expression l regular expressi
which w e d en o te r, we_ describe the on,
as L(r) . W e ca n language, it repr
consider some fa esents,
1. T h e constants cts:
E (n ul l string
de~oting the la n
g u ag es lE \ an d ) an~ $ ~empty set) are re g u la r ex
1:hat is,
$, respectively. pression,
-L(e) = \e \, an
2. u a is any sym d L(~) = $.
the language \a\ b~ l, th e~ a is regular expressi
. That 1s L(a) = \a \. on. This expression
3. A variable, us denotes
ua ll y capitalized an .
representi· ng any l d italic such as·
anguage . L.
4.1.4. Inductive 1s a v an.a ble
Step '
There are four
and one for the parts to the inductive step, one fo
introduction of pa re h
1. If r and r ar rantheses.
e regul~r expressi ac of three oper
1
denoting the unio 2
ons, then r + r . ators
n of L(r i) and L(r 1
That is, 2) .
2 is a regular
L(r 1 + r 2) = L(r 1) expression
If r and r2 are regula u L( r 2)
2
r e~pr sion, th
d en o ·t·1n g fue concatenating of L( r ) es en r r .
. 1 and L(r ) 1 2 18
\
Tha t 1s, L (r if 2) = L(r ) .
2 a regular ex
. 1,. L( r ) press1on.
f r is a re gu la r ex
3. l pression then r . 2
1s a regular e
closure of L (r) · That is,
L(r,.) = (L(r))* Xpression d \
. gu la en ot in g the
4. If r 1s a re . r expression, then (r), a parent
the same langua he .
expressi.on, denottng ge as r. sized
r i
' s al so \
a regular
~eG~:-:- -
~o rJi lal
- ~~~~~-- -
ly,
-- - -~
IJ\R EXPRESSION AND LANGUAGES
L(( r)) - L(r).
83
-
cu ss so me ex am ple s of build · r ex ·
,. 1 w let us dis mg reg ula
r,,o . press1on for better
rst an dm g.
u.rt de r express·
IJtple 4.1. W rit e the st one ion over alp ha be t (a, b' c)
E~a1·ng at lea st on e a an d at lea

ta1n ·o - - -
, .,. ,.- - f l ag e of reg 1 expressio .
,on Jution. Let us us t an a yse the lan gu u ar n
So . str · · .
ve ry sim ple an d the lan gu ag e will ha ve set of
proble m is
are 1 a 2 b 3 an y co b'Inatio _mgs, hke ab,
baa aa ... . So str ing s n of a's b'
a bab aaaab, - - XJ mbinatio m
of b ' s_
pbbb , b ' h 1ace s 1, 2, an d 3 an y co n
e's is po ssi ble at t e p a, , c me ans
and )*
+C .
(p +b • .
So regular express10n 1s
b(a_+ b + c)* ;:._ (l + b + c)* b(a + b + c)* a(a + b. +
r ·=J!± b + c)* a~a !_ b +. .c)*
. • . --
c)* . • •

If r = '1 + r2, (say)


then minimum str ing of r 1 is 3,!!
Example 4.2. W rit e the reg
_an d mi nim um str ing of r2

ula r :e~_pr ess ion


is ba.
-
for the set of strings of O's
m the right en d is 1. ·
and l's whose tenth sy mb ol fro uired .
en the set of str ing s, acc ord ing to the req
Solution. 1: = {0, 1} giv n
are 111 000 00~ 000 , 100 1'1010 101010 .... . So clearly we concer
regular expressio n ht are
e 1, rest 9 places from the rig
about only tenth
either 0 or 1. Sim
po
ila
sit
rly
ion
po
an
--
sit
d
ion
it
s
sh~
11 th on wa rd fro m rig ht en d ma y,__be
~
_
__ either
O

· ·
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
'
~+ » ~

r = (0 + 1)* 1(0 + 1)9.' ·' '


strings of an
r ex pr ess ion for the set of
Example 4.3. Write . the regula O's
an ~ l's su ch tha t in ev ery prefix, th e.,.» umber of
equa] number of O's t 1.
b~
differs from the numb ef< tf'l's 0,
ly the set of str ing s is ha vin g str ing s like 101010101
Solution. Cl ear
10101010,
' .'
ono,. 1001 ...
So regular ~xpresion will be
r = (01 + 10)*.
O's and
. Wr ite a reg ula r ex pr ess ion for the set of strings of
, Example 4.4
15
not co nta ini ng 101 as a su
bs tri ng .
ar that string
n. Wh en we ana lys e the set of strings then it becomes cle d th
ma Solutio · untere en no
Y star t with 'O' and O's ma y be rep eat ed , wh ere ve r one 1s enco
sing! 0 1001 ....
like 0000010 00000111111 , 100
e must follow it so strings are
r = (0*1 *00)* O; 1*. . '
Regular expression is .
· e the regular expression over alphabet {a, b} for the set
Example .4·s· writ ' h ·
of stri
th ed by od d nu mb er of b s t at 15
for thengs w1 even number of a's fol low
I
anguage
L = (,,2"b "' + : n ~ 0, m ~ OJ.
2 1
~
eoAY AND FORMAL LANGu4Q
84 TH ·~ - - - - -
AUTOMATA -----~,
=--- . - - - - - - - .- --f 1 nguage
--:--:-;~11
L wi
start with even nulllb'
er
Solution. Clearly every string O a f b's.
0
of a's. String should end with odd num~er * b
Regu lar expression . . r - (aa) (bb) .
1s - f the language
ression or }
Example 4.6. Write the regular ex~ of consecutive zeros ·
L = {w e {0 1}* : w has no pair . ust be followed by :i l. SucL
' o occurs, tt m mber of l's th 11
Solu_tion. Clearly whenever a d followed by an~ n~e unaccounted at is
a substnng may be proceeded an • ting of all 1 s a here
(1 "'011 ''")"'. But strings ending in Oor consis · '
· · 1 *(O + e)
that is 1* (0 + e). . . r :::(l*0ll*)* (0 + e) + -
So regular expression is . 1 _ + 01)* (0 + e)
1
Another possible solution i~r2 - ( ok different, both answ~rs. are correct,
"Although the two express1onGlo erally, there.are an unlimited nulllber
language. en ,,
as they denote the same . en language.
of regular expressions for any g1v . for the language
ular expression
Example 4.7. Write the reg L - {anbm In 2': 4, m ~ 3} .
- f t . gs such that every strmg start
ntain the set o ,s rm
Solution. Language co t the most 3 b's. 2 3
with atleast 4 a's and end a_ b _ a4 a* (e · + b + b + b ).
sion will e r - •
So regular expres . for the language
W ·t the regular expression
Example 4.8. ri e L = {an bm : (n + m) is even}
b in two cases
Solution. We kn<?w (n -'.- m) can e even
Case 1.n and m both are even.
Case 2.n and m both are odd. . n
Let regular expression for case 1 is r 1 the
r1 = (aa)* (bb)*
Let regular expression for case 2 is r2 then
r2 = (aa)* a (bb)* b
Let regular ;xpression for language L is r
iliffi . r=~+~
· r = (aa)* (bb)* + (aa)* a (bb}* b.
Example 4.9. Write the regular expression for the language
L = {abn w : n ~ 3, w e (a, bt}
Solution. Clearly every string of language L starts with a followed by
atleast three h's, followed by atleast one a,or one b, that is strings are like ab3a,
nbbbbbbn, nbbbbbbbb, abbbb ....a
So regular expression is : r = ab 3b* (a + b)+
Here + is a positive closure i.e. (a + bt = (a + b)* - e.
Example 4.10. Write the regular expression for the language
L = {w : I w I mod 3 = O}, w e (a, b)•}
Solution. Let us first understand the m~aning of I w I mod 3 = O, when
length of string belongs to the language L, is divided by 3 then reminder
should be 0, that is length of w must be_0, 3, 6, 9 .... Clearly multiple of th.ree,
~ pAESSION AND LANGUAGES
i>~~~e~-------:::-: -~-~-: -=---;; :--: -:~ --- -
d~G e,cpress1on 1s : r = ((a + b)3)*
1/oi,gt1Hlf
~e te ,11. Write the regu lar expression for the
4 1
~%iitt1' L = {w e (a, b)* : n/w) mod 3 = O~nguage

sotullo t\. n' (w) mod 3 = 0 means, number of a's in stnng


.
.
should be 0, 3,
9 •·· · r expression is : r = (b*ab*ab*ab*)*
6, 50 regu 1a
MPARATIVE STUDY OF REGULAR EXPRESSION
4,2, ~~GULAR SETS AND FINITE AUTOMATA '
Regular set Finite automata
Start... t:°\


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

fig. 4.7 (a).


. f me smaller expression r and s. The autornat·
2. The expression rs or so . . b · Ion
4 7
for the concatenation is show m Fig. · ( ).

Fig. 4.7 (b);


Note that the start state of the first automation becomes _the start state of
the whole, and the accepting state of the second automahon becomes the
accepting of the whole. The idea is tha~ the only paths ~rom start to accepting
state to go first through the automation of r, where it must follow a path
labelled by a string in L(r), and then .through the automation for s, where it
follows a path labelled by a string in'L(s). Thus, the path in the automation of
Fig. 4.7 (b) are all and only those labelled by strings in L(R) L(s).
3. The expression is r* for some smaller expression r. Then we use
automation of Fig. 4.7 (c). That automation allow us to go either :
(i) Directly from the start state to the accepting state along a path
labelled e.
(ii) To the _start state of automation for r, throug~ lhat automation one or
more hm:s, ~d then to accepting state. This set of paths allow us to
a~cep~ strings m L(r), L(r), L(r), L(r), L(r), L(r), and so on thus covering
a l strings in L(r*~ except perhaps e which was covered by the direct
arc to the accepting state.

where R is automation for r. Fig. 4.7 (c).

4. The expression is (r) f


also serves a th or the smaller ex .
language de~ e automation for (r), since th~ress1on r. The automation for r
ed by the expression. parentheses do not change the
Example 4 12 0
for regular ex. re . . ne the basis of above . .
Soiut' p ss1on a . (a + b)* • b b d1scuss1on find the automation
ion. Let us solve . . .
Step 1 Th b . tb1s problem ste b
automatio~ fota :~~ ;~f:lar ~xpression in~ol~e~tep :
mation for b. Since br are a and b, we start with
ackets are evaluated first. We
RfSSION AND LANGUAGES
~~p .
ing automat·Ion for a
87
,.~,) ted au tom atw n for (a + b), us .
anct autorn ation
"tv ,,strt.JCfig- 4.8(a)
r
·"t C0 jrt
(JV~O\'vf1
/I 5

+ b).
fig. 4.S(a). Automation for (a
to take next,. we. construct autom a Ion fo

. 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

· mation for 1 . (0 + 1). .


(c). Auto
fig. 4.9 . (b) and e-NFA of Fig. 4.9 by a
9
e to connect e-NFA of F~g. 4. regular
Now we hav expression as shown in
. d out the e -NFA for given
e-transition to fm
fig. 4.9(d).

Fig. 4.9 (d). Automation for (0 + 1)*


1(0 + 1).
4.4. CONSTRUCTION OF REGULA
R EXPRESSION FROM DFA
Now it is clear that regular expressio
n is mathematical expression for
given regular language. We have already a
know that for every regular langua
there exist a deterministic finite aut ge
omate. We can conclude that reg
expression, regular language for that reg ular
ular expression and deterministic fini
automata for U1at regualr language are te
similar things in different representatio
So we can construct regular express n.
ion for every deterministic autom
ata.
4.4.1. Ardens's Theorem
L~t P and 9
be two
contam null strmg e, thenregular expression over alphabet !;. If p does not

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.)
(

" use of Ard!" 'sF. T~teoA_remt to tfind Regular Expression of a


,.,.~. terministIc u oma a
1nI e .
0e . h' h are ma de r~garding the tr ans1. 0on
certain ass um pti ons w 1c
fhere are
e e-t ran siti ons .
5yste~ : he transition dia gra m sho uld not hav
state.
(t) T ist have onl y a sin gle init'ial
----
(ii) It )lll
••') It vertices are q1 ... qn. .
.
~
. ) q- is fina_l state. .
; .. den~te s the reg ula r exp res sio n representing the set of labels of edges
(iv
(v) trbm q; to qi' ·we can get the fol
lowing set of equation in q1 ... qn.
.•. (1)
ql = qlw ll + q2W21 + ... + qnwnl + E
(since q1 is the initial state)
... (2)
q2 = ql W12 + q2W22 + ... qnW112

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

qt = e(0 + 1(1 + 011)* (00 + 010))*


in q4 equation q4 =q11(1 + '011)* 01 = q1(1(1 + 0l)*0l)
pt1t qz _q4 ~ (0 + 1(1 + 011)* (00 + 010))* (1(1 + 011)*01)
uired regular expression 1s
So %q + 1(1 + 011)* (00 + 010))* (1(1 + 011)*01)

ALGEBRAIC LAWS FOR RE NS


45. ·-. 11 ·
· this section, we w1 see a co ec~ f algebraic laws that bring the
. InOf wh~!1 two _regular laws are equ~valent. Instead of examuung specific
,ssuel ex.pressions~ we shall consider -pairs. hof regular expressions with
rcgu ar T . . .
. bles as arguments. wo expressions wit variables are equivalent if
:::~ever languages we substitute for the variables, the result of the two
ressions are the same language.
· express10n
exp Like arithmatic · s h ave a number of laws
· s, the regu1ar express10n
that work for them. Many of these are similar t9 the ·laws for arithmatic, if we
think of union as addition and concatenation as multi.plication. However, there
are a few places where the analogy breaks down, 1_and there are also some
laws that apply to regular expressions but have no analog for arithmatic,
especially when the closure operator is involved. The next sections from a
catalog of the major laws.
4.5.1. Associativity and Commutativity
Commutativity is the pl'UpNty. of an operator that says that we can •switch
the order of its operands and get the saroe result. For example, the associative
law of multiplication is (x x y) x z = x x (y x z). Here are three laws of these
~pes that hold for regular ,.exp-r-e-S&ie : Co t,,nlt
·h~ 1 ve hwJ
. I p+ Q = Q + P. ~ U,µJO u.,,
This law, the commutative aw or the union, says .that e take umon oj
two languages in ei ther~1' . • J1- Ad cJ aA-f l/ ~ J..a_;.,J
. . ·
llus law, the ass~
/(P + Q) + R = P + (Q + -!3)
on says !Im- , may
j iake
'le,
>1 l)..i,.«Ot<--
the umon of
~ree l~nguages either by taking the union of the first two initially, or taking
e union of the last two initially.
w Note that, together with the commutative law for union, we ..c.oncI~ t
greoca~ J~~ the union ,. of any collectiQILO_f languages with \nl 0rd_er aocl
llp1ng d h resulT w.i lloethe - same. Intuitivelf·i a strmg· ·s m · L1 u L2 u
... +L . ' an t e...r
Kif and only if it is in on; - ; ~e of the L/s. -
V
92 ND FOR AL LAN ~~
AUTOMATA THEORY A
- - --- -- ~ = -- -.-1..l~IMJ~ ()&L t
,;y(J)t1Jt, V\..
.
(PQ)R = P(QR) l{ta1 Lwe ·can c _ca~ :nat~
. t natio n, says L'h e last two imtia n
This \aw, the associative law for conca e f' t two or u• ~
th ree languages by-concatenafing eit. her for 1rs Y.
-
.. when th~ pe~ator ~
4.5.2. ldent,t,e s e such tha\ __ the same value. Ft\
~ . a va1u
AI\ identity for an o2_erator is It ~ vt
r value.t the_re~u==_m + 0 == m, and 1 is th~
applied to identi and some o~ e. since O + m fher
. • 'd ·t e are three laws for
insta nce is the 1 enti Y for add1 t1on,
. 1 )( x == x x 1 == x.
. t them beIOw ·
. , . 1· tion since we 1 is
identity for the mulhp 1ca ' oncepts; . the ide~ .ty fO r un.ion.
. . 1 ing these c h t 4> 1s _ ~ one.a-le .
regular expression mvo v is law assets t _a -;-rdenfit Y 1or~
► .1. + L~= L + 4, = L. Th ts that e 1s ~~ r for concatenation.
L-nation.
"' Th. law asse
► eL = e = L. is --that IP is .the .ariJU.-----
ill1e1L~
:_ ,_.
~
~ .i. This law assets -- -
► ¢,L = L4, = ..,. - - ~
.....--~
s an asmeri - - ~
d sets that operator
4.5.3. Distr two opera t@I_ t of t:,e 1..
operatL.
· A disg:ibtrl 4-- rc~:5;pplied to eac~ a~; ed law of multiplication
~ pssthed down to example is the d1stn
can Th most common .
individually. e .
over addition, that is x (y + z) = x x y + x x ; expressio
n, that we most
. :n ·analogous law for regu a~u tativ e. These laws are
However, there ~~ce concatenation is not com left dist :
state in two forms.= PQ + PR. This law, is t e , ribu ted law of
---
► P(Q +tenat fil ion over unio
- · n. .
conca n - nP -+- .RP This law, lS . t~ ~ t d~ "b u te..,.. d 1
aw of
► (Q + ru~ .· - ,\ 0
concatenation over_~mon. 1i 1 ' -o 'I
ov
4.5.4. The ldempot~nt ,L~~ t 'f the result of appl ying it to two of
An operator is said1't0' be i~empote~ 1 The common
arithmatic operators
the same values as argtfiftents 1~ that va ulea.nd x x x -:;; x
in general (although
are not 1'dempo ten,t x•+rx *f x m h' genera
h the equality holds, such as O + 0 = ~
there are some values ~f X or~ lC
However, union and mtersection are co mmon exam ples of id';"mpo ' tent
operators. Thus for the regular expression, we may a~se h f 11 · 1 .
rt t e 0 . owing aw·
► L + L = L. This law, the idempo~~_ n . t . J a ~ ~
unio n 01-two ide~ti~ ake the
can replace them y one copy of
the express.ion. __.J
4.5.5. Laws to/c~sure .
There are nu_m 6er of laws involving the closure oper
ators. We shall list
them here, and give some explaJTatien-f,Gr-wh.y...they-
are true . . .
1. ~ This law says that closing an !~P- ressi
on t~at .Js alrea dy
closed does not change the language. The language of
(L *)* is 9 11' strin~
created for concatenating strings in tfie language L*. But
th~
themselves composed of strings from L. Thus, the strin
g in (L*)* is also a
concatenation of strings from Lan d therefore in the lang
uage ofL•.
2. ~* = e. The closure of ~ contains only the string
e. - -- -
3. e* = e:. It is easy to check that onl /strm g-th
at can be fcp:med by-
concatenating any number of copies of the emp tyit
rmg is the emp ty
strin g itself. / / __,.,
~ pflESSIOINN~
-- AN::D:_:L:.;A::::
NG =::U
~A:=
G==:
E :.....-:-:~-:---:-:---:-- -- -~ 93
G~j)~ EX~ L*L. Recall that positive closure of L, L~
~~ ( ::: LL LLL----➔=--: .. . Also, L,. =
E + L + tL + LLL + ...
is defined to be
4
. L + LL + LL,. = L + LL + LLL + LLLL + ...
'fh°US L+ =LL*.
t proves . 'I
'fha + E. The proof is easy, smc . , +
I ,t - L +
e the exp ans ion of L includes every
. ans .
5..... ~ he exp ion of L* excep.t _e • N ote th at 1f . the
language
tertn tains the string e then the additional
+e is not needed; that is

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

You might also like