Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

ATC Module 1

Uploaded by

Deekshith M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
34 views

ATC Module 1

Uploaded by

Deekshith M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 75
Automata Theory & Compiler Design 21CS51 Module 1 INTRODUCTION TO AUTOMATA THEORY Computation is any type ofcalculation or use of computing technology in information processing. Computation is a process evoked when a computational model acts on its inputs under the control of an algorithm to produce its results, The study of computation is paramount to the discipline of computer science. ‘What are the capabilities and limitations of computers? We seek mathematically precise answers: Complexity Theory: Easy problem: sorting. Hard problem: scheduling. What makes some problems computationally hard and others casy? Computability Theory: Which problems are solvable by computers and which are not? Both Complexity Theory and Computability Theory require a precise definition of a computer. Automata Theory deals with definitions and properties of mathematical models of computation. Theory of computation begins with a computational model. A computational model may be accurate in some ways, but not in others. Introduction to Finite Automata: Before there were computers in 1930°s, Allan Turing studied an abstract machine that had all the capabilities of today’s computers, at least as far as in what they could compute. His main goal was to describe precisely the boundary between what a computer machine could do and what it could not do. His conclusion not only applied to his abstract Turing machines, but also today’s real machines. ‘The simpler kinds of machines which exist in between 1940's and 1950"s, which we today call it as jite Automata” were studied by number of researchers. These automata, originally proposed to model brain function, turned out to be extremely useful for other purposes. These theoretical develo sats bear diteetly un what cumputer scicutists du today. Sume of the convepts like finite automata, regular expressions, and formal grammars are used in design and construction of important kinds of software, including parts of compilers. Other concepts like Turing machine help us understand and what we can expect from our software, Automata theory is the study of abstract computing devices or machines. Like an execution of an algorithm is carried out automatically by a computer. In this subject we use only abstract machines to execute our programs. An abstract is a brief summary of research article, thesis or a document. It is nothing but a theoretical concept without thinking of a specific example. Finite automata can compute only very primitive functions; therefore, it is not an adequate computation model, In addition, a finite-state machine's inability to generalize computations hinders its power. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page I Automata Theory & Compiler Design 21CS51 Module 1 Imagine a Modem CPU. Every bit in a machine can only be in two states (0 or 1). Therefore, there are a finite number of possible states. In addition, when considering the parts of a computer a CPU interacts with, there are a finite number of possible inputs from the computer's mouse, keyboard, hard disk, different slot cards, etc, As a result, one can conclude that a CPU can be modeled as a finite-state machine The Turing machine can be thought of as a finite automaton or control unit equipped with an i te storage (memory). Its "memory" consists of an infinite number of one-dimensional array of cells. Turing's machine is easentially an abstract model of modern-day computer execution and storage, developed in order to provide a precise mathematical definition of an algorithm or mechanical procedure. ‘Why to Study Theory of Computation ‘Why do we need to study Automata Theory (Theory of Computation)? Theory of computation lays a strong foundation for a lot of abstract areas of computer science. TOC teaches you about the elementary ways in which a computer can be made to think. Implementations come and go, but today’s programmers can't read code from 50 years ago, Programmers from the early days could never have imagined what a program of today would look like. In the face of that kind of change, TOC is very important to study the mathematical properties, of problems and of algorithms for solving problems that depend on neither the details of today's technology nor the programming fashion of early days. It is desirable to know which problem can be algorithmically solved and which cannot. Understanding which problems can be algorithmically solved is one of the ‘main objectives of theory of computation. ¥ TOC provides a sct of abstact stucturcs that arc useful for solving certain classes of problems. These abstract structures can be implemented on whatever hardware/software platform is available. Y Using these abstract structures the required design efforts can be determined for the actual model, Y Using TOC, problems are analyzed by finding the fundamental properties of the problems themselves such as: 1. Is there any computational solution to the problem? If not, is there a restricted but useful variation of the problem for which a solution does exist. 2. If a solution exists, can it be implemented usi W some fixed amount of memory"? ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 2 Automata Theory & Compiler Design 21CS51 Module 1 3. Ifa solution exists, how efficient is it? More specifically, how do its time and space requirements grow as the size of the problem grows'? 4. Are there groups of problems that are equivalent in the sense that if there is an efficient solution to one member of the group there is an efficient solution to all the others’? Y TOC plays an important role in compiler design, in switching theory, design and analysis of digital circuits, etc. Applications of Automata Theory i, Automata is required for designing software and checking the behavior of digital circuits ji, Software for verifying systems of all types that have finite number of distinct states. iii, Structural representation of automata like grammars and regular expressions are very useful in designing of software for lexical analyzer of typical compiler. iv. In designing of software for identifying the words, phrases and other patterns in large bodies of text such as collection of web pages. v. Computational Biology: DNA & proteins are strings. vi. In artificial intelligence and knowledge engineering, game theory and computer graphics. vii, Automata are essential for the study of the limits of computation like a. What can a computer do at all? (Decidability) b. What can a computer do efficiently?(Intractability) Abstract Machine: An abstract machine or abstract computer 1s a conceptual or theoretical mode! ot a computer hardware or software system which really does not exist. The machines are hypothetical computers. ‘These machines have commonly encountered hardware features and concepts and avoid most of the details that are often found in real computers or machines ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 3 Automata Theory & Compiler Design 210851 Module 1 CENTRAL CONCEPTS OF AUTOMATA THEORY Languages and Strings: phabets: An alphabet is a finite non empty set of symbols or characters, ‘We use the symbol © for an alphabet. Example: 1. = {0, 1}, the binary alphabet 2, B= fab... 2}, the English lower-case alphabets. Strings: A string is a finite sequence, possibly empty, of symbols chosen from some alphabet 5. Example: ¢,0, 1,01, 10, 11, 110, 01110 ... is the strings from the binary alphabet, 2 = {0, 1} = . Given any alphal | which we will write as e. | is the empty string, j | | 2. The set of all possible strings over an alphabet © is written as E*. | | 3. E"notation exploits the Kleene Star operator. | Empty string: Iris the string with zero occurrences of symbols or characters. This string is denoted by &, may be chosen from any alphabet whatsoever. Length of a string: The /engsh of a string‘ w’, is the number of symbols or characters in‘w’. It is denoted by: |w| Example: |¢|=0 |10010|=5 | NOTE: For any symbol ‘c’ and string ‘w’, we define the function N, that the symbol ‘c’ occurs in string 'w’. So, for example, N,(abbaaa) = 4 [ {() to be the number of times | | | | | | | Concatenation of strings: Concatenation of two strings ‘s’ and ‘t’ is the string formed by appending the string ‘t” to string It is denoted by: s|[t or st Example: If the siring s~ good and the string t~ bye then st ~ goodbye. (ow ~ seem). 7 | | =x) 3. Concatenation, as a function defined on strings, is associative. So for all s, 1, w | | i } ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 4 Automata Theory & Comy ller Design 21CS51 Module 1 Replication: For each string w and each natural aumber k, the string w; is defined as: wi=e wht yt w For example: a’ = aaa (bye)* = byebye ab’ = aabbb Reversal of a string: The reversal of a string w is obtained by writing the symbols in reverse order, which is denoted by w® Example: w= 11001 w*= 10011 | | | I Relations on Strings Substring: A string’s’ is a substring of a string ‘1’ if‘s’ occurs contiguously as part of *’ Example: aaa is a substring of string aaabbbaaa ‘aaaaaa is not a substring of string azabbbaaa Proper Substring: A string ‘s’ is a proper substring of a string ‘1’ if ‘s’ is a substring of ‘f” and s#t Example: aaabbbaaa not a proper substring of string aaabbbaaa | NOTE: 1. Every string is a substring (although not a proper substring) of itself. | 2. The empty string e is a substring of every string. Prefix of a string: Prefix is a string of any number of leading symbols. A string ‘s? is a prefix of *t? if Ix € 5" (t=sx) Example: Prefixes of a string abba are: ¢ , a, ab, abb, abba, Proper Prefix: A string ‘s’ is a proper prefix of a string ‘t” if ‘s’ is a prefix of 1” ands #t. Example: Proper prefixes of a string abba are: ¢ , a, ab, abb ( where abba is not a proper prefix) | NOTE: 1. Every string is (although not a proper prefix) a prefix of itself. | 2. The empty string ¢ is a prefix of every string. Suffix of a string? Suffix is a suing of any number of wailing symbols. A Sing "is agit of 7 if ak €Y" (t= xs) ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 5 Automata Theory & Compiler Design 210851 Module 1 Example: Suffixes of a string abba are: ¢ , a, ba, bba, abba. Proper Suffix: A string ‘s’ is a proper suffix of « string “1” if‘s” is a suffix of ‘t’ and st Example: Proper suffixes of a string abba are: ¢, a, ba, bba( where abba is not a proper suffix) Every string is (although not a proper suffix) a suffix 0 2. The empty string « is a suffix of every strin, Language: A language is a (Finite or Infinite) set of all strings, which are chosen from some ©’, where © is a particular finite alphabet. Example: 5 = {a, b} Y= {s,a,b, aa, ab, ba, bb, aaa, aab,, } Suppose Language contains set of all strings of a’s and b’s with an equal number of each is given by: L =f, ab, ba, aabb,baab, baba, } Powers of an alphabet : Power of an alphabet is the set of strings of certain length (k) obtained from an alphabet 5. It is denoted by * Example: If % = {0,1}, then 2" ={ ¢ }, Z' = { 0, 1}, 2? = { 00,01,10,11}, 2? = { 000, 001, 010,011,100,101,110,111} and so on. ‘The set of languages defined on is P(S)), the power set of Yor the set of all subsets of [If Y= O then Y" is { €} and P(X), is {, { €} } } = iis the empty language, is a language over any alphabet. i | | 2, L= {e} the language consisting of only the empty string, is also a language over any | | I alphabet, ‘Techniques for Defining Languages ‘We will use a variety of techniques for defining the languages that we wish to consider, Since languages are sets, we can define them using any of the set-defining techniques as: L= {we 5* | description of w} Example: 1 Language contains strings of a’s and b’s with all a's precede all b’s can be defined as: L={ wE (a,b) | Alla’s precedeall 6’s in w} The strings which are defined in the language L are: L {,a, b, aa, ab, bb,aabbb..... . } ‘The strings aba, ba, and abe are not defined in the language L. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 6 Automata Theory & Compiler Design 21CS51 Module 1 Rules: For the above language all it says that any a’s there; are must come before all the b's (if any) If there are no a's or no B's, then there can be none that violate the rule, So the strings ¢, a, aa, and bb trivially satisfy the rule and are in L. Example 2: Let L = {x : Ay (a, b}* (¢= ya)}. Give an English description of the language. a Language L. contains strings of a’s and b’s ending with ‘a’, L { a, aa, ba, aaa, baa, bbaa. Note: ( a, b) " means all strings can be formed by concatenating the symbols a and b zero or more times Example 3:) Let = {x #y zx, y € (0,1, 2.3. 4.5. 6.7, 8, 9}” and, when x and y are viewed as the decimal representations of natural numbers, squcre(x) =y } ‘The strings 349 and 12#144 are in L. ‘The strings 348 and 12#12#12 are not in L.. Concatenation of Languages: Let L; and L2 be two languages defined over some alphabet Y. Then their concatenation, written as LiL is: Lila = fw € Ss SEL (HEL: (w=st))} Example 1: Let: Li = {cat, dog. mouse, bird}. Ly = {bone, food} LyL2= {catbone, catfood, dogbone. dogfood. mousebone, mousefood, birdbone, birdfood} { NOTE: 1. {c} is the identity for concatenation: L{e} 2, Z is a zero for concatenation; LO=2L=D 3. Concatenation defined on languages, is associative. So, forall languages Ly, L2, L3: (Lila bs = Li (Las) | | | | | L ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 7 Automata Theory & Compiler Design 210851 Module 1 FINITE AUTOMATA (FINITE STATE MACHINE) Finite automata are abstract model of a digital computer. It is used to recognize regular languages, within input taken from alphabet ©. It is also called Finite State Machine (FSM) The job of FA is to accept or reject an input depending on whether the pattern defined by the FA occurs in the input. Ithas 3 components i, Input file i, Control unit iti, Output Automaton —| output Input tape: It is divided into number of cells each of which can hold one symbol, Control unit: machine has some finite states (qi, 4), 42, 43, 44,---), one of which is the start state(qo). Based on the current input symbol, state of the machine can change. Output unit: Output may be accept or reject. When end of the input is encountered, the control unit may be in accepting or reject state State: A statcis ispreseuted using dice Start state: A circle with an arrow not originating from any node Transition from one state to another state on input, say 1 is represented as ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 8 Automata Theory & Compiler Design 21CS51 Module 1 ‘Transition from one state to another state on input, say 0 or 1 is represented as ©2—© start and end state, say forinput symbol 1 is same represented as 4 Types of Finite Automata: Different types of Finite automata are: 1. Deterministic Finite Automata (DFA) 2. Non Deterministic Finite Automata (NFA) 3, Non Deterministic Finite Automata with ¢ moves (e - NFA) DETERMINISTIC FINITE AUTOMATA (DFA) or DETERMINISTIC FINITE STATE MACHINE (DFSM) DFA or DFSM is five-tuple indicating M= (Q, 5, 8, q0, F), where M is a deterministic machine with Q—- Non empty finite set of states ¥ — Non empty finite set of input alphabets (symbols) 6 — Transition function, which maps from Q x E> Q yo € Q is the start (initial) state. Fis subset of Q, he final (accepting) state Language accepted by a DFA: Let I= (Q. E, 6, qo, F) be a DFA. A string w is accepted by the machine M, if and only if transition for W takes the initial state qo to final state F. ie: 3°(q0, w) is in F. w|w€Z" and 8 (q0, w) is ‘The language accepted by DFA is L(M) in F} Tra ion diagram: Itis a graphical representation with circles, double circles, arrow and arcs with labels etc. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 9 Automata Theory & Compiler Design 21CS51 Module 1 o 1 OQ, © Os Formally M = (Q.¥.6.q1. F) where 2 Q={a,92} 2° r={0,1} © a isthe start state; F = { a2 } © dis Given by tm wamition abte ‘Transition Table: Itis a tabular representation of transition function 6, For the above transition diagram, 8 is given jon function of DFA to strings: The transition function 8 ( q, a) = p accepts two parameters namely state y and input symbol “a and retums a state ‘p’ which is the next state of the machine. But if there is a change of state q to state p on input string ‘w’, then extended transition function can be used, which is denoted by 8” Basis: oe) =4 How DFA processes strings: What are dhe moves made by the following DFA while processing he string 01101, 0 1 We know that 8°(q, 2) =q_ ie: 3(q1. 2) = a1 (qu, 01101) =6 (qu, ®), 0) = 6(a1 0) S(q.0) =a ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 10 Automata Theory & Compiler Design 210851 Module 1 8 (qi, ON101) =8 (Sq, ©, 1) = 5 (ay 1) S(q.01 = 8'(qi, 01101) =8 (5°q1, OD, 1) = 5(@,1) on) = 3 8 (q, 01101) =6 ( 8'qu, O11), 0) = 5(@2, 0) (q1,0110) a1 5(qi, 01101) (6%qu, 0110), 1) = 6 (a 0) 8'(qi, 01101) = qe . After reading the string w= 1101, the machine is in state q2, which is final state. So, the string 01101 is accepted by DFA. DFA/DFSM Design Techniques Pattern recognition Problems: i. Identify the minimum string. ii, Construct a DFA for minimum string using ». iii, Identify the transitions which are not defined in minimum string DFA iv. Construct the complete DFA for the given alphabet by referring the minimum string DFA. Answer: Language contains a minimum string as single a. Note: if length of string is ‘m’ then naturally we should have m+1 number of states to design FA, So DFA corresponding to this minimum string is , Identify the transitions in each state which are not defined, In qq for a input transition is not defined. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 11 Automata Theory & Compiler Design 210851 Module 1 To reach q: state from qo we need one input a, and in state qi define input a, So the resultant string aa which is accepted by q1, as it is defined in the language. ie: (qua) ~ ar Therefore the DFA for the above problem is given by M = ({ qo,qi }. { a}. 5, qo, {qi} ) Language contains a minimum string as single a, and there is no restrictions on b, so it could be ¢ in minimum case or any number of b's, So DFA corresponding to this minimum string is —O--© Identify the transitions in each state which are not defined. In qo for b input transition is not defined, q, for a and b input transition is not defined, In qo, on input b, it goes to qu, since any number of b’s followed by at least one a is accepted by qi (ie ba, bba,bbba......), So before the suffix a, machine is in qy and current state is also qo, Take self loop in qo on input b. ( qa, b ) = qo To reach qi state from qo we need one input a, and in state qi define a input a. So the resultant string is aa or a followed by any number of a’s is accepted, since it is defined in the language. So take a self loop in state 4, ie: 8( a1) =a There is no restriction on b, so any number of b’s can be accepted in state qu, take a self loop for input b. 8( arb) = a1 ‘Therefore the DFA for the above problem is given by M=( { qo,qi }, { a,b}, 8, qo, {ar} ) ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 12 Automata Theory & Compiler Design 210851 Module 1 ‘There is no restriction on number of b’s, but L contains only one a. Therefore the DFA for the above problem is given by M~( { qo,qi }. {a,b},8. qo, {ai} ) OR 4 Minimum length string is abb, so the DFA for minimum string is given by: Oe) ‘Therefore the DFA for the given problem is M=( { qo,qiqaar}. {,b}, 8, qv, {as} ) ‘Where, 6 is as shown in transition diagram: ~ ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 13 Automata Theory & Compiler Design 210851 Module 1 Answer is similar to that of previous problem, except consider non final states of previous problem as a final state and final state as a non final state. The DFA for the problem which is not ending with abb is M = ( { qo, qi,42.43 }. { @, b}, 8, qo, fo, 41, eh) Start OR Minimum string is aab, so the DFA for minimum string: Start, qe % & DFA M=({ o,au.qzar}, {a,b}. 8, ao, tae ) ‘Where, 6 is given in TD. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 14 Automata Theory & Compiler Design 210851 Module 1 Note: DFA design procedure is same as that of previous problem except, make final state as non final and non final state as final state of previous problem. DFA M= ({ qo, qu.dzqs }, €@, b}, 8, qo, {do, 41,42} ) Where, 8 is as shown in transition diagram. Minimum string is ab, DFA corresponding to this is given by: In state qo on input b and in q; on input a, machine enters trap state (starts with ab) and also ab followed by any number of a’s and b’s in qrstate is accepted. DFA M=( { qo.4i,4¢,4}, {a,b}, 8, ao, fac} ) ‘Where, 6 is as shown in transition diagram. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 15 Automata Theory & Compiler Design 210851 Module 1 Minimum string is 000 and its DFA is: = DFA M= ({ qo, qi, 42,43 }, £0, 1}, 8, 40, fas} ) ‘Where. 6 is as shown in transition diagram. Noter A language is regular if it is accepted by a DFA. ‘That means if it possible to design DFA for the given language L = {awa | w € (a+b) }, then we say that L is regular, ‘The minimum string for the language L = { awa) is aa, and its DFA is ~O-—O--© DPA M=({qp.a1.ar}. £4.) 8,40, fa) ) Where, 8 is as shown in transition diagram ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 16 Automata Theory & Compiler Design 210851 Module 1 @) " Therefore the given language is regular. OR Minimum string is ab or ba, and its DFA is DFA M=({ qo,qi,42, 43,46 }, 1, } 8, ao, {3,44} ) ‘Where & is as shown in transition diagram ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 17 Automata Theory & Comy ller Design 210851 Module 1 ‘Minimum string is 012 and its DFA sat) —* (0) ++) *-@) DFA M=({ qo, 41,42, 46.44, (0, 1,2}, 8, qo, {ar }) where qcis the trap state and 6 is as shown in transition diagram. ‘Minimum string is bb and its DFA is L= {e,b, bb, ab, ab, bba, a, baa, aa, 3 Start b ©) DFA M=({ qo,ai, aca}, {a,b}, 5, qo, {ar}) where qcis the trap state and 6 is as shown in transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 18 Automata Theory & Compiler Design 21CS51 Module 1 ‘Minimum string is 0011 and its DFA ‘The DFA M = ({ qo,qi, 42,43, 42.4}, { 0, 1}, in transition diagram: go far}) where q.is the trap state and 6 is as shown OR Draw a DPA to accept the Language L~ { Nw) <3, w € (a, b) Minimum string: L = {¢, a, aa, aaa,b, ba,bba,abbb,, I The DFA M = ({ go, qi, 42,43, 4:3, £ @, b}, 5, qo, {go,}) where quis the trap state and 6 is as shown in transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 19 Automata Theory & Compiler Design 210851 Module 1 The set of all strings that either begins or ends or both with substring ab. ii, The set of strings with at least one “a” and exactly two “b’s, OR L= {w| Nw) > 1 and Ni(w) =2 } iii, Set of all strings of even length. OR L= {w | wis even length and w €f a, b)"} DFA to accept the strings of a’s and b’s ending with ab can be written as shown below: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 20 Automata Theory & Compiler Design 21CS51 Module 1 The two DFA’s can be joined to accept the strings of a’s and b’s beginning with ab or ending with ab or both can be written as shown below: The DFA M ~ ({ qo, 41,42, 43, 44,45}. £8, b}, 8, qo, fa2.qs,}) where 5 is as shown in transition diagram’ trap state Therefore the DFA M=({ qo,q, 42,43, 45,454). { @, b}, 8. {4s,}) and quis the trap state where 8 isay shown in (ansition diagram, ‘trap state if more than two b's ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 21 Automata Theory & Compiler Design 210851 Module 1 iii. DFA M=({ qo,a1 }, { 8, b}, 8, do, {ao,}) where @ is as shown in transition diagram: ‘a,b The possible strings in L = {0111, 01010101...11, ie: L= { strings of all 0s and 1's with at least one 01 and followed by an even number of I's with minimum two 1's (no zero 1's) } The DFA M = ({ go, 41, 42, 45,44, G+ £9 1}, 8, go {ae }) where 8 is as shown in transition diagram: Minimum length string = 011 and its DFA: sti 0 1 1 SE ae a @ The DFA M = ({qo, 41, 42,43,44,4:}- { 0, 1}. 8, qo, {qs }) where qris the trap state and 6 is as shown in transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 22 Automata Theory & Compiler Design 21CS51 Module 1 Minimum length string = 010 and its DFA: ‘The DFA M = ({o,41,42,48,44,40). £0, 1}, 3, qo, {43 }) where quis the trap state and 6 is as shown in transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 23 Automata Theory & Compiler Design 210851 Module 1 The DFA M = ({qo, qi, 42,943,415, £0, 1}, 8, qo, {a.q3 }) where 6 is as shown in transition diagram: fo.a 3 (£ qo, 41, 42,43, 43. £0, 1}, 8, do, {q2}) where 3 is as shown in transition diagram: ‘The DFA ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 24 Automata Theory & Compiler Design 210851 Module 1 The DFA M =({ qo,qi}, { 0, 1}, 8,40, {q1}) where 6 is as shown in transition diagram o ‘The DFA M = ({ qo, 41,42}. £0, 1},.5, qo, {4,42}) where 6 is as shown in transition diagram Answer: L={€a,b, ab, ba, aba, bab,abab, } The DFSM M=({ qo,qiqz.d}, {a,b}. 6, ao, {4c 41,42}) where 8 is as shown in transition diagram; where state d is the dead state or trap state ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 25 Automata Theory & Compiler Design 210851 Module 1 Answer: Language L contains strings of a’s and 5's in which any number of b’s immediately preceded or followed by even number of a’s L= {c,, bb, aab, aabb, baa, aabaab, aabaaaabbbbbb. } The DFSM M=({ qo,q1,4}, {a,b}, 3, qo, (qo}) where 3 is as shown in transition diagram Modulo-: Answer: Modulo- 3 results in three remainders: 0, 1, 2. So in qo state, no input symbol required to reach that state, length is 0. Therefore qois identified as remainder 0 state. Similarly in qu (length 1), remainder is 1, and qz remainder is 2 (length 2). Afterwards it enters to qo and the same process is repeated. Final state is qo since the |w| mod 3 = 0 (remainder 0 state, which is qo) ‘The DFA M=({ qo.qi,42}, {8b}, 5, qo, {qo}) where 6 is as. shown in transition diagram: ee 2S a) i) hw| mod 3 not equal to 0) ‘The DFA M=(f qo.qu.q2}, {,b}. 8, qo, {41,42}) where 8 is as shown in transition diagram: ab start, ab ® b © ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 26 Automata Theory & Comy Obtain a DFA to accept the language L = { w: |w| mod 5 = 0 } and w € (a,b) The possible strings in L ={ ¢, ababb, aaaaa, bbbbb, baaab, } Possible remainders are 0,1,2,3, 4. So there are 5 states, remainder 0 is the start state and also the ller Design 210851 Module 1 final state. ‘The DFA M=({ qo, ai, 42,4. 44}, {8b}. 3, qo, (40}) where 3 is as shown in transition diagram ab OOO) Obtain a DFA to accept the language L = { w: |w|mod 5 <> 0} and w € (a,b)" The DFA M = ({ qo, 41, 42, 43, 44}, { @, 6}, 5, go, { di, 42, 43, 443) where 3 is as shown in transition diagram Answer: Hore mod 3 results in three reminders 0, 1,2 and mod 7 results twa remainders 0, 1 Let us consider jw| mod 3 = x and it results in three states say Qi = { 0, 1,2} jw| mod 2 Therefore the number of states required to design DFA for the given language can be obtained by and it results in two states say Qz = {0,1}. taking cross product of Q; and Qe Q=AXQ Q= {(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), 2, 1) J Here (0, 0) state is cor idered as start state, (Because in start state length of the string required to reach that sate = 0; that means jw] mod3 = 0 inod 3 — 0 and 0 mud 2 — 0, (0, 0) state. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 27 Automata Theory & Compiler Design 21CS51 Module 1 Final state: To accept strings of w such that |w| mod 3 > |w| mod 2, is the pairs (x, y) such that x > y are final states. So the final states are {(0,0), (1,0), (1,1), (2,0),(2,1) } The DFA M = ({ qo,di, 2, 43, 44, ds} {, B}. 8, do, £ do, di, d2, d4,s}) where 6 is as shown in transition diagram ab ak, ab =6*©"©--@"-©- © an ao) en Obtain a DFA to accept the language L= { w: |w| mod 3 < |w|mod2 and w€ (a,b)"). Q={0,1,2} Q@={0,1} Q= {(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2,1) }- Here (0, 0) state is considered as start state. Final states are; x * qo qo a a e 0 ® u a ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 33 Automata Theory & Compiler Design 21CS51 Module 1 SHORTCUT METHOD 1. For any divisible by n number problem, rst write the transition table for ‘n’ number of states and then translate Transition table into Transition diagram. Design a DFA to accept all binary numbers which are divisible by 5. OR which are multiple of 5. Easy to solve by using short cut method: Write the transition table for binary number divisible by 5. Total number of states required is State > 7 T —>* a0 qo a qa ” B oe a w Be a 2 % @ w Now translate the above TT into Transition diagram: 1111 ete are not Answer remains same as that of previous problem, but the number should always start with a 1 Ifa binary number starts with a 0, that number should never be accepted and machine enters to trap state on input 0. So let us rename the final state of previous problem as qrand have a new start state qo and from this state on input 1 machine enters into state qu, and the remaining procedure is same as that of previous problem, ATHMARANJAN K { of ISE, SIT MANGALURU. Page 34 Automata Theory & Compiler Design 210851 Module 1 Therefore the DFA for the above problem is M=( qo,qi.42, 43.48}. {0,1}, 8,40, {49}) where 8 is as. shown in transition diagram: Answer is same as that of divisible-S problem; but reverses the direction of all arrow marks except the arrow labeled with start. Therefore the DFA for the above problem is M=({ qo. 4,42, 43,44}, £0, 1}, 8,qo, {qo}) where 6 is as shown in transition diagram: Answer: Y= (0, 1,2,3.4,5,6, 7, 8,9} Decimal Number ‘Number of states required to design DFA = 3 ‘When we divide decimal number 0, 3, 6 and 9 remainder is = 0 Similarly when we divide decimal number 1, 4 and 7 remainder is = 1 Also decimal number 2, 5, 8 results in remainder 2. Remainder 0, 1, and 2 states are qo, qu, q2 respectively. DFA is given by the following Transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU. Page 35 Automata Theory & Compiler Design 210851 Module 1 NON-DETERM nite State Machine (NDFSM) ‘Why do you we need NFA? Sometimes itis difficult to construct a DFA for complicated languages. In such cases, there is a need to construct a machine very easily which can be achieved by constructing an NFA. Afier constructing an NFA, DFA can be easily constructed. So the drawbacks of DFA can be overcome using NFA. But practically an NFA does not exist. Define NFA. NFA is five-tuple indicating M= (Q, ©, 8, q0, F), where M is a Non-deterministic machine with Q—- Non empty finite set of states E — Non empty finite set of input alphabets (symbols) 8 ~ Transition function, which maps from Q x E> 2% qo € Qs the start (initial) state Fis subset of Q, is the final (accepting) state Language accepted by a NFA: Let M- (Q, 5, 6, yo, F) be a NFA. A suing w is accepted by the machine M, if'and only if wansition for w takes the initial state q0 to final state F. ie: 8'(q0, w) is in F. ie: 6°(q0, w) contains at least one accepting state. ‘The language accepted by NFA is L(M) = { w|w €" and 6'(q0, w) NF #9} ‘Obtain an NFA for the following languages: i. L={ abab" or aba" | n> 0} ii, L= { w (ab +ba) |w € (a,b)" } ie: ending with ab or ba iii, L= { abw| w €( a,b)" } ie: starting with ab iv. L= { wab | w € ()" } ie: ending with ab ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 36 Automata Theory & Compiler Design 21CS51 Module 1 NFA for the above problem is M =({ qo. 1.42. a3.asar}, { a, bY, 8, do, { acas}) where 6 is as shown in transition diagram: iv, ES fabwlwe(aby! } NFA for the above problem is M = ({ qo, qi, 42}, { 8. b}, 8, qo, { 42) where 5 is as shown in transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 37 Automata Theory & Compiler Design 21CS51 Module 1 i v. L= [wab | w€( ab)" } NFA for the above problem is M =({ qo, qi, az}, { @, b}, 6, qo, {q2) where 6 is as shown in [3 (a) ; cy transition diagram: start, vi. L= { awa| w€( ab)" }. NFA for the above problem is M=({ qo, 41,41}, 1, b}, 8,qo, (4) where 8 is as shown in transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 38 Automata Theory & Compiler Design 21CS51 Module 1 How the input string 01010 is processed by the above NFA? NFA starts from the initial state qo and remains there on reading the 0. On reading a | next it may go cither to qo oF to qi as scen in the third column of below Figure. Now the next symbol 0 is read. We need to consider transitions out of both qo and qi, qo goes to itself while q; moves to qz. When the next symbol 1 is read we need to consider two transitions once again - one out of qo and the other out of q2. qo can go either to qo or to qi whereas q2 has no transition on 0 or 1 and hence it dies. With the last input 0 qo goes 10 qo while qi goes to q2. Since qy is an accepting state the NFA accepts o1010 % ——= % ——= % ——= % ——> %——»> % ™ % n NN « NN (stuck) o 1 o 1 0 h ‘The states an NFA is in during the processing of input sequence 01010 DFSM ‘NFSM DFSM ean make exactly one move, for | NFSM has zero or more moves for each input symbol each input symbol in 5, in © from every state of the automaton. An NDFSM M. may have one or more transitions that are labeled « It is deterministic finite automaton, | Non deterministic finite automaton where the moves of where the moves of the automaton can be | automaton cannot be predicted. predicted. NFSM with ¢- transitions, where automaton can change the state without reading input. ‘Number of states are more compared to | Number of states are less compared to DFA and easier NFA and difficult to construct to construct DFA cannot guess about its input NFDSM with e-transitions enable machine to guess at the correct path before it actually sees the input ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 39 Automata Theory & Compiler Design 210851 Module 1 CONVERSION OF NFA TO DFA U! (G SUBSET CONSTRUCTION METHOD Procedure: + The subset construction method of NFA to DFA conversion starts from an NFA by considering the start state of NFA as the start state of DFA and the other components of the DFA states are constructed as follows: * .Qo(set of all states in DFA) is the set of subsets of Qu (Set of all states in NFA) : ie: Qn is the power set of Qu. If Qu has n states, then Qo will have 2° states. Often not all these states aie avvessible ium the stat state of Qp, Inacvessible states cau Le “duuwu away” su effectively, the number of states of DFA may be much smaller than 2”. © Fp is the final state(s) of DFA, which is the set of subset of Qy that include at least one accepting state of NFA. ab a start, 0 Given NFA contains 3 states: Qu = {qo, 41,42} Start state of NFA qp is the start state of DFA. Qo = the subset of Qu states = fo, {a0}. far} {a2} .f40, 41} {40,42}. fai.de}. fdo.a1,42}}. Since qois the start state of NFA, the start state of DRA = {qo} and start writing the transition, funetion from qo state. Spl {40}, 8) = SNC do, 8) = { Goan} 8p( {an}, a) = Sn a1 a) = 9 8p( {40}, b ) = SxC ao, b)= { Go} BoC {ai}, b) = SnCu, b)= { aa} So( {a2}, a) = Sx( a2, 2) = Sp( {4o, a1}, 8) = Sn qo, @) U Bx( 41,2) = f aoa} Vo ={ doa} So {a2}, b) = Sy( a2, b) = 9 Sp( {v.41}, b) = Sn( do, b) U Sv( ai, b) = { ao} U far} = bdo. qa} So( {40,42}, 8) = Sx( qo, a) U Sx q2,a)= { qoa}U@ ={ goa} Sp( {40,42}, b) = SxC qo, b) U Bx dz, b) = { 40) Ue =f ao} Sof fai, az},0)~ Sx aa) U Sx a2,a)— Ue —~@ Sof {41,42}, b) = nC a1, b) U 6x gz. b) = {a2} U9 ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 40 Automata Theory & Compiler Design 210851 Module 1 So( (40. a1, 42}. 2) = Bx( qo, a) U Sx( qi, a) U Sxl gz, a) = {qo.a1} U U9 = {ao.a1) Sp( {40, di, 2}, b ) = Sx do, b) U Sx ai, b) U Sx a2, b)= {a0} U {az} Uo = fao.42} ‘Transition table of DFA: a b ° 9 ° tai} 9 {az} *{a} 9 9 *fa.a2} ° fa} *{qoai.de} {qo a1} tao} From the above table we observe that only {qo, {qo, 41} and {qo, q2} are reachable from start state qo, and all other states are inaccessible states. So by discarding all those inaccessible states from the above transition table we get the DFA equivalent to given NFA is: a b > {ao} tana} {ao} tao, a1} tac. an} {qo.a2} *{qoae} {qo an} {ao} ‘The final state of DFA Fp = {qv,42} (since qa is the final state of NFA) The DFA M = ({(qo). (ou), (Go.42), { a, b}. 8, do, { (doq2)}) where 6 is as_shown in transition diagram: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 41 Automata Theory & Compiler Design 210851 Module 1 8° 1 pea) | oF q o i a OC Answer: ‘Transition table of DFA using subset construction method: 6 Je 1 @ ° ° ta 9 tr “tr a] “an [on From the above table we observe that only {p}. {p.q}. {p. r} and {p, q. r} are reachable from start state {p}, all other states are inaccessible states, So by discarding all those inaccessible states from the above transition table we get the DFA as é 1 tp} {pq} | tp} tp. a} ia | per} “por} | teach | tp.ah “pear | tad | tpg) The final state of DFA Fp = { (P.r), (P, 4, ")}_ (since ris the final state of NFA) The DFA M = ({ (p), (P.q); (Pt), (P.4.4) {0,1}, 8, {P}. 4 (P.1), (P,4.0)} where 31s as shown in transition diagram ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 42 Automata Theory & Compiler Design 210851 Module 1 LAZY EVALUATION SUBSET CONSTRUCTION METHOD An alternative subset construction method used to convert NFA to DFA is LAZY Evaluation subset construction method which will overcome the problem faced in above method. The above complete subset construction method is very slow and time consuming. Since all 2" are not reachable for every DFA, so this method will speed up the process by avoiding extra work This method is used to convert the given NFA to its equivalent DFA. Start state of DFA will be same as start state of NFA. Write the transitions for the start state for the input symbol and repeat the process until no new states are formed. The language accepted by NF. and DFA will be same. Procedure: i. The start state of DFA is the start state of NFA. Initially set of states of DFA Qp has only one state ie: the start state. ji, Identify the transitions of DFA: For each state of DIA, and for each input symbol ‘a’ in 5, compute the transitions as uo 82@0.2) = Vave.a ‘That is to compute Sp (Qv, a ), we look atall the states p in Qp, see what states in NFA goes to from p on input a, and take the union of all those states. iii, Identify the final state of DFA ie Fp; is the sets that include at least one accepting state of NFA. Note: In case in NFA to DFA conversion problem, particular method is not specified, then always go for LAZY evaluation method: ‘Convert the following NFA to DFA, s|]° | =p [ia le *q ip} | fa. r_ jo igh The start state of DFA = {p} (since p is the start state of NFA) Initially DFA has only one state ie: start state. Qo = { {p} } Find the transitions from {p} on input 0 and 1 Sn( (P), 0) = Sx(p, 0) = {ahs Sn( (p), 1) = Sx(p,1) =o Add the new state {q} to Qo= { {p}. {a} } ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 43 Automata Theory & Compiler Design 210851 Module 1 Sp( (q), 0) = x(a, 0) = {P}, So( (a), 1) = Sx(a, 1) = fa. 1} Add the new state { q, 1} 10 Qn. ie: = { fp}, {a}. {a.0}} Sn( (4, 1), 0) ~ Sv(q, 0) U Sx(r, 0) ~ {p} Vem fp} Sn( 4.1), 1) =Sy(9, 1) U Snlr, 1) = fa.) UL a= fa} Finally DFA has Qo = {{p}, (a). {4.1)} and the final state of DFA Fy= { {q}, {ar} } and 6 is 3 0 1 =p} igo * (ah ip) | fart *ta.t} | {ph {gt} The equivalent DFA is given by M =( {{ qo}, {41} . {41,42} }. (0.1 3.5, {qo}, fq1.2} ) ‘Transition table of DFA: 3 a b —40 tah ® a fanz} | tad [taney [load [tad In DFA @ indicates that there is no transition defined (or it enters to trap state) ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 44 Automata Theory & Compiler Design 21CS51 Module 1 i”. (a » mele a | Answer: Step 1: Start state of DFA is the start state of NFA; ie qo Initially set of DFA states has Qo = { (qo) } Write the transition function in state qo Sp( do, @) = By( ao, a) = { dos aa} Sp( {ao b) = Sn qo, b) = f do, a3} Add the new state { qo, q1} and{ qo, q3} to Qo & write the transition function for the new states and repeat the same process until we get no more new states. Sp {ao ai}, a) = { Bx qo, a) U Sy ai, a)} = tao a} Ufo} = {q0,a1} (already existing state no need to add into Qp) So( {qos ai} b) = {xC qo, b) U Sx qi, b)} = {qo. as} U fa} = {40,42 qs} (new state added to Qn) 5p( {doy d3}s a) = { Bx qo, a) U Bx( qs, a)} = qo, a} U fas} = {an ais} (new state added to Qn) Sd( tao, as}, b )= { Sx( go, b) U Sx( qs, b)} = {qo, a3} U {9} = {q0.45} (Already existing state) Sp( {ao G2, a3}, 4) = { Sxl qo, a) U Sn( qo, a) U Sx( qs, a) } = tao. ai} Uf) U ta} = {4444} (Already existing state) Sp {aos G2, ds}s b)= { x6 qo, b) U Sy qa, b) U SyC qu, b) } ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 45 Automata Theory & Compiler Design 21CS51 Module 1 = {a0, a3} U fo} Uo} = {qo, 43} (Already existing stete) SoC {40 G1, 4a} ) = { Syl qo, a) U SxCqu, a) U Sx( qa, a) } = tq. a} U LOU Lo} ={q0,q1} (Already existing state) So tao a, du} b= { Bx( do, b) U Sx( qu, b) U Ext as, b) } ~ fava} U far) Uo) = (qo. q2,43} (Already existing state) Since there is no more new state, we stop the process and finally DFA has 5 states; Qb= Edo}, Edo» ai}, E40, ds}, {40 42, a3}, £40, an, a4}} Since qz and q4 are the final states of NPA; the set of states of DFA containing at least one of these states is considered as the final state. Therefore the final state of DFA= { {qo, qz, qs}. {qo 41, 44}} —p | eo | @ 4 tes | w [is | a *s | an ° Start state of DFA = { p}, Initially Qy = { (p) } Bu p,0)~Sy(p,0)—{Pe#} Bu EP 1) ~SuCP,1)~ fa} Add these new states to Qo = { (p), (@)s (Pst) } ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 46 Automata Theory & Compiler Design 21CS51 Module 1 Sp( 4,0) =Sy(q,0)= {1,8}, Sp( {a, 1) =Sx(q, 1) = { p} -- already existing state So{ (P.r), 0) = Su(p, 0) U Sx(r, 0 pt} Utp.s} = fpsrs} Sol (p.r), 1) ~ Sx(p, 1) U Sur, 1) = fal VE} ~ Case} Add the new states {r,s}, { p.t,s}, {4.1} to Qp, ie:Qn = { (p), (4), (P.2), (48), E paesh far} } So( (F.), 0.) = du(r, 0) U dx(s, 0) = {ps} Ular} = fprars} Sf (1,8), 1) = Sv(r, 1) U Sxls,1) = fr} Uo= fr} So( (Pts), 0) = Sx(p. 0) U Su(t, 0) USu(s,0) = fp.t} Ufp.s} Ula} = tpeasrs} Sp( ( p.t.s), 1) = Sv(p, 1) U Sv(t, LU Bv(s, 1) = {a} Vir} Vo= fart S0{ (4, 9), 0) = Sn(q, 0) U Sx(r, 0) = fe,8} Utps} = tpers s} Sn( (a), 1) =Sx(q, 1) U dnt, 1) = fp} Ul} = fps rh Add the new states {r}, { p.a.t.s} to Qo. ieQo= { (P), (4), (P.0), (8), {PS} {4,t}, (Podsrss)oEr} } Sp( (P.4.1.), 0.) = Sn(p, 0) U dug. 0) U Sv(F, 0) Vdn(s, 0) = fp.t} Uirs}Ufp.s} Wig} = — {Psqsrss} Spf (p.ass). 1) = dx(p, 1) U SN(q, 1) U dx(r, 1) Udx(s,1) = ta} Utp}U tr} Uo= {par} So( 1,0) =Sn( 1,0) = Eps}. Sn( fr, 1)=Sx(1, 1) = fr} Qo= {(P), (@), (2), (5), Pats} fat}, (.a.458), fF}, (Psst), (P,8) } Sv( (punt), 0) —Sn(p, 0) U SN(y, 0) U Sule, 0) fp} Ules} tps} — {pas} So( (p.a.r), 1) =Sx(p, 1) USN(q, 1) U Sx(r, 1 )= fa} UfP}U fr} = fp.ar} So( (Ps), 0.) = Sup, 0) U x(s, 0) = fp.t} Uia.t} = fp.at} Sp( (p.s). 1) = Sn(p, 1) U 6x(s, 1) = {a} Uo= fa} Since there is no more new states, finally DFA has Qo= { (P), (@), (P.1), (68), Eps} tat}, (pats), {t}, (P.4.9, (P.s) } Where {p} is the start state and Final state Fp ~ {(p.r). (ts), { p.ts}.(4s1}, (P.4sts).{0}, (0.4.0). (S)} E= {0,1} 8 is as shown in transition table. ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 47 Automata Theory & Compiler Design 210851 Module 1 DFA 0 1 > (P} tpt ta @ irs} @ i Bs} o pr Bry fan) rs Pans @ “a RAS PH *p.s har Tal *pns | mans) | an “pan ters) | an “pans | Pansy | ian EPSILON - NFA (e- NFA) Here an NFA is allowed to make a transition spontaneously without receiving an input symbol. - NFA is five-tuple indicating E= (Q, £, 5, q0, F), where E is a Non-deterministic machine with e- moves where Q—- Non empty finite set of states ¥ — Non empty finite set of input alphabets (symbols) 6 — Transition function, which maps from Q x ¥ J fe} > 2° qo € Qis the start (initial) state. F is subset of Q, is the final (accepting) state. Language accepted by a s- NFA: Let E= (Q, 8, 8, qo, F) be a e-NFA. A string w is accepted by the machine E, if and only if transition for w takes the initial state q0 to final state F. ie: 8°(q0, w) is in F. ie: 8(qo, w) contains at least one accepting state. Epsilon-Closure: ‘What is epsilon-closure? Epsilon closure of any state q is the set of all stetes which are reachable from state q on ¢-transitions only. e-closure(q) 1s denoted by ECLOSE(q).. Recursive definition of epsilon-closure: ATHMARANJAN K Department of ISE, SIT MANGALURU, Page 48

You might also like