You are on page 1of 62

Theoretical Computer Science

Unit 2: Regular Expressions and


Languages

Faculty Name : Dr. Vanita Mane

1
Index
Lecture 7 – Regular Language and Expression : Definition 3

Lecture 8 – Regular Language and Expression Examples 15

Lecture 9 – Regular Expression Examples and Equivalance 26

2
Lecture 7

Lecture No 7:
Regular Language and Regular
Expression (RE): Definition
Regular Expression (RE)
• The language accepted by finite automata can be described by
simple expressions called Regular Expressions.

• The languages accepted by some regular expression are referred to


as Regular languages.

• Regular expressions are used for representing certain sets of strings


in an algebraic fashion.

• ε is a Regular Expression indicates the language containing an


empty string. (L (ε) = {ε})

• φ is a Regular Expression denoting an empty language. (L (φ) = { })

Lecture 7 – Regular Language and


4
Expression : Definition
Operations on Regular Language
• Union ( + ) : If L and M are two regular languages then their
union L U M is also regular language.
L U M = { s | s is in L or s is in M }  
• Concatenation ( . ): If L and M are two regular languages
then their concatenation is also regular language.
L . M = { st | s is in L and t is in M}  
• Kleen closure (repetition) : If L is a regular language then its
Kleen closure L* will also be a regular language.
L* = Zero or more occurrence of language L.  
• Positive closure: If L is a regular language then its Positive
closure L+ will also be a regular language.
L+  = One or more occurrence of language L.  
Lecture 7 – Regular Language and
5
Expression : Definition
Formal Definition

The class of regular expressions over Σ is defined recursively as


follows:
1. Regular expressions over Σ, include letters,¢(empty set),and
є(empty string of length zero).
2. Every symbol a ε Σ is a regular expression over Σ.
3. If R1 and R2 are regular expressions over Σ, then following
are Res
(R1+R2) : ‘+’ indicates alternation (parallel path)
(R1⋅R2) :the operation‘.’ denotes concatenation (series
connection)
(R1)*: ‘*’ denotes iteration (closure or repetitive
concatenation).
4.
6
Regular
Lecture 7 – expressions
Regular Language and are only those that are obtained using
Expression : Definition
Identities Related to Regular Expressions
• Given R, P, L, Q as regular expressions, the following identities
hold −
1.R + ∅ = ∅ + R = R  (The identity for union)
2.R.ε = ε.R = R  (The identity for concatenation)

3.∅ . R = R . ∅ = ∅
4.ε* = ε and ∅* = ε
5.R + R = R  (Idempotent law)
6.RR* = R*R
7.R*R* = R*
Lecture 7 – Regular Language and
7
Expression : Definition
Identities Related to Regular Expressions(cont..)
8. (R*)* = R*
9. (PQ)*P =P(QP)*
10. L (M + N) = LM + LN  (Left distributive law)
11. (M + N) L = ML + NL  (Right distributive law)
12. ε + RR* = ε + R*R = R*
13. (P + Q)* = (P*Q*)* = (P*+Q*)*

Lecture 7 – Regular Language and


8
Expression : Definition
Regular Language Examples
Regular Expression Regular Language

L(r) = {1}
r =1 State
Current Current input
r =0 L(r) = {0}

We can choose either 0 or 1 but not both. Thus


r = 0+1 language consist of, L(r) = {0} ∪ {1} = {0,1}

r =0.1 L(r) = {01}

String containing only 1, any number of times


r = 1* L(r) = {e ,1,11,111,1111,11111,……….}
String starting with 0 followed by any number of
r = 01* 1.L(r) = 0. {e ,1,11,111,…….}={0, 01, 011, 0111,
…..}
Lecture 7 – Regular Language and
9
Expression : Definition
Regular Language Examples
Regular Regular Language
Expression
r =(01*)(01) L(r) = {001, 0101, 01101, 011101, …..}
Current State Current input
r = a* b* L( r ) = { ԑ, a, aa, aaa, b, bb, bbb, ab, aab,abb, abbb,..}

