Finite-State Machines with No Output Ying Lu Based on Slides by Elsa L Gunter, NJIT, Costas Busch...
-
Upload
virginia-turkett -
Category
Documents
-
view
225 -
download
1
Transcript of Finite-State Machines with No Output Ying Lu Based on Slides by Elsa L Gunter, NJIT, Costas Busch...
Finite-State Machines with No OutputYing Lu
Based on Slides by Elsa L Gunter, NJIT,
Costas Busch, and Longin Jan Latecki, Temple University
Kleene closure
• A and B are two sets of strings. The concatenation of A and B isAB={xy: x string in A and y string in B}
• Example: A={0, 11} and B={1, 10, 110}AB={01,010,0110,111,1110,11110}
• A0={λ}, where λ represents the empty stringAn+1=AnA for n=0,1,2,…
Let A be any set of strings formed of characters in V.Kleene closure of A, denoted by A*, is
0
*
k
kAA
Examples:If C={11}, then C*={12n: n=0,1,2,…}
If B={0,1}, then B*={all binary strings}.
Finite State Automata
• A FSA is similar to a compiler in that: – A compiler recognizes legal programs in some (source) language.
– A finite-state machine recognizes legal strings in some language.
• Example: Pascal Identifiers– sequences of one or more letters or digits,
starting with a letter:
letterletter | digit
S A
Finite Automaton
• Input
“Accept” or“Reject”
String
FiniteAutomaton
Output
Finite State Automata
• A finite state automaton over an alphabet is illustrated by a state diagram:
– a directed graph– edges are labeled with elements of alphabet,– some nodes (or states), marked as final– one node marked as start state
Transition Graph
initialstate
accepting state
statetransition
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
Initial Configuration
•
1q 2q 3q 4qa b b a
5q
a a bb
ba,
Input Stringa b b a
ba,
0q
Reading the Input
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
0q 1q 2q 3q 4qa b b a
accept
5q
a a bb
ba,
a b b a
ba,
Input finished
Rejection
•
1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
0q
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
reject
a b a
ba,
Input finished
Another Rejection
•
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
•
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
reject
Another Example
a
b ba,
ba,
0q 1q 2q
a ba
a
b ba,
ba,
0q 1q 2q
a ba
a
b ba,
ba,
0q 1q 2q
a ba
a
b ba,
ba,
0q 1q 2q
a ba
a
b ba,
ba,
0q 1q 2q
a ba
accept
Input finished
Rejection Example
a
b ba,
ba,
0q 1q 2q
ab b
a
b ba,
ba,
0q 1q 2q
ab b
a
b ba,
ba,
0q 1q 2q
ab b
a
b ba,
ba,
0q 1q 2q
ab b
a
b ba,
ba,
0q 1q 2q
ab b
reject
Input finished
Finite State Automata
• A finite state automaton M=(S,Σ,δ,s0,F) consists of
• a finite set S of states,
• a finite input alphabet Σ,
• a state transition function δ: S x Σ S,
• an initial state s0,
• F subset of S that represent the final states.
Finite Automata
• Transition
• Is read ‘In state s1 on input “a” go to state s2’
• At the end of input– If in accepting state => accept
– Otherwise => reject
• If no transition possible (got stuck) => reject
• FSA = Finite State Automata
21 ss a
Input Alphabet
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
ba,
Set of States
•
Q
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
543210 ,,,,, qqqqqqQ
ba,
Initial State
•
0q
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
Set of Accepting States
•
F
0q 1q 2q 3qa b b a
5q
a a bb
ba,
4qF
ba,
4q
Transition Function
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
SS :
ba,
10 , qaq
2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q 1q
50 , qbq
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
32 , qbq
Transition Function
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b
0q
1q
2q
3q
4q
5q
1q 5q
5q 2q
5q 3q
4q 5q
ba,5q5q5q5q
Language accepted by FSA
• The language accepted by a FSA is the set of strings accepted by the FSA.
• in the language of the FSM shown below: x, tmp2, XyZzy, position27.
• not in the language of the FSM shown below: • 123, a?, 13apples.
letterletter | digit
S A
Example
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
abbaML M
accept
Example
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
abbaabML ,, M
acceptacceptaccept
•
Example•
a
b ba,
ba,
0q 1q 2q
}0:{ nbaML n
accept trap state
Extended Transition Function
•
*
QQ *:*
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
20 ,* qabq
3q 4qa b b a
5q
a a bb
ba,
ba,
0q 1q 2q
40 ,* qabbaq
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
50 ,* qabbbaaq
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
qwq ,*
Observation: if there is a walk from to with label then
q qw
q qw
q qkw 21
1 2 k
50 ,* qabbbaaq
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
Example: There is a walk from to with label
0qabbbaa
5q
Recursive Definition )),,(*(,*
,*
wqwq
q qw1q
qwq
),(
,*
1
1
1
,*
),(,*
qwq
qwq
)),,(*(,* wqwq
Language Accepted by FSAs
• For a FSA
• Language accepted by :
•
FqQM ,,,, 0
M
FwqwML ,*:* 0
0q qw Fq
Observation
• Language rejected by :
FwqwML ,*:* 0
M
0q qw Fq
Example
• ML = { all strings with prefix }ab
a b
ba,
0q 1q 2q
accept
ba,3q
ab
Example
• ML = { all strings without substring }001
0 00 001
1
0
1
10
0 1,0
Example
• *,:)( bawawaML
a
b
ba,
a
b
ba
0q 2q 3q
4q
Deterministic FSAs
• If a FSA has for every state exactly one edge for each letter in alphabet then FSA is deterministic
• In general a FSA is non-deterministic.
• Deterministic FSA special kind of non-deterministic FSA
Example FSA
• Deterministic FSA
• Regular expression: (0 1)* 1
0 1
1
0
Example DFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0 1
1
0
Example DFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0 1
1
0
Example DFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0 1
1
0
Example DFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0 1
1
0
Example DFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0 1
1
0
Example DFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0 1
1
0
Example DFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0 1
1
0
Example NFSA
• Regular expression: (0 1)* 1
• Non-deterministic FSA
0
1
1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
• Guess
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
• Backtrack
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
• Guess again
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
• Guess0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
• Backtrack
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
• Guess again
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
Example NFSA
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
• Guess (Hurray!!)
0
1
1
• Regular expression: (0 1)* 1
• Accepts string 0 1 1 0 1
If a language L is recognized by a nondeterministic FSA, then L is recognized by a deterministic FSA
NFSA FSA
How to Implement an FSA
A table-driven approach:• table:
– one row for each state in the machine, and
– one column for each possible character.
• Table[j][k] – which state to go to from state j on character k,
– an empty entry corresponds to the machine getting stuck.
The table-driven program for a Deterministic FSA
state = S // S is the start state
repeat {k = next character from the input
if (k == EOF) // the end of inputif state is a final state then acceptelse reject
state = T[state,k]
if state = empty then reject // got stuck
}
In-Class Exercise
• Construct a finite-state automaton that recognizes the set of bit strings consisting of a 0 followed by a string with an odd number of 1s.
Appendix
Regular Expressions
Regular expressions
describe regular languages
Example:
describes the language
*)( cba
,...,,,,,*, bcaabcaabcabca
Recursive Definition,,
1
1
21
21
*
r
r
rr
rr
Are regular expressions
Primitive regular expressions:
2r1rGiven regular expressions and
Examples
)(* ccbaA regular expression:
baNot a regular expression:
Languages of Regular Expressions
: language of regular expression
Example
rL r
,...,,,,,*)( bcaabcaabcacbaL
Definition
For primitive regular expressions:
aaL
L
L
Definition (continued)
For regular expressions and
1r 2r
2121 rLrLrrL
2121 rLrLrrL
** 11 rLrL
11 rLrL
ExampleRegular expression: *aba
*abaL *aLbaL *aLbaL *aLbLaL
*aba ,...,,,, aaaaaaba
,...,,,...,,, baababaaaaaa
Example
Regular expression bbabar *
,...,,,,, bbbbaabbaabbarL
Example
Regular expression bbbaar **
}0,:{ 22 mnbbarL mn
Example
Regular expression *)10(00*)10( r
)(rL = { all strings with at least two consecutive 0 }
Example
Regular expression )0(*)011( r
)(rL = { all strings without two consecutive 0 }
Equivalent Regular Expressions
• Definition:
• Regular expressions and
• are equivalent if
1r 2r
)()( 21 rLrL
Example
L= { all strings without two consecutive 0 }
)0(*)011(1 r
)0(*1)0(**)011*1(2 r
LrLrL )()( 211r 2rand
are equivalentregular expr.
Example: Lexing
• Regular expressions good for describing lexemes (words) in a programming language– Identifier = (a b … z A B … Z) (a
b … z A B … Z 0 1 … 9 _ ‘ )*
– Digit = (0 1 … 9)
Implementing Regular Expressions
• Regular expressions, regular grammars reasonable way to generates strings in language
• Not so good for recognizing when a string is in language
• Regular expressions: which option to choose, how many repetitions to make
• Answer: finite state automata