SlideShare a Scribd company logo
1 of 21
NORTH WESTERN UNIVERSITY
Computer Science & Engineering
Course Titel: Compiler Design
Course Code: CSE-4103
Presentation on: REGULAR EXPRESSION
Submitted by:
Name : MD. MONIRUL ISLAM
ID: 20151116010
Submitted to:
Name : SAJIB CHATTERJEE
Lecturer
Computer Science & Engineering
North Western University , Khulna
INDEX:
* General Introduction
* Equivalence of FA and RE
* DFA to RE: State Elimination
* Converting a RE to an Automata
* Algebra for Regular Expression’s
* References.
General Introduction
► Regular expression is an important notation for specifying patterns. Each pattern
matches a set of strings, so regular expressions serve as names for a set of strings.
Programming language tokens can be described by regular languages.[1]
R is a regular expression if it is:
1. a for some a in the alphabet , standing for the language {a}
2. ε, standing for the language {ε}
3. Ø, standing for the empty language
4. R1+R2 where R1 and R2 are regular expressions, and + signifies
union (sometimes | is used)
5. R1R2 where R1 and R2 are regular expressions and this signifies
concatenation
6. R* where R is a regular expression and signifies closure
7. (R) where R is a regular expression, then a parenthesized R is
also a regular expression
This definition may seem circular, but 1-3 form the basis
Precedence: Parentheses have the highest precedence, followed by *,
concatenation, and then union.
RE Examples
• L(001) = {001}
• L(0+10*) = { 0, 1, 10, 100, 1000, 10000, … }
• L(0*10*) = {1, 01, 10, 010, 0010, …} i.e. {w | w has exactly a single 1}
• L()* = {w | w is a string of even length}
• L((0(0+1))*) = { ε, 00, 01, 0000, 0001, 0100, 0101, …}
• L((0+ε)(1+ ε)) = {ε, 0, 1, 01}
• L(1Ø) = Ø ; concatenating the empty set to any set yields the empty set.
• R ε = R
• R+Ø = R
• Note that R + ε may or may not equal R (we are adding ε to the language)
• Note that RØ will only equal R if R itself is the empty set.
Equivalence of FA and RE
Finite Automata and Regular Expressions are equivalent. To show this:
- we can express a DFA as an equivalent RE
- we can express a RE as an ε-NFA. Since the ε-NFA can be converted to
a DFA and the DFA to an NFA, then RE will be equivalent to all the
automata we have described.
DFA to RE: State Elimination
 Eliminates states of the automaton and replaces the edges with regular
expressions that includes the behavior of the eliminated states.
 Eventually we get down to the situation with just a start and final node, and
this is easy to express as a RE
State Elimination
 Consider the figure below, which shows a generic state s about to be eliminated.
The labels on all edges are regular expressions.
 To remove s, we must make labels from each qi to p1 up to pm that include the
paths we could have made through s.
q1
qk
s
p1
pm
.
.
.
.
.
.
R1m
R11
SQ1
QK
Rkm
Rk1
P1
Pm
q1
qk
p1
pm
.
.
.
R11+Q1S*P1
Rkm+QkS*Pm
.
.
.
Rk1+QkS*P1
R1m+Q1S*Pm
DFA to RE via State Elimination
 Starting with intermediate states and then moving to accepting states, apply the state
elimination process to produce an equivalent automaton with regular expression labels
on the edges.
The result will be a one or two state automaton with a start state and accepting state.
 If the two states are different, we will have an automaton that looks
like the following:
Start
S
R
T
U
Figure 01 : (R+SU*T)*SU*
 If the start state is also an accepting state, then we must also perform a state elimination
from the original automaton that gets rid of every state but the start state. This leaves the
following:
Start
R
Figure 02: R*.
 If there are n accepting states, we must repeat the above steps for each accepting states
to get n different regular expressions, R1, R2, … Rn. For each repeat we turn any other
accepting state to non-accepting. The desired regular expression for the automaton is
then the union of each of the n regular expressions: R1 R2…  RN
DFARE Example
Convert the following to a RE
Figure 03: Convert to RE
First convert the edges to RE’s:
3Start 1 2
1 1
0
0
0,1
3Start 1 2
1 1
0
0
0+1
Eliminate State 1:
To: 3Start 1 2
1 1
0
Figure 04: Convert to RE
0+1
3Start 2
11
0+10 0+1
Note edge from 33
Figure 05: (0+10)*11(0+1)*
Converting a RE to an Automata
 we can convert an automata to a RE. To show equivalence we must also go the other
direction, convert a RE to an automaton.
 We can do this easiest by converting a RE to an ε-NFA