r = a. (a + b) L(r) = { aa , ab }
r = (aa)* L( r ) = {ԑ, aa, aaaa, aaaaaa, .......}

r = (a + b) . (a +b) L(r) = { aa , ab , ba , bb}

This will generate all string formed using symbols a & b.


r = (a + b) * L(r) ={ԑ, a, b, aa, ab, ba, bb, aab, aba, baa, abb, ………}

Lecture 7 – Regular Language and


10 Expression : Definition
Regular Language Examples
Regular Regular Language
Expression
Current State HereCurrent
we have choice between ab and b but we can
input
r = ( ab + b) * not generate single a.
L(r) = {ԑ, b , ab ,bb , abb , bab , abab , ………}

It will generate any combination of a and b which is


r = (a + b) * a necessarily concatenated by ‘a’ i.e. ends with a.
L(r) ={a, aa, ba, aaa, aba, baa, bba, aaba, abaa, baaa,…}

It will generate string which starts with a and ends with b.


r = a ( a + b )* b L( r ) = { ab, aab, abb, aaab, aabb, abab, abbb, aaaab,
aaabb, ….}

Lecture 7 – Regular Language and


11
Expression : Definition
Regular Expression Examples
1. Construct a RE over Σ = {0,1} that represents all string over given
alphabet.
🡪 L = { Є, 0, 1, 01, 10, …..}
🡪 r= (0 + 1)*
2. Construct a RE over ∑ ={a, b} which describe all words with ‘a’ followed
by any number of b’s.
🡪 L = { a, ab, abb, abbb …..}
🡪 r = ab*
3. Construct a RE over ∑ ={a, b} that describe all words with ‘a’ followed
by one or more b’s.
🡪 L= {ab , abb , abbb, …..}
🡪 r = abb* or r = ab+
4. Construct a RE over ∑ ={a, b , c} that describe all words that start with
an ‘a’ followed by any number of b’s and then end with c.
🡪 L= {ac, abc, abbc, abbbc …..}
🡪 r = a b *c

Lecture 7 – Regular Language and


12
Expression : Definition
Regular Expression Examples(cont..)
5. Construct a RE over ∑ ={a} that describe all words where count of ‘a’ is
odd.
🡪L= {a , aaa , aaaaa , …..}
🡪 r = a(aa)* OR (aa)*a
6. Construct a RE over ∑ ={a, b, c} that describe all words where single ‘a’ or
‘c’ comes in the start then odd number of b’s.
🡪L= {ab , cb , abbb , cbbb, …..}
🡪 r = (a+c)b(bb)*
7. Construct a RE over Σ = {0,1} that represents all strings that have two
consecutive 0s.
🡪 r= (0+1)*00(0+1)*
8. Construct a RE over Σ = {a, b} that represents all strings with at least one
a's.
🡪r = (anything) a(anything)
🡪 r = (a+b)* a (a+b) *
Lecture 7 – Regular Language and
13
Expression : Definition
Regular Expression Examples(cont..)
9. Write the RE for the language starting with a but not having consecutive b's.
🡪L = {a, aba, aab, aba, aaa, abab, .....}  
🡪 R = {a + ab}*  
10. Write the regular expression for the language L over ∑ = {0, 1} such that all the
string do not contain the substring 01.
🡪 L = {ε, 0, 1, 00, 11, 10, 100, .....}  
🡪 R = (1* 0*) 
11. Write the RE for the language over ∑={0} having even length of the string.
🡪 L = {ε, 00, 0000, 000000, ......} 
🡪 R = (00)*  
 12. Write the regular expression for the language containing the string in which
every ‘0’ is immediately followed by ’11’.
🡪 L = {1, 11, 011, 1011, 011011, 11011......}
🡪 R = (011 + 1)* 

