Download presentation
Presentation is loading. Please wait.
1
Chapter 11 Finite State Machine
Discrete Mathematics: A Concept-based Approach
2
Introduction: The syntax of a natural language is very complex and difficult to completely characterize (differentiate). The process followed in the automatic translation of one language to another had led to the concept of formal languages. These are used to model natural languages and to communicate with computers. Unlike natural languages, it is possible to specify a well defined set of rules of syntax by which certain formal languages such as programming languages can be constructed. Discrete Mathematics: A Concept-based Approach
3
Finite state machine is the simplest of different types of automata
Finite state machine is the simplest of different types of automata. This type of automaton does not have temporary storage and accepts the regular set generated by the right linear grammar. It is basically a recognition device. It is also called finite state machine in the literature and abbreviated as FSA or FSM or even FA. We find certain variations in this machine, namely Deterministic Finite State Automata (DFA) and Non Deterministic Finite State Automata (NDFA). We shall deal with these machines in this chapter. Discrete Mathematics: A Concept-based Approach
4
Deterministic Finite Automata (DFA) We define DFA formally as 5-tuple, as these machines (M) are deterministic in their operations. M = (Q , , , q0, F) , where Q: represents the finite set of states called internal states : represents input alphabet, which is a finite set of symbols : Transition function, : Q x Q qo : Initial state and qo Q. F : Set of final states and F Q. The machines starts from the initial state qo . As it accepts symbols from the input string, it changes the internal states and at the end of the input it reaches the final state, if the string input is accepted. Other wise the machine remains at a non-final state at the end of the input. The formal model of FSM is given in Fig. Discrete Mathematics: A Concept-based Approach
5
The input is stored on the infinite tape, which is divided into cells and each cell holds one input symbol. The finite control scans the cells from left-to-right one symbol at a time for an input. The current state of the machine and the input read from the tape together determine the next state for the machine. A typical move in FSM is given as under (q1, a) = q (1) Here, the machine is in the state ‘q1’ and reads an input symbol ‘a’ on the tape and makes transition to a new state ‘q2’. The states are shown as circles with a state label inside. The edges are shown across the states; with an arrow head and labels on them. Discrete Mathematics: A Concept-based Approach
6
The label on the edge indicates the input upon which the machine makes transition from the given state to the next. A typical move of FSM is shown in Fig. The finite control is capable of sensing the end of the input. Say, a blank cell at the end of the input mean end of the input string. At the end of the input, if the machine is in the final state, then the input is said to be accepted. Otherwise, it is said to be rejected. Discrete Mathematics: A Concept-based Approach
7
Example : Design a FSM to perform serial addition of binary digits. Fig. FSM for Serial Addition. The serial adder accepts a pair of bits, namely 00, 01, 10 and 11. The output of the serial adder is 0 or 1. The machine is in the state ‘q0’ corresponding to ‘no carry’ situation during addition. The state ‘q1’ corresponds to ‘carry generated’ situation during addition. When 00 is input to the machine, it remains the machine is in the state ‘q0’ and outputs 0. When inputs are 00, 01, and 10, the machine remains in the state ‘q0’. Discrete Mathematics: A Concept-based Approach
8
In the state q0, when input 11 is encountered, the machine outputs 0 and makes transition to a state ‘q1’, as carry is generated. In the machine is in the state q1, when an input 11 is encountered, it produces the output 1 and continues in the state ‘q1’. In the state ‘q1’, when input 00 is encountered, it outputs 1 and goes to the state ‘q0’, as no carry is generated. In this FSM, we have not deliberately marked the final state as each one of them represents the final state. Discrete Mathematics: A Concept-based Approach
9
Theorem: When M = (q, , , q0, F) is a FSM and G be the corresponding transition graph, for every qi, and qj belonging to Q and w *(qi, w) = qj iff there is a walk w from qi to qj. Proof: The proof is through induction on the length (w) of the input string. Basis: For n=1 and n=2 the theorem holds good Induction: Assume that the claim is true for all strings of length ‘v’ with |v| n and prove for w of length (n+1). We shall write w of length n+1 as concatenation of sub strings v and a. w = va. Suppose that *(qi, v) = qk and | v | = n. There is a walk in G corresponding to v and leveled qi to qk Discrete Mathematics: A Concept-based Approach
10
Fig. from theorem But *(qi, w) = qj and the machine M must have transition (qk, a) = qj upon the input ‘a’. There must be an edge from ‘qk’ to ‘qj’ with label ‘a’. Hence, there is a walk from ‘qi’ to ‘qj’ and hence the theorem is proved. Discrete Mathematics: A Concept-based Approach
11
NDFA Non – Deterministic Finite Automata (NDFA) Computer systems are deterministic in nature. But the concept of non determinism is a useful tool in the construction of DFA, as direct construction of a DFA is more complex. In DFA, the transition function ‘ ’ is a mapping from Q x Q. This means that for a given state of the machine, upon reading an input symbol from the tape, the machine has a unique or definite move. But in NDFA, there exists more than one possibility for a given input. This causes the machine to choose one of 2Q possibilities (2Q is the power set of Q). Hence ‘ ’ in NDFA is defined as under : Q x 2Q Discrete Mathematics: A Concept-based Approach
12
Example : Consider the machine M = (Q, , , q0 ,F), where
Q = {q0,q1,q2}, ={a,b}, F={q2} and is given in Table 11.4. Table 11.4 Transition Function Fig.119: NDFA for above Example The NDFA for the transition function given in Table 11.4 is given in Fig For the state ‘q0’, the machine has two possibilities, either to stay in the state ‘q0’ or move to the state ‘q1’, upon the input ‘a’. This makes the machine nondeterministic in nature. The language accepted by the machine is: L = {anbm : n,m } q0 q1 q2 A B q0 {q0, q1} q1 {q2} q2 Discrete Mathematics: A Concept-based Approach
13
NDFA and Regular Expressions
NDFA and Regular Expressions Consider the following theorems, which show the analogy between the regular languages or regular expressions and the non deterministic finite automata (NDFA). Theorem : There exist finite state automata (FSA) for a regular language. Proof: Let ‘L’ be a regular language generated by a right linear grammar given by G = (N,T,P,S). Let ‘M’ be a finite state automaton accepting this ‘L’ and defined as M =(Q, , , qo, F). Where Q = N {q} and q N , q0 = S. F = {q} { A | A N and A is in P }. Discrete Mathematics: A Concept-based Approach
14
The - transition function is defined as follows If A a B is a rule in P then (A, q) = B If A a is a rule in P then (A, a) = q Corresponding to the derivation of word W, we have a sequence of moves made by M to accept the W. Conversely, corresponding to a sequence of move to accept W by M, these exists a derivation for W in G. Hence, the theorem L(G) = T(M) and the machine is non deterministic in nature. Discrete Mathematics: A Concept-based Approach
15
Example: Let G = (N,T,P,S) be a right linear grammar, Where N = {S, A}, T = {a,b} P = { S aA, A bS, A b } L(G) = (ab)+ = {ab, abab, ababab, . . .} is the regular set. Let M = (Q, , ,q0, F) be the NFSM, where Q = {S, A, q}, = {a, b}, q0 = S and F = {a}. The ‘ ’ is defined as under (q0,a) = A (A, a) = (q, a) = (q0, b) = (A, b) = {q0, q} (q, b) = Hence, M also accepts L(G). The corresponding NFSM is given in Fig. Discrete Mathematics: A Concept-based Approach
16
Discrete Mathematics: A Concept-based Approach
17
Algorithm 11.2 : Conversion from NFA to DFA
Start. 1. Create a graph GD with vertex {q0} and is the initial vertex. 2. Repeat the following steps until no more edges are missing. 2.1 Take any vertex qj of GD that has no outgoing edge for some a and compute the transition (qj,a). Say (qk,a), (qi, a) for (qi, qj, qk) 2.2 Take the union of all the transitions to give the new set. (ql, qm, qn) 2.3 Create a vertex if it does not exist. 2.4 Add on edge from {qi, qj, qk} to {ql, qm, qn} and label a. 3. Every state of GD whose label contains. If F is n as the final state. 4. If is acceptable then the vertex {q0} is also made a final vertex. Stop Discrete Mathematics: A Concept-based Approach
18
Example Consider the following NFA and convert into a deterministic machine. The transition function is given in Table Figure. Transition function. 1 q0 {q0,q1} {q1} Q1 {q2} Q2 Discrete Mathematics: A Concept-based Approach
19
Table 11.11 NFA to DFA conversion table
1 {q0} {q0,q1} {q1} {q2} {q0,q2} {q0.q1,q2} {q1,q2} {q0,q1,q2} Discrete Mathematics: A Concept-based Approach
20
The equivalent DFA is given in Fig. In Table 11.11 the state
{q0,q2} is not shown in the final DFA because the state has no transition from other states. Discrete Mathematics: A Concept-based Approach
21
NFA with Є–transitions (Є -NFA): The Є-NFA allows the automata to have transition on empty string (Є) calledЄ -transitions. This means that, without any input the FSM changes its states. For example Є -transition represented by (q0, Є)=q1 means that the FSM changes its state from q0 to q1 without any input. Thus, it introduces a hidden non-determinism in FSM. However, Є-transitions are useful in specifying optional inputs and render the design of FSM simple. For example, in a typical programming language, while specifying a numeric constant, the sign is optional. As an example, Є-NFA to recognize to accept an integer constant is given in figure. Discrete Mathematics: A Concept-based Approach
22
Fig. NFA with Є-transitions for integer constant with optional sign.
Discrete Mathematics: A Concept-based Approach
23
Output Associated Finite Automata:
The finite automata not only used as acceptor but also used to produce output at the end of the input. The labels on the edges indicate the output. Consider the Example , which is a serial adder and shown in, Fig. Serial adder. Fig. FSM for serial adder. The state ‘q0’ corresponds to the state ‘no carry’ generated after adding the bits. The state ‘q1’ refers to the state ‘carry generated’ after the addition of bits. When bits 01, 00 and 10 are added, in the state ‘q0’ no carry is generated. When bits 01 and 10 are added with 1, output is 0 and carry is generated. When bits 11 are added in state q1, the bit 1, the carry is generated and output generated is 1. In the state q1, when 00 is encountered, output is 1 and no carry is generated and hence machine makes transition to the state q0. Discrete Mathematics: A Concept-based Approach
24
Moore and Mealy Machines:
There are two different types of output associated FSM’s called Moore machine and Mealy machine. When the output is associated with states , the FSM’s are called Moore machines. When the output is associated with transitions, the FSM’s are called Mealy machines. Moore machines: Similar to DFA, Moore machine is also defined as six tuples M = (Q, , , ∆, λ, q0). The symbol ∆ is called output alphabet and λ is a mapping from Q to ∆, giving output associated with each of the states. Rest of the symbols carry the meaning as stated in DFA. The output of Moore machine for a given input ai is defined as Discrete Mathematics: A Concept-based Approach
25
Moore machines: Consider the Moore machine given in figure and the input string The set of states Q={ q0, q1, q2 }, input alphabet ∑={0,1} and output alphabet ∆ ={ 0,1,2}. The mapping λ is defined as under Fig. Example of a Moore machine. Discrete Mathematics: A Concept-based Approach
26
Moore machines: From the extend transition function of DFA (q0, 1) = ( (q0, ), 1) = q1 (q0, 10) = ( (q0, 1), 0) = q2 (q0, 101) = ( (q0, 10), 1) = q2 (q0, 1010)= ( (q0, 101), 0) = q1 So, on the input 1010 the sequence of states is q0 q1 q2 q2 q1 and the output is (q0) (q1) (q2) (q2) (q1) is 01221 Discrete Mathematics: A Concept-based Approach
27
Mealy Machines: Similar to Moore machine, Mealy machine is also defined as six-tuple M = (Q, ∑, , ,q0, ), where Q , ∑, , ,q0 mean similar to what is said in Moore machine. is the mapping from Q x ∑ to (q, a) gives the output associated with the transition from state q on input a. The output of M in response to input a1, a an is (q0, a1) (q1, a2). . . (qn-1, an) where q0, q qn-1 is the sequence of states such that (qi-1, ai) = qi for . Consider the example given in figure. The label o /a on the transition from state q0 to q1 indicates that (q0, 0) = q1 and (q0, 0) = a. For the given input 01100, the output of the machine is aabab, Discrete Mathematics: A Concept-based Approach
28
Example of a Mealy machine.
which is described as follows. i.e., for any transition, the associated output is . Discrete Mathematics: A Concept-based Approach
29
Compiler Design: Fig. Example of Mealy machine.
Discrete Mathematics: A Concept-based Approach
30
Applications Of Finite Automata:
The concept of finite automata has several applications in many areas like compiler design, special purpose hardware design, protocol specification etc. Some of the applications are described below. Compiler Design : Lexical analysis is an important phase in compiler. In lexical analysis, transition diagram (FA) is used in recognition of tokens. The tokens are the basic constructs in a programming language, such as keywords, numbers, identifiers, operators etc. The typical identifiers in a language comprising of letter followed by letter or digit is given in (b) are shown below. Discrete Mathematics: A Concept-based Approach
31
Applications Of Finite Automata:
The relation between FA and regular expressions is used in many packages developed on UNIX, such as LEX. Furthermore, many UNIX tools use extensively the concept of regular expressions Fig. (a) NFA for keyword begin (b) NFA for identifier. Discrete Mathematics: A Concept-based Approach
32
Applications Of Finite Automata:
Hardware design : In the design of computers, FA is used to design control unit of a computer. A typical sequence of operations in a computer consists of a repetition of instructions and each instruction involves the actions fetch, decode, fetch the operand, and execute. Each state of a FA represents a specific stage of instruction cycle. Protocol specification : Any system consisting of an interconnected set of subsystems requires a protocol for establishing proper coordination between the sub systems. A protocol comprises of a set of rules for the purpose of coordination and it is described by transition diagrams. An example of a Banking system is shown in Fig. Discrete Mathematics: A Concept-based Approach
33
Protocol specification :
Fig. NFA for banking system. State a represents the entry of a customer. After he/she wishes to credit some cash to his/her account , the system is said to be in state b. When the cashier receives the cash the system is said to be in state c. After the updating of the account the system reaches the final state d. If the customer wants to withdraw cash, he submits the withdrawal slip and now the system is said to be in state e. Discrete Mathematics: A Concept-based Approach
34
In state e , the cheque / withdrawal slip is verified to confirm that there is sufficient balance in the account. After this verification, system transits to state f . In this state, the customer is paid cash and system reaches a new state g. The customer account is finally updated and the system reaches the final state d. Discrete Mathematics: A Concept-based Approach
35
Summary: An alphabet is a non-empty finite set of symbols. A word symbol used here is an abstract-entity. In the operations on strings, which is concatenation process is formed by writing the letters (symbols) of first string followed by letters of second string. A discussion on grammar is made and it consists of a finite set of rules, that specify the syntax of the language and hence helps in structuring the sentences of a languages. In formal languages, the grammar helps in the generation of the valid strings in the language and to identify whether a given string is in the language. Discrete Mathematics: A Concept-based Approach
36
Summary: A finite state machine is an abstract model of a machine which accept discrete inputs, produces discrete output and has the internal memory to keep track of certain information about previous inputs. A finite state automation is a special type of finite state machine in which there is no output , and some of its finite states are distinguished as accepting states. The two types automaton discussed are deterministic finite state automaton and Non-deterministic finite state automaton. Applications of finite automaton are discussed in detail in Compiler design, Hardware design and protocol specifications. Discrete Mathematics: A Concept-based Approach
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.