Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular...
Transcript of Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular...
Lecture 8Deterministic Finite Automata
CS 241: Foundations of Sequential ProgramsWinter 2018
Troy Vasiga et alUniversity of Waterloo
1
Review
I formal languages give a theoretical basis for communication andorganizing processes
I terminology (alphabet, word, language)
I specification vs. recognition
I studying language levels that increase in power/complexity
I regular languages are composed of union, concatenation andrepetition
2
Recognizers: Finite Automata
Regular languages can be recognized by finite automata.We begin with deterministic finite automata, also called DFAs.
I states
I transitions
I start state
I final states
3
Finite Automata Example 1
Example: selected opcodes from MIPS assembly language, wherealphabet is the ASCII characters.
4
Observations About Finite Automata
I ability to trace
I transitions out of a state are unique
I errors
I size of this language
I DFA M and language L(M)
5
Finite Automata Example 2
Example: MIPS labels, where the alphabet is ASCII characters.
6
More Finite Automata Examples
Let Σ = {a, b, c}.I strings with exactly one a and exactly one b and no c’s
I strings with one a, one b and one c
I strings with at least one a
I string with an even number of a’s
7
More Finite Automata Examples
Let Σ = {a, b, c}.I strings with an even number a’s and odd number of b’s
I strings with an even number a’s or odd number of b’s
8
DFA summary
I a.k.a. finite state machines
I start state
I final/accepting states
I implicit error state
I accepted and rejected words
I L(M) – the language recognized by DFA M
I Notice that L(M) = L(M ′) even though M 6= M ′
9
Formal Definition
A DFA is a 5-tuple (Σ,Q, q0,A, δ) where
I finite alphabet Σ
I finite set of states Q
I start state q0I set of final/accepting states A ⊆ Q
I transition function: δ : Q × Σ→ Q
10
DFA Interpreter Algorithm
Input: A word w = w1w2...wn where each wi ∈ ΣOutput: true if accepted, false if rejected
11
Implementing DFAs
Need to implement the transition function somehow
12
Where are DFAs used?
13
NFAs
I L = {bba, baa, bbaa, bbbaa, bbbbaa, ...} which is either 2 b’sfollowed by an a, or 1 or more b’s followed by 2 a’s
I try to derive this using a DFA
14
NFAs
I L = {bba, baa, bbaa, bbbaa, bbbbaa, ...} which is either 2 b’sfollowed by an a, or 1 or more b’s followed by 2 a’s
I try to derive using a nice NFA
15
NFA definition
Same as a DFA with the following change:
T : Q × Σ→ 2Q
That is, we can be in a set of states, and thus T is a relation instead of afunction.
16
NFA Interpreter Algorithm
Input: A word w = w1w2...wn where each wi ∈ ΣOutput: true if accepted, false if rejected
17
Implementing an NFA interpreter
18
Differences between NFAs and DFAs
19
A few words about the subset construction
Apply the subset construction on the example languageL = {bba, baa, bbaa, bbbaa, bbbbaa, ...} which is either 2 b’s followed byan a, or 1 or more b’s followed by 2 a’s
20
Review
I An example comparing DFAs and NFAs: create an NFA (then aDFA) for all words over Σ = {a, b} with the subword aba in them.
I Can convert between an NFA and DFA using the subset construction
I define each set of states that can be occupied at the same stepI one state in the DFA for each unique set of states in the NFA
21
Killer app for Finite Automata/Transducers
Scanner: see asm.*
22