Lecture 7 – Regular Language and


14
Expression : Definition
Thank You
Lecture 8

Lecture No 8:
Equivalence of RE and ε-NFA
Regular Expression to Finite Automata
• We can obtain equivalent DFA for given Regular expression .
• Flow is as shown below :
Regular
Expressi
on

NFA with
Є moves DFA

NFA
without
Є moves

Lecture 8: Equivalence of RE and ε-


17
NFA
RE to NFA with ԑ moves Conversion
• RE is notational way of representing the specification of the
language.
• When we generate FA using RE, it will be non deterministic due to ԑ
moves.
• Then it can be reduced to non-deterministic finite automata
without ԑ moves and finally we will get the DFA.
• We have to follow some procedure for conversion as follows :
⮚ Step 1:
Let R be a RE reading single symbol say ‘a’ generating 2 states , Start
state say ‘s’ and Final state say ‘f’ then the transition will be
shown as follows:

a
s ff
Lecture 8: Equivalence of RE and ε-
18
NFA
RE to NFA with ԑ moves Conversion(cont..)
⮚ Step 2 :
Let R1 and R2 are two regular expressions having Start and Final States as S1,
F1 and S2, F2 respectively.

R1 R2
S1 F1 S2 F2
2.1 🡪 R1.R2 (Concatenation/ Series) :
R1 Є R2
S1 F1 S2 F2
OR
R1 R2
S1 F1 F2
Lecture 8: Equivalence of RE and ε-
19
NFA
RE to NFA with ԑ moves Conversion(cont..)
2.2 R1 + R2 ( Parallel ) :
R1
Step 1: S1 F1

R2
S2 F2
Step 2:
R1
Є S1 F1 Є
S3 F3
R2
Є S2 F2 Є
Lecture 8: Equivalence of RE and ε-
20
NFA
RE to NFA with ԑ moves Conversion(cont..)
2.3 R1* (Closure) :
Step 1:
R1
S1 F1
Zero
Step 2: Occurrence
Є
R1 F
S S1 F1
Є Є Є
Single More than
Occurrenc one
e Occurrence
Lecture 8: Equivalence of RE and ε-
21
NFA
RE to NFA with ԑ moves Conversion(cont..)

2.4 R1+ (Closure) :


Step 1:
R1
S1 F1

Step 2:

R1 F
S S1 F1
Є Є
Single Є More than
Occurrenc one
e Occurrence
Lecture 8: Equivalence of RE and ε-
22
NFA
Example 1:
Q. Draw NFA with Є-moves for the RE a. (a + b). b.
Step 1: Draw (a + b)

a
Є Є

b
Є Є

Lecture
Lecture 8:
16:Equivalence
EquivalenceofofRE
REand
andε-ε-NFA
23
NFA
Example 1: (cont..)
Step 2: Concatenate ‘a’ and (a + b)
a
Є Є

a
Є b
Є
Step 3: Concatenate a.(a + b) with b.
a
Є 3 4 Є
1 2 7 8
a b
Є b
5 6 Є
Lecture 8: Equivalence of RE and ε-
24
NFA
Example 2:
Q. Draw NFA with Є-moves for the RE a. (a + b)*.
Step 1: Draw (a + b)

a
Є Є

b
Є Є

Lecture 8: Equivalence of RE and ε-


25
NFA
Example 2: (cont..)
Step 2: Draw (a + b)*
Є

a
Є Є

Є Є
Є b
Є

Lecture 8: Equivalence of RE and ε-


26
NFA
Example 2: (cont..)
Step 3: Concatenate ‘a’ with (a + b)*

a
Є 4 5 Є
1 2 3 8 9
a Є b Є
Є 6 7 Є
Є

Lecture 8: Equivalence of RE and ε-


27
NFA
Example 3:
Q. Draw NFA with Є-moves for the RE (a* + b*).
Step 1: Draw a* and b* as per the steps of conversion

