finite state automata - sfs.uni-tuebingen.deddekok/dsa3/lectures/fsa.pdffinite state automata....
TRANSCRIPT
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
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
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Chomsky hierarchy
Daniël de Kok
Finite state automata
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
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
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
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
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
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Closure properties
Daniël de Kok
Finite state automata
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
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
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
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
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Pumping lemma
Daniël de Kok
Finite state automata
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
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
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
NFA to DFA conversion
Daniël de Kok
Finite state automata
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Chomsky hierarchy Closure properties Pumping lemma NFA to DFA conversion Implementation
Implementation
Daniël de Kok
Finite state automata
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
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
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
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
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
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
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
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
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
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
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
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
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