CSC 473 Automata, Grammars & Languages 9/29/10 Lecture 03 1 C SC 473 Automata, Grammars & Languages Automata, Grammars and Languages Discourse 03 Finite Automata C SC 473 Automata, Grammars & Languages 2 Finite Automata / Switching Theory (CS) / (CE) • Boolean operators / Gates (Elem. Switching Ops) 0 1 1 1 1 0 1 1 0 0 1 1 1 0 1 0 1 0 0 0 0 0 ¬ x x ⊕ y x ∨ y x ∧ y y x C SC 473 Automata, Grammars & Languages 3 Boolean Functions / Combinatorial Circuits • Circuit ∨ ∧ ∧ ¬ 1 x 2 x 1 (sum) z 2 (carry) z 1 2 x x ! 1 2 x x ! 1 2 x x ! 1 2 x x ! H half adder H H 1 x 2 x ∨ 3 x 1 2 x x ! 1 (sum) z 1 2 x x ! 1 2 3 ( ) x x x ! " 2 (new carry) z F full adder (old carry)
32
Embed
CSC 473 Automata, Grammars & Languages 9/29/10 · CSC 473 Automata, Grammars & Languages 9/29/10 Lecture 03 1 C SC 473 Automata, Grammars & Languages Automata, Grammars and Languages
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Machines that Recognize• Detection of an “event”, i.e., a pattern in input• Recognition of just those words in some language L• Definition of a language• Ex: detect abab –all non-overlapping occurrences
ab
ab
b
a
a b a b
C SC 473 Automata, Grammars & Languages 14
Ex: C Comments /* … */• Filter in the lexical scanner
• Recognizer
{,/}a !" # =
:aa:! !
/:!
/:!
:/a a
:!"
/:/
:!"
empty
/:!:a ! :!"
:a!
/a !
a
!/ ! /
(transducer) notationin : out
C SC 473 Automata, Grammars & Languages 15
Finite Automaton (Finite State Machine, FSA)
• Defn 1.5: A (deterministic) finite automaton is a 5-tuple
• is a finite set, the states• is a finite set, the alphabet• is the transition function• is the start state• is the set of accepting (final) states
• Ex:
0( , , , ),M Q q F!= "
F Q!
Q
: Q Q! " # $
!
0q Q!
00( , , , ),M Q q F!= " 0 1 2
{ , , }Q q q q=
1{ , } { }a b F q! = =
0 1 0 2
1 2 1 0
2 2 2 2
( , ) ( , )
( , ) ( , )
( , ) ( , )
q a q q b q
q a q q b q
q a q q b q
! !
! !
! !
= =
= =
= =
0q 1
q
2q
a
b
b a
,a b
0M
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 6
C SC 473 Automata, Grammars & Languages 16
How FA Compute• FA is a finite structure—like
a program—fixed and static• Need to define the behavior of M on input w
Sequence of configurations Like trace of a program on given data Dynamic and input-dependent
• Ex: start on input Look at sequenceof “moves” determined by the transition function:
Since in accepting state when input exhausted, w isrecognized by
0( , , , ),M Q q F!= "
0M w ababa=
(q0,ababa)!(q
1,baba)!(q
0,aba)!
(q1,ba) ! (q
0,a)!(q
1,!)
" (q0,ababa)!
#(q
1,!)
0M
C SC 473 Automata, Grammars & Languages
www.jflap.org
JFLAP is a package of graphical tools which can be used as an aid inlearning the basic concepts of Formal Languages and Automata Theory.
C SC 473 Automata, Grammars & Languages 18
Info on JFLAP• Website http://www.jflap.org/
Downloads Tutorial
• Lectura (linux) install cd /usr/local/jflap java -jar JFLAP.jar
• X11 forwarding (graphics) ssh -X lectura
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 7
C SC 473 Automata, Grammars & Languages 19
How FA Compute (contʼd)• Given a FA• Defn: configuration of M is an element of• Defn: yields in one step (or moves) relation
between configurations is defined by
where Notes: is a function, since δ is. Is undefined.
• Defn: yields is the relation Means “moves in zero or more steps to”
• Defn: A string w is recognized (accepted) by M ⇔
0( , , , ),M Q q F!= "
(q,aw)!( !q ,w)" #(q,a) = !q
Q!
" #
!
, , ,a w q q Q!
"# $ # $ #
!
(q,!)!
!!
(q,w)!
!(q,w)
(! f " F)(q
0,w)!
#(f,$)
C SC 473 Automata, Grammars & Languages 20
How FA Compute (contʼd)• Defn: The language recognized (accepted) by M is
• Defn 1.16: A language S is regular iff there is some FAthat recognizes it, i.e.,
• Ex: In FA
( )[ is a FA ( )]M M S L M! " =
L(M) = {w : M accepts w}
= {w :(! f " F)(q0,w)!
#(f,$)}
(q0,a) !
!(q
1,")
(q0,aba)!
!(q
1,")
!
(q0,(ab)
ia)!
!(q
1,")
# L(M0) = {(ab)
ia : i $ 0}
0M
C SC 473 Automata, Grammars & Languages 21
Example:• Coin checker for 30¢ coffee. Σ={n,d,q}
15
05
10
3025
20
35
40
45
50
i = make change for i – 30 ¢ & vend coffee
q
n
n
n
n
n
n
d
d
d
d
dd
q
q
q
q
q
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 8
C SC 473 Automata, Grammars & Languages 22
Regular Operations & Regular Expressions
• The regular operations on languages are: union (∪), concatenation (⋅ ) and Kleene star (*).
• So called because the class of regular languages areclosed under them—i.e., applying these operators toregular languages results in a regular language. (We willprove these closure results later.)
• In fact, these three operations (∪, ⋅ ,*) actually characterizewhat it means to be a regular language: any regularlanguage can be built up from alphabet symbols and afinite number of these regular operations.
• This motivates the notion of regular expression: asequence of symbols, like an arithmetic expression, thatdefines a regular language using regular ops.
C SC 473 Automata, Grammars & Languages 23
Regular Expressions• A syntax for describing sets of strings (languages)
Terse Eliminates fussy “{ }” Reminiscent of arithmetic expressions Obeys some useful “algebra”, e.g., (E*F*)* = (E+F)*
• Syntax for regular expressions over Σ,+, ⋅ ,*,(,) E → (E+E) ( text uses ∪ not +; some authors use |) → (EE) (usually suppress the ⋅ in E ⋅ E) → (E*) → ε (some authors use λ ) → ∅ → a for each a in Σ
• suppress (,) where possible: (a+b)*a not ( ( (a + b)* ) ⋅ a )
C SC 473 Automata, Grammars & Languages 24
Regular Expressions (contʼd)• Meaning rules for the syntax• The meaning (denotation) of an expression, L(E), is a set
of strings (a language)• Rules expression E language L(E) ∅ a {a} ε {ε} (E+F) L(E)∪L(F) (EF) L(E)⋅L(F) (E*) L(E)*
Nondeterminism• Real computing devices are deterministic: the current
configuration and instruction determines the nextconfiguration. The relation is a function.
• Why the concept of nondeterminism? Provides powerful, economical descriptive ability Provides a way to specify languages without over-specifying and
complex handling of cases Can be algorithmically converted to a deterministic description (at
the sacrifice of some economy and with added complexity) Generalization of determinism
• Ex: abab occurs somewhere in w: …abab…
!
a b a b
a,ba,b a,b
ab a
C SC 473 Automata, Grammars & Languages 27
Nondeterminism (contʼd)• Ex: w∈Σ* has penultimate symbol b: w = …b?
• Ex: w∈Σ* has ≥ 2 a’s: w = … a … a …
b b
a,b a
b
a,ba
a a
a,b a,ba,b
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 10
C SC 473 Automata, Grammars & Languages 28
ε-Moves Can Be Useful• SNOBOL arithmetic constants (no floating E)
Use to specify “optional characters” like Unix command line [opt]
ε
−
d
d=digit
d d
ε
••
d
C SC 473 Automata, Grammars & Languages 29
Nondeterministic Finite Automaton• Defn 1.5: A nondeterministic finite automaton is a 5-tuple
is a finite set, the states is a finite set, the alphabet transition function is the start state is the set of accepting (final) states
• Ex:
0( , , , ),M Q q F!= "
F Q!
Q
: ( { }) ( )Q Q! "# $ % & P
!
0q Q!
01( , , , ),M Q q F!= "
0 1{ , }Q q q=
1{ , } { }a b F q! = =
0 0
0 0 1
1 1 1 1
( , ) { }
( , ) { , }
( , ) { } ( , ) { }
q a q
q b q q
q a q q b q
!
!
! !
=
=
= =
1Mb
a,b a,b
0q
1q
C SC 473 Automata, Grammars & Languages 30
DFA vs NFA• DFA δ
• For each state q and input symbola, there is exactly one choice ofnew state (or no transition isdefined at all). Each transition“consumes” an input symbol
• Special case of NFA!
• NFA δ
• There may be multiple choices forthe same input symbol
• There may be ε-moves that do not“consume” an input character
• There can be “chains” of ε-moves• ε-moves can create even more
choice for the next input character
qa
b
qa
a
b
b
ε
ε
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 11
C SC 473 Automata, Grammars & Languages 31
How NFA Compute• Given a NFA• Defn: configuration –• Defn: yields in one step (or moves) relation
between configurations
• Defn: yields = Means “COULD move in zero or more steps to”
• Defn: w is recognized (accepted) by M ⇔
Same as before, but has the meaning “if there exists somesequence of moves from the start config to some accepting config”
0( , , , ),M Q q F!= "
(q,aw)!( !q ,w)" !q # $(q,a)
( , )q w Q!
" # $
!
!!
(! f " F)(q
0,w)!
#(f,$)
(q,w)!( !q ,w)" !q # $(q,%) (ε-move)
C SC 473 Automata, Grammars & Languages 32
How NFA Compute (contʼd)• Defn: The language recognized (accepted) by M is
• Ex: In NFA This provides no “evidence” that aabbba is accepted (or not) However, also via a separate computation sequence:
And so aabbba is recognized!
L(M) = {w : M accepts w}
= {w :(! f " F)(q0,w)!
#(f,$)}
(q
0,aabbba) !
!(q
0,")
1M
(q
0,aabbba) !
!(q
1,")
C SC 473 Automata, Grammars & Languages 33
“Tree” of Computations• Ex: NFA M1 0
( , )q aabbba
0( , )q abbba
0( , )q bbba
1( , )q bba
0( , )q bba
0( , )q ba 1
( , )q ba
0( , )q a
1( , )q a
0( , )q !
1( , )q !X null
“evidence”
∃ acceptingComputation⇒ w∈L(M1)
1q F!
! =
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 12
C SC 473 Automata, Grammars & Languages 34
Computation Tree: Example• Ex: L = {w: w begins & ends same } 2 a
3
4
a,b
1
a,b
bb
a
( , )2 ! ( , )3 !
( , )1 ababa
( , )2 baba
( , )2 aba
( , )2 ba ( , )3 ba
( , )2 a
X3∈F
( , )1 abab
( , )2 bab
( , )2 ab
( , )2 b ( , )3 b
( , )2 !
XX
reject abab¬∃ path to Faccept ababa
∃ some path to F
C SC 473 Automata, Grammars & Languages 35
Example with ε-Moves• String length a multiple of 2 or 3
0
2
4
3ε
ε
a
5
6a
aaa( , )0 aaa
( , )2 aaa ( , )4 aaa
( , )3 aa
( , )2 a
( , )5 aa
( , )6 a
( , )3 ! ( , )4 !
!
X 4∈F ⇒Accept aaa
← ε-moves
C SC 473 Automata, Grammars & Languages 36
Example with ε-Moves• a*b*
( , )0 aab
( , )1 aab ( , )0 ab
( , )1 ab ( , )0 b! X
1∈F ⇒Accept aab
X ( , )1 b
( , )1 !
ε
a
0 1
b
ε-moves “consume” no input symbols
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 13
C SC 473 Automata, Grammars & Languages 37
Equivalence of NFA to DFA• There is an algorithm to convert any NFA into a DFA
We show basic idea assuming NFA has no ε-moves Then (later) modify the construction for NFAs with ε-moves
• Ex: L = {x : last symbol of x appeared previously } Σ={a,b}
• Idea: given input string, keep track of all possible reachedstates after reading each letter. At end of input, see if afinal state is among those reached
p
q
r
s
a
b
a,ba,b
a,b
a
b
N0:
C SC 473 Automata, Grammars & Languages 38
Equivalence of NFA and DFA (contʼd)• Computation paths through NFA N0 on w = abba
p p p p p
r r r
q q q q
r r
s
s
q
a
a b b a
a
b a
b b a
b
b b a
as F!
C SC 473 Automata, Grammars & Languages 39
Equivalence of NFA and DFA (contʼd)• Idea: keep a list of all possible states reachable by each
prefix of w (“parallel worlds”). For NFA N0:
0
{ } { , { , { , { ,
} , , ,
} , ,
} }
{ } { , , , }
( ) since { , , , }
a b b a
abba
p p p p p
q q q q
r r r
s s
p p q r s
abba L N p q r s F
! ! ! !
" !
# $ % &
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 14
C SC 473 Automata, Grammars & Languages 40
Equivalence of NFA and DFA (contʼd)• Equivalent DFA M will have:
State set P (Q) Alphabet Σ Start state “set” {q0} Accepting states {X ⊆ Q : X ∩ F ≠ ∅} Deterministic transition function δ′ : P (Q) × Σ → P (Q)
• Ex: For NFA N0:({ , }, ) { , , }
({ , }, ) { , , }
({ }, ) { , }
({ }, ) { , }
p q a p q s
p q b p q r
p a p q
p b p r
!
!
!
!
" =
" =
" =
" =
…
C SC 473 Automata, Grammars & Languages 41
Equivalence of NFA and DFA (contʼd)• Thm: [Rabin-Scott Construction]. Let L = L(N) for some NFA
N with no ε-moves. There is an algorithm to constuct a DFAM equivalent to N, i.e. with L(M) = L(N).
Pf: Given N we construct a DFA M and then verify that itrecognizes the same set as N.
Construction: Given NFA construct where
( , , , , )N Q s F!= "
( , , , , )M Q s F!" " " "= #
!Q = P(Q), !s = {s}, !F = {X " !Q : X # F $ %}
and !& : !Q ' ( ) !Q is defined as:
(*S + Q,a " () !&(S,a)
= {q " Q :(,p " S)q " &(p,a)}
C SC 473 Automata, Grammars & Languages 42
Equivalence of NFA and DFA (contʼd)• Picture of δ′
!"(S,a) = !S
a
aa
b
a
b
S!
S
S Q S Q!" "
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 15
C SC 473 Automata, Grammars & Languages 43
Equivalence of NFA and DFA (contʼd)• Verification: Show (1) M is a DFA and (2) L(M)=L(N) (1) δ′ is a function by the construction, and Q′ is finite: |Q′ | = 2|Q| . So M is a DFA. (2) To show equivalence we prove the• Lemma:
Pf: By induction on the length of the input string w. Base |w|=0.
Step Suppose (IH) the lemma is true Let To show:
(p,w) !
N
!(q,") # $Q.q % Q & ({p},w)!
M
!(Q,")
(p,!) !
N
"(q,!) # p = q # ({p},!)!
M
"({q},!)
.| | .w w k! "
| w |= k + 1,w = ua,|u|= k.
(p,ua) !
N
!(q,") # $Q.q % Q & ({p},ua)!
M
!(Q,")
C SC 473 Automata, Grammars & Languages 44
Equivalence of NFA and DFA (contʼd) ⇒. Assume Then ∃ state r with and Then By (IH) (*)By construction of M Let ThenUsing this with (*) results in:
So
(p,ua) !
N
!(q,").
(p,ua) !
N
!(r,a) !
N(q,")
(p,u) !
N
!(r,").
!R.r " R # ({p},u)!
M
$(R,%)
( , ).q r a!"
( , ).q R a! "#
( , ).Q R a! "= !Q.q " Q # (R,a)!
M
$(Q,%).
!Q.q " Q # ({p},ua)!
M
$(R,a)!
M
$(Q,%).
!Q.q " Q # ({p},ua)!
M
$(Q,%).
C SC 473 Automata, Grammars & Languages 45
Equivalence of NFA and DFA (contʼd) ⇐. Assume Then ∃ state R with So (1)By construction and (2)Since we have from (IH) (3)Combining (2) & (3):
So (p,ua) !
N
!(q,"). !
(p,ua) !
N
!(r,a) !
N(q,")
({p},u)!
M
!(R,")
. ( , )r R q r a!" # #
( , ) .R a Q! " =
({p},ua)!
M
!(R,a)!
M
!(Q,").
!Q.q " Q # ({p},ua)!
M
$(Q,%).
(r,a) !
N
!(q,").
(p,u) !
N
!(r,")
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 16
C SC 473 Automata, Grammars & Languages 46
Equivalence of NFA and DFA (contʼd) We now finish the verification proof. Let From the Lemma
That is, for some for some
( !s,w)!
M
"(Q,#)
!Q.f " Q # ({s},w)!
M
$(Q,%) &
.f F!
(s,w) !
N
!(f,") #
( !s,w)!
M
"(Q,#) $ Q % F & '.
(s,w) !
N
!(f,") f F! "
.Q F !"
( ) ( ).L M L N! = !
C SC 473 Automata, Grammars & Languages 47
Example: ε-Free NFA → DFA• Consider the previous NFA
Conversion: NFA → DFA• Thm: There is an algorithm to convert any NFA to an
equivalent DFA .• Pf: Construction: Given NFA
construct new NFA where
Verification.
Pf: By induction on |w|
M = (2Q,!, "# , "s, "F )
( , , , , )N Q s F!= "
!F = {S " Q | S # F $ %}
!" : 2Q# $ % 2
Q
!s = E(s)
!"(S,a) = !{E(p)| p # "(q,a) for some q # S}
(q,w) !N
!(p,") # (E(q),w) !
M
!(P,")
for some set P containing p
C SC 473 Automata, Grammars & Languages 51
Conversion: NFA → DFA• Thm 1.39: [Rabin-Scott Theorem]: There is an algorithm
to convert any NFA into an equivalent DFA.• Corollary 1.40: A language is regular ⇔ some NFA
recognizes it.• Ex: Start with an NFA N1 as follows:
ε
−
1 2 3 4b
b b
dε
1N
! = {b,",d}
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 18
C SC 473 Automata, Grammars & Languages 52
Conversion: NFA → DFA• Ex:
ε
−
1 2 3 4b
b b
dε
1N
E(1) = {1} E(2) = {1,2,3}
E(3) = {1,3} E(4) = {4}
1!
b
2 2!
b
2 3!
b
4 4!
b
"
1!
#
" 2!
#
3 3!
#
" 4!
#
"
1!
d
" 2!
d
" 3!
d
" 4!
d
3
Useful summary
C SC 473 Automata, Grammars & Languages 53
Conversion: NFA → DFA• Ex:
ε
−
1 2 3 4b
b b
dε
1N
!s = E(1) = {1}
!"( !s,b) = E(2) = {1,2,3}
!"( !s,#) = $
!"( !s,d) = $
!"({1,2,3},b) = E(1)%E(2)%E(3)%E(4) = {1,2,3,4}
!"({1,2,3},#) = E(3) = {1,3}
!"({1,2,3},d) = $
C SC 473 Automata, Grammars & Languages 54
Ex: NFA → DFA (contʼd)• Ex:
ε
−
1 2 3 4b
b b
dε
1N
!"({1,3},b) = E(2)#E(4) = {1,2,3,4}
!"({1,3},$) = %
!"({1,3},d) = %
!"({1,2,3,4},b) = E(2)#E(4) = {1,2,3,4}
!"({1,2,3,4},$) = E(3) = {1,3}
!"({1,2,3,4},d) = E(3) = {1,3}
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 19
C SC 473 Automata, Grammars & Languages 55
Conversion: NFA → DFA (contʼd)
1M
−1 123 13 1234
b
b
b
d
− b
C SC 473 Automata, Grammars & Languages 56
Regular Expression → NFA• Thm 1.55: There is an algorithm that, given a regular
expression E, constructs a NFA N such that L(E) = L(M). Pf: Induction on the # of operator symbols in E. Base: E = ε ∅ a∈Σ
Step: Assume (IH) the result is true of all expressionswith ≤ operator symbols (+,⋅,*). Let E have k+1 ops.
Three cases: Case E = (E1+E2). By IH, ∃ FA M1 , M2 with L(E1) = L(M1)
and L(E2) = L(M2). Construct the following NFA M.
a
C SC 473 Automata, Grammars & Languages 57
1M
2M
ε
ε1F
2F
M
1 2F F F= !
1 2( ) ( ) ( )L M L M L M= !
Case +
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 20
C SC 473 Automata, Grammars & Languages 58
Regular Expression → NFA (cont”d)• Case E = (E1⋅E2). By IH, ∃ FA M1 , M2 with L(E1) = L(M1)
and L(E2) = L(M2). Construct the following NFA M.
C SC 473 Automata, Grammars & Languages 59
2M
ε
M
2F F=
1 2( ) ( ) ( )L M L M L M= i
Case •
2F
1F
1M
ε
Unmark final states in M1
C SC 473 Automata, Grammars & Languages 60
Regular Expression → NFA (cont”d)• Case E = (E1)*. By IH, ∃ FA M1 with L(E1) = L(M1).
Construct the following NFA M.
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 21
C SC 473 Automata, Grammars & Languages 61
s
M
1{ }F F s= !
1( ) ( )L M L M
!=
Case *
1F
1M
ε
εε
QED
C SC 473 Automata, Grammars & Languages 62
ε
b
εε
ε a ε a
ε
b
a
a
Example: Reg. Exp.→NFA• (b+aa)*
Not very economical
C SC 473 Automata, Grammars & Languages 63
Regular Expressions—Applications• Regexp used in various development tools
qed – interactive text editor. 1st version Lampson & Deutsch 1967◆ Regexp added by Ken Thompson, Bell Labs, ca. 1968
Regexp compiled into NFA in machine code Rabin-Scott idea used to scan “on the fly” One of the first software patents
◆ Offspring ed by Ken for Unix◆ Many others followed: em, vi / ex, sam, qedx, …
grep, egrep - pattern search in a file shell – command line interpreter lex – lexical analyzer generator sed – non-interactive stream editor awk – pattern scanning and processing language perl – pattern-driven programming language
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 22
C SC 473 Automata, Grammars & Languages 64
Applications (contʼd)• Regular expressions = “patterns” meaning awk regexp matches >=1 r r+ matches >=0 r r* matches 0 or 1 r r? matches r then s rs
matches r or s r|s match literal “c” \c match begin/end line ^ $
match any char . group exprs (s)
character list [abc…] negated char list [^abc…]
C SC 473 Automata, Grammars & Languages 65
Applications--Examples -?[0-9]+ nonempty digit strings, optional sign[^0-9] any char except digit\[.*\] reference citations in a paperg/^[ ]*$/d delete blank linesg/[ ]+/d delete lines with a blankEx: match is always (1) leftmost and (2) longest file: abcddddef vi: s/d*/x/ → xabcddddef s/d+/x/ → abcxefEx: csh: sort roll[1-5] | egrep “C SC|MATH” | pr
C SC 473 Automata, Grammars & Languages 66
Applications--ExamplesEx: traditional spelling mnemonic• “i before e, except after c, or when pronounced ̒ aʼ, as in ʻneighborʼ and ʻweighʼ --except for ʻweirdʼ examples.”• grep “[^c]ei” /usr/share/dict/words > foo cat foo abseil Aeneid ageing Alamein albeit atheistBoeing Budweiser caffein canoeist deice deicticdilettanteism dreidl ...
• if you think this spelling rule is sufficient, you will be deficient,inefficient, unscientific and far from omniscient
L Regular ⇔ L Denoted by a Reg. Expr.• Weʼve defined “regular” as meaning: recognized by a DFA
(equiv. to rec. by an NFA)• This equivalence result is known as “Kleeneʼs Theorem”• Weʼve already shown the ⇐ direction—we constructed an
NFA from a regular expression (Using Rabin-Scott wecould convert this NFA to a DFA.)
• Now we show the ⇒ direction: given a DFA M constructa regular expr. E with L(M) = L(E).
• Thm (Kleene): There is an algorithm that, given a DFA M ,computes a regular expression E such that L(M) = L(E).
• Pf: Given the graph of the DFA, use the “node eliminationalgorithm” to gradually eliminate all nodes in favor ofexpressions on the edges of the graph.
C SC 473 Automata, Grammars & Languages 69
B
C
1
0
01
A 1 0
Sa
ε ε
B
C
1
0
01
A 1 0
B1
00
0
1
ε ε
Kleeneʼs Thm: use “Generalized NFA”
A
Sa
01
A
Sa
1(1+01)*00+0
εε
S
a
ε(1(1+01)*00+0)*ε
E =(1(1+01)*00+0)*
1. Add init. S, &final a
2. Elim. C
3. Elim. B 4. Elim. A
Order: CBA
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 24
C SC 473 Automata, Grammars & Languages 70
Ex: Node Elimination AlgorithmB
A
b
b
ab
a
C
B C
A
b
b
ab
a
Add ε-moves:
S εa
ε
C SC 473 Automata, Grammars & Languages 71
B Cb
b
ba*a
S εa
ε
B
bb
bba*a
S εa
b
(bb+bba*a)*bS a
Elim. A:
Elim. C:
Elim. B:
ACB
Ex: Node Elimination Algorithm (ACB)
A
AC
C SC 473 Automata, Grammars & Languages 72
B C
A
b
b
ab
a
S εa
ε
Ex: Node Elimination: other orders
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 25
C SC 473 Automata, Grammars & Languages 73
Bb
S εa
B
bb
bba*a
S εa
b
(bb+bba*a)*bS a
Elim. C:
Elim. A:
Elim. B:
CAB=ACB
Ex: Other elimination orders (CAB)
bb
A
bba
a
CA= AC (above)
C
C SC 473 Automata, Grammars & Languages 74
Bb
S εa
A
S a
(bb)*b
(bb)*b+(bb)*bba*a(bb)*bS a
Elim. C:
Elim. B:
Elim. A:
CBA
Ex: Other elimination orders (CBA)
bb
A
bba
a
CB
C
(bb)*bb
a
a(bb)*b
C SC 473 Automata, Grammars & Languages 75
A
S a
b(bb+ba*ab)*S a
Elim. B:
Elim. A:
BAC=ABC
Ex: Other elimination orders (BAC)
BA =AB
B
a
Cb
bbε
bab
S aCb bb
ba*ab
ε
Elim. C:
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 26
C SC 473 Automata, Grammars & Languages 76
Ex: All elimination orders equiv (BAC = ACB)
BAC b(bb+ba*ab)*
ACB (bb+bba*a)*b
Easy to prove by induction: for any expression E, b(Eb)*=(bE)*b.Using this identity:
b(bb+ba*ab)* = b[ (b+ba*a) b]* = [b (b+ba*a) ]*b
= (bb+bba*a)*b
Further regular expression simplication is possible:
Good exercise: show results of all other elimination orders areequivalent to these, using regular expression algebra
C SC 473 Automata, Grammars & Languages 77
Algebra of Regular Expressions• ∃ an algebra for symplifying regular expressions• Can use this algebra to construct RegExps from FSA
r+s = s+r (r+s)+t=r+(s+t) r+r=r r+∅=r
(rs)t = r(st) rε = εr = r r∅ = ∅r = ∅
r(s+t) = rs + rt (r+s)t = rt + st
∅* = ε (r*)* = r* r* = r + r*
r* = ε + rr* (r*s*)* = (r+s)*
C SC 473 Automata, Grammars & Languages 78
Solving Regular Expr Equations• Can solve “linear equations” with regexp variables
X = aX + b
=a(aX + b) + b = a2X + ab + b
= a2 (aX + b) + ab + b = a3X + a2b + ab + b … X = a*bCheck: a[a*b] + b = aa*b + b = (aa*+ε)b = a*bEx:
Xb
a
YX = aX + bYY = ε X = a*b
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 27
C SC 473 Automata, Grammars & Languages 79
Solving RegExp Equations (contʼd)• Ex: NFA RegExp B
C
1
0
01
A 1 00 1
1 00 1
___ elim. ____ 10
0 110
0 110
___ elim. ____ (110)0
0 110(110)0
[0 110(110)0]
[0 110(110)0]Simplify using reg. algebra:
0 110(110)0 [ 110(11
A A B
B B C
C A B
B B C
A A C
C A C
C C A
A A A
A
!
!
!
!
!
"
"
"
" "
" " "
" " "
" " " "
" " " " "
= + +
= +
= +
=
= + +
= +
=
= + +
= + +
= +
+ = + 0)]0
(110)0
((110) 0)A
"
" "
" " "
=
# = Gauss-Jordan elimination& back-substitution
C SC 473 Automata, Grammars & Languages 80
Ex: Node Elimination Example via Algebra
• Want B. C is accept state.
• Elim. A:
• Elim B:• Elim C:• ∴
B
A
b
b
ab
a
C
A aA aB
B bC
C bA bB !
= +
=
= + +
*A a aB=
*
B bC
C ba aB bB !
=
= + +
B bC= *( * )
C ba abC bbC
ba ab bb C
!
!
= + +
= + +( * ) *C ba ab bb= +
( * ) *B bC b ba ab bb= = + Simplifies to = ( * ) *B b ba b
C SC 473 Automata, Grammars & Languages 81
Closure Properties• A class of languages is said to be closed under an
operation if applying that operation to members of theclass results in a language that is again a member of theclass. Example: the regular languages are closed underthe operations of union, concatenation and Kleene star.
• Thm: The regular languages are closed under intersectionand complementation.
Pf: Complementation. Let L = L(M) where is a DFA. Then the FA is also deterministic, and So w leads to a non-
accepting state in ⇔ w leads to an accepting state in So
( , , , , )M Q s F!= "
( , , , , )M Q s F!= "
(s,w) !
M
!(q,") # (s,w) !
M
!(q,").
.MM
( ) ( ).L M L M!
= " #
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 28
C SC 473 Automata, Grammars & Languages 82
Closure Properties (contʼd) Intersection. Let be regular. By DeMorganʼs law
Since the regular languages are closed undercomplementation and union, the result follows.
1 2,L L
1 2 1 2( ).L L L L! = "
!
C SC 473 Automata, Grammars & Languages 83
Closure Properties (contʼd)• Another proof of closure under ∩ illustrates the technique
called “cross-product construction”. See Sipser text,Theorem 1.25.
• Thm: The class of regular languages is closed under theintersection operation.
Pf: Assume and , where the automata
are deterministic. Construction. Construct a “cross-product machine” M as
follows: where the transition function is defined by:
Machine M simulates the two given machines “in parallel”, keeping each machine state in one component of
1 1 1 1 1 1 1 1( ), ( , , , , )L L M M Q s F!= = "
2 2 2 2 2 2 2 2( ), ( , , , , )L L M M Q s F!= = "
1 2 1 2 1 2 1, 2 1 2( , , ,( ), )M Q Q s s F F! != " # $ # " "
Closure Properties (contʼd) Verification By an easy induction on |x|, can show that
Therefore, for a pair of final states
This says that
i.e., that
((q1,q
2),x) !
M
!((p
1,p
2),") #
(q1,x)!
M1
!(p
1,") $ (q
2,x)!
M2
!(p
2,") !
((s1,s
2),x)!
M
!((f
1,f
2),") #
(s1,x)!
M1
!(f
1,") $ (s
2,x)!
M2
!(f
2,")
1 1 2 2,f F f F! !
1 2( ) ( ) ( )x L M x L M x L M! " ! # !
1 2( ) ( ) ( ).L M L M L M= ! !
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 29
C SC 473 Automata, Grammars & Languages 85
Closure Properties (contʼd)• Defn: A homomorphism h is a function that maps each
symbol of to a string over somealphabet Δ, i.e.,
• The homomorphism is extended to operate on stringscharacter-by-character, i.e.,
• It is further extended to languages element-wise, i.e.,
• Thm: If L is regular and h is a homomorphism, then h(L) isregular.
Pf: Assume L is recognized by a DFA
1 2{ , , , }
na a a! = …
1 1 2 2( ) , ( ) , , ( )
n nh a w ha w ha w= = =…
1 2 1 2( ) ( )( ) ( ).
n nhc c c hc hc hc=… …
( ) {( ): }.h L hw w L= !
( , , , , ).M Q s F!= "
C SC 473 Automata, Grammars & Languages 86
Closure Properties (contʼd)• Construction: Construct the machine where for each transition in
M:
put into Mh the transition An easy induction establishes that
from which it follows that
( , , , , )h h
M Q s F!= "
ap q h(a)
p q
(s,w) !
M
!(q,") # (s,h(w)) !
Mh
!(q,"),
( ) ( ( )).h
L M h L M= !
Note: GNFA
C SC 473 Automata, Grammars & Languages 87
What is Not Regular?• FA have a very limited computing ability. They cannot, for
example, recognized strings of well-nested parentheses,or well-formed arithmetic expressions, or even thelanguage of strings of the form w#w, having two copies ofthe same substring.
• How can we show some languages are not regular? Wewill give a property that all regular languages must have(called the pumping property). Then, to show that alanguage L is not regular, we argue that it lacks thispumping property.
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 30
C SC 473 Automata, Grammars & Languages 88
Pumping Lemma• Thm [Pumping Lemma for Regular Languages]. Suppose
that L is an infinite¹ regular language. Then
¹All finite languages are regular, so only infinite languages areof interest.
( )( )[
( , , )(
( 0) )]i
p w w L w p
x y z w xyz y xy p
i xy z L
!
" # $ % & '
" = % ( % )
% # & $
C SC 473 Automata, Grammars & Languages 89
Pumping Lemma (English)• Thm [Pumping Lemma for Regular Languages]. Suppose
that L is an infinite¹ regular language. Then there is somenumber p (called the pumping length) such that:
if w is any string in L with |w| ≥ p, then w can be factored into 3 substrings, w = xyz, that satisfy the following 3 conditions: (i) y ≠ ε [y is not empty] (ii) |xy| ≤ p [the prefix and pumped part are short] (iii) for every i ≥ 0, xyiz ∈ L [“pumped up” and
“pumped down” (i = 0) versions of the string must also be in L]
¹All finite languages are regular, so only infinite languages areof interest.
C SC 473 Automata, Grammars & Languages 90
Pumping Lemma (contʼd)• Pf: Let be a DFA recognizing L and
let p be the number of its states. Let be an input string of length n where n
≥ p. Let be the sequence of states that M
enters while processing w so that for This state sequence has length n+1 ≥ p+1. Among the first p+1 states of this sequence, at least 2
must be the same state [“pigeonhole principle”]. Call thefirst of these 2 and the second . Because
occurs among the first p+1 places in thesequence we have that k ≤ p+1.Define the following substrings of w:
( , , , , )M Q s F!= "
1 2 nw a a a= !
1 2 1, , ,
n nr r r r
+!
1( , )
i i ir r a!
+=
1 .i n! !
jr
k jr r=
1 2 1, , ,
n nr r r r
+!
1 1 1, ,
j j k k nx a a y a a z a a
! != = =! ! !
k jr r=
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 31
C SC 473 Automata, Grammars & Languages 91
a
rk
rn+1r1
1 1jx a a
!= !
1j ky a a
!= !
=rjk n
z a a= !
Pumping Lemma (contʼd)• Picture:
• From the picture, we see that there is an accepting pathfrom to a final state for all the strings ofthe form . Also, since it mustbe that Furthermore, so
.
1s r= 1n
r+
, 0i
xy z i ! j k!
.y !" 1k p! +
.xy p! !
C SC 473 Automata, Grammars & Languages 92
Non-regular Examples• Ex: is not regular. Pf: By contradiction. Suppose L is regular. Then by the
Pumping Lemma,
Then it follows that is a perfect square. This is impossible. For suppose for a so large that Then
Non-regular Examples (contʼd)• Ex: is not regular. Pf: By contradiction. Suppose L is regular. Then by
closure properties of the regular languages is regular. Now We show cannot
be regular, which provides a contradiction. If is regular, then there are substrings with such that Case 1. y is entirely in the aʼs. Assume it is in the aʼs
before the 2 bʼs (The other subcase is symmetric). Then
and But then where This is a contradiction.
{ : { , }}R
L w w w a b!
= " #
* *
1L L a bba= !
1{ : 0}.
n n
L a bba n= !1L
1L
y !"
, ,x y z
10 .
nn xy z L! " #
, , ( 0)p q r s
x a y a z a bba q= = = >
.p q r s+ + =p r s
a a bba L!
.p r s+ <
CSC 473 Automata, Grammars & Languages 9/29/10
Lecture 03 32
C SC 473 Automata, Grammars & Languages 94
Non-regular Examples (contʼd)• Case 2. y contains a b. Then has more than 2
bʼs, and so This is a contradiction.
Contradictions in all cases ⇒ contradiction to theassumption that is regular. So is not regular.
• Ex: is not regular. See Text,Example 1.73.
• Ex:
is not regular. Pf: Suppose B is regular. Then so is as is its homomorphic image Contradiction.
1L
3xy z
3
1.xy z L!
1L
{ : 0}n n
L a b n= !
{ : is a well-nested
string of parentheses}
B w w=
{( ) : 0}n n
n= !
()B B! !
" = #
{ : 0}.n na b n !
!
!
C SC 473 Automata, Grammars & Languages 95
Decision Problems• For a property/predicate P the decision problem for P is:
Given: x Question: Is P(x) true?
• Ex: Given DFA M, is it true that L(M) = ∅?• Thm: For DFA M all the following decision problems are
solvable, i.e. there exists an algorithm to decide thequestion for any input:
Given Question1. M,w w ∈ L(M) ?2. M L(M) = ∅ ?3. M L(M) = Σ* ?4. M, M′ L(M) ⊆ L(M′ ) ?5. M, M′ L(M) = L(M′ ) ?
C SC 473 Automata, Grammars & Languages 96
Decision Problems (contʼd)
Pf: Assume given DFAs for inputs. (1) Trace w through M. “Yes” if leads from s to some q ∈ F (2) “Yes” if there is some q ∈ F reachable from s (3) Convert (4)