Chapter 5 Finite Automata. 2 5.1 Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.

Slides:



Advertisements
Similar presentations
4b Lexical analysis Finite Automata
Advertisements

1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
61 Nondeterminism and Nodeterministic Automata. 62 The computational machine models that we learned in the class are deterministic in the sense that the.
Fall 2006Costas Busch - RPI1 Deterministic Finite Automata And Regular Languages.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
1 Languages and Finite Automata or how to talk to machines...
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Nondeterminism.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
1 Non-Deterministic Automata Regular Expressions.
Introduction to Finite Automata Adapted from the slides of Stanford CS154.
1.Defs. a)Finite Automaton: A Finite Automaton ( FA ) has finite set of ‘states’ ( Q={q 0, q 1, q 2, ….. ) and its ‘control’ moves from state to state.
CS Chapter 2. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Fall 2004COMP 3351 Another NFA Example. Fall 2004COMP 3352 Language accepted (redundant state)
Costas Busch - LSU1 Non-Deterministic Finite Automata.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Chapter 9 Turing Machine (TMs).
Nondeterminism (Deterministic) FA required for every state q and every symbol  of the alphabet to have exactly one arrow out of q labeled . What happens.
Formal Language Finite set of alphabets Σ: e.g., {0, 1}, {a, b, c}, { ‘{‘, ‘}’ } Language L is a subset of strings on Σ, e.g., {00, 110, 01} a finite language,
Finite-State Machines with No Output
NFA ε - NFA - DFA equivalence. What is an NFA An NFA is an automaton that its states might have none, one or more outgoing arrows under a specific symbol.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
REGULAR LANGUAGES.
1 Unit 1: Automata Theory and Formal Languages Readings 1, 2.2, 2.3.
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
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.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II:
Prof. Busch - LSU1 NFAs accept the Regular Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CHAPTER 1 Regular Languages
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
CS 3813: Introduction to Formal Languages and Automata Chapter 2 Deterministic finite automata These class notes are based on material from our textbook,
Chapter 6 Properties of Regular Languages. 2 Regular Sets and Languages  Claim(1). The family of languages accepted by FSAs consists of precisely the.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 1 Regular Languages Some slides are in courtesy.
Modeling Computation: Finite State Machines without Output
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.
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.
Finite Automata Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture 20Mar 30, 2010Carnegie Mellon.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
Finite Automata A simple model of computation. 2 Finite Automata2 Outline Deterministic finite automata (DFA) –How a DFA works.
1 Finite Automata. 2 Introductory Example An automaton that accepts all legal Pascal identifiers: Letter Digit Letter or Digit "yes" "no" 2.
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
1 Chapter 2 Finite Automata (part a) Hokkaido, Japan.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Lexical analysis Finite Automata
Non Deterministic Automata
Chapter 2 Finite Automata
Deterministic Finite Automata
Two issues in lexical analysis
Recognizer for a Language
Chapter 2 FINITE AUTOMATA.
Jaya Krishna, M.Tech, Assistant Professor
Deterministic Finite Automata
Hierarchy of languages
Some slides by Elsa L Gunter, NJIT, and by Costas Busch
Non-Deterministic Finite Automata
Non Deterministic Automata
Finite Automata.
4b Lexical analysis Finite Automata
4b Lexical analysis Finite Automata
Chapter 1 Regular Language
Presentation transcript:

Chapter 5 Finite Automata

2 5.1 Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for pattern-recognition type applications, such as the lexical analyzer of a compiler n An abstract (computing) machine M, which is implementation independent, can be used to determine the acceptability (the outputs) of input strings (which make up the language of M)

3 Lexical Analyzer n Recognizes occurrences of (valid/acceptable) strings concisely n Use a (state) transition diagram for producing lexical analysis routines, e.g., Figure 1 (next page) n Use a transition table whose entries provide a summary of a corresponding transition diagram, which consists of rows (representing states), columns (representing symbols) and EOS (End_of_string)  Entries of a transition table contain the values “accept”, “error”, next states. e.g., Figure 3 n Can be encoded in a program segment, e.g., Figure 2

4 Transition Diagram and Table letter digit letter digit Figure 2. A transition table constructed from the transition diagram of Figure 1 Figure 1. A transition diagram representing the syntax of a variable name

5 Instruction Sequence Figure 3. An instruction sequence suggested by the transition diagram of Figure 1

6 5.2 Deterministic Finite Automaton n DFA (Deterministic Finite Automaton) is a quintuple M = (Q, , , q 0, F), where 1) Q is a finite set of states 2)  is a finite set of (machine) alphabet 3)  is a transitive function from Q x  to Q, i.e.,  : Q x   Q 4) q 0  Q, is the start state 5) F  Q, is the set of final (accepting) states letter digit letter digit