Є
a* 🡪
Є a Є
Є
b* Є
🡪
Є b Є

28
Lecture
Lecture 8:
16:Equivalence
EquivalenceofofRE
REand
andε-ε-NFA Є
NFA
Example 3: (cont..)
Step 2: Draw (a*+b*) i.e. parallel path
Є
Є a Є 5
2 3 4
Є Є Є
1 10
Є
Є Є
Є b Є
6 7 8 9

Є
Lecture 8: Equivalence of RE and ε-
29
NFA
Example 4:
Q. Draw NFA with Є-moves for the RE 1(011)*0.
Step 1: Draw NFA for 011.

0 1 1

Step 2: Draw NFA for (011)*.


Є
Є 0 1 1 Є

Є
Lecture 8: Equivalence of RE and ε-
30
NFA
Example 4: (cont..)
Step 3: Finally draw NFA for 1.(011)*. 0

Є
Є 0 1 1 6 Є 7
1 2 3 4 5 8
1 0
Є

Lecture 8: Equivalence of RE and ε-


31
NFA
Example 5:
Q. Draw NFA with Є-moves for the RE (1 + Є)* + 00.
Step 1: Draw (1 + Є)

1
Є Є

Є
Є Є

Lecture 8: Equivalence of RE and ε-


32
NFA
Example 5: (cont..)
Step 2: Draw (1 + Є)*
Є

1
Є Є

Є Є Є
Є Є

Є
Step 3: Draw NFA for 00.

0 0
Lecture
Lecture 8:
17:Equivalence
Equivalenceof
ofRE
REand
andε-ε-NFA
33
NFA
Example 5: (cont..)
Step 4: Draw parallel path for (1 + Є)* + 00.
Є

Є 4 1 5 Є
Є 2 3 8 9
Є Є Є Є
Є 6 7 Є
1 13
Є
Є Є
0 0
10 11 12

Lecture 8: Equivalence of RE and ε-


34
NFA
Thank You
Lecture 9

Lecture 9:
Equivalence of RE to DFA
Conversion: Indirect Method
NFA with ε- Transitions

• ε-closure of a state
– ε-closure of state ‘q’ is set of states ‘p’ such that
there is a path “from q to p” labelled ‘ε’ directly
or indirectly.
– It is a set of states with distance zero from state
‘q’.
– The state itself is included in its ε-closure.

Lecture 9: Equivalence of RE to DFA


37
Conversion: Indirect Method
Example 1 for ε-closure

ε ε

• ε-closure of A={A, B, C}
• ε-closure of B={B, C}
• ε-closure of C={C}

Lecture 9: Equivalence of RE to DFA


38
Conversion: Indirect Method
Rules for converting NFA with ε- Transitions to NFA without ε

1. Find ε-closure of each state


2. Find transition of ε-closure on each state

3. Again find ε-closure of each transition


4. Repeat steps 1-3 for all states

Note: The states which includes final state of ε-NFA in its ε-closure will
be considered as Final state in converted NFA.

Lecture 9: Equivalence of RE to DFA


39
Conversion: Indirect Method
Example 1 : Convert Given ε – NFA to NFA and then to DFA

ε ε State y= ε- x=Inpu δ(y, x) ε-


closur t closur
e e

A A,B,C 0 A, C A,B,C

State ε-closure A A,B,C 1 B.C B,C

A A,B,C B B,C 0 C C

B B,C B B,C 1 B,C B,C

C C C C 0 C C

C C 1 C C

Lecture 9: Equivalence of RE to DFA


40
Conversion: Indirect Method
Example 1 continued..

Converted NFA
Converted DFA
State 0 1 State 0 1

->A* A,B,C B,C


->A* A,B,C B,C

B* C B,C A,B,C* A,B,C B,C

C* C C B,C* C B,C

C* C C

Lecture 9: Equivalence of RE to DFA


41
Conversion: Indirect Method
Example 1 continued..

Converted DFA

State 0 1

->A* A B,C

B,C* C B,C

C* C C

Lecture 9: Equivalence of RE to DFA


42
Conversion: Indirect Method
Example 2: Convert Given ε – NFA to NFA and then to DFA

State ε-closure State ε-closure

1 1 5 5

2 2,3,7 6 6, 7, 3

3 3 7 7

4 4 8 8
Lecture 9: Equivalence of RE to DFA
43
Conversion: Indirect Method
Example 2 continued..

Stat y=ε- x=Inp δ(y, x) ε- State 0 1


e closure ut closure
1 1 0 - - ->1 - 2,3,7
1 1 1 2 2,3,7
2 4,8 -
2 2,3 7 0 4,8 4,8
2 2,3 7 1 - -
Lecture 9: Equivalence of RE to DFA
44
Conversion: Indirect Method
Example 2 continued..

Stat y=ε- x=Inp δ(y, x) ε- State 0 1


e closure ut closure
3 3 0 4 4 3 4 -
3 3 1 - -
4 - 5
4 4 0 - -
4 4 1 5 5
Lecture 9: Equivalence of RE to DFA
45
Conversion: Indirect Method
Example 2 continued..

Stat y=ε- x=Inp δ(y, x) ε- State 0 1


e closure ut closure
5 5 0 - - 5 - 6,7,3
5 5 1 6 6,7,3
6 4,8 -
6 6,7,3 0 4,8 4,8
6 6,7,3 1 - -
Lecture 9: Equivalence of RE to DFA
46
Conversion: Indirect Method
Example 2 continued..

Stat y=ε- x=Inp δ(y, x) ε- State 0 1


e closure ut closure
7 7 0 8 8 7 8 -
7 7 1 - -
8 - -
8 8 0 - -
8 8 1 - -
Lecture 9: Equivalence of RE to DFA
47 Conversion: Indirect Method
Example 2 continued..

NFA
DFA
State 0 1 State 0 1
->1 - 2.3.7
-->1 - 2,3,7
2 4,8 -
2,3,7 4,8 -
3 4 -
4,8* - 5
4 - 5
5 - 6,7,3
5 - 6,7,3
6,7,3 4,8 -
6 4,8 -
7 8 -
8* - -
Lecture 9: Equivalence of RE to DFA
48
Conversion: Indirect Method
Example 2 continued..

Minimized DFA
State 0 1
State 0 1
-->1 - 2,3,7
-->1 - 2,3,7
2,3,7 4,8 -
2,3,7 4,8 -
4,8* - 5
4,8* - 5
5 - 6,7,3
5 - 2,3,7
6,7,3 4,8 -

Lecture 9: Equivalence of RE to DFA


49
Conversion: Indirect Method
Example 2 continued..

Minimized DFA
State 0 1

-->1 - 2,3,7
2,3,7 4,8 -
4,8* - 1

Lecture 9: Equivalence of RE to DFA


50
Conversion: Indirect Method
Thank You
Practice Problems
Regular Expression Examples
1. Define the language such that all words begin and end
with ‘a’ and in between any word using ‘b’.
🡪 ab*a

2. Consider the language (a*b*) express it in English.


🡪 It is a combination of a* and b* i.e. string containing only a’s
followed by a string containing only b’s. Hence, language will
be all strings in which any number of a’s are followed by any
number of b’s.

3. Write regular expression for the language any string in


‘a’ or any string in ‘c’ followed by any string in ‘b’.
🡪 ( a* + c* ) b*

53 Practice Problems
Regular Expression Examples(cont..)
4. Describe ( a ( aa )* b ( bb )* )*
🡪String of odd number of consecutive a’s followed by string of odd
number of consecutive b’s.
🡪 It is alternating sequence of odd a’s and odd b’s, starting with a
ending with b.

