13-Nov-1513-Nov-1513-Nov-15 State Machines. What is a state machine? A state machine is a different way of thinking about computation A state machine.

Slides:



Advertisements
Similar presentations
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Finite Automata with Output
Finite state machines.
CS5371 Theory of Computation
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
14-Jun-15 State Machines. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number.
25-Jun-15 State Machines. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 Intro to Finite Automata Chapter 2 introduces the concept of a Finite Automata (or FA). An FA has several properties: It is theoretical. It allows computer.
CS 201 Functions Debzani Deb.
Introduction to Finite Automata Adapted from the slides of Stanford CS154.
14-Jul-15 State Machines Abbreviated lecture. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine.
Lecture 23: Finite State Machines with no Outputs Acceptors & Recognizers.
1 Unit 1: Automata Theory and Formal Languages Readings 1, 2.2, 2.3.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Input, Output, and Processing
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Lexical Analyzer (Checker)
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Chapter 05 (Part III) Control Statements: Part II.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 5: Structured Programming
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Tokenizers 29-Nov-15. Tokens A tokenizer is a program that extracts tokens from an input stream A token is a “word” or a significant punctuation mark.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
19-Dec-15 Tokenizers. Tokens A tokenizer is a program that extracts tokens from an input stream A token has two parts: Its value—this is just the characters.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
1 CS161 Introduction to Computer Science Topic #8.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
Exercise 1 Consider a language with the following tokens and token classes: ID ::= letter (letter|digit)* LT ::= " " shiftL ::= " >" dot ::= "." LP ::=
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process 
INTRO TO STATE MACHINES CIS 4350 Rolf Lakaemper. State Machines A state machine represents a system as a set of states, the transitions between them,
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 14: Theory of Automata:2014 Finite Automata with Output.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Finite Automata.
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Chapter 5: Structured Programming
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
Chapter 4 C Program Control Part I
Finite-State Machines (FSMs)
Repetition (While-Loop) version]
Introduction to Computer Science / Procedural – 67130
Suppose we want to print out the word MISSISSIPPI in big letters.
Finite-State Machines (FSMs)
Finite State Machines Part I
Stack Data Structure, Reverse Polish Notation, Homework 7
Lesson 2: Building Blocks of Programming
Electronics II Physics 3620 / 6620
Chapter 2 Edited by JJ Shepherd
Tokenizers 25-Feb-19.
State Machines 6-Apr-196-Apr-19.
Tokenizers 26-Apr-19.
Tokenizers 3-May-19.
State Machines 8-May-19.
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
State Machines 16-May-19.
Presentation transcript:

13-Nov-1513-Nov-1513-Nov-15 State Machines

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 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 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 only “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 A s in a string is even or odd Circles represent states; arrows represent transitions 4 Inputs are the characters of a string The “output” is the resultant state The double circle represents a “final” (accepting) state A evenodd start A anything but A

Error 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 state 5

Nondeterministic state machines There are two ways in which a state machine may be nondeterministic: There may be two or more transitions from a state for the same input—either arrow may be followed There may be an “empty transition” (denoted by the Greek letter ε) from a state—the transition might or might not be taken A nondeterministic machine is said to accept (recognize) an input if there is any way for it to get to a final state, given that input 6

Simplifying 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 input 7

8 Example 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 ) ( ) ( ) ( OK Error ) **** ( *

9 Nested 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 features

10 Nested parentheses III This machine is based on a state machine, but it obviously is not just a state machine OK ( do count=1 ) and count==1 do count=0 ( do count++ ) and count>1 do count-- start

11 The 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 machine ready waiting runningdead start

12 Example: Making numbers bold In HTML, you indicate boldface by surrounding the characters with... Suppose we want to make all the integers bold in an HTML page—we can write a state machine to do this NORMALNUMBER digit output digit nondigit output nondigit *: output * end of input output start digit output digit end

13 State 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 transitions

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(" ");

15 The two states case NORMAL: if (Character.isDigit(ch)) { result.append(" " + ch); state = NUMBER; break; } else { result.append(ch); } break; case NUMBER: if (!Character.isDigit(ch)) { result.append(" " + ch); state = NORMAL; break; } else { result.append(ch); } break;

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 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 limitations

17 The End