SlideShare a Scribd company logo
1 of 26
Regular Expressions
CSE 2233
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Regular Languages
Regular Language:
– A language is called a regular language if some finite automaton(DFA) recognizes it.
– A language is regular if and only if some nondeterministic finite automaton(NFA) recognizes it.
– If a language is described by a regular expression(RE), then it is also regular.
2
NFA
DFA
RE
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Regular Operations
▸ 3 operations are defined on languages called Regular Operations.
▸ Here, the name Regular Operations is just a name that has been chosen for these 3 operations.
▸ Regular Operations are used to study properties of the Regular Languages.
Definition:
Let Σ be an alphabet and A,B ⊆ Σ* be languages. Then the regular operations are as follows:
 Union: The language A⋃B ⊆ Σ* is defined as, A⋃B = { w | w ∈ A or w ∈ B }
 Concatenation: The language AB ⊆ Σ* is defined as, AB = { wx | w ∈ A and x ∈ B }
 Kleene star/ Star: The language A* is defined as, A* = { x1x2 … … xk | k≥0 and each xi ∈ A }
3
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Regular Operations - Example
Let,
Alphabet Σ = { a, b, c, … … … , z }
Language A = { good, bad } ⊆ Σ*
Language B = { boy, girl } ⊆ Σ*
Then,
A⋃B = { good, bad, boy, girl }
AB = { goodboy, goodgirl, badboy, badgirl }
A* = { 𝜀, good, bad, goodgood, goodbad, badgood, badbad, goodgoodgood, goodgoodbad, … … }
4
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Regular Languages – Closure Property
▸ The regular languages are closed with respect to the regular operations:
if A, B ⊆ Σ* are regular languages, then the languages A⋃B, AB, and A* are also regular.
▸ There are many other operations on languages aside from the regular operations under which the
regular languages are also closed.
 Subtraction:
The language AB ⊆ Σ* is defined as, AB = { w | w ∈ A and w ∉ B } = A⋂ഥB
 Complement:
The language ഥA ⊆ Σ* is defined as, ഥA = { w | w ∈ Σ* and w ∉ A } = Σ*A
 Intersection:
The language A⋂B ⊆ ∑* is defined as, A⋂B = { w | w ∈ A and w ∈ B } = ҧ𝐴 ∪ ത𝐵
 Reverse:
