Professional Documents
Culture Documents
Lecture 6 Regular Expressions
Lecture 6 Regular Expressions
Lecture 6 Regular Expressions
• 𝑹+ ≔ 𝑹𝑹∗
Basically 𝑅∗ but without the “zero concatenation” case
e.g. 1+ , the language with strings of one or more concatenations of 𝟏
• 𝑹∪∅=𝑹
• 𝑹𝜺 = 𝜺𝑹 = 𝑹
• 𝑹∅ = ∅
Language Recognized by a Regular Expression
• If 𝑹 = 𝜺, then 𝑳 𝑹 = 𝜺
• If 𝑹 = ∅, then 𝑳 𝑹 = ∅
• If 𝑹 = 𝑹𝟏 ∪ 𝑹𝟐 , then 𝑳 𝑹 = 𝑳 𝑹𝟏 ∪ 𝑳 𝑹𝟐
• If 𝑹 = (𝑹𝟏 𝑹𝟐 ), then 𝑳 𝑹 = 𝑳 𝑹𝟏 𝑳 𝑹𝟐
• If 𝑹 = (𝑹𝟏∗ ), then 𝑳 𝑹 = 𝑳 𝑹𝟏 ∗
Regular Expression Examples
Describe the languages of the following regular expressions:
Let 𝚺 = 𝒂, 𝒃 :
Language of regular expressions:
• 𝑳 𝒂 = 𝒂
• 𝑳 𝒂 ∪ 𝒃 = 𝑳 𝒂 ∪ 𝑳 𝒃 = 𝒂 ∪ 𝒃 = 𝒂, 𝒃 • 𝑳 𝜺 = 𝜺
• 𝑳 ∅ =∅
• 𝑳 𝒂 𝒂∪𝒃 = 𝑳 𝒂 𝑳 𝒂 ∪ 𝒃 = 𝒂 𝒂, 𝒃 = 𝒂𝒂, 𝒂𝒃 • 𝑳 𝑹𝟏 ∪ 𝑹 𝟐 = 𝑳 𝑹𝟏 ∪ 𝑳 𝑹𝟐
• 𝑳 𝑹𝟏 𝑹𝟐 = 𝑳 𝑹𝟏 𝑳 𝑹𝟐
• 𝑳 𝑹∗ = 𝑳 𝑹 ∗
• 𝑳 𝒂∪𝒃 ∗ =𝑳 𝒂∪𝒃 ∗ = 𝒂, 𝒃 ∗ = 𝚺∗
Theorem: A language is regular if and only if there exists some regular expression
that describes it
Case 1: 𝑹 = 𝒂 , 𝒂 ∈ 𝚺
𝒂
𝑞1 𝑞2
• 𝜹 𝒒𝟏 , 𝒂 = 𝒒𝟐
Case 2: 𝑹 = 𝜺
𝑞1
Case 3: 𝑹 = ∅
𝑞1
• NFA 𝑵 = 𝑞1 , Σ, 𝛿, 𝑞1 , ∅ with
Case 4: 𝑹 = 𝑹𝟏 ∪ 𝑹𝟐
𝑴𝟏 𝑴
𝜺
𝑴𝟐
𝜺
Regular Expression to NFA
Case 5: 𝑹 = 𝑹𝟏 𝑹𝟐
𝑴𝟏 𝑴
𝜺
𝑴𝟐
Regular Expression to NFA
Case 6: 𝑹 = 𝑹𝟏∗
𝑴𝟏 𝑴 𝜺
𝜺
Equivalence of Regular Expressions
𝑴 0
1
0
1 𝑹
0 1
1
0
Generalized NFA (GNFA)
A GNFA 𝑮 is almost like an NFA except:
• 𝑮 has exactly one start state
• 𝑮 has exactly one accept state
𝑞0 𝑏 𝑞1 𝜀 𝑞0 𝑏 𝑞1 𝜀
𝑠 𝑓
• Finally, the only states left are 𝒔 and 𝒇 with a single regular expression 𝑹 transition
𝑮 𝑎 𝑎∪𝑏
𝑠
𝜀 𝑞0 𝑏 𝑞2 𝜀
𝑓 𝑠
𝑹 𝑓
Removing a State from GNFA
Remove states from 𝑮 (not 𝒔 or 𝒇) one by one, while not changing the language
• Consider state to be removed: 𝒒𝐫𝐢𝐩
• When removed, transitions through 𝒒𝐫𝐢𝐩 must be replaced
𝑹𝟐
𝑹𝟏 𝑹𝟑
𝒒𝒊 𝒒𝐫𝐢𝐩 𝒒𝒋
𝑹𝟒
• The strings obtained from some 𝒒𝒊 going through 𝒒𝐫𝐢𝐩 to some 𝒒𝒋 look like:
𝑹𝟏 , concatenated with zero or more repeats of 𝑹𝟐 , concatenated with 𝑹𝟑
• Note: even though conversion says for each 𝒒𝒊 , 𝒒𝒋 ∈ 𝑸′, we really only have to do
this for each 𝒒𝒊 , 𝒒𝒋 where 𝒒𝒊 can get to 𝒒𝒋 through 𝒒𝐫𝐢𝐩
GNFA to Regular Expression Example
𝑹𝟐
∗
𝑹𝟏 𝑹𝟑 𝑹𝟏 𝑹𝟐 𝑹𝟑 ∪ 𝑹𝟒
𝒒𝒊 𝒒𝐫𝐢𝐩 𝒒𝒋 𝒒𝒊 𝒒𝒋
𝑹𝟒
𝑮 𝑎 𝑎∪𝑏
𝜀 𝑞0 𝑏 𝑞1 𝜀 𝒒𝐫𝐢𝐩 = 𝑞1
𝑠 𝑓
𝑮′ 𝑎
𝑏 𝑎∪𝑏 ∗
𝜀 𝑞0
𝑠 𝑓
GNFA to Regular Expression Example
𝑹𝟐
∗
𝑹𝟏 𝑹𝟑 𝑹𝟏 𝑹𝟐 𝑹𝟑 ∪ 𝑹𝟒
𝒒𝒊 𝒒𝐫𝐢𝐩 𝒒𝒋 𝒒𝒊 𝒒𝒋
𝑹𝟒
𝑮 𝑎
𝑏 𝑎∪𝑏 ∗
𝜀 𝑞0 𝒒𝐫𝐢𝐩 = 𝑞0
𝑠 𝑓
𝑮′
𝑎∗ 𝑏 𝑎 ∪ 𝑏 ∗
𝑠 𝑓
DFA to GNFA to Regular Expression Example
𝑎 𝑎, 𝑏
𝑏
DFA 𝑴 𝑞0 𝑞1
𝑎 𝑎∪𝑏
𝜀 𝑏 𝜀
GNFA 𝑮 𝑠 𝑞0 𝑞1 𝑓
𝑎∗ 𝑏 𝑎 ∪ 𝑏 ∗
Simplified GNFA 𝑮 𝑠 𝑓
Regular Expression 𝑹 𝒂∗ 𝒃 𝒂 ∪ 𝒃 ∗
Equivalence of Regular Expressions
Theorem: A language is regular if and only if there exists some regular expression
that describes it
𝑏
𝑎 𝑏 𝑏
𝑞1 𝑞3
𝑎
𝜀 𝑎
𝜀 𝜀
GNFA 𝑮 𝑠 𝑞0 𝑓
𝑏 𝜀 𝑎 𝑎
𝑏
(∅-transitions are omitted) 𝑞2 𝑞4
𝑏
DFA to Regular Expression Example 2
Describe the language recognized by the DFA 𝑴 as a regular expression 𝑹
𝑎 𝑏 𝑏
𝑞1 𝑞3 𝒒𝐫𝐢𝐩 = 𝑞3
𝑎
𝜀 𝑎
𝜀 𝜀
GNFA 𝑮 𝑠 𝑞0 𝑓
𝑏 𝜀 𝑎 𝑎
𝑏
(∅-transitions are omitted) 𝑞2 𝑞4
𝑏
(𝑏 𝑏)∗ 𝑎 ∪ 𝑎
𝑞1
𝑎
𝜀 𝜀 𝜀
GNFA 𝑮′ 𝑠 𝑞0 𝑓
𝑏 𝜀 𝑎 𝑎
𝑏
(∅-transitions are omitted) 𝑞2 𝑞4
𝑏
DFA to Regular Expression Example 2
Describe the language recognized by the DFA 𝑴 as a regular expression 𝑹
(𝑏 𝑏)∗ 𝑎 ∪ 𝑎
𝑞1
𝑎
𝜀 𝜀 𝜀
GNFA 𝑮 𝑠 𝑞0 𝑓
𝑏 𝜀 𝑎 𝑎
𝑏
(∅-transitions are omitted) 𝑞2 𝑞4