-Inductive construction
-Start with a simple basis, use that to build more complex parts of the NFA
Basis:
R=a
R=ε
a
ε
R=Ø
R=S+T
S
T
ε
ε
ε
ε
R=ST S ε
ε
ε
T
R=S* S ε
Figure 06: RE to an Automata
Figure 07: RE to an Automata
Figure 08: RE to an Automata
RE to ε-NFA Example
Convert R= (ab+a)* to an NFA
We proceed in stages, starting from simple elements and working our way up
a
a
b
b
ab
a bε
ab+a
a bε
a
ε
ε
ε
ε
(ab+a)*
a bε
a
ε
ε
ε
ε
εε
ε
Figure 09: RE to NFA
Figure 10: RE to NFA
Algebraic Laws for RE’s
Just like we have an algebra for arithmetic, we also have an algebra for regular
expressions.
-While there are some similarities to arithmetic algebra, it is a bit different with regular
expressions.
Algebra for RE’s
Commutative law for union:
L + M = M + L
Associative law for union:
(L + M) + N = L + (M + N)
Associative law for concatenation:
(LM)N = L(MN)
Note that there is no commutative law for
concatenation,
i.e. LM  ML
The identity for union is:
L + Ø = Ø + L = L
The identity for concatenation is:
L ε = ε L = L
The annihilator for concatenation is:
ØL = LØ = Ø
Left distributive law:
L(M + N) = LM + LN
Right distributive law:
(M + N)L = LM + LN
Idempotent law:
L + L = L
Laws Involving Closure
(L*)* = L*
i.e. closing an already closed expression does not change the language
Ø* = ε
ε* = ε
L+ = LL* = L*L
more of a definition than a law
L* = L+ + ε
L? = ε + L
more of a definition than a law
References
[1] Compiler Design - Regular Expressions ,tutorialspoint.
[2] Wikipedia.
[3] Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman-Compilers -
Principles, Techniques, and Tools-Pearson_Addison Wesley (2006).
THANK YOU

More Related Content

What's hot

Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examples
ankitamakin
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
danhumble
 

What's hot (20)

Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
context free language
context free languagecontext free language
context free language
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examples
 
Context free languages
Context free languagesContext free languages
Context free languages
 
Introduction to fa and dfa
Introduction to fa  and dfaIntroduction to fa  and dfa
Introduction to fa and dfa
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Finite automata
Finite automataFinite automata
Finite automata
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Automata Theory
Automata TheoryAutomata Theory
Automata Theory
 
Automata
AutomataAutomata
Automata
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Thoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's ClassificationThoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's Classification
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 

Similar to Regular expression

02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
raosir123
 

Similar to Regular expression (20)

Flat unit 2
Flat unit 2Flat unit 2
Flat unit 2
 
FLAT.pdf
FLAT.pdfFLAT.pdf
FLAT.pdf
 
re1.ppt
re1.pptre1.ppt
re1.ppt
 
Re1 (3)
Re1 (3)Re1 (3)
Re1 (3)
 
re1.ppt
re1.pptre1.ppt
re1.ppt
 
re1.ppt
re1.pptre1.ppt
re1.ppt
 
Unit2 Toc.pptx
Unit2 Toc.pptxUnit2 Toc.pptx
Unit2 Toc.pptx
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
UNIT_-_II.docx
UNIT_-_II.docxUNIT_-_II.docx
UNIT_-_II.docx
 
compiler Design course material chapter 2
compiler Design course material chapter 2compiler Design course material chapter 2
compiler Design course material chapter 2
 
1LECTURE 8 Regular_Expressions.ppt
1LECTURE 8 Regular_Expressions.ppt1LECTURE 8 Regular_Expressions.ppt
1LECTURE 8 Regular_Expressions.ppt
 
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping LemmaTheory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
 
QB104541.pdf
QB104541.pdfQB104541.pdf
QB104541.pdf
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Unit ii
Unit iiUnit ii
Unit ii
 
Regular Expressions To Finite Automata
Regular Expressions To Finite AutomataRegular Expressions To Finite Automata
Regular Expressions To Finite Automata
 
Compiler Design Material 2
Compiler Design Material 2Compiler Design Material 2
Compiler Design Material 2
 
RegularExpressions.pdf
RegularExpressions.pdfRegularExpressions.pdf
RegularExpressions.pdf
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 

Recently uploaded (20)

Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 