The language AR ⊆ Σ* is defined as, AR = { wk wk-1 … … w2 w1 | w=w1w2 … … wk ∈ A }
5
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Regular Expressions
Arithmetic Expressions:
▸ We use the operations + and x to build up expressions such as (5+3)x4
▸ The value of arithmetic expression is the number 32.
Regular Expression:
▸ We use regular operations to build up expressions describing languages called Regular Expressions.
▸ The value of regular expression is a language.
▸ For example, (0⋃1)0*
Note:
REs have an important role in computer science applications. It provides powerful methods to describe
different string patterns in UNIX commands, modern programming languages, text editors etc.
6
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Regular Expressions – Definition
Regular Expression:
R is a regular expression if R is
▸ a for some a in the alphabet Σ,
▸ 𝜀,
▸ 𝜙,
▸ (R1 ⋃ R2), where R1 and R2 are regular expressions,
▸ (R1 o R2), where R1 and R2 are regular expressions, or
▸ (𝑅1
∗
), where R1 is a regular expression.
7
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Regular Expressions – Precedence Order
8
Precedence of Regular Operations:
1. Parentheses
2. Star
3. Concatenation
4. Union
So according to the precedence sequence: (ab U a)* = ( ( (a) (b) ) U ( a ) ) *
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
RE – Practices
9
Language, L(R) Regular Expression
{ w | w contains a single 1 } 0* 1 0*
{ w | w consists of exactly three 1’s } 0* 1 0* 1 0* 1 0*
{ w | w has at least a single 1 } (0 | 1)* 1 (0 | 1)*
{ w | w contains at least three 1’s } (0 | 1)* 1 (0 | 1)* 1 (0 | 1)* 1 (0 | 1)*
{ w | w has at most one 1 } 0* | 0* 1 0*
{ w | w contains an even number of 1’s } (0* 1 0* 1 0*)* 0*
{ w | the number of 1’s withing w can be evenly divided by 3 } (0* 1 0* 1 0* 1 0* )* 0*
{ w | w is a string of even length } (ΣΣ)*, here Σ = (0 | 1)
{ w | the length of w is a multiple of 3 } (ΣΣΣ)*, here Σ = (0 | 1)
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
RE – Practices
10
Language, L(R) Regular Expression
{ w | w starts with 101 } 101 (0 | 1)*
{ w | w contains the string 001 as a substring } (0 | 1)* 001 (0 | 1)*
{ w | w ends with 3 consecutive 1’s } (0 | 1)* 111
{ w | w doesn’t end with 11 } 𝜀 | 0 | 1 | (0 | 1)* (00 | 01 | 10)
{ w | w ends with an even nonzero number of 0’s } ( (0 | 1)* 1 | 𝜀 ) 00 (00)*
{ w | w starts and ends with the same symbol } 0 | 1 | 0Σ*0 | 1Σ*1, here Σ = (0 | 1)
{ w | w has at least 3 characters and the 3rd character is 0 } (0 | 1) (0 | 1) 0 (0 | 1)*
{ w | every zero in w is followed by at least one 1 } 1* (01+)*
{ w | w consists of alternating zeros and ones } (1 | 𝜀) (01)* (0 | 𝜀)
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
RE – Practices
11
Language, L(R) Regular Expression
{ 01, 10 } 01 | 10
01* | 1* (0 | 𝜀) 1*
{ 𝜀, 0, 1, 01 } (0 | 𝜀) (1 | 𝜀)
𝜙 R𝜙
{ 𝜀 } 𝜙*
{ w | w starts with 0 and has odd length or, starts with
1 and has even length }
0 ( (0 | 1) (0 | 1) )* | 1 (0 | 1) ( (0 | 1) (0 | 1) )*
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
RE  NFA
▸ R = a for some a ∈ Σ. Then L(R) = { a }, and the following NFA recognizes L(R)
▸ R = 𝜀 . Then L(R) = { 𝜀 } and the following NFA recognizes L(R)
▸ R = 𝜙 . Then L(R) = 𝜙, and the following NFA recognizes L(R)
▸ R = R1 U R2 , so L(R) = L(R1) U L(R2) = NFA1 recognizes L(R1) U NFA2 recognizes L(R2)
▸ R = R1 o R2 = R1R2 = = L(R1) o L(R2) = NFA1 recognizes L(R1) o NFA2 recognizes L(R2)
▸ R = R1
* = L(R1)* = ( NFA1 recognizes L(R1) )*
12
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
RE  NFA : Example 1
Convert the following RE to equivalent NFA:
(ab U a)*
13
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
RE  NFA : Practices
14
▸ ( a U b ) * a b a
▸ ( 0 U 1 ) * 0 0 0 ( 0 U 1 )* = Σ* 0 0 0 Σ* where Σ = 0, 1 = 0 U 1
▸ ( ( ( 0 0 )* ( 1 1 ) ) U 0 1 ) *
▸ ( 0 1 U 0 0 1 U 0 1 0 ) *
▸ a ( a b b ) * U b
▸ a+ U ( a b ) +
▸ ( a U b+ ) a+ b+ [ Hint: replace a+ with (a a*) ]
▸ 1* ( 0 1+)*
▸ ( Σ Σ Σ ) *
▸ 0 Σ* 0 U 1 Σ* 1 U 0 U 1
▸ 𝜙*
▸ ( 0 U 𝜀 ) 1*
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
DFA  GNFA  RE
15
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Steps to follow:
GNFA
▸ A GNFA is a nondeterministic finite automaton(NFA) in which each
transition is labeled with a regular expression over the alphabet set
instead of only members of the alphabet or 𝜀.
▸ A single initial state with all possible outgoing transitions and no
incoming transitions.
▸ A single final state without outgoing transitions but all possible
incoming transitions. The accept state is not the same as the start
state.
▸ A single transition between every two states, including self loops.
16
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
GNFA – Formal Definition
A generalized nondeterministic finite automaton is a 5 tuple,
(Q, Σ, 𝛿, qstart, qaccept ), where
▸ Q is the finite set of states
▸ Σ is the input alphabet
▸ 𝛿: ( Q - {qaccept} ) x ( Q - {qstart} )  ℛ is the transition function
[if you travel from state a 𝜖 ( Q - {qaccept} ) to state b 𝜖 ( Q - {qstart} ) then
this relation will generate ℛ regular expression ]
▸ qstart is the start state and
▸ qaccept is the accept state
17
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
GNFA – State Minimization Rule
18
Removing qrip state,
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
DFA  GNFA : Steps to Follow
19
▸ Add a new start state with an 𝜀 arrow to the old start state
▸ Add a new accept state with 𝜀 arrows from the old accept states to this new accept state
▸ If any arrows have multiple labels, replace each with a single arrow whose label is the union of the
previous labels
▸ Add arrows labeled 𝜙 between states that had no arrows. [better no to show this in the diagram, for
simplicity]
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CONVERT(G):
1. Let k be the number of states of G.
2. If k=2, then G must consist of a start state, an accept state, and a single arrow connecting them and
labeled with a regular expression R
— Return the expression R.
3. If k>2, we select any state qrip ∈ Q different from qstart and qaccept and let G’ be the GNFA (Q’, Σ, 𝛿’, qstart,
qaccept), where
Q’ = Q - {qrip}
and for any qi ∈ Q’–{qaccept} and any qj ∈ Q’–{qstart}, let
𝛿’(qi, qj) = (R1) (R2)*(R3) U (R4)
for R1 = 𝛿(qi, qrip), R2 = 𝛿(qrip, qrip), R3 = 𝛿(qrip, qj), and R4 = 𝛿(qi, qj)
4. Compute CONVERT(G’) and return this value.
GNFA  RE : Algorithm
20
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
GNFA  RE : Removing State 2
21
Here, 2 is associated with states 1 and a. All possible combinations are (1,1), (1,a), (a,a), (a,1). But there
will be no outgoing from a, so no need to consider (a,a) and (a,1) states.
For (1,1), R = a U (b (aUb)* 𝜙)) = a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
GNFA  RE : Removing State 2
22
Here, 2 is associated with states 1 and a. All possible combinations are (1,1), (1,a), (a,a), (a,1). But there
will be no outgoing from a, so no need to consider (a,a) and (a,1) states.
For (1,a), R = 𝜙 U (b (aUb)* 𝜀) = b(aUb)*
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
GNFA  RE : Removing State 1
23
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
DFA  GNFA  RE : Example 2
24
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
DFA  GNFA  RE : Practices
25
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Practice 1 Practice 2
26
THANKS!
Any questions?
You can find me at imam@cse.uiu.ac.bd
References:
Chapter 1(section 1.3), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser

