DOC PREVIEW
Penn CIT 594 - State Machines

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

State MachinesWhat is a state machine?State machine I/OExample I: Even or oddError statesSimplifying drawings IExample II: Nested parenthesisNested parentheses IINested parentheses IIIThe states of a ThreadExample: Making numbers boldState machines in JavaOutline of the bold programThe two statesConclusionsThe EndJan 13, 2019State Machines2What 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 in—it 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 don’t have that restriction3State 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 only “result” of a state machine may be the state it ends up in4Example 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Inputs are the characters of a stringThe “output” is the resultant stateThe double circle represents a “final” (accepting) stateAeven oddstartAanything but A anything but A5Error statesAgain, a state machine is a way of doing certain kinds of computationsThe input is a sequence of values (typically, a String)Some inputs may be illegal (for example, syntax errors in a program)A state machine is used to recognize certain kinds of inputsWe say the machine succeeds if it recognizes its input, otherwise it failsSome states may be marked as final states (they are drawn with concentric circles)A state machine succeeds if:It is in a final state when it reaches the end of its inputA state machine fails if:It encounters an input for which it has no defined transitionIt reaches the end of its input, but is not in a final stateState machines may have a error state with the following characteristics:The error state is not a final stateAn unexpected input will cause a transition to the error stateAll subsequent inputs cause the state machine to remain in the error state6Simplifying drawings IState machines can get pretty complicatedThe formal, mathematical definition of a state machine requires it to have a transition from every state for every possible inputTo satisfy this requirement, we often need an error state, so we can have transitions for illegal (unrecognized) inputsWhen we draw a state machine, we don’t need to draw the error state--we can just assume it’s there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 machine—it does not mean we look for an asterisk in the input7Example II: Nested parenthesisThe following example tests whether parentheses are properly nested (up to 3 deep)How can we extend this machine to handle arbitrarily deep nesting?start)()()(OKError)* * * *(*8Nested parentheses IIQuestion: How can we use a state machine to check parenthesis nesting to any depth?Answer: We can’t (with a finite number of states)We need to count how deep we are into a parenthesis nest: 1, 2, 3, ..., 821, ...The only memory a state machine has is which state it is inHowever, if we aren’t required to use a pure state machine, we can add memory (such as a counter) and other features9Nested parentheses IIIThis machine is based on a state machine, but it obviously is not just a state machineOK( do count=1) and count==1do count=0( do count++) and count>1 do count--start10The states of a ThreadA Thread is an object that represents a single flow of execution through a programA Thread’s lifetime can be described by a state machinereadywaitingrunning deadstart11Example: Making numbers boldIn HTML, you indicate boldface by surrounding the characters with <b> ... </b>Suppose we want to make all the integers bold in an HTML page—we can write a state machine to do thisNORMAL NUMBERdigitoutput <b>digitnondigitoutput </b>nondigit*: output *end of inputoutput </b>startdigitoutput digitend12State machines in JavaIn a state machine, you can have transitions from any state to any other stateThis is difficult to implement with Java’s loops and if statementsThe trick is to make the “state” a variable, and to embed a switch (state) statement inside a loopEach case is responsible for resetting the “state” variable as needed to represent transitions13Outline 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>");14The two statescase NORMAL: if (Character.isDigit(ch)) { result.append("<b>" + ch); state = NUMBER; break; } else { result.append(ch); } break;case NUMBER: if (!Character.isDigit(ch)) { result.append("</b>" + ch); state = NORMAL; break; } else { result.append(ch); }break;15Conclusions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You can implement the problem as a state machine (e.g. making integers bold) Best done as a switch inside some kind of loopPure state machines have some severe limitationsJava lets you do all kinds of additional tests and actions; you can ignore these limitations16The


View Full Document

Penn CIT 594 - State Machines

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download State Machines
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view State Machines and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view State Machines 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?