 In today's lecture, we will show that final automata and regular grammars are equivalent. That means equivalence of finite automata and regular grammars. We say that a finite automaton a is equivalent to a regular grammar g, if the language accepted by the finite automaton a is precisely the language generated by the regular grammar g. We say that the automaton a and the grammar g are equivalent. In order to establish this equivalence, we first proved that given a DFA, given a DFA, we can construct an equivalent regular grammar. Then for converse, given a regular grammar, we construct an equivalent finite automata. So, we proved that given a DFA, we can construct an equivalent regular grammar and for a converse, given a regular grammar, we construct an equivalent finite automata. So, first we proved that if a is a DFA, then the language of the DFA a can be generated by a regular grammar. So, we proved this by constructing a regular grammar for any given DFA. Suppose, a is the DFA contained the elements q sigma delta q 0 and f. Now, we will construct a regular grammar g, which is n sigma p s, where the state of non-terminals n is exactly the state of the DFA a. Then the start symbol of the grammar s is nothing but the start state of the DFA a and the set of productions is of the form a goes to small a b, where capital letters indicate non-terminals and small term indicates terminal symbols from the alphabet. So, a goes to small a b is in the production set, if delta a a capital a small a, where a is a state small a is a symbol, this is equal to b. Also, it contains all those productions of the form a goes to capital a goes to small a, such that delta capital a small a belongs to f. So, there is a construction that we will use for the given for the grammar from the given DFA a. In addition, if the initial state say q 0 is a final state say q 0 belongs to f, then we include the production s goes to epsilon in b. So, for q 0 being a final state we include this particular production as goes to epsilon in the set of productions for the grammar. Now, from the construction is quite clear that for this particular rule, we can show that if epsilon belongs to the language of the automaton a, then epsilon must also belong to the language of the grammar g. Similarly, if epsilon belongs to l of g, then epsilon must also belong to l of a. Now, from the construction since it is clear that if epsilon belongs to l of a, it must also belong to l of g and vice versa. Now, for any n greater than equal to 1, just consider the string x say a 1 a 2 up to say a n containing n symbols from the alphabet. So, x a string say this string belongs to the language of the DFA, it is accepted by the DFA. So, where x is any arbitrary string that means delta hat q 0 a 1 a 2 up to a n belongs to phi. So, after processing this string starting at start set q 0 eventually arrive at one of final states of the DFA f. Now, this implies that there exist a sequence of states q 1 q 2 up to say q n such that. So, there must exist some states such that delta q i minus 1 a i equal to q i for all i greater than equal to 1 and less than equal to n and q n belongs to f. So, if this string to be accepted by the automaton, so process this string eventually arrive at the final state. So, in such a case there must exist a sequence of states q 1 q 2 up to q n such that delta q i minus 1 a i equal to q i for all i greater than equal to 1 and less than equal to n and such that q n eventually belongs to f. So, as per the construction of the grammar we must have q i minus 1 goes to a i q i is a production of the grammar for all i greater than equal to 1 and less than equal to n minus 1 and q n minus 1 goes to a n. So, this is as per the construction of the grammar that we have just described. Now, using this production rules we can eventually derive the string x in g. To derive x in g we start with the start symbol s which is nothing but the start symbol or initial state of the DFA which is q 0. So, this derives in one step a 1 q 1 because according to construction we have a production q 0 goes to a 1 q 1 since delta q 0 a 1 equal to q 1 must be applied to process the string starting in a start set q 0. So, this again in one step gives us a 1 since q 1 goes to a 2 q 2 must also be a production since eventually delta q 1 a 2 q 2 must be a production sorry this will be a move in the DFA. So, it is a 1 a 2 q 2 and so on eventually we must have in few steps a 1 a 2 up to say a n minus 1 q n minus 1 and finally, applying the production q n minus 1 goes to a n we will have a 1 a 2 a n minus 1 a n and which is nothing but x the string x. Thus x must belong to L of g since we can derive this string x starting the start symbol of the grammar conversely to show that if x belongs to L of g then x we can derive or the the automaton a can accept the string x. Suppose the string y which is b 1 b 2 up to say b m is generated by the grammar g for some m get turn equal to 1 that is there must exist a derivation in 0 more step the start symbol should derive the string y in g the grammar g should be able to derive the string y starting the start symbol of the grammar. Now, since every production rule of g is of the form a goes to a b or a goes to a because the regular grammar the derivation as goes to y has exactly m steps and the first m minus 1 steps because the length is your m numbers of symbols is m. So, the production the sequence must have exactly m steps and the first m minus 1 steps of the production should use the rule of the form a goes to small a b where small a is a terminal symbol and eventually in the final step we have to use this kind of production precisely the derivation will written as starting with the start symbol s the first steps of the form say small b 1 capital b 1 where small b 1 is a terminal symbol from the from sigma and b 1 is a non terminal then b 1 goes to b 2 b 1 goes to some production of this form b 2 capital b 2 in the next step we will get it to be b 1 b 2 b 2 and so on eventually in m minus 1 steps we will have b 1 b 2 up to b m minus 1 capital b m minus 1 in every step we have used a production of this form a goes to small a b and the final step is a m h step this non terminal b m minus 1 can be substituted by a terminal by using this kind of production a goes to small a that means it is b 1 b 2 up to b m minus 1 and then this is b m. Now, from this derivation and if you consider the construction of the grammar g then see that in the automaton we must have for each production for every production of the form say b 1 goes to b 2 b 2 we must have a move of the of the of kind say delta b 1 b 2 equal to b 2 now from the construction we know that delta b i minus 1 delta b i minus 1 small b i must be equal to b i according to our construction and also since we have used the last step the production b m minus 1 goes to small b m. Therefore, delta b m minus 1 b m must belong to final step it must be a final step. So, we must have productions of this form and this must be a final step now using this set of transitions. So, what we can do we can consider the processing the string starting in the starts at q 0 the string y is nothing but delta hat q 0 is nothing but s it is b 1 b 2 up to b m. So, if we consider it to be delta hat delta s b 1 first to take the first symbol and then consider remaining string b 2 b 3 up to b m. So, using the first move delta s b 1 is nothing but s equal to q 0. So, q 0 b 1 will get it to be b 1 b 2 b 3 up to b m following this eventually what we will get it to be delta hat b m minus 1 b m, but this is nothing but delta since a single step and single symbol delta b m minus 1 we can replace this hat by simple transition b m, but this must belong to a final step according to our construction. So, therefore, y must also belong to the language of the automaton a and hence we have found that l of a is equal to l of g. So, this proves this theorem. So, let us illustrate this example this theorem by giving an example just consider the d f a containing two states q 0 and q 1 where q 1 is a final step and q 0 is a initial step q 0 on a goes to the final step q 1 and on b it remains on the same step b q 1 on b remains on the same step b and on a it comes back to the initial step. So, for this d f a let us construct the equivalent regular grammar using our construction rules. So, the grammar g contains n sigma p s where n is the set of states is nothing but q 0 and q 1 and the starts symbol s is nothing but q 0 sigma is obviously the two symbols a b and where the set of production tools can be found from the moves of the d f a like this. So, q 0 since q 0 on a goes to q 1. So, q 0 a q 1 will be a production rule q 0 on b goes to q 0. Therefore, q 0 goes to b q 0 is also a production rule and since q 0 on a goes to a final step which is q 1. Therefore, q 0 goes to a is also a production rule according to construction. Similarly, and these are all possibilities for q 0 similarly for q 1 is to consider all the transitions on the moves q 1 on a goes to q 0. Therefore, it is a q 0 is a production rule q 1 on b goes to q 1 again. So, q 1 on b goes to q 1 and since q 1 on b goes to q 1 and q 1 is a final step. Therefore, q 1 goes to b must also be a production. So, these are only possible productions that we have for this grammar. So, this is the equivalent regular grammar corresponding to this d f a. Let us consider and the example say this is the d f a q 1 is a start step q 1 on a goes to q 2 where q 2 is also a final step q 1 is a start step and a final step q 2 is a final step and q 1 on b remains the same step q 2 on a goes to state q 3 and q 2 on b say goes to q 1 and q 3 on a and b remains in the same step a b. So, therefore, we can construct according to or considering the rules and equivalent regular grammar say g containing the elements n sigma p s where n is a set of states which is nothing but q 1 q 2 q 3 s is the start symbol which is the start state of the d f a which is q 1 sigma is the set containing a and b and the set of productions will be can derived from all the transition moves of the d f a. So, if we start with q 1. So, q 1 on a goes to q 2. So, therefore, q 1 goes goes to a q 2 will be a production rule. Similarly, q 1 on b goes to q 1. So, therefore, q 1 b q 1 must be a production rule again since q 1 on b goes to q 1 and q 1 is a final step. Therefore, q 1 goes to b will also be production rule. Similarly, q 1 on a goes to q 2 where q 2 is a final step. Therefore, q 1 goes to a must also be a final step and since q 1 is the initial state and this also a final step. Therefore, q 1 goes to epsilon is also a production rule according to our construction. Then, we have considered all possible all the transition moves all the moves of the automata from set q 1 on every input symbol. Similarly, on set q 2 if you consider all the moves q 2 on a goes to q 3. Therefore, it is a q 3 q 2 goes to a q 3 will be a production. Similarly, q 2 on b goes to q 1. Therefore, b q 1 q 2 goes to b q 1 must be a production. But, q 2 on b goes to q 1 where q 1 is a final step. Therefore, q 2 goes to b must also be a production according to our construction. So, these are only moves from q 2 on every input symbol. Similarly, considering q 3 you will find that q 3 goes to q 3 goes to a q 3 and b q 3 are also productions of this grammar. Of course, here q 3 is a trap state in the DFA and therefore, we can remove or delete all the production rules involving q 3 without disturbing the language generated by the grammar. That means, we can remove these production rules and these production rules and the resultant simplified grammar will be this one only. So, is the equivalent regular grammar corresponding to this given DFA. After showing that given any DFA we can construct an equivalent regular grammar. We now move to show that if L is generated by a regular grammar then L is regular. That means, for L we can construct an equivalent DFA. So, before going to construct a DFA we will first introduce the concept of generalized finite automaton or simply DFA. So, generalized finite automaton or GFA is a non-thetamistic finite automaton in which the transitions may be given via strings from a finite sheet instead of just symbols. That is formally a GFA is a 6 triple containing 6 elements q sigma x delta q 0 f. All other elements except for x are identical to a DFA here x is basically a subset from a finite subset x is a finite subset of strings from sigma star finite subset of sigma star. And here the moves are of the form delta the transition from delta is basically from q cross x to the power set of q. So, instead of a single symbol from sigma we take a string from sigma star. So, that is how we define a GFA. So, even though it may be seems to be more powerful, but you can show that GFA is no more powerful than an NFA. Instead given any move say delta p x say is equal to q. So, we have this kind of transition or move in a GFA. So, if we have a transition like this or move like this we can always replace this transition by sequence of transitions introducing a few intermediate states such that if x equal to say a 1 a 2 up to say length k for some k greater than or equal to 2. In such a case we will introduce k minus 1 states in between which are say p 1 p 2 up to say p k minus 1. So, that from p on a 1 on the first symbol it goes to say p 1 from p 1 on the second symbol a 2 it goes to p 2 and like that eventually from p k minus 1 it goes to on a k state q. So, this move delta p x equal to q can be replaced by a sequence of moves introducing a few finite number of states. And we can show that or you can easily be prove that the language of this new finite element on NFA is equivalent or identical to the original GFA. So, similarly all other transitions can be replaced by a sequence of transitions in the in an equivalent NFA. Hence, for every GFA we can construct an equivalent NFA removing the sequence I mean transitions on strings. Now, we show that if L is generated by a regular grammar, then L is regular. That means, we can construct an equivalent DFA or equation final automaton to accept the language generated by the grammar G. So, let us give a construction to give a set of rules to construct an equivalent final automaton from the given regular grammar. Suppose, the given regular grammar is G containing the four tuples connect the four elements and sigma B S. Now, we know that since G is regular every production is of the form a goes to a b sorry a goes to x b where x belongs to sigma star or it may be of the form a goes to x. So, x may be any string from sigma star and here a and b are non terminals it belongs to the set of non terminals. Now, what will do we will construct a GFA from this given grammar G. So, that it accepts the same language generated by the grammar that means L G. Now, what will do let x be the set of strings that are on the right hand side of the production. That means, if this is a production. So, this string x will belong to this set x. So, when a goes to x this will belong to this set x. That means, x a set of all strings x such that a goes to x b belongs to the set of productions or a goes to x belongs to the set of productions in the grammar. So, that is how we define the set of strings x from sigma star. Now, since p is a finite state this x is a finite subset of sigma star. So, it must be finite. Now, let us construct the GFA GFA say a is a GFA containing the six elements is a six table where this set of states of the GFA q is basically the set of non terminals union one special state say it is dollar. So, numbers of states will be equal to numbers of non terminals in the grammar plus one special state which indicate by is dollar symbol. And the star state is nothing but the star symbol of the grammar. And the final state set of final state is nothing but the only state containing the symbol dollar and we define the transition function like this. So, delta a x equal to b if and only if a goes to x b is a production of the grammar. So, if a goes to x b is a production of the grammar then we include delta x equal to b as a transition or a move in the GFA. Similarly, delta a x equal to dollar if and only if a goes to x is a production of the grammar. So, there are only two rules that we have introduced to construct the moves of the GFA from the productions of the grammar. Now, we claim that the language of the GFA a is nothing but the language generated by the grammar G that is what we have to prove. Say let W belongs to L of G. So, since W is generated by the grammar G therefore, there must be a derivation say which has suppose k steps. So, G must have a derivation which has k steps which is obtained by following k minus 1 production rules in the first k steps and eventually in the kth step. So, in the first k steps will be using the production rules of the form a i minus 1 goes to x i a i and the kth step we must use a production of the form say a k minus 1 goes to x k. So, if W belongs to L of G obviously, must have a derivation and the derivation suppose if it has a blank k in the first k steps it must be we must use this kind of production rules for i greater than equal to 1 less than equal to k minus 1 and the final step we have to use to replace the last non-terminal. We must use a production rule of form a k minus 1 goes to x k. So, in this case of course, the start symbol s is a 0. Thus for W equal to x 1, x 2 up to say x k we can show the derivation like say s started s which is equal to a 0 in 1 step it must derive x 1 1 1 a 1 in the next step a 1 goes to x 2 a 2 has to be used. So, it is x 2 a 2 and so on eventually in k minus 1 step we will have x 1 x 2 up to x k minus 1 a k minus 1 and the kth step we have to use replace this a k minus 1 by x k by using this kind of rule x 1 x 2 up to x k minus 1 x k which is nothing but W. So, the derivation must be like this. So, if these are sequence of derivation or sequence steps in the derivation. So, according to the construction of the GFA we must have production rules of the form a i minus 1 x i equal to a i. That means, if we start at q 0 and process W with nothing but s x 1 because q 0 is the start symbol of the grammar s W is x 1 x 2 up to say x k. So, this will give since s is say is a 0 x 1 x 2 up to x k. So, in one step we will get it to be a 1 x 2 x 3 up to x k because according to the construction delta a 0 x 1 equal to a 1 is a transition or move in the GFA. Since, we have a move like this for every production of the form say a i minus 1 goes to x i a i. So, in the next step accordingly what will have from a 1 x 2 delta a 1 x 2 you will get a 2 x 3 up to say x k. And eventually in k minus 1 moves will have a k minus 1 x k. And finally, since in the derivation the final step we have used this production a k minus 1 goes to x k to replace a k minus 1 by x k. So, accordingly we must have accordingly we must have a k minus 1 x k because since is a production of the form a goes to x. So, this must be a final step and eventually we will consume the whole string w and we will arrive at the final step which is duller because a k is basically must be a final step according to the construction. So, therefore, since we have consumed the whole string we know that the language L is a subset of L a. Similarly, you can show the converse. So, converse can be shown exactly in a similar way. Therefore, eventually you can show that L of a equal to L of g. Now, let us explain the construction by giving one or two examples. In the first example let us consider the grammar s goes to small a s small b s a b a and say a goes to small a a b a and epsilon. Suppose, we have these six productions in the grammar. So, we will first construct from this. So, this is a this is a regular grammar it follows all the rules of a regular grammar. So, we will first construct a g f a. The transition map of the g f a is delta and we construct like this for epsilon symbol a b and we have a string a b in the right hand side of the production. Those are the only possible strings that we have a b a b and epsilon and the states in the g f a will be the non-terminals s a and the new non-terminal dollar. So, this s on epsilon there is no move on epsilon. So, it will be fine. So, s on small a it goes to s according to our construction because s goes to a s is a production in the grammar. So, therefore, delta s a equal to s we have a move like this in the g f a. Similarly, since s goes to b s is a production delta s b equal to s is a move. Therefore, del s on b goes to s itself. Similarly, s on a b goes to state a following the same rules a on epsilon there is no move. So, there is a move a on epsilon a goes to epsilon is a production that we have a goes to epsilon is a production. Therefore, a goes to dollar must be included. So, that means delta a epsilon equal to dollar must be a move according to our construction that we have already introduced. Then a on a small a it remains in the same state a a on b again since a goes to b a is a production it remains in the same state a and on a b there is no move because there is no production sort of form a goes to a b something. So, therefore, this is phi there is no move and from dollar we give no move. So, this is the generalized final automata that we have now we have only one transition from the state s to the state a on the string a b all the transitions are on symbol a either on symbol a or b. So, the only transition on some string is for the string a b and that is from star set s to the state a we can remove this by introducing a new state say it is b. So, from s we can go to b on symbol a and from b we can go to state a on symbol b to replace the move from s to state a on the string a b. So, introducing this modification we will get the new transition map delta on epsilon a and b as on epsilon goes to s on epsilon goes to phi and s on a initially it went to star set s now it will also go to b. So, now therefore, it will be s and b and s on b it will remain in the same state and on a b we have already removed. So, s on a on epsilon it goes to the final state dollar and on a there is no sense on b there is no sense you just copied it then have introduced a new non-terminal b. So, b on small b goes to a because according to this move b on small b goes to a and there is no other move from b on epsilon or on the symbol a similarly from s from dollar there is no transition. So, this is a transition map for a given final automata that means if you draw the transition diagram start with s this is a start symbol. So, s on a it may go to s or it may go to and then s on b will go to s again therefore, s on a and b may go to s b on b on b goes to a or b on small b goes to a a on a and b goes to a itself and then a on epsilon goes to dollar which is a final state of course this on epsilon goes to dollar and dollar does not go this state does not go to anywhere on any of the inputs symbol. So, these are required transition diagram for a given regular grammar let us consider another example say s goes to b s a a a goes to b a say a b and b goes to b b a s. Now, here we will find that there is no any production switch contains a string all the production right hand side of the production there is no string involved all our symbol. So, therefore, it is easy to draw the transition diagram directly. So, the start set will be the start symbol of the grammar is s. So, s on b from this production s goes to b s s on b it remains on the same state and s on a small a it goes to the state a from this production s goes to a. Now, a goes to b a for this we have again s a goes to b a we have a self look similarly, a goes to a b on a it goes to the state b. Then b on small b goes to b that means it will be a self look on b and b on a goes to from this production b goes to a s b on a goes to s and eventually of course, we need the general production say epsilon. So, b on epsilon goes to final state because this is the only production of the kind of the form a goes to a goes to x where x belongs to sigma star. Since, b goes to epsilon epsilon is a string from sigma star. So, this must be a final state. So, therefore, b on epsilon goes to dollar. So, this is the required final automata which is equivalent to this regular grammar.