More Related Content

What's hot

What's hot (20)

Push down automata
Push down automataPush down automata
Push down automata
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Lecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdfLecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdf
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFATOC 3 | Different Operations on DFA
TOC 3 | Different Operations on DFA
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
push down automata
push down automatapush down automata
push down automata
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02
 
3.4 deterministic pda
3.4 deterministic pda3.4 deterministic pda
3.4 deterministic pda
 
AI 1 | Introduction to Artificial Intelligence
AI 1 | Introduction to Artificial IntelligenceAI 1 | Introduction to Artificial Intelligence
AI 1 | Introduction to Artificial Intelligence
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
AI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction ProblemAI 7 | Constraint Satisfaction Problem
AI 7 | Constraint Satisfaction Problem
 

Similar to TOC 5 | Regular Expressions

Similar to TOC 5 | Regular Expressions (20)

TOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite AutomataTOC 4 | Non-deterministic Finite Automata
TOC 4 | Non-deterministic Finite Automata
 
Matrix Transformations on Paranormed Sequence Spaces Related To De La Vallée-...
Matrix Transformations on Paranormed Sequence Spaces Related To De La Vallée-...Matrix Transformations on Paranormed Sequence Spaces Related To De La Vallée-...
Matrix Transformations on Paranormed Sequence Spaces Related To De La Vallée-...
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
05 8640 (update email) multiset cs closure propertie (edit lafi)2
05 8640 (update email) multiset cs closure propertie (edit lafi)205 8640 (update email) multiset cs closure propertie (edit lafi)2
05 8640 (update email) multiset cs closure propertie (edit lafi)2
 
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix MappingDual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 
Automata
AutomataAutomata
Automata
 
Automata
AutomataAutomata
Automata
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
1LECTURE 8 Regular_Expressions.ppt
1LECTURE 8 Regular_Expressions.ppt1LECTURE 8 Regular_Expressions.ppt
1LECTURE 8 Regular_Expressions.ppt
 