Regular expression

  • 1. NORTH WESTERN UNIVERSITY Computer Science & Engineering Course Titel: Compiler Design Course Code: CSE-4103 Presentation on: REGULAR EXPRESSION Submitted by: Name : MD. MONIRUL ISLAM ID: 20151116010 Submitted to: Name : SAJIB CHATTERJEE Lecturer Computer Science & Engineering North Western University , Khulna
  • 2. INDEX: * General Introduction * Equivalence of FA and RE * DFA to RE: State Elimination * Converting a RE to an Automata * Algebra for Regular Expression’s * References.
  • 3. General Introduction ► Regular expression is an important notation for specifying patterns. Each pattern matches a set of strings, so regular expressions serve as names for a set of strings. Programming language tokens can be described by regular languages.[1]
  • 4. R is a regular expression if it is: 1. a for some a in the alphabet , standing for the language {a} 2. ε, standing for the language {ε} 3. Ø, standing for the empty language 4. R1+R2 where R1 and R2 are regular expressions, and + signifies union (sometimes | is used) 5. R1R2 where R1 and R2 are regular expressions and this signifies concatenation 6. R* where R is a regular expression and signifies closure 7. (R) where R is a regular expression, then a parenthesized R is also a regular expression This definition may seem circular, but 1-3 form the basis Precedence: Parentheses have the highest precedence, followed by *, concatenation, and then union.
  • 5. RE Examples • L(001) = {001} • L(0+10*) = { 0, 1, 10, 100, 1000, 10000, … } • L(0*10*) = {1, 01, 10, 010, 0010, …} i.e. {w | w has exactly a single 1} • L()* = {w | w is a string of even length} • L((0(0+1))*) = { ε, 00, 01, 0000, 0001, 0100, 0101, …} • L((0+ε)(1+ ε)) = {ε, 0, 1, 01} • L(1Ø) = Ø ; concatenating the empty set to any set yields the empty set. • R ε = R • R+Ø = R • Note that R + ε may or may not equal R (we are adding ε to the language) • Note that RØ will only equal R if R itself is the empty set.
  • 6. Equivalence of FA and RE Finite Automata and Regular Expressions are equivalent. To show this: - we can express a DFA as an equivalent RE - we can express a RE as an ε-NFA. Since the ε-NFA can be converted to a DFA and the DFA to an NFA, then RE will be equivalent to all the automata we have described.
  • 7. DFA to RE: State Elimination  Eliminates states of the automaton and replaces the edges with regular expressions that includes the behavior of the eliminated states.  Eventually we get down to the situation with just a start and final node, and this is easy to express as a RE
  • 8. State Elimination  Consider the figure below, which shows a generic state s about to be eliminated. The labels on all edges are regular expressions.  To remove s, we must make labels from each qi to p1 up to pm that include the paths we could have made through s. q1 qk s p1 pm . . . . . . R1m R11 SQ1 QK Rkm Rk1 P1 Pm q1 qk p1 pm . . . R11+Q1S*P1 Rkm+QkS*Pm . . . Rk1+QkS*P1 R1m+Q1S*Pm
  • 9. DFA to RE via State Elimination  Starting with intermediate states and then moving to accepting states, apply the state elimination process to produce an equivalent automaton with regular expression labels on the edges. The result will be a one or two state automaton with a start state and accepting state.  If the two states are different, we will have an automaton that looks like the following: Start S R T U Figure 01 : (R+SU*T)*SU*
  • 10.  If the start state is also an accepting state, then we must also perform a state elimination from the original automaton that gets rid of every state but the start state. This leaves the following: Start R Figure 02: R*.  If there are n accepting states, we must repeat the above steps for each accepting states to get n different regular expressions, R1, R2, … Rn. For each repeat we turn any other accepting state to non-accepting. The desired regular expression for the automaton is then the union of each of the n regular expressions: R1 R2…  RN
  • 11. DFARE Example Convert the following to a RE Figure 03: Convert to RE First convert the edges to RE’s: 3Start 1 2 1 1 0 0 0,1 3Start 1 2 1 1 0 0 0+1
  • 12. Eliminate State 1: To: 3Start 1 2 1 1 0 Figure 04: Convert to RE 0+1 3Start 2 11 0+10 0+1 Note edge from 33 Figure 05: (0+10)*11(0+1)*
  • 13. Converting a RE to an Automata  we can convert an automata to a RE. To show equivalence we must also go the other direction, convert a RE to an automaton.  We can do this easiest by converting a RE to an ε-NFA -Inductive construction -Start with a simple basis, use that to build more complex parts of the NFA Basis: R=a R=ε a ε R=Ø
  • 14. R=S+T S T ε ε ε ε R=ST S ε ε ε T R=S* S ε Figure 06: RE to an Automata Figure 07: RE to an Automata Figure 08: RE to an Automata
  • 15. RE to ε-NFA Example Convert R= (ab+a)* to an NFA We proceed in stages, starting from simple elements and working our way up a a b b ab a bε
  • 17. Algebraic Laws for RE’s Just like we have an algebra for arithmetic, we also have an algebra for regular expressions. -While there are some similarities to arithmetic algebra, it is a bit different with regular expressions.
  • 18. Algebra for RE’s Commutative law for union: L + M = M + L Associative law for union: (L + M) + N = L + (M + N) Associative law for concatenation: (LM)N = L(MN) Note that there is no commutative law for concatenation, i.e. LM  ML The identity for union is: L + Ø = Ø + L = L The identity for concatenation is: L ε = ε L = L The annihilator for concatenation is: ØL = LØ = Ø Left distributive law: L(M + N) = LM + LN Right distributive law: (M + N)L = LM + LN Idempotent law: L + L = L
  • 19. Laws Involving Closure (L*)* = L* i.e. closing an already closed expression does not change the language Ø* = ε ε* = ε L+ = LL* = L*L more of a definition than a law L* = L+ + ε L? = ε + L more of a definition than a law
  • 20. References [1] Compiler Design - Regular Expressions ,tutorialspoint. [2] Wikipedia. [3] Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman-Compilers - Principles, Techniques, and Tools-Pearson_Addison Wesley (2006).