Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular...

22
Lecture 8 Deterministic Finite Automata CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1

Transcript of Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular...

Page 1: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Lecture 8Deterministic Finite Automata

CS 241: Foundations of Sequential ProgramsWinter 2018

Troy Vasiga et alUniversity of Waterloo

1

Page 2: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 3: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 4: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Finite Automata Example 1

Example: selected opcodes from MIPS assembly language, wherealphabet is the ASCII characters.

4

Page 5: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 6: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Finite Automata Example 2

Example: MIPS labels, where the alphabet is ASCII characters.

6

Page 7: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 8: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 9: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 10: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 11: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

DFA Interpreter Algorithm

Input: A word w = w1w2...wn where each wi ∈ ΣOutput: true if accepted, false if rejected

11

Page 12: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Implementing DFAs

Need to implement the transition function somehow

12

Page 13: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Where are DFAs used?

13

Page 14: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 15: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 16: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 17: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

NFA Interpreter Algorithm

Input: A word w = w1w2...wn where each wi ∈ ΣOutput: true if accepted, false if rejected

17

Page 18: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Implementing an NFA interpreter

18

Page 19: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Differences between NFAs and DFAs

19

Page 20: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 21: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

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

Page 22: Deterministic Finite Automata - UWcs241/tmjvasiga/lec08.pdf · Recognizers: Finite Automata Regular languages can be recognized by nite automata. We begin with deterministic nite

Killer app for Finite Automata/Transducers

Scanner: see asm.*

22