draft
draftdraft
draft
 
A Fifth-Order Iterative Method for Solving Nonlinear Equations
A Fifth-Order Iterative Method for Solving Nonlinear EquationsA Fifth-Order Iterative Method for Solving Nonlinear Equations
A Fifth-Order Iterative Method for Solving Nonlinear Equations
 
Chapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdfChapter 3 REGULAR EXPRESSION.pdf
Chapter 3 REGULAR EXPRESSION.pdf
 
Matrix Transformations on Some Difference Sequence Spaces
Matrix Transformations on Some Difference Sequence SpacesMatrix Transformations on Some Difference Sequence Spaces
Matrix Transformations on Some Difference Sequence Spaces
 
Fuzzy random variables and Kolomogrov’s important results
Fuzzy random variables and Kolomogrov’s important resultsFuzzy random variables and Kolomogrov’s important results
Fuzzy random variables and Kolomogrov’s important results
 
Statistical machine translation
Statistical machine translationStatistical machine translation
Statistical machine translation
 
lec13.ppt
lec13.pptlec13.ppt
lec13.ppt
 
5. Rania.pdf
5. Rania.pdf5. Rania.pdf
5. Rania.pdf
 
5. Rania.pdf
5. Rania.pdf5. Rania.pdf
5. Rania.pdf
 
Conformal Boundary conditions
Conformal Boundary conditionsConformal Boundary conditions
Conformal Boundary conditions
 

More from Mohammad Imam Hossain

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
DBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related QueriesDBMS 6 | MySQL Practice List - Rank Related Queries
DBMS 6 | MySQL Practice List - Rank Related Queries
 
DBMS 9 | Extendible Hashing
DBMS 9 | Extendible HashingDBMS 9 | Extendible Hashing
DBMS 9 | Extendible Hashing
 
Web 6 | JavaScript DOM
Web 6 | JavaScript DOMWeb 6 | JavaScript DOM
Web 6 | JavaScript DOM
 
Web 5 | JavaScript Events
Web 5 | JavaScript EventsWeb 5 | JavaScript Events
Web 5 | JavaScript Events
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

