Académique Documents
Professionnel Documents
Culture Documents
24-Jul-17
What is a state machine?
A state machine is a different way of thinking about
computation
A state machine has some number of states, and
transitions between those states
Transitions occur because of inputs
A pure state machine only knows which state it is
init has no other memory or knowledge
This is the kind of state machine you learn about in your
math classes
When you program a state machine, you dont have that
restriction
2
State machine I/O
State machines are designed to respond to a sequence
of inputs, such as
The individual characters in a string
A series of external events
State machines may produce output (often as a result
of transitions)
Alternatively, the result of a state machine may be
the state it ends up in
3
Example I: Even or odd
The following machine determines whether the number of As
in a string is even or odd
Circles represent states; arrows represent transitions
A
even odd
start
A
5
Simplifying drawings I
State machines can get pretty complicated
We can simplify the drawing by leaving out the error
state
The error state is still part of the machine
Any input without a transition on our drawing is assumed to
go to the error state
Another simplification: Use * to indicate all other
characters
This is a convention when drawing the machineit does not
mean we look for an asterisk in the input
6
Example II: Nested parenthesis
The following example tests whether parentheses are properly
nested (up to 3 deep)
( ( (
OK
start
) ) ) )
* * * ( *
Error
How can we extend this machine to handle
*
7
Nested parentheses II
8
Nested parentheses III
( do count++
( do count=1
start
OK
9
The states of a Thread
A Thread is an object that represents a single flow of
execution through a program
A Threads lifetime can be described by a state machine
waiting
10
German vocabulary tutor I
I once wrote a program to provide memorization drill on
English-German vocabulary
Words were presented in English
The user had to type in the German translation
A word was considered learned when the user answered
correctly three times in a row
Each word pair (English + German) could be thought of
as a state machine
11
German vocabulary tutor II
wrong wrong
wrong
start 0th right 1st right 2nd right 3rd right out
12
Example: Making numbers bold
end of input
digit output </b>
end
start output <b>digit
14
Outline of the bold program
void run() {
int state = NORMAL;
for (int i = 0; i < testString.length(); i++) {
char ch = testString.charAt(i);
switch (state) {
case NORMAL: { not inside a number }
case NUMBER: { inside a number }
}
}
if (state == NUMBER) result.append("</b>");
15
The two states
16
Conclusions
A state machine is a good model for a number of
problems
You can think of the problem in terms of a state machine
but not actually do it that way (e.g. German vocabulary)
You can implement the problem as a state machine (e.g.
making integers bold)
Best done as a switch inside some kind of loop
17
The End
18