Finite State Machines Dr K R Bond 2009

Slides:



Advertisements
Similar presentations
Automata Theory Part 1: Introduction & NFA November 2002.
Advertisements

Modeling Computation Chapter 13.
Finite State Machines Finite state machines with output
4b Lexical analysis Finite Automata
Chapter Section Section Summary Set of Strings Finite-State Automata Language Recognition by Finite-State Machines Designing Finite-State.
1 The scanning process Main goal: recognize words/tokens Snapshot: At any point in time, the scanner has read some input and is on the way to identifying.
Computational Language Finite State Machines and Regular Expressions.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 1 Regular Languages Contents Finite Automata (FA or DFA) definitions, examples,
Finite Automata Chapter 5. Formal Language Definitions Why need formal definitions of language –Define a precise, unambiguous and uniform interpretation.
Topics Automata Theory Grammars and Languages Complexities
Topic #3: Lexical Analysis
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
Finite-State Machines with No Output Longin Jan Latecki Temple University Based on Slides by Elsa L Gunter, NJIT, and by Costas Busch Costas Busch.
Finite-State Machines with No Output
REGULAR LANGUAGES.
AUTOMATA THEORY Reference Introduction to Automata Theory Languages and Computation Hopcraft, Ullman and Motwani.
Regular Expressions and Finite State Automata  Themes  Finite State Automata (FSA)  Describing patterns with graphs  Programs that keep track of state.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
Lexical Analyzer (Checker)
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
Dr. Eng. Farag Elnagahy Office Phone: King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
CS1Q Computer Systems Lecture 11 Simon Gay. Lecture 11CS1Q Computer Systems - Simon Gay 2 The D FlipFlop The RS flipflop stores one bit of information.
Finite State Machines 1.Finite state machines with output 2.Finite state machines with no output 3.DFA 4.NDFA.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 1 Regular Languages Some slides are in courtesy.
Sahar Mosleh California State University San MarcosPage 1 Finite State Machine.
Modeling Computation: Finite State Machines without Output
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
Language Translation Part 2: Finite State Machines.
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
1 Chapter 2 Finite Automata (part a) Hokkaido, Japan.
Theory of Computation Automata Theory Dr. Ayman Srour.
Theory of Computation Automata Theory Dr. Ayman Srour.
Finite-State Machines (FSM) Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth Rosen.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Finite Automata.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
1.3 Finite State Machines.
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
Lexical analysis Finite Automata
Compilers Welcome to a journey to CS419 Lecture5: Lexical Analysis:
Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Modeling Arithmetic, Computation, and Languages
Chapter 2 Finite Automata
Finite Automata a b A simplest computational model
G. Pullaiah College of Engineering and Technology
Pushdown Automata.
Pushdown Automata.
Deterministic Finite Automata
[Week#03,04] (b) - Finite Automata
Two issues in lexical analysis
Recognizer for a Language
Chapter 9 TURING MACHINES.
Some slides by Elsa L Gunter, NJIT, and by Costas Busch
Non-Deterministic Finite Automata
Recognition of Tokens.
What Are They? Who Needs ‘em? An Example: Scoring in Tennis
NFAs and Transition Graphs
Finite Automata.
Finite Automata.
4b Lexical analysis Finite Automata
4b Lexical analysis Finite Automata
Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC312 Automata Theory Transition Graphs Lecture # 9
NFAs and Transition Graphs
Formal Languages and Automata Theory BODDEDA HARITHA LAKSHMI,
Regular grammars Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Automata theory and formal languages COS 3112 – AUTOMATA THEORY PRELIM PERIOD WEEK 1 AND 2.
Presentation transcript:

Finite State Machines Dr K R Bond 2009 NEW A2 COMPUTING Finite State Machines Dr K R Bond 2009 © Dr K R Bond 2009

NEW A2 COMPUTING Why finite state machines are useful Many kinds of task can be modelled using a structure called a finite state machine. Finite state machines (FSMs) are used extensively in applications in computer science. For example, finite state machines are the basis for any kind of controller, e.g. traffic lights © Dr K R Bond 2009

NEW A2 COMPUTING Programs for Specifying a language, eg given any string, an FSM determines if that string is in the language or not Programs for spell checking grammar checking indexing or searching large bodies of text recognising speech processing text containing mark-up languages such as XML and HTML Networking protocols that specify how computers communicate. © Dr K R Bond 2009

NEW A2 COMPUTING An FSM is used when searching for words in a large piece of text that match a given pattern such as br*v*, where the * can match any sequence of non-blank characters. The FSM would find matches, for example, in brave, bravo and brevity. © Dr K R Bond 2009

NEW A2 COMPUTING s a | b b | a t The a part of the label is called the transition’s trigger and denotes the input symbol An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b s a | b b | a t The b part which is optional denotes the output symbol State transition diagram: a directed graph whose nodes represent the states State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. State transition diagram for a Finite State Machine with outputs © Dr K R Bond 2009

NEW A2 COMPUTING A finite state machine (FSM) has a set of input symbols (input symbol alphabet) If it produces output, it has a set of output symbols (output symbol alphabet). The FSM in the figure has an input alphabet consisting of the symbols a and b and an output alphabet consisting of the symbols a and b. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING We can express this using set notation as follows: Input alphabet = {a, b} Output alphabet = {a, b} {} enclose the symbols which are members of the set. Using this input alphabet, input strings to this FSM can be any combination of a’s and b’s, e.g. aabbaab, bbaa, ba. ……, bbb State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING The FSM has a finite set of states, {s, t}. The FSM has a transition function that maps a state-symbol pair (current state and input symbol) to a state (next state) and possibly generates an output (output symbol) depending on the type of FSM. We can use a transition table to represent this transition function. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING Current state s t Input symbol a b Next state Output symbol State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING Transition function: maps (input symbol, current state)  (output symbol, next state) Transition table: lists the mappings in tabular form (input symbol, current state)  (output symbol, next state) State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING The FSM has one state which is designated as the start or initial state. This is indicated by the symbol on the state transition diagram. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING Thus starting in state s, for input aabba the output generated by the FSM is bbaab. Satisfy yourself that the purpose of this FSM is to convert a’s into b’s and b’s into a’s. s a | b b | a t State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING Remember A finite-state machine represents a system as a set of states, the transitions between the states along with the associated inputs and outputs drawn from the machine’s alphabet of symbols. One state is designated the start state. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING To prevent ambiguity as to the machine’s next move (i.e. in order that its behaviour is deterministic) we require that every state has exactly one transition or edge leaving it for each of the symbols in the input symbol alphabet. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING s a | b b | a t A state transition diagram for which there is more than one outgoing transition or edge for the same trigger This diagram describes an FSM which is nondeterministic because it contains an ambiguity. s a | b b | a t State s has two transitions labelled with the trigger a State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. The FSM has two choices to exit state s when the input symbol is a © Dr K R Bond 2009

NEW A2 COMPUTING There is another way that an FSM can be nondeterministic. This occurs when for one or more input symbols in the input symbol alphabet there is no matching transition from a state. s a b t To see why this is nondeterministic, consider the situation when the FSM is in state s and the next input symbol is b. There is no outgoing transition for this case State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING States that have no outgoing transitions are called halting states. If the FSM has not exhausted the input when it finds itself in halting state then it will usually report an error. It will usually report an error if it is in a state with outgoing transitions but none of these has a trigger which matches the current input symbol State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

(input symbol, current state)  (action, next state) NEW A2 COMPUTING The FSM may be constructed to perform an action at the output stage instead of outputting a symbol: If a match is found for the pair (input symbol, current state) then an associated action function will be called and the current state will be set to the next state Time displayed Date displayed button a display pressed date pressed time State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. (input symbol, current state)  (action, next state) © Dr K R Bond 2009

(input signal, current state)  (action, next state) NEW A2 COMPUTING (input signal, current state)  (action, next state) Time displayed Date displayed button a display pressed date pressed time A digital watch with a button a which is pressed to switch state The input symbol is replaced by a signal which signals the pressing of button a event State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING Finite State Automata A finite state automaton (FSA) or finite automaton is a finite state machine that produces no output whilst processing the input. Finite state automata (plural of finite state automaton) solve decision problems, i.e. problems where the outcome is either YES or NO. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING A finite state automaton simply runs through the input sequence of symbols, one by one, changing states as a result of the current state and the current symbol from the input it sees. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

NEW A2 COMPUTING a b a or b a or b Finite state automaton with an alphabet of {a, b} and four states, S0, S1, S2 and S3, starting state S0 and one accepting state S3 On reaching the end of the input it stops and depending on which state it stopped in, it outputs a YES or a NO. S0 a b a or b S1 S2 S3 An FSA always has one or more accepting states. The states which cause the FSA to produce a YES response are called accepting states. All other states are therefore, by default, states which cause the FSA to produce a NO response. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. a or b Accepting states are indicated by a state with a double ring, e.g. S3 © Dr K R Bond 2009

NEW A2 COMPUTING a b a or b PC Activity S0 a b S2 S1 a or b S3 PC Activity Use the FSM simulator to implement the FSA described by this figure. Try different combinations of a’s and b’s The FSA in this figure recognizes any string that starts with an “a” and is followed by zero or more “a”s or “b”s, e.g. abbbbaaabbbaabb is a valid string, baaabbbaa is not. Check this for yourself. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009