TOC 5 | Regular Expressions

  • 1. Regular Expressions CSE 2233 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 2. Regular Languages Regular Language: – A language is called a regular language if some finite automaton(DFA) recognizes it. – A language is regular if and only if some nondeterministic finite automaton(NFA) recognizes it. – If a language is described by a regular expression(RE), then it is also regular. 2 NFA DFA RE Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 3. Regular Operations ▸ 3 operations are defined on languages called Regular Operations. ▸ Here, the name Regular Operations is just a name that has been chosen for these 3 operations. ▸ Regular Operations are used to study properties of the Regular Languages. Definition: Let Σ be an alphabet and A,B ⊆ Σ* be languages. Then the regular operations are as follows:  Union: The language A⋃B ⊆ Σ* is defined as, A⋃B = { w | w ∈ A or w ∈ B }  Concatenation: The language AB ⊆ Σ* is defined as, AB = { wx | w ∈ A and x ∈ B }  Kleene star/ Star: The language A* is defined as, A* = { x1x2 … … xk | k≥0 and each xi ∈ A } 3 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 4. Regular Operations - Example Let, Alphabet Σ = { a, b, c, … … … , z } Language A = { good, bad } ⊆ Σ* Language B = { boy, girl } ⊆ Σ* Then, A⋃B = { good, bad, boy, girl } AB = { goodboy, goodgirl, badboy, badgirl } A* = { 𝜀, good, bad, goodgood, goodbad, badgood, badbad, goodgoodgood, goodgoodbad, … … } 4 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 5. Regular Languages – Closure Property ▸ The regular languages are closed with respect to the regular operations: if A, B ⊆ Σ* are regular languages, then the languages A⋃B, AB, and A* are also regular. ▸ There are many other operations on languages aside from the regular operations under which the regular languages are also closed.  Subtraction: The language AB ⊆ Σ* is defined as, AB = { w | w ∈ A and w ∉ B } = A⋂ഥB  Complement: The language ഥA ⊆ Σ* is defined as, ഥA = { w | w ∈ Σ* and w ∉ A } = Σ*A  Intersection: The language A⋂B ⊆ ∑* is defined as, A⋂B = { w | w ∈ A and w ∈ B } = ҧ𝐴 ∪ ത𝐵  Reverse: The language AR ⊆ Σ* is defined as, AR = { wk wk-1 … … w2 w1 | w=w1w2 … … wk ∈ A } 5 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 6. Regular Expressions Arithmetic Expressions: ▸ We use the operations + and x to build up expressions such as (5+3)x4 ▸ The value of arithmetic expression is the number 32. Regular Expression: ▸ We use regular operations to build up expressions describing languages called Regular Expressions. ▸ The value of regular expression is a language. ▸ For example, (0⋃1)0* Note: REs have an important role in computer science applications. It provides powerful methods to describe different string patterns in UNIX commands, modern programming languages, text editors etc. 6 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 7. Regular Expressions – Definition Regular Expression: R is a regular expression if R is ▸ a for some a in the alphabet Σ, ▸ 𝜀, ▸ 𝜙, ▸ (R1 ⋃ R2), where R1 and R2 are regular expressions, ▸ (R1 o R2), where R1 and R2 are regular expressions, or ▸ (𝑅1 ∗ ), where R1 is a regular expression. 7 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 8. Regular Expressions – Precedence Order 8 Precedence of Regular Operations: 1. Parentheses 2. Star 3. Concatenation 4. Union So according to the precedence sequence: (ab U a)* = ( ( (a) (b) ) U ( a ) ) * Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 9. RE – Practices 9 Language, L(R) Regular Expression { w | w contains a single 1 } 0* 1 0* { w | w consists of exactly three 1’s } 0* 1 0* 1 0* 1 0* { w | w has at least a single 1 } (0 | 1)* 1 (0 | 1)* { w | w contains at least three 1’s } (0 | 1)* 1 (0 | 1)* 1 (0 | 1)* 1 (0 | 1)* { w | w has at most one 1 } 0* | 0* 1 0* { w | w contains an even number of 1’s } (0* 1 0* 1 0*)* 0* { w | the number of 1’s withing w can be evenly divided by 3 } (0* 1 0* 1 0* 1 0* )* 0* { w | w is a string of even length } (ΣΣ)*, here Σ = (0 | 1) { w | the length of w is a multiple of 3 } (ΣΣΣ)*, here Σ = (0 | 1) Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 10. RE – Practices 10 Language, L(R) Regular Expression { w | w starts with 101 } 101 (0 | 1)* { w | w contains the string 001 as a substring } (0 | 1)* 001 (0 | 1)* { w | w ends with 3 consecutive 1’s } (0 | 1)* 111 { w | w doesn’t end with 11 } 𝜀 | 0 | 1 | (0 | 1)* (00 | 01 | 10) { w | w ends with an even nonzero number of 0’s } ( (0 | 1)* 1 | 𝜀 ) 00 (00)* { w | w starts and ends with the same symbol } 0 | 1 | 0Σ*0 | 1Σ*1, here Σ = (0 | 1) { w | w has at least 3 characters and the 3rd character is 0 } (0 | 1) (0 | 1) 0 (0 | 1)* { w | every zero in w is followed by at least one 1 } 1* (01+)* { w | w consists of alternating zeros and ones } (1 | 𝜀) (01)* (0 | 𝜀) Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 11. RE – Practices 11 Language, L(R) Regular Expression { 01, 10 } 01 | 10 01* | 1* (0 | 𝜀) 1* { 𝜀, 0, 1, 01 } (0 | 𝜀) (1 | 𝜀) 𝜙 R𝜙 { 𝜀 } 𝜙* { w | w starts with 0 and has odd length or, starts with 1 and has even length } 0 ( (0 | 1) (0 | 1) )* | 1 (0 | 1) ( (0 | 1) (0 | 1) )* Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 12. RE  NFA ▸ R = a for some a ∈ Σ. Then L(R) = { a }, and the following NFA recognizes L(R) ▸ R = 𝜀 . Then L(R) = { 𝜀 } and the following NFA recognizes L(R) ▸ R = 𝜙 . Then L(R) = 𝜙, and the following NFA recognizes L(R) ▸ R = R1 U R2 , so L(R) = L(R1) U L(R2) = NFA1 recognizes L(R1) U NFA2 recognizes L(R2) ▸ R = R1 o R2 = R1R2 = = L(R1) o L(R2) = NFA1 recognizes L(R1) o NFA2 recognizes L(R2) ▸ R = R1 * = L(R1)* = ( NFA1 recognizes L(R1) )* 12 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 13. RE  NFA : Example 1 Convert the following RE to equivalent NFA: (ab U a)* 13 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 14. RE  NFA : Practices 14 ▸ ( a U b ) * a b a ▸ ( 0 U 1 ) * 0 0 0 ( 0 U 1 )* = Σ* 0 0 0 Σ* where Σ = 0, 1 = 0 U 1 ▸ ( ( ( 0 0 )* ( 1 1 ) ) U 0 1 ) * ▸ ( 0 1 U 0 0 1 U 0 1 0 ) * ▸ a ( a b b ) * U b ▸ a+ U ( a b ) + ▸ ( a U b+ ) a+ b+ [ Hint: replace a+ with (a a*) ] ▸ 1* ( 0 1+)* ▸ ( Σ Σ Σ ) * ▸ 0 Σ* 0 U 1 Σ* 1 U 0 U 1 ▸ 𝜙* ▸ ( 0 U 𝜀 ) 1* Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 15. DFA  GNFA  RE 15 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Steps to follow:
  • 16. GNFA ▸ A GNFA is a nondeterministic finite automaton(NFA) in which each transition is labeled with a regular expression over the alphabet set instead of only members of the alphabet or 𝜀. ▸ A single initial state with all possible outgoing transitions and no incoming transitions. ▸ A single final state without outgoing transitions but all possible incoming transitions. The accept state is not the same as the start state. ▸ A single transition between every two states, including self loops. 16 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 17. GNFA – Formal Definition A generalized nondeterministic finite automaton is a 5 tuple, (Q, Σ, 𝛿, qstart, qaccept ), where ▸ Q is the finite set of states ▸ Σ is the input alphabet ▸ 𝛿: ( Q - {qaccept} ) x ( Q - {qstart} )  ℛ is the transition function [if you travel from state a 𝜖 ( Q - {qaccept} ) to state b 𝜖 ( Q - {qstart} ) then this relation will generate ℛ regular expression ] ▸ qstart is the start state and ▸ qaccept is the accept state 17 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 18. GNFA – State Minimization Rule 18 Removing qrip state, Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 19. DFA  GNFA : Steps to Follow 19 ▸ Add a new start state with an 𝜀 arrow to the old start state ▸ Add a new accept state with 𝜀 arrows from the old accept states to this new accept state ▸ If any arrows have multiple labels, replace each with a single arrow whose label is the union of the previous labels ▸ Add arrows labeled 𝜙 between states that had no arrows. [better no to show this in the diagram, for simplicity] Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 20. CONVERT(G): 1. Let k be the number of states of G. 2. If k=2, then G must consist of a start state, an accept state, and a single arrow connecting them and labeled with a regular expression R — Return the expression R. 3. If k>2, we select any state qrip ∈ Q different from qstart and qaccept and let G’ be the GNFA (Q’, Σ, 𝛿’, qstart, qaccept), where Q’ = Q - {qrip} and for any qi ∈ Q’–{qaccept} and any qj ∈ Q’–{qstart}, let 𝛿’(qi, qj) = (R1) (R2)*(R3) U (R4) for R1 = 𝛿(qi, qrip), R2 = 𝛿(qrip, qrip), R3 = 𝛿(qrip, qj), and R4 = 𝛿(qi, qj) 4. Compute CONVERT(G’) and return this value. GNFA  RE : Algorithm 20 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 21. GNFA  RE : Removing State 2 21 Here, 2 is associated with states 1 and a. All possible combinations are (1,1), (1,a), (a,a), (a,1). But there will be no outgoing from a, so no need to consider (a,a) and (a,1) states. For (1,1), R = a U (b (aUb)* 𝜙)) = a Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 22. GNFA  RE : Removing State 2 22 Here, 2 is associated with states 1 and a. All possible combinations are (1,1), (1,a), (a,a), (a,1). But there will be no outgoing from a, so no need to consider (a,a) and (a,1) states. For (1,a), R = 𝜙 U (b (aUb)* 𝜀) = b(aUb)* Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 23. GNFA  RE : Removing State 1 23 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 24. DFA  GNFA  RE : Example 2 24 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 25. DFA  GNFA  RE : Practices 25 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Practice 1 Practice 2
  • 26. 26 THANKS! Any questions? You can find me at imam@cse.uiu.ac.bd References: Chapter 1(section 1.3), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser