Finite Automata & Regular Languages
Download
Report
Transcript Finite Automata & Regular Languages
Deterministic Finite Automata
CS 130: Theory of Computation
HMU textbook, Chapter 2 (Sec 2.2)
State-driven programs
Many applications carry out actions only when
particular conditions apply at the given
moment
conditions => state
Examples:
Buying items online: a payment can be made only
after checkout
Performing a simple arithmetic calculation using a
calculator: an operator character should be
pressed only after some sequence of digits have
been entered for the first operand
Model: finite automaton
At any point in time, the “program” or
automaton will be in one state from a set of
states
In the calculator example, the states are:
reading the first operand
first operand read (+ or - is pressed)
reading the second operand
result displayed/ready for next calculation
(= is pressed)
A transition from one state to another state
occurs on a given input symbol
Accepting valid calculator input
We want the automaton to allow (accept)
input strings like this
But “reject” string like this
123+456=
3333-5=
+-2=2
567-=
Symbols are read from left to right and cause
transitions from one state to another based
on the current state and current input symbol
Deterministic Finite Automata
A DFA or deterministic finite automaton M is a
5-tuple, M = (Q, , , q0, F), where:
Q is a finite set of states of M
is the finite input alphabet of M
: Q Q is the state transition function
q0 is the start state of M
F Q is the set of accepting states or final states
of M
DFA example 1
State diagram
Q = { q0, q1 }
= { 0, 1 }
F = { q1 }
0
M
q0
1
0
q1
1
q0
0
q0
1
q1
q1
q1
q0
State
Table
State table &
state transition function
State table
q0
0
q0
1
q1
q1
q1
q0
State transition function
(q0, 0) = q0, (q0, 1) = q1
(q1, 0) = q1, (q1, 1) = q0
A DFA processing a string
Given a string of input symbols
w = a1a2 … an
A DFA M processes the string w as follows:
Start with state qx = q0
For each input symbol ai :
qx = (qx, ai)
If resulting qx F, then w is accepted;
otherwise w is rejected
Extending for strings
Let w be a string over and
let M = (Q, , , q0, F) be a DFA
Define ^: Q X * Q as follows
If w = , then ^(q, w) = ^(q, ) = q
If w ≠ , i.e., w = xa,
(where x is a string and a is a symbol) then
^(q, w) = ^(q, xa) = (^(q, x), a)
A DFA M accepts a string w
when ^(q0, w) F
Language recognized by a DFA
The language L(M) that is recognized by a
DFA M = (Q, , , q0, F), is the set of all
strings accepted by M.
That is, L(M) = { w * | M accepts w }
= { w * | ^(q0, w) F }
Example: For the previous DFA, L(M) is the
set of all strings of 0s and 1s with odd parity,
that is, odd number of 1s.
DFA Example 2
Recognizer for 11*01*
1
B
* means zero or more
occurrences of the
preceding symbol
0
1
1
C
A
0
0
Trap
D
0,1
DFA Example 2
M = (Q, , , q0, F), L(M) = 11*01*
Q = { q0=A, B, C, D }
= { 0, 1 }
0
1
F={C}
A
D
B
B
C
B
C
D
C
D
D
D
DFA Example 3
0
Modulo 3 counter
B
1
0
2
A
1
2
2
1
C
0
DFA Example 3
M = (Q, , , q0, F)
Q = { q0=A, B, C }
= { 0, 1, 2 }
F={A}
0
1
2
A
A
B
C
B
B
C
A
C
C
A
B
DFA Example 4
Recognizing simple calculator expressions
M = (Q, , , q0, F)
Q = { q0=ready, readingop1, op1read,
readingop2 }
= { 0…9, +, -, = }
F = { ready }
See course website for a sample Java
program that simulates this DFA (study how
the transition function is implemented)
Regular Languages and DFAs
A language L * is called regular if
there exists a DFA M such that L(M)=L
Examples of regular languages
Binary strings with odd parity
Language described by 11*01*
Strings that form integers divisible by 3
Simple calculator expressions
Next
Variations on Finite Automata
(rest of Chapter 2)
Nondeterministic Finite Automata (NFAs)
Equivalences of these variations with DFAs
Regular expressions as an alternative
model for regular languages
(Chapter 3)