5. Define the language such that all words that starts with and
ends with different letter over ∑= {x,y}.
🡪 RE= x (x+y)* y + y (x+y)* x

6. Describe in simple English the language represented by the


regular expression RE = (1 + 10)*
🡪L(r) can be described as a language over ∑= {0,1} having strings
beginning with ‘1’ and not having two consecutive 0’s.

54 Practice Problems
Regular Expression Examples(cont..)
7. Represent the language over ∑= {0, 1} containing all possible
combinations of 0’s and 1’s but not having two consecutive 0’s.
🡪Language containing strings that start with ‘1’ and do not have two
consecutive 0’s is represented by the RE , R1= (1 + 10)*
🡪Similarly the language containing strings that start with 0 and do not
have 2 consecutive 0’s can be represented as, R2= 0.(1 + 10)*.
🡪Combining two RE we get, R= (1 + 10)* + 0.(1 + 10)*

8. Define the language over ∑= {0, 1, 2} such that every string from
the language contains any number of 0’s followed by any number of
1’s followed by any number of 2’s.
🡪 Any number of 0’s means zero or more occurrences of 0’s. It is denoted
by 0*. Similar thing for input 1 and 2 also.
🡪 RE= 0*1*2*

55 Practice Problems
Regular Expression Equivalence
9. Show that (a. b)* ≠ a*.b*.
Solution :
Let R1 = (a. b)* and R2 = a*.b*
L(R1) = { Є, ab, abab, ababab,……} ------- (1)
L(R2) = L(u). L(v) where u = a* and v = b*
= { Є, a, aa, aaa,……}.{ Є, b, bb, bbb,……}
= { Є, a, b, aa, bb, ab, abb, aaa, bbb……} ------- (2)
Comparing equation (1) and (2) we can say,
(a. b)* ≠ a*.b*

56 Practice Problems
Regular Expression Equivalence (cont..)
10. Show that (a + b)* = (a + b)* + (a + b)*
Solution :
Let R1 = (a + b)* and R2 = (a + b)* + (a + b)*
L(R1) = { Є, a, b, aa, ab, bb, ba ….……} ------- (1)
L(R2) = L(u) U L(v) where u = (a + b)* and v = (a + b)*
= { Є, a, b, aa, ab, bb, ba ….……} U { Є, a, b, aa, ab, bb, ba ….……}
= { Є, a, b, aa, ab, bb, ba ….……} ------- (2)
Comparing equation (1) and (2) we can say,
(a + b)* = (a + b)* + (a + b)*

57 Practice Problems
Regular Expression Equivalence (cont..)
11. Show that (a* b*)* = (a + b)*
Solution :
Let R1 = (a* b*)*
L(R1) = {{ Є, a, aa, aaa, aaaa ….……}.{ Є, b, bb, bbb, bbbb ….……}}*
= { Є, a, b, aa, bb, ….……} *
= { Є, a, b, aa, bb, ab, ba ….……} ------- (1)
Let R2 = (a + b)*
L(R2) = { Є, a, b, aa, ab, bb, ba ….……} ------- (2)
Comparing equation (1) and (2) we can say,
(a* b*)* = (a + b)*

58 Practice Problems
Example 12:
Q. Draw NFA with Є-moves for the RE (11 + 01)*.
Step 1: Draw NFA for RE=11 .

1 1

Step 2: Draw NFA for RE=01 .

0 1

59 Practice Problems
Example 12: (cont..)
Step 3: Draw parallel path for 11 and 01 i.e.
(11 + 01).
1 1
Є Є

Є 0 1 Є

60 Practice Problems
Example 12: (cont..)
Step 4: Finally draw repetition for (11+01)
i.e. (11+01)*
Є

3 1 4 1 5
Є Є
1 2 9 10
Є Є
Є 0 1 Є
6 7 8

61 Practice Problems
Thank You

You might also like