Course 6
Regular Expressions (part 2)
The structure and the content of the lecture is based on http://www.eecs.wsu.edu/~ananth/CptS317/Lectures/index.htm
1
Building Regular Expressions
Let E be a regular expression (r.e.) and the
language represented by E is L(E)
Then:
(E) = E
L(E + F) = L(E) U L(F) (+ can be replaced with |)
L(E F) = L(E) L(F) (the operator between E,F, resp L(E),
L(F) can be replaced with •)
L(E*) = (L(E))*
2
Examples
3.
𝑅 = (𝑎|𝑏). Then L(𝑅) = {𝑎} ∪ {𝑏}= {𝑎, 𝑏}
𝑅 = (𝑎𝑏). Then L 𝑅 = 𝑎𝑏
𝑅 = 𝑎(𝑏|𝑎). Then L 𝑅 = 𝑎 𝑏, 𝑎 = 𝑎𝑏, 𝑎𝑎
4.
𝑅 = 𝑎∗ . Then L 𝑅 =
1.
2.
5.
6.
7.
8.
9.
𝑅
𝑅
𝑅
𝑅
𝑅
∞
𝑗
𝑗=0{𝑎 }
= {𝜆, 𝑎, 𝑎2 , … } = {𝑤 ∈ {𝑎∗ }} = {𝑎𝑛 |𝑛 ≥ 0}
= (𝑎|𝑏)∗ . Then L 𝑅 = (𝐿𝑎 ∪ 𝐿𝑏 )∗ =({𝑎} ∪ {𝑏})∗ =({𝑎, 𝑏})∗
= 𝑎(𝑎|𝑏)∗ . Then L 𝑅 = 𝑎({𝑎, 𝑏})∗ = 𝑎𝑤 𝑤 ∈ 𝑎, 𝑏 ∗
= (𝑏|𝑎)∗ 𝑎. Then L 𝑅 = ({𝑏, 𝑎})∗ 𝑎 = 𝑤𝑎 𝑤 ∈ 𝑎, 𝑏 ∗
= 𝑎(𝑎|𝑏|𝑐)∗ 𝑐. Then L 𝑅 = 𝑎𝑤𝑐 𝑤 ∈ 𝑎, 𝑏, 𝑐 ∗
= 𝑎(𝑎|𝑏)(𝑎|𝑏|𝑐)∗ . Then L 𝑅 = 𝑎𝑎𝑤, 𝑎𝑏𝑤 𝑤 ∈ 𝑎, 𝑏, 𝑐 ∗ = {𝑤 ∈
3
Examples
1.
2.
3.
4.
5.
6.
L R = 𝑤 𝑒𝑛𝑑𝑠 𝑤𝑖𝑡ℎ 𝑏 𝑎𝑛𝑑 𝑐𝑜𝑛𝑡𝑎𝑖𝑛𝑠 𝑎𝑡 𝑙𝑒𝑎𝑠𝑡 𝑜𝑛𝑒 𝑎 . 𝑅 = 𝑎 𝑏 ∗ 𝑎 𝑎 𝑏 ∗ 𝑏
L R = 𝑤 𝑤𝑜𝑟𝑑 𝑜𝑓 𝑎′ 𝑠 𝑎𝑛𝑑 𝑏 ′ 𝑠 𝑜𝑑𝑑 𝑙𝑒𝑛𝑔𝑡ℎ . 𝑅 = ( 𝑎 𝑏 (𝑎|𝑏))∗
L R = 𝑤 𝑤𝑜𝑟𝑑 𝑜𝑓 𝑎′ 𝑠 𝑎𝑛𝑑 𝑏 ′ 𝑠 𝑤𝑖𝑡ℎ 𝑒𝑣𝑒𝑛 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑏′ 𝑠 . 𝑅 =
𝑎∗ (𝑎∗ 𝑏𝑎∗ 𝑏𝑎∗ )∗
L R = 𝑤 ∈ 𝑎, 𝑏 , 𝑤 𝑒𝑛𝑑𝑠 𝑤𝑖𝑡ℎ 𝑎𝑎 𝑜𝑟 𝑏𝑏 . 𝑅 = 𝑎 𝑏 ∗ (𝑎𝑎|𝑏𝑏)
L R = 𝑤 ∈ 1,0 ∗ , 𝑤 ℎ𝑎𝑠 𝑎𝑙𝑡𝑒𝑟𝑛𝑎𝑡𝑖𝑛𝑔 0′ 𝑠 𝑎𝑛𝑑 1′ 𝑠 . 𝑅 =
(10)∗ | 01 ∗ |0(10)∗ |1 01 ∗
L R = 𝑤 ∈ 𝑎, 𝑏 , 𝑤 ≡ 1 𝑚𝑜𝑑 4 . 𝑅 =
𝑎𝑏 𝑎𝑏 𝑎𝑏 𝑎𝑏
∗
(𝑎|𝑏)
4
Precedence of Operators
Highest to lowest
* operator (star)
. (concatenation)
+ operator
Example:
01* + 1
=
( 0 . ((1)*) ) + 1
5
Finite Automata (FA) & Regular
Expressions
To show that they are interchangeable,
consider the following theorems:
Proofs
in the book
Theorem 1: For every DFA A there exists a r.e. R
such that L(R)=L(A)
Theorem 2: For every r. E. R there exists an NFA E such that L(E)=L(R)
-NFA
NFA
Kleene Theorem
Theorem 2
Reg Ex
DFA
Theorem 1
6
DFA
Theorem 1
Reg Ex
DFA to RE construction
Informally, trace all distinct paths (traversing cycles only once)
from the start state to each of the final states
and enumerate all the expressions along the way
Example:
1
q0
0
(1*) 0
1*
0,1
0
q1
1
q2
(0*) 1
(0 + 1)*
1
(0+1)*
00*
Q) What is the language?
1*00*1(0+1)*
7
Reg Ex
Theorem 2
-NFA
RE to -NFA construction
Given a r.e., we can always built an -NFA recognizing L(r.e.) using
the following diagrams.
a
-NFA recogn. word. a
-NFA recogn. lang.
R
R
S
-NFA recogn. lang. R|S; R,S – r.e.
-NFA recogn. lang. ∅
S
-NFA recogn. lang. RS; R,S – r.e.
R
-NFA recogn. lang. R*; R– r.e.
8
Reg Ex
-NFA
Theorem 2
RE to -NFA construction
(0+1)*01(0+1)*
Example:
(0+1)*
01
(0+1)*
0
1
0
1
0
1
9
Other examples
Construct the -NFA for the following r.e.
Then transform them into NFA or DFA.
1.
2.
3.
4.
(𝑎∗ 𝑏)|𝑐 ∗
𝑎𝑏𝑐
𝑖𝑜|𝑚𝑎
𝑎 𝑏 𝑏∗
10
Summary
Regular expressions
Equivalence to finite automata
DFA to regular expression conversion
Regular expression to -NFA
conversion
11
© Copyright 2026 Paperzz