Title: Finite-State Machines with No Output Longin Jan Latecki Temple University
1Finite-State Machines with No Output Longin Jan
Latecki Temple University
- Based on Slides by Elsa L Gunter, NJIT,
- and by Costas Busch
2Big Picture Three Equivalent Representations
Regular expressions
Each can describe the others
Regular languages
Finite automata
Kleenes Theorem For every regular expression,
there is a deterministic finite-state automaton
that defines the same language, and vice
versa. More in Ch. 13.4
3Kleene closure
- A and B are subsets of V, where V is a
vocabularyThe concatenation of A and B
isABxy x string in A and y string in B - Example A0, 11 and B1, 10,
110AB01,010,0110,111,1110,11110 - What is BA?
- A0?An1AnA for n0,1,2,
4Let A be any subset of V. Kleene closure of A,
denoted by A, is
Examples If C11, then C12n n0,1,2, If
B0,1, then BV.
5Regular Expressions
- Regular expressions
- describe regular languages
- Example
- describes the language
6Recursive Definition
Primitive regular expressions
7Examples
A regular expression
8Languages of Regular Expressions
-
- language of regular expression
- Example
-
9Definition
- For primitive regular expressions
10Definition (continued)
- For regular expressions and
-
11Example
12Example
13Example
14Example
15Example
16Equivalent Regular Expressions
- Definition
- Regular expressions and
- are equivalent if
17Example
all strings without two consecutive 0
18Example 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)
19Implementing 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
20Finite (State) Automata
- A FA 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
letter digit
letter
S
A
21Finite Automaton
Input
String
Output
Accept or Reject
Finite Automaton
22Finite State Automata
- A finite state automation 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
23Transition Graph
initial state
accepting state
transition
state
24Initial Configuration
Input String
25Reading the Input
26 27 28 29Input finished
accept
30Rejection
31 32 33 34Input finished
reject
35Another Rejection
36reject
37Another Example
38(No Transcript)
39(No Transcript)
40(No Transcript)
41Input finished
accept
42Rejection Example
43(No Transcript)
44(No Transcript)
45(No Transcript)
46Input finished
reject
47Finite State Automata
- A finite state automation M(S,S,d,s0,F) consists
of - a finite set S of states,
- a finite input alphabet S,
- a state transition function d S x S ? S,
- an initial state s0,
- F subset of S that represent the final states.
48Finite Automata
- Transition
- s1 ?a s2
- Is read In state s1 on input a go to state
s2 - If end of input
- If in accepting state gt accept
- Otherwise gt reject
- If no transition possible (got stuck) gt reject
- FSA Finite State Automata
49Input Alphabet
50Set of States
51Initial State
52Set of Accepting States
53Transition Function
54 55 56(No Transcript)
57Transition Function
58Exercise
Construct the state diagram for M(S,S,d,s0,F),
where Ss0, s1, s2, s3, S0,1, Fs0, s3and
the transition function d
state Input 0 Input 1
s0 s0 s1
s1 s0 s2
s2 s0 s0
s3 s2 s1
59Language 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.
letter digit
letter
S
A
60Example
- FSA that accepts three letter English words that
begin with p and end with d or t. - Here we use the convenient notation of making the
state name match the input that has to be on the
edge leading to that state.
a
t
p
i
o
d
u
61Example
accept
62Example
accept
accept
accept
63Example
trap state
accept
64Extended Transition Function
65(No Transcript)
66(No Transcript)
67(No Transcript)
68 Observation if there is a walk from to
with label then
69 Example There is a walk from to
with label
70Recursive Definition
71 72Language Accepted by FAs
- For a FA
- Language accepted by
-
73Observation
74Example
all strings with prefix
accept
75Example
all strings without substring
76Example
77Deterministic FSAs
- If FSA has for every state exactly one edge for
each letter in alphabet then FSA is deterministic - In general FSA in non-deterministic.
- Deterministic FSA special kind of
non-deterministic FSA
78Example FSA
- Regular expression (0 ? 1) 1
- Deterministic FSA
79Example DFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
80Example DFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
1
0
1
0
81Example DFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
1
0
1
0
82Example DFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
1
0
1
0
83Example DFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
1
0
1
0
84Example DFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
1
0
1
0
85Example DFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
1
0
1
0
86Example NFSA
- Regular expression (0 ? 1) 1
- Non-deterministic FSA
0
1
1
87Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
88Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
89Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
90Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Guess
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
91Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Backtrack
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
92Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Guess again
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
93Example NFSA
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
- Guess
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
94Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Backtrack
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
95Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Guess again
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
96Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
97Example NFSA
- Regular expression (0 ? 1) 1
- Accepts string 0 1 1 0 1
- Guess (Hurray!!)
- Regular expression (0 1) 1
- Accepts string 0 1 1 0 1
0
1
1
98If a language L is recognized by a
nondeterministic FSA, then L is recognized by a
deterministic FSA Example 10, p. 812
NFSA
FSA
99How to Implement an FSA
- A table-driven approach
- table
- one row for each state in the machine, and
- one column for each possible character.
- Tablejk
- which state to go to from state j on character k,
- an empty entry corresponds to the machine getting
stuck.
100The 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 input
- if state is a final state then accept
- else reject
- state Tstate,k
- if state empty then reject // got stuck