Download presentation
Presentation is loading. Please wait.
Published byAusten Wade Modified over 6 years ago
1
Regular languages, regular expressions, & finite automata (intro)
CS 350 β Fall 2018 gilray.org/classes/fall2018/cs350/
2
πΏ={hello,bonjour,konnichiwa,β¦}
Ξ£={a,b,c,β¦,y,z}
3
Ξ£={a,b,c,β¦,y,z} Ξ£ β ={ π€ 0 β¦ π€ π |βπ,πββ. π€ π βΞ£}
4
Ξ£={a,b,c,β¦,y,z} Ξ£ β ={ π€ 0 β¦ π€ π |βπ,πββ. π€ π βΞ£} abcabcβ Ξ£ β πβ Ξ£ β
Examples: abcabcβ Ξ£ β πβ Ξ£ β aaaaβ¦β Ξ£ β
5
Regular languages
6
Regular expressions: E
7
βπβΞ£.β(a)={a}
8
βπβΞ£.β(a)={a} The interpretation of regex βaβ is the singleton set
containing just the string βaβ. βπβΞ£.β(a)={a}
9
All characters in the alphabet are regular expressions.
βπβΞ£.β(π)={π}
10
There is also an empty/null language, , and an empty-string language, .
Γ π β(Γ)={} β(π)={π}
11
Composite forms of regular expressions can be derived, from other composite forms, and terminally, from null, empty, or single-character REs. A minimal and sufficient set of derived forms is: disjunction of REs, composition of REs, and kleene star of REs.
12
ΓβπΈ, πβπΈ, πβΞ£βΉπβπΈ πβπΈβΉ(π)βπΈ π 0 βπΈβ§ π 1 βπΈβΉ π 0 π 1 βπΈ π 0 βπΈβ§ π 1 βπΈβΉ π 0 | π 1 βπΈ πβπΈβΉ π β βπΈ
13
πβπΈ::=π€|Γ|π |(π) | π 0 π 1 | π 0 + π 1 | π β π€βΞ£={β¦}
There is one base case defining regexes and four inductive cases. Both β|β and β+β are commonly used to signify disjunction in regexes. πβπΈ::=π€|Γ|π |(π) | π 0 π 1 | π 0 + π 1 | π β π€βΞ£={β¦}
15
Interpreting Regexes
16
Thus, a|bc|bcd* is the same as (a)|(bc)|(bc(d*))
Precedence: kleene star (*), concatenation (ab), then disjunction (a|b). Thus, a|bc|bcd* is the same as (a)|(bc)|(bc(d*))
17
β(π)={π} β(π)={π} β(Γ)={} β( π 0 π 1 )=β( π 0 )ββ( π 1 )
Juxtaposition is language concatenation, disjunction is language union, kleene star is interpreted as kleene closure: β( π 0 π 1 )=β( π 0 )ββ( π 1 ) β( π 0 | π 1 )=β( π 0 )βͺβ( π 1 ) β( π 0 β )=β( π 0 ) β
18
Language concatenation: Kleene-closure of a Language:
πΏβ πΏ β² ={ π 0 π 1 | π 0 βπΏβ§ π 1 β πΏ β² } Kleene-closure of a Language: πΏ β ={ π 0 π 1 β¦ π π |πβββ§ π π βπΏ}
19
Kleene-closure can also be defined as a fixed point!
β(π ) β =πΏ,where π π (πΏ)=πΏ π π (πΏ)=πΏββ(π)βͺ{π}
20
This means kleene star is idempotent
( πΏ β ) β = πΏ β This means kleene star is idempotent β(π ) β =πΏ,where π π (πΏ)=πΏ π π (πΏ)=πΏββ(π)βͺ{π}
21
L is the language of odd-length strings of zeros. Give a regex for L.
Try an example: L is the language of odd-length strings of zeros. Give a regex for L. πΏ={ π 1 β¦ π 2π+1 |πβββ§ π π =0}
22
L is the language of odd-length strings of zeros. Give a regex for L.
Try an example: L is the language of odd-length strings of zeros. Give a regex for L. 0(00 ) β
23
Try an example: L is the language of all strings over alphabet {0,1} where every 1 has an adjacent 1.
24
e+ is syntactic sugar for ee*
Try an example: L is the language of all strings over alphabet {0,1} where every 1 has an adjacent 1. (0| 111 β ) β =(0| ) β e+ is syntactic sugar for ee*
25
Try an example: L is the language of odd decimal integers greater than zero. Give a regex for L.
26
Try an example: L is the language of odd decimal integers greater than zero. Give a regex for L. (1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)β(1|3|5|7|9)|1|3|5|7|9
27
[a-g] is a character class, and is syntactic sugar for (a|b|c|d|e|f|g)
Try an example: L is the language of odd decimal integers greater than zero. Give a regex for L. [a-g] is a character class, and is syntactic sugar for (a|b|c|d|e|f|g) [1β9][0β9]β(1|3|5|7|9)|1|3|5|7|9
28
Try an example: L is the language of odd decimal integers greater than zero. Give a regex for L. is syntactic sugar for π? ([1β9][0β9]β)?(1|3|5|7|9) (π|π)
29
Regexes in Python import re rβaβ rβabβ ππ π rβbβ rβa|bβ π|π π
30
Regexes in Python import re rβa*β π β rβb+β π + rβc?β π?
31
(π§π§|π§π§π§|π§π§π§π§) rβ\dβ rβ[0-9]β rβz{2,4}β Regexes in Python import re
(0|1|2|3|4|5|6|7|8|9) rβ[0-9]β (0|1|2|3|4|5|6|7|8|9) rβz{2,4}β (π§π§|π§π§π§|π§π§π§π§)
32
Regexes in Python import re >>> m = re.match(rβ\dβ,β5β)
>>> m.group(0) β5β
33
Regexes in Python import re >>> m = re.match(rβ\dβ,ββ)
>>> m == None True
34
Regexes in Python import re
>>> m = re.match(rβ(\d)\d\dβ,β456β) >>> m.group(0) β456β >>> m.group(1) β4β
35
Regexes in Python import re
>>> m = re.match(rβ(\d)\d\dβ,β4567β) >>> m.group(0) β456β >>> m.group(1) β4β
36
Regexes in Python import re
>>> m = re.match(rβ(\d)\d\dβ,β4567β) >>> m.group(0) β456β >>> m.group(1) β4β >>> m = re.match(rβ^(\d)\d\d$β,β4567β) >>> m == None True
37
Finite Automata
38
Every automata has a set of states, one of which must be a designated start state. This state is marked by an incoming arrow, like so. q0
39
There may also be zero or more final statesβalso called accept states
There may also be zero or more final statesβalso called accept states. These are shown with an extra circle around them. q0 q1
40
The starting state may also be an accept state.
q0 The starting state may also be an accept state.
41
These are also two of the simplest languages.
β(Γ) β(π) q0 q0 These are also two of the simplest languages.
42
Ξ£ Edges are labeled with characters from .
This DFA is equivalent to the RE: a Ξ£ a q0 q1 β¦it reads the character a, and then accepts it.
43
This encodes the language
b q0 q1 q2 This encodes the language {a,ab}
44
This encodes the language
b a b q0 q1 q2 This encodes the language π + π β
45
Edges not shown implicitly reach a dead state.
b a b q0 q1 q2 b a dead a,b
46
Try an example: L is the language of all strings over {0,1} where there are an even number of 1s.
47
Try an example: L is the language of all strings over {0,1} where there are an even number of 1s. 1 1 q0 q1
48
Try an example: L is the language of all strings over {0,1,2} where there are an odd number of 1s and no 2s.
49
Try an example: L is the language of all strings over {0,1,2} where there are an odd number of 1s and no 2s. 1 1 q0 q1
50
Deterministic Finite Automata (DFA)
Non-deterministic Finite Automata (NFA)
51
Equivalent models of regular languages
βConverts toβ GNFA βConverts toβ RE DFA βMinimizes toβ NFA βConverts toβ βConverts toβ
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.