finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdffinite state automata....
TRANSCRIPT
![Page 1: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/1.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Finite state automata
Daniël de Kok
Daniël de Kok
Finite state automata
![Page 2: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/2.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Today
Chomsky hierarchyClosure propertiesProving that a language is not regularNFA to DFA conversionImplementation of finite state automata
Daniël de Kok
Finite state automata
![Page 3: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/3.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Chomsky hierarchy
Daniël de Kok
Finite state automata
![Page 4: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/4.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Formal grammar
A formal grammar G consists of:
A finite set of non-terminal symbols NA finite set of terminal symbols
∑A finite set of production rules of the form(N ∪ Σ)∗N(N ∪ Σ)∗ → (N ∪ Σ)∗
A start symbol S , S ∈ N
Daniël de Kok
Finite state automata
![Page 5: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/5.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example
N = {S}Σ = {a, b}Production rules:S → a S bS → ε
Defines the language anbn, n ≥ 0
Daniël de Kok
Finite state automata
![Page 6: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/6.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Chomsky hierarchy
A, B : non-terminalsα, β, γ: arbitrary strings of terminals and non-terminalsx is a string of terminal symbols
Grammar type Production rulesUnrestricted α→ β, α 6= εContext Sensitive αAβ → αγβ, γ 6= εContext-Free A→ γRegular A→ xB or A→ x
Daniël de Kok
Finite state automata
![Page 7: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/7.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Conversion of a regular grammar
Create a state si for Ni ∈ N.Designate the state corresponding to S to be the start state.Create a state sf , F = {sf }For rules of the form Ni → Nj , make a transition between siand sj with an ε transition.For rules of the form Ni → ε, make an ε transition from si tosf .For rules of the form Ni → x , make a chain of transitions andstates, recognizing x , starting at si and leading to sf .For rules of the form Ni → xNj , make a chain of transitionsand states recognizing x , starting at si and leading to sj .
Daniël de Kok
Finite state automata
![Page 8: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/8.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example
Convert the following grammar to an automaton:S → AS → BA → aaSB → bbSA → cB → ε
Daniël de Kok
Finite state automata
![Page 9: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/9.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Closure properties
Daniël de Kok
Finite state automata
![Page 10: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/10.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Definition
Non-deterministic finite state automata (NFAs) are closed under:
UnionIntersectionConcatenationNegationKleene closure
(This means that operator application to the language of theoperands leads to a language that can also be described by anNFA.)
Daniël de Kok
Finite state automata
![Page 11: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/11.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Kleene closure
The star (*) in regular expressions.L(A∗) = {ε,A,AA,AAA, ...}Procedure:
Create a new accepting state sk .Create an epsilon transition from sk to the start state.Create an epsilon transition from all accepting states (exceptsk) to sk .Make sk the new start state.
Daniël de Kok
Finite state automata
![Page 12: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/12.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example Kleene closure
0start 1 2
a
a b
0 1 2
3start
a
a b
εε
Daniël de Kok
Finite state automata
![Page 13: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/13.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example Kleene closure
0start 1 2
a
a b
0 1 2
3start
a
a b
εε
Daniël de Kok
Finite state automata
![Page 14: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/14.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Pumping lemma
Daniël de Kok
Finite state automata
![Page 15: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/15.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Intuition
Consider: anbn
An automaton has k states (it is finite).The automaton revisits some state after processing k a’s.ai and aj lead to state q
Now, suppose that the rest of the input state contains b’s.Given ai , after receiving i b’s, it must accept the input, butnot j b’s.Clearly, this is not possible, because when the b’s started, ai
and aj were in the same state.
Daniël de Kok
Finite state automata
![Page 16: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/16.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Pumping lemma
Citing Hopcroft, et al. 2001: Let L be a regular language. Thenthere exists a constant n (which depends on L), such for that everyw ∈ L such that |w | ≥ n, we can break w into three strings,w = xyz , such that:
1 y 6= ε
2 |xy | ≤ n
3 For all k ≥ 0, the string xykz is also in L
Daniël de Kok
Finite state automata
![Page 17: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/17.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
NFA to DFA conversion
Daniël de Kok
Finite state automata
![Page 18: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/18.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Introduction
Any NFA can be converted in a DFA accepting the samelanguage.Important: it proves that an NFA is not more powerful than aDFA.The conversion is done using the so-called subset construction.
Daniël de Kok
Finite state automata
![Page 19: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/19.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Subset construction
Given the NFA < QN ,Σ, δN , q0,FN >, we construct the DFA< QD ,Σ, δD , {q0},FD >:
QD is the power set of QN (the set of subsets).FD contains the subsets of QD , for which hold:∀S ∈ FD : S ∩ FN 6= ε
For each subset S ∈ QD and input symbol in a in Σ:δD(S , a) =
⋃s∈S δN(s, a)
Note: we do not consider ε transitions yet.
Daniël de Kok
Finite state automata
![Page 20: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/20.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example (NFA)
Automaton
0start 1 2a b
a
State table
State a b0 0,11 22*
Daniël de Kok
Finite state automata
![Page 21: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/21.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example (NFA)
Automaton
0start 1 2a b
a
State table
State a b0 0,11 22*
Daniël de Kok
Finite state automata
![Page 22: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/22.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}{1} {2}{2}*{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 23: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/23.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b
{0} {0,1}{1} {2}{2}*{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 24: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/24.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}
{1} {2}{2}*{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 25: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/25.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}{1} {2}
{2}*{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 26: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/26.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}{1} {2}{2}*
{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 27: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/27.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}{1} {2}{2}*{0,1} {0,1} {2}
{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 28: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/28.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}{1} {2}{2}*{0,1} {0,1} {2}{1,2}* {2}
{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 29: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/29.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}{1} {2}{2}*{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 30: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/30.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Power set state table
State table
State a b0 0,11 22*
Power set state table
State a b{0} {0,1}{1} {2}{2}*{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
Note: only states {0}, {0,1}, and {2}are reachable.
Daniël de Kok
Finite state automata
![Page 31: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/31.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example (DFA)
Power set state table
State a b{0} {0,1}{1} {2}{2}*{0,1} {0,1} {2}{1,2}* {2}{0,1,2}* {0,1} {2}
DFA
{0}start {0, 1} {2}a b
a
Daniël de Kok
Finite state automata
![Page 32: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/32.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
State creation
The number of states becomes large quickly: |QD | = 2|QN |
Usually, most states are not reachable.Solution: create states as they are needed.
Daniël de Kok
Finite state automata
![Page 33: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/33.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example 2 (NFA)
Automaton
0start
1
2
3
a
a
b
c
dd
State table
State a b c d0* 1,21 32 33 0,3
Daniël de Kok
Finite state automata
![Page 34: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/34.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example 2
State table
State a b c d0* 1,21 32 33 0,3
Power set state table
State a b c d
{0}* {1,2}{1,2} {3} {3}{3} {0,3}
{0,3}* {1,2} {0,3}
Daniël de Kok
Finite state automata
![Page 35: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/35.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example 2
State table
State a b c d0* 1,21 32 33 0,3
Power set state table
State a b c d{0}* {1,2}
{1,2} {3} {3}{3} {0,3}
{0,3}* {1,2} {0,3}
Daniël de Kok
Finite state automata
![Page 36: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/36.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example 2
State table
State a b c d0* 1,21 32 33 0,3
Power set state table
State a b c d{0}* {1,2}{1,2} {3} {3}
{3} {0,3}{0,3}* {1,2} {0,3}
Daniël de Kok
Finite state automata
![Page 37: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/37.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example 2
State table
State a b c d0* 1,21 32 33 0,3
Power set state table
State a b c d{0}* {1,2}{1,2} {3} {3}{3} {0,3}
{0,3}* {1,2} {0,3}
Daniël de Kok
Finite state automata
![Page 38: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/38.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example 2
State table
State a b c d0* 1,21 32 33 0,3
Power set state table
State a b c d{0}* {1,2}{1,2} {3} {3}{3} {0,3}
{0,3}* {1,2} {0,3}
Daniël de Kok
Finite state automata
![Page 39: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/39.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Example 2 (DFA)
Power set state tableState a b c d{0}* {1,2}{1,2} {3} {3}{3} {0,3}
{0,3}* {1,2} {0,3}
Automaton
{0}start {1, 2}
{3}
{0, 3}a
b,c d
a d
Daniël de Kok
Finite state automata
![Page 40: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/40.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Implementation
Daniël de Kok
Finite state automata
![Page 41: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/41.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Introduction
Broadly two ways to represent the automaton:
Table-basedAs a graph of Java objects
Daniël de Kok
Finite state automata
![Page 42: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/42.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Java object graph
Java has in its memory directed object graphs, e.g.:
We have a Map<String, String> reference on the stack.The stack reference points to an instance of a classimplementing Map on the heap.The map has Entry instances, where each Entry contains areference to a key object and a value object.etc.
Java objects graphs can be cyclic, e.g.: a value in a map couldrefer to that map.
Daniël de Kok
Finite state automata
![Page 43: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/43.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Automata in the object graph
Automata map nicely to Java object graphs.States are objects.Transitions contain references to other objects.If a state becomes unreachable, Java’s garbage collection willclaim it.
Daniël de Kok
Finite state automata
![Page 44: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/44.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Tricky issues
How do we represent epsilons?When are two states equal?
When they are the same object?When they have the same outgoing transitions/finalness?
Daniël de Kok
Finite state automata
![Page 45: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/45.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
State
A state:
Has outgoing transitions (possibly multiple per character forNFAs).Is accepting or not accepting.
public class State {private final SetMultimap<Character, State> transitions;private boolean accept;
// ...}
Daniël de Kok
Finite state automata
![Page 46: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/46.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Constructors
public State() {this(false);
}
public State(boolean accept) {this.accept = accept;transitions = HashMultimap.create();
}
Daniël de Kok
Finite state automata
![Page 47: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/47.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Adding outgoing transitions
public void addTransition(char ch, State toState) {transitions.put(ch, toState);
}
Daniël de Kok
Finite state automata
![Page 48: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/48.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Epsilon transitions
How do we implement epsilons?
Reserve one character to represent epsilons: bad, maybesomeone wants to use that character.Do some class/interface wrapping around char, so that bothepsilons and characters can be represented.Do not explicitly represent epsilons:
Observation: an epsilon transition can be followed withoutmoving the tape.Solution: follow the epsilon transition when the transition isbeing added.Warning: this only works if the state pointed to is ‘frozen’.
Daniël de Kok
Finite state automata
![Page 49: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/49.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Epsilon transitions (2)
public void addEpsilonTransition(State toState) {Preconditions.checkNotNull(toState);
if (toState.isAccept()) {setAccept(true);
}
for (Map.Entry<Character, State> toTransition :toState.transitions.entries()) {
transitions.put(toTransition.getKey(),toTransition.getValue());
}}
Daniël de Kok
Finite state automata
![Page 50: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/50.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Automaton
The automaton should provide:
A reference to the start state.Some useful methods.
Daniël de Kok
Finite state automata
![Page 51: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/51.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Automaton (2)
public class FSA {private State startState;
public FSA(State startState) {Preconditions.checkNotNull(startState);this.startState = startState;
}
// ...}
Daniël de Kok
Finite state automata
![Page 52: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/52.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Automaton: getting all states
public Set<State> getStates() {Set<State> states = new HashSet<State>();
Queue<State> stateQueue = new LinkedList<State>();stateQueue.add(startState);while (!stateQueue.isEmpty()) {
State state = stateQueue.poll();states.add(state);
for (State toState :state.getTransitions().values()) {
if (!states.contains(toState)) {stateQueue.add(toState);
}}
}return states;
}
Daniël de Kok
Finite state automata
![Page 53: Finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdfFinite state automata. Chomsky hierarchyClosure propertiesPumping lemmaNFA to DFA conversionImplementation Pumpinglemma](https://reader030.vdocument.in/reader030/viewer/2022041201/5d4a2a8b88c9931f708b8e8d/html5/thumbnails/53.jpg)
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Automaton: get accept states
public Set<State> getAcceptStates() {Set<State> acceptStates = new HashSet<State>();
for (State state : getStates()) {if (state.isAccept()) {
acceptStates.add(state);}
}
return acceptStates;}
Daniël de Kok
Finite state automata