7 Transition Diagram Figure 5. A transition diagram representing the syntax of a real number

8 Transition Table Table 1. A transition table constructed from the transition diagram of the previous figure accept

9 Deterministic Finite Automaton Figure 6. A representation of a deterministic finite automaton … … Input tape

10 Computation in DFA Figure 5.2 Computation in a DFA M: Q = {q 0, q 1 }  ( q 0, a) = q 1  = {a, b}  ( q 0, b) = q 0 F = { q 1 }  ( q 1, a) = q 1  ( q 1, b) = q 0 a b a q 0

11 State Diagrams n Defn The state diagram of a DFA M = (Q, , , q 0, F) is a labeled graph G defined by the following: i. For each node N  G, N  Q ii. For each arc E  G, label(E)   iii. q 0 is depicted iv. For each f  F, f is depicted v. For each  (q i, a) = q j,  E(q i, q j ) and label(E) = a  a transition is represented by an arc vi. For each q i  Q & a  ,  ! E(q i, q j ) & label(E) = a, where q j  Q n Example: Construct the state diagram of L(M) for DFA M: L(M) = {w | w contains at least one 1 and an even number of 0 follow the first 1} q0q0 q1q1 q2q >

12 Definitions n Defn Let m = (Q, , , q 0, F) be a DFA. The language of m, denoted L(m), is the set of strings in  * accepted by m. n Defn (Machine configuration). The function (“yields”) on Q x  + is defined by [q i, aw] [  (q i, a), w] where a  , w   *, and   M. Also, [q i, u] [q j, v] denotes a sequence of 0 or more transitions. n Defn The function ( ): Q x  *  Q of a DFA is called the extended transition function such that   (q i, ua) =  (  (q i, u), a)) MM M * M *    

13 State Diagrams (Continued) n Example: Give the state diagram of a DFA M such that M accepts all strings that start and end with a, or that start and end with b, i.e., M accepts strings that start and end with the same symbol, over the alphabet  = {a, b}  Note: Interchanging the accepting states and non-accepting states of a state diagram for the DFA M yields the DFA M’ that accepts all the strings over the same alphabet that are not accepted by M. b a b a q0q0 q1q1 q2q2 q3q3 q4q4 a b b b a a >

14 DFA and State Diagrams n Construct a DFA that accepts one of the following languages over the alphabet { 0, 1 } i. “The set of all strings ending in 00”. ii. “The set of all strings when interpreted as a binary integer, is a multiple of 5, e.g., strings 101, 1010, and 1111 are in the language, whereas 10, 100, and 111 are not”.

15 State Diagrams n Theorem Let M = (Q, , , q 0, F) be a DFA. Then M’ = (Q, , , q 0, Q - F) is a DFA w/ L(M’) =  * - L(M) Proof: Let w   * and be the extended transition function constructed form .  Examples and (page 157) partial function n An incompletely specified DFA M is a machine defined by a partial function from Q   to Q such that M halts as soon as it is possible to determine that an input string is (not) acceptable.  M can be transformed into an equivalent DFA by adding a non-accepting “error” state and transitions out of all the states in M with other input symbols to the “error” state. w  L(M’). Conversely, if w  L(M), For each w  L(M), (q 0, w)  F. Thus, then (q 0, w)  Q - F and thus w  L(M’).

Non-deterministic Finite Automata(NFA) n Relaxes the restriction that all the outgoing arcs of a state must be labeled with distinct symbols as in DFAs n The transition to be executed at a given state can be uncertain, i.e., > 1 possible transitions, or no applicable transition. n Applicable for applications that require backtracking technique. n Defn A non-deterministic finite automaton is a quintuple M = (Q, , , q 0, F), where i. Q is a finite set of states ii.  is a finite set of symbols, called the alphabet iii. q 0  Q the start state iv. F  Q, the set of final (accepting) states v.  is a total function from (Q   ) to  (Q), known as the transition function

17 NFA  Every DFA is an NFA, and vice versa  Hence, in an NFA, it is possible to have (p, a, q 1 )   and (p, a, q 2 )  , where q 1  q 2 n Example. Consider the following state diagram of NFA M:  M stays in the start state until it “guesses” that it is three places from the end of the computation. accept reject accept or reject start Deterministic Computation Non-deterministic Computation q0q0 q2q2 q1q1 q3q3 1 0,1 >

18 Advantages of NFAs over DFAs n Sometimes DFAs have many more states, conceptually more complicated n Understanding the functioning of the NFAs is much easier.  Example M 1 (DFA) and M 2 (NFA) accept (a  b)* bb (a  b)*  Example An NFA accepts strings over { a, b } with substring aa or bb. q0q0 q1q1 q2q2 b a b > M1:M1: a, b a q0q0 q1q1 q2q2 b b > M2:M2: q0q0 q4q4 q2q2 q1q1 q3q3 b b a a >

Lambda Transitions n A transition of any finite automata which shifts from one state to another without reading a symbol from the input tape is known as -transition n -transition is labeled by on an arc in the state transition diagram n -transition represent another form of non-DFA computations n Provide a useful tool for designing finite automata to accept complex languages n Defn An NFA with -transition, denoted NFA-, is a quintuple M = (Q, , , q 0, F), where i) Q, , q 0, and F are the same as in an NFA ii)  : Q  (   { })   (Q)  Example (  ) and compared with the equivalent DFA in Ex  Example (  ) and Example (*)

Lambda Transitions M1M1 M2M2 M1M1 M2M2 M

Removing Non-determinism n Given any NFA(- ), there is an equivalent DFA. n Defn The -closure of a state q i, denoted -closure(q i ), is defined recursively by (i) Basis: q i  -closure(q i ) (ii) Recursion: let q j  -closure(q i ) and q k   (q j, )  q k  -closure(q i ) (iii) Closure: each q j  -closure(q i ) is obtained by a number of applications of (ii) n Defn The input transition function t of an NFA- M = (Q, , , q 0, F) is a function from Q    (Q) such that  t is used to construct an equivalent DFA (2) )),( (( ),( )( aqclosureaqt j q i i q j     (1) (3)

22 Removing Non-determinism n Example: Consider the transition diagram in Fig. 5.3 on p. 171 to compute t( q 1, a): -closure(q 1 ) = t(q 1, a) = -closure(  (q 1, a))  -closure(  (q 4, a)) = -closure({ q 2 })  = { q 2, q 3 }  = { q 2, q 3, q 5, q 6 } n Given M = (Q, , , q 0, F), t =  iff there is no -transition in  n Example n To remove the non-determinism in an NFA(- ), an equivalent DFA simulates the exploration of all possible computations in the NFA (- )  the nodes of the DFA are sets of nodes from the NFA(- )  node Y  Q in NFA(- ) can be reached from node X  Q in NFA(- ) on ‘a’ if  q  Y and  p  X such that  (p, a)  q in the DFA q1q1 a q4q4 q2q2 q5q5 q3q3 q6q6 a { q 1, q 4 } -closure({ q 5 }) { q 5, q 6 }

23 Removing Non-determinism n Example Transition tables are given (below) for the transition function . Compute the input transition function t of the NFA- with state diagram M. The language of M is a + c*b* tabc q0q0 { q 0,q 1,q 2 }{ } q1q1 { q 1 }{ } q2q2 { q 1 }{ q 1,q 2 }

24 DFA Equivalent to NFA- n Algorithm Construction of DM, a DFA Equivalent to NFA- Input: an NFA- M = (Q, , , q 0, F), input transition function t of M 1. Initialize Q’ to { -closure(q 0 ) } 2. Repeat 2.1. IF there is a node X  Q’ and a symbol a   with no arc leaving X labeled a, THEN Let Y =  q i  X t(q i, a) IF Y  Q’, THEN set Q’ = Q’  { Y } Add an arc from X to Y labeled a ELSE done := true UNTIL done 3. the set of accepting states of DM is F’ = { X  Q’ | X contains q i  F }

25 Removing Non-determinism n Example. Consider the t-transition table for Example  ’ a b c {q 0 } {q 0, q 1, q 2 }   {q 0, q 1, q 2 }* {q 0, q 1, q 2 } {q 1 } {q 1, q 2 } {q 1 }*  {q 1 }  {q 1, q 2 }*  {q 1 } {q 1, q 2 }     n Theorem Let w   * and Q w = { q w 1, …, q w j } be the set of states entered upon the completion of the processing of the string w in M. Processing w in DM terminates in state Q w. (Prove by induction on |w|.) tabc q0q0 { q 0,q 1,q 2 }{ } q1q1 { q 1 }{ } q2q2 { q 1 }{ q 1,q 2 }

26 Determinism and Non-determinism n Corollary The finite automata M and DM (as shown in Algorithm 5.6.3) are . n Example and Example show NFA  DFA n (Transformation) Relationships between the classes of finite automata: DFA  NFA-  NFA