 Welcome to the lecture on syntax analysis part 3. So, so far we covered the principles of syntax analysis, contracts free grammars and push down automata and we also saw some definitions regarding l l 1. Today we will continue with l l 1 parsing go on to cover recursive descent parsing as well. Some recap on l l 1, so we just you know use l l 1 instead of strong l l 1 because strong l l 1 and l l 1 are equivalent as far as look ahead 1 is concerned and since it is not easy to implement look ahead longer than 1, we will not worry about l l 2 l l 3 and so on. We also defined first and follow sets last time let us do a recap on the definitions. The first of alpha is a set consisting of the symbols a which can be derived from alpha. So, a in t and alpha derives a x and x is also a terminal string first of epsilon by definition is epsilon and the follow is all those symbols a which actually are after the non terminal a in any sentential form. So, that is why it is defined as all those symbols a such that s derives alpha a a beta thereby signifying that a occurs in a sentential form and we collect all these symbols little a which are after this non terminal a a can be either a terminal symbol or the end of file symbol dollar. So, what we need to I know understand is that first of alpha is determined by alpha alone whereas, follow is determined by the context that is we require that s derives at a sentential form containing a. Here was an example here is the example that I presented last time. So, for this grammar s prime going to s dollar s going to a s r c a going to b a r s b b going to b a r s the first and follow are computed here. We begin you know very simply with first f s prime which is nothing, but the first of s because all the strings from s prime actually are derived by s and what does s derive all strings you know derived by s begin with either a or c. So, first of s would be a comma c similarly, for first of a we have all the strings derivable from a you know that they begin with little b and because there is a non terminal s here all the strings which are derivable by s are also included in the first of a and for b. So, that gives us you know the first of a as a b and c. So, now as far as b is concerned the symbols b and the first of s are included in the first of b. So, that is how these are all computed. Now, what about the follow follow of s it says is a b c dollar. So, let us see where s occurs. So, s prime derives s dollar. So, this is the first sentential form where s occurs and dollar follows it. So, this dollar is because of that and s prime derives s dollar and if we replace s by a s dollar then you know finally, a can be replaced by s b and now b is a non terminal. So, let us make it derive some string all these strings will follow this non terminal s. So, if we apply b going to b a little b follows s. So, that b is included in the follow of s and then we have in a similar way this sentential form a s a a s s where little a follows s. So, a also gets into the follow set of s finally, we derive a s c s dollar from s prime where little c follows s. So, c is also included in the follow set of s follow of a in a similar way is a comma c because s prime derives a a a a s dollar where little a follows capital A and in the other one s prime derives a a c where little c follows capital A. So, these two are in the follow of a. So, this is as far as the example is concerned to compute the first for terminals and non terminals it is fairly straight forward terminal symbols the first set does not change it is the symbol itself and first of epsilon is always epsilon for the non terminals we begin with phi and then we consider and we need to iterate the computation this entire thing until even one until even until every first set stops changing. So, even if one of them is changing then we need to iterate once more this is because the first set computation you know even if one of them changes it may actually make a few others change as well. So, then the computation is quite straight forward we look at the first you know production a a going to x 1 x 2 etcetera x n. Now, first of a obviously includes the first of x 1. So, first of a equal to first of a union first of x 1 minus epsilon. Now, if x 1 actually first of x 1 produces epsilon in that case whatever is derived by x 2 is also derived by a. So, that is what is being checked here is epsilon in first of x i. So, if so we include the first of x 2 also into first of a similarly if there is x 3 and x 1 and x 2 both of them produce epsilon then we include you know the first of x 3 also into the first of a. So, this keeps going and if x 1 x 2 etcetera x n all of them first of all these symbols contain epsilon then epsilon itself has to be included in the first of a. So, if we reach i equal to n that is all the symbols have been looked at that means all of them have produced epsilon and epsilon is in the first of x n. So, that is the last one we include epsilon into the first set of a. So, now suppose we have a string beta for which we want to compute the first set it is not different from computing the first set of a non-terminal. If the string is the string beta is x 1 to x n we do exactly what we did before for the non-terminals except that this algorithm has placed beta in place of instead of a in all the lines. So, we consider x 1 x 2 etcetera x n. So, first of x 1 is obviously in first of beta then if x 1 produce you know can produce epsilon that is epsilon in first of x 1 then first of x 2 goes into first of beta and so on and so forth exactly the way it was before. Now, let us look at an example to understand how this algorithm works. So, prime going to s dollar s going to a s or epsilon. So, this is the difference between the this grammar and the previous one the other two productions are the same a going to b a or s b b going to c a or s. So, to begin with we initialize the first sets of all the non-terminals to phi the null set and iteration 1 first of s is a comma epsilon because it derives a and there is s going to epsilon as well. So, that includes epsilon to the first set of s now the first set of a here is b. So, which begins the string b a and then we have the non-terminal s. So, b union first of s of course minus epsilon and union the first of b minus epsilon why s you know derives epsilon. So, first of s we see here has produced an epsilon. So, therefore, both the first of s and first of b have to be included in a and what about b itself b produces c a and s now this c is obviously non-null, but b produces s and first of s contains epsilon. So, finally, a going to s b will also produce epsilon and therefore, we need to you know include it, but not immediately at this point of time we do not know that first of b is phi. From the analysis you know if we apply b going to c a and b going to s then we know that you know b is first of b includes first of s and therefore, it includes epsilon, but at this point in iteration 1 first of b has been initialized to phi and therefore, we cannot include epsilon into the first set of a at this point of time it will be included in the next iteration. First of b is c. So, at the now we know what first of b will be it will be different it is c of course and the first of s first of s actually includes an epsilon and therefore, you know we include epsilon into the first of b. So, c a epsilon is the first set of b. So, this is as far as the first iteration is concerned in iteration 2 now first of s has epsilon first of a has b a first of b has c a epsilon first of s does not change it is just a epsilon, but first of a changes. So, from here we see the same b union first of s and then since s includes an epsilon first of s includes an epsilon first of b as well and since first of b also includes epsilon we need to include epsilon as well. So, b union first of s minus epsilon union first of b minus epsilon and then we have to include epsilon because first of you know both s and b derive epsilon. So, that means this becomes b a c epsilon which is different first of b c a epsilon you know has no has not changed from the last iteration. So, these are the values which stabilize and do not change in iteration 3. So, these are the first values now let us look at the algorithm for computing follow of a non-terminal what we saw was the definition before, but now let us see how it is computed initialize the follow set of every non-terminal to phi and follow of s is initialize to dollar because s is the start symbol and we have s prime going to s dollar. So, dollar will be always present in follow of s. So, we do this for every production until no follow set has changed exactly the way we computed the first set that the production be x 1 a going to x 1 x 2 x n. So, the assumption is not all these symbols are epsilon. So, in other words it is not a production a going to epsilon follow of x n is. So, we are know we cannot compute the follow of a we need to actually compute the follow of x n you know and then the other symbols as we go along, but to compute the follow of a we need to look at the production in which the right hand side contains an a until the time we cannot compute the follow of a because follow of a requires a context derivable from the start symbol. So, for the non-terminal x n it is easy to see that in a sentential form when we replace a by the right hand side x 1 to x n the symbols which were following a we also follow x n. Therefore, follow of x n is follow of x n union whatever is the previous value from the previous iterations union follow of a. So, now let us call rest as follow of a we will see why it is required. Now, we are going to look at the symbols x 1 to x n in the reverse order from x n downwards to x 1. So, n down to 2. So, in such a case after x n for which we have computed the follow in the first iteration we go to x n minus 1 x n minus 2 etcetera. So, let us take just two symbols that is n equal to 2 let us say we have computed the follow of x 2 now we are looking at the follow of x 1. Follow of x 1 contains all the symbols in the first of x 2 that is obvious because x 2 derives a certain number of strings and they are all following x 1, but suppose x 2 also produces epsilon in such a case when we replace a by x 1 x 2 and make x 2 derive the epsilon all the symbols which follow a in a sentential form will also follow x 1 in other derivations. Therefore, the follow of a which is nothing, but you know the symbols which follow a will also be included in the follow of x 1. So, here is the generalization for the production a going to x 1 to x n. So, it says for i equal to n down to 2 if epsilon is in the first of x i so that is n is i is n to begin with. So, first of x n so if this is going to produce an epsilon then follow of the symbol which is prior to this that is x of n minus 1 x i minus 1 will be follow of x i minus 1 union the first of x i. Obviously, this particular symbol first of x n will be included in the follow of x n minus 1 union the rest. So, in the first instance rest will be follow a, but later it will become different now rest is again made follow of x i minus 1 then we go to the next symbol and so on and so forth. So, suppose epsilon was not in x n at all the first of x n then you know follow of a will never be following the symbols of x n minus 1 for as the symbol x n minus 1. So, we do not have to worry about the epsilon part we simply say follow of x i minus 1 is follow x i minus 1 union first of x the rest part does not come into picture and of course, rest is initialized to follow of x i minus 1. So, this iteration continues until the follow sets have not changed. So, let us take an example. So, here it is the same grammar that we considered for the first computation to begin with follow of s is dollar and others are all phi the first computation let us assume has been completed. So, first of a is epsilon first of s is a epsilon first of a is a b c epsilon first of b is a c epsilon we computed in the example prior to this. In iteration 1 we use the symbol x union equal to y to mean x equal to x union y this is very similar to x plus equal to y in c it is used to you know reduce the amount of material present on the slide s going to a s. So, here this is the s that we consider now follow of s will be follow of s from the previous iterations and union dollar. So, why is that follow of this symbol is the s and this is the s that we are considering to contain dollar. So, follow of this s is follow of this s inclusive. So, that is dollar and rest would be follow of this s which is again dollar the next symbol is a. So, follow of a is follow of a union first of s minus epsilon union rest the reason is this s you know first of s contains epsilon here. So, whatever follows this s will also follow this a and therefore, follow of a will include first of s union the rest is nothing but the follow of s. So, that computes the follow of s and a here the next for this production. So, now let us take the production a going to s b here b first of b also contains epsilon. So, follow of s will contain follow of first sorry follow of b will contain follow of b union follow of a. So, that is because b derives you know this is the last symbol. So, once a replaces is replaced by s b whatever is following a will also follow b and the rest is the carded as follow a. Now, the fact that b produces epsilon is useful in computing the follow of this s follow of s will be first of b minus epsilon union rest union rest is nothing but follow of a that is because b produces epsilon. So, all the symbols which follow a will also follow this s. So, that gives us a c dollar from this production follow of a will be follow of a union follow of b and from this production follow of s will be follow a phase union follow of b. At the end of iteration one these are the values we got and in iteration two the production a a s uses follow of s equal to a c dollar. So, that is there is no change there whereas, follow of a equal to follow of a union first of s minus epsilon union rest that is because b produces an epsilon. So, that gives us a c dollar now the follow set has changed. So, follow of a was different now it has changed and the production a to s b we compute follow of b. So, that again changes to a c dollar follow of s does not change and at the end of iteration two we have follow of s equal to follow of a equal to follow of b equal to a c dollar and the follow sets do not change any further. So, here in these cases we required two iterations, but it is possible that more than two will be required that is easy to see. So, let us consider the L L 1 conditions now. So, far we computed the first and follow. So, now we are going to define the L L 1 grammar condition based on the first and follow. The reason we want to do that is we want to show an algorithm for computing the parsing table for L L 1 grammars and those are based on first and follow. Suppose G is our grammar G is L L 1 if every pair of productions a to alpha and a to beta the following conditions satisfied. So, the point is during L L 1 parsing when there is a choice of productions a going to alpha or a going to beta we should be able to say that one of them applies by looking at the next symbol in the input. So, this can be asserted if for every choice of productions we are able to satisfy this particular condition which is stated here. The condition says direction symbol set of alpha intersection direction symbol set of beta is phi. So, what is direction symbol set? Direction symbol set of gamma is if epsilon is in the first of gamma then it is first of gamma minus epsilon union of follow a otherwise it is just first of gamma. So, if you know gamma stands for either alpha or beta. So, in other words you know whatever symbols alpha derives and beta derives the symbols of these should not be the same otherwise the choice cannot be made by looking at the next input symbol. So, there is an equivalent formulation in the how you know Seti and Ullman's book how lamb and Seti and Ullman. It says first of alpha dot follow a intersection first of beta dot follow a equal to phi for the same productions a to alpha and a to beta. These conditions are identical it is easy to see that because suppose alpha does not produce any epsilon in that case epsilon in first of alpha is false. So, the direction symbol set simply becomes first of alpha. So, the follow is inconsequential in that case. Suppose alpha does produce epsilon. So, first of alpha contains epsilon in such a case if this is epsilon then the follow set of a will also be included in the first computation. So, we also do that here you know if epsilon is in the first of gamma first of gamma minus epsilon union follow of a. So, all the elements in the follow of a will also be included in direction symbol set of alpha in this case the same is true for this as well first of beta dot follow a. So, direction symbol intersection or this intersection are identical as far as the L L 1 condition holds. Now, this is the condition for the grammar to satisfy the L L 1 parsing property. Suppose the condition is satisfied can we build a parsing table L L 1 parsing table from the grammar definitely the process is quite simple for each production a to alpha we check each symbol s in direction symbol alpha. So, s may be either a terminal symbol or the end of file symbol dollar just add the production a to alpha to the parse table at the point a comma s we will see an example of this. So, and make each undefined entry as error. So, with the other formulation first and follow formulation the table construction is very similar consider the first of alpha add the productions. And if epsilon is in first of alpha then add for all follow of a as well and if dollar is in the follow of a add it for the dollar as well. So, after the construction of the table so of course, we could test the productions for the L L 1 property and then build the table or we could build the table and check if any slot in the L L 1 table has more than one production two or more then the grammar is not L L 1. So, these two conditions are identical. So, let us take up a few examples and understand what exactly this L L 1 property means. So, here is a very simple grammar the sentences or statements are either if a s else s or while a s or begin s l and where s l is a statement list. So, we derive s s prime from s l and s prime derives a semi colon s l or epsilon. As far as production one is concerned each of the three alternatives begin with a different token here is if here is while and here is begin. So, these three terminal symbols are all different. So, as far as p 1 is concerned there is the L L 1 property is satisfied for p 2 there is no choice. So, there is nothing to there is that L L 1 property is trivially satisfied for p 3 we have s prime going to semi colon s l or epsilon. So, we need to compute the direction symbol set for this side and direction symbol set for this epsilon as well direction symbol set of semi colon s l all the strings begin with semi colon. So, it is just a non null symbol. So, it is just semi colon and direction symbol set of epsilon to compute that their symbol of epsilon it produces an epsilon of course. So, therefore, it is nothing but the follow of s prime to compute the follow of s prime we look at the production where s prime is placed. So, this production has s s prime on the right hand side and since this is the last symbol here it tells us that follow of s prime you know is nothing but follow of s l. Now, look at s l s l is contained here and what follows s l is the token end. So, direction symbol set of epsilon is end and again semi colon and end are two different symbols they do not intersect to non phi. Therefore, the L L 1 condition is satisfied and the grammar is indeed L L 1 here is a different example. So, if you observe in this example I carefully avoided the two possibilities of if statements if a then s if a then s else s these are the two possibilities. So, I included only one of them what happens if we include both. So, let the grammar be s prime going to s dollar s going to if i d s or if i d s else s or a. So, a is any other you know the statement now trivially you can see that these two alternatives begin with the same symbol if. And therefore, our hunch is that the L L 1 test will be not satisfied. So, let us formally check it out for this you know s prime a producing s dollar. So, direction symbol set of s dollar is if comma a because direction symbol set is nothing, but the first set if there is no epsilon involved. So, s produces if and a as the first symbols or the first tokens. So, if and a are included in the this symbol of s dollar this symbol of the terminal symbol a is of course, just a divorcing of symbol of if i d s is if because the first symbol is a normal if symbol here also it is normal and if. So, this is also if. So, if you look at the alternatives these two these two and then this and this for the first pair it is indeed phi the intersection is phi for the second pair the intersection is phi, but for the third pair if i d s and if i d s s the sets contain the symbols if and therefore, it is not equal to phi. So, the L L 1 test fails let us see what happens if we fill the table. Because using direction symbols to check the L L 1 property is one way building the table and then you know checking the slot is a simpler option. So, let us see how it can be done. So, for the non terminal s prime. So, s prime going to s dollar. So, we consider the direction symbol set of s dollar it contains two symbols if and a. So, the production s prime going to s dollar is added for the two symbols if and a for the production s going to if i d s we look at the direction symbol set of if i d s and that is just if. So, for the non terminal s on the symbol if we add the production s going to if i d s and for the second production s going to if i d l c s the direction symbol set again contains f if. So, for the non terminal s for the symbol if which is contained in the direction symbol set we add the production s going to if i d s l s and the last one s going to a direction symbol set of a is a. So, for the combination of s and a we add the production s going to a. So, now this slot you know contains two alternatives. So, two productions and therefore, the grammar is not l l 1 of course, the problem with this grammar is it is ambiguous we have seen this example before. So, if you look at this the two parse trees which are produced by for the same string if i d if i d a 1 l c a 2 we already know this example we know that there are two parse trees possible because the grammar is ambiguous precisely what creates the makes the grammar fail the l l 1 test. This is the original grammar that we just now saw suppose we apply what is known as left factoring. So, the problem is if i d s is common to both these productions suppose we make a you know factoring out of it make the if i d s as common to both these productions and introduce a new non terminal called s 1 which goes to either epsilon or else s. So, between these two if we simply expand s 1 we get both the productions which are here the rest of the grammar remains the same. Now, you compute the direction symbol sets for s dollar it is the same for a it is the same for if i d s s 1 it is just if single one if i d a and then direction symbol set of l c s is just s and for the epsilon. So, here is epsilon. So, direction symbol set of this epsilon will be follow of s 1 s 1 is here. So, it tells us that follow of s 1 is nothing but follow of s s is now in two places this s give rise to gives rise to dollar and this s gives rise to first of s 1. So, first of s 1 contains else. So, direction symbol set of epsilon contains else comma dollar. If you look at the intersections this intersection of course, this is this as if and that as a. So, it is non phi but for this intersection we have non phi you know which is sorry the previous intersection if and a it is phi whereas, for this intersection it is not phi. So, this contains else and this contains else and dollar. If you fill the table entries. So, exactly the way before s prime gets these two entries s now has just one entry for if and one entry for a, but the two entries have now shifted to s 1 and else. So, the problem has just shifted to a different set of productions from the if i d production to else s production this grammar is still ambiguous. So, if you look at the string if i d if i d a 1 else a 2 and we know that we can produce these two parse trees. So, until this point the parse trees are identical. So, both of them are the same, but here do we produce this epsilon or do we produce else s a 2 else a 2 this is one you know ambiguity and for this s again the expansion is if i d s s 1. So, if i d s s 1 and for this s do we produce an epsilon or do we expand it to else a 2 this is the other ambiguity. So, because of these two the two parse trees are different and because the grammar is different you know ambiguous and it fails the l l 1 test. So, another example and in this case the grammar is indeed l l 1. So, we have this good old grammar s prime going to s dollar s going to a s or c a going to b a or s b b going to b a or s. We have already worked out the first and follow sets. So, I just reproduce them here now let us see what the direction symbol sets are for s dollar it is the first of s. So, first of s contains a c. So, direction symbol set of s dollar is a c for the string a is direction symbol set is obviously little a because a is not a null character. Similarly, del sim c is c del sim of b a is b del sim of s b is really first of s. So, which is nothing, but a comma c del sim of b a is little b because it starts with b and del sim of s is first of s which is a comma c. Now, these are the three pairs which we have here for a a s and c the direction symbol sets do not intersect they produce phi for b a and s b this is b a this is s b. So, b a is b and s b is a comma c again they produce a phi for b a and s one is a little b and the other one is a comma c again it produces a phi. So, the element test is satisfied. Now, let us fill the table for the production s prime going to s dollar for the symbols a and c we place this production s prime going to s dollar in the table for the two productions a s and c let us take one at a time s going to a s the del sim is a. So, we place this production for the combination s and a the production s going to c has del sim c equal to c. So, for the combination of s and c we place s to c. So, similarly a to b a is placed you know for the combination a to b and a to s b has first of s equal to b a del sim is also b a. So, sorry del sim of s is a c. So, for a and c we place the production a going to s b and a going to s b similarly for these two productions for b we produce we provide the production b going to b a and for the first of s that a comma c we place the productions b going to s. So, the table has no conflicts that is no slot has more than one entry and therefore, the grammar is indeed l l 1 another example. So, this time the example tries to show that rewriting a grammar or transforming a grammar makes it l l 1. So, here is a grammar for statement list s prime going to s l dollar and statement list is s l s or s s going to a. So, we produce as a is any statement. So, we produce as many s as necessary using the recursive production here. So, note that this is a left recursive grammar the theorem is that all left recursive grammars fail to be l l 1 here is a hint of why that happens see s l produces s l and s and s l also produces s. So, in some sense the first of s l and this first of s obviously will contain the same entries and therefore, der simp of these two will obviously not be phi and that is why the you know l 1 property fails. So, we can see that happening here der simp of s l dollar is just a because first of s l is nothing but a first of s l is first of s and that is a der simp of a is a der simp of s l s is also a because that is nothing but first of s l which is first of s and that is a and der simp of s is a. So, you can easily see that the intersection of der simp s l s and der simp s which is a is not phi and therefore, the table has entries s l going to s l dollar and s l s and s l going to s for the combination s l and a. So, the grammar is not a little one suppose this left recursive grammar is made into a right recursive grammar. So, it is actually the same language it is just that we rewrite the grammar as a right recursive grammar. So, s prime produces s l dollar that remains as it before instead of s l going to s l s or s we make the production as s l going to s a rewrite it and then a going to s a or epsilon s going to a remains as it is. So, between these two the a part produces as many instances of the statements as necessary. So, but the grammar is not left recursive anymore it is right recursive because a appears here and it is the same symbol as the l h s a. So, this is a right recursive production whereas, this is a left recursive production. So, if we compute the direction symbol sets for this we get for s l dollar and a we get the same sets for dorsum of s a we get a because dorsum of s a happens to be first of s and that gives us a and what about dorsum of epsilon. So, here is epsilon this is nullable it produces an epsilon there is. So, it includes the dorsum of epsilon will include the first follow of a and let us see where a occurs it occurs here, but that again tells us that it is follow of a it occurs here. So, that tells us that the follow of s l is also required and s l occurs here and that includes a dollar the follow of s l includes a dollar. So, dorsum of epsilon uses dollar and now for the alternatives s a and epsilon we have s a as a dorsum of s a as a and dorsum of epsilon as dollar. So, these two actually intersect to phi therefore, the grammar is indeed l l 1 and we can fill up the table in this fashion there are no conflicts here. So, a going to a and a combination is a going to s a and a and dollar combination is a going to epsilon. So, this is the l l 1 passing table for the right recursive grammar. Now, this gives us a hint that if we are able to convert left recursive grammars to right recursive grammars then maybe some of the grammars which fail the l l 1 test can be made to succeed in the test it is indeed true. So, now we study some of the grammar transformations. So, as I mentioned before to compute the first end follow sets we must make sure that the grammar has no useless symbols and I also mentioned at that time that the elimination of useless symbols will be dealt with later. So, now we come to the algorithm which eliminates the useless symbols what exactly is a useful non terminal and what is a useless non terminal. The grammar transformations that we study are elimination of useless symbols elimination of left recursion and what is known as left factor. We have seen instances of this before, but let us do it formally now. The grammar you know let it be n t p s as usual suppose a non terminal x is in is a it occurs in a sentential form that is s derives alpha x beta and this alpha x beta finally, derives w which is a string in the language of g. So, s derives w. So, here I should have put a star here to show that there are more than one productions possible and that is the non terminal must occur in a sentential form and finally, we should be able to derive a string from the sentential form in which the non terminal occurs. If both these happen then you know the non terminal x is useful because it has been instrumental in producing a string in the language. If it is not so then x is useless. So, now let us be more you know let us pin down the conditions which are required to make x useful the first condition is x must derive w and w must be a string. So, that is x derives some terminal string the second condition is s x must occur in some sentential form s derives alpha x beta. So, that is x occurs in some string derivable from s both these conditions must hold at the same time. If we consider them individually then you know actually some useless symbol may slip into our grammar. Let us take an example here is s the grammar is s going to a b r c a b going to b c r a b a going to a c going to a b r b d going to d. So, let us see if what these conditions yield. So, before that if you look at the productions it is very clear you know intuitively that this non terminal b is useless why. See what happens is this non terminal b again has two productions b going to b c and b going to a b, but there are no productions which yield a terminal string for b. So, in other words if we apply the production s going to a b we can keep on applying b going to b c or b going to a b, but we will never be able to finish it off by using a terminal production because it has done. So, that is the reason why b is a useless symbol and it so happens that once b is useless the productions containing b will also be useless and they have to be eliminated. So, s going to a b must be eliminated b going to b c must be eliminated b going to a b of course must be eliminated then c going to a b must also be eliminated. So, what we really do is we systematically apply the two conditions here we collect the non terminals which produce terminal strings to begin with. So, a going to a and c going to b these are the two productions which have only terminal symbols on the right hand side. So, they indeed produce terminal strings. Now, we take those non terminals which have only the non terminals corresponding to these productions on the right hand side. It so happens that s going to c a is such a production and of course d going to d is another production which has been included because it produces only terminal strings on the it has only terminal strings on the right hand side. So, s going to c a has c and a which are already included here. So, the first condition now is satisfied right every non terminal produces some terminal symbol. So, those are the only productions which we have included, but now what about the non terminal d even though it produces a terminal string on its own does it occur in a sentential form which is derived from s definitely not because s to c a is the only production which we have considered which contains non terminals on the right hand side and there is no d in it. So, d going to d is also useless and the symbol d happens to be useless. So, finally, applying the condition to above we get s going to c a a going to a and c going to b this is the grammar which contains only useful symbols. So, now let us formalize the two algorithms and see how they can be stated. So, the first condition x derives w. So, g be the grammar and g prime is the new grammar. So, we iterate we start with n old equal to phi and n new is all those non terminals x which have only terminal strings on the right hand side. So, those are included in n new while n old not equal to n new keep iterating. So, we accumulate n new in n old or rather retain n old and n new becomes n old union all those symbols x such that x to alpha is a production and alpha contains only those symbols which have already been included in n old and of course, otherwise it is a terminal symbol. So, this gives us a few more symbols hopefully and that will be our n new. We go back now n new is bigger therefore, it is not equal to n old. So, again expand n new by including more and more symbols until we cannot grow it anymore and once we come out we say n prime is n new t prime is the old t itself s prime is p prime is all the symbols p you know all the productions p which contain only the symbols in n prime union t prime in the right hand side part of the production. So, this is exactly what I explained just now you know in this example. So, we included a to a c to b d to d and s to c a in this first step. The second algorithm checks whether the non terminal x is present in any sentential form that is whether it can be derived from s. So, to do that we start with the start symbol and then try to include as many symbols as possible in the set n prime consider the productions of the symbols which are present in n prime. So, a going to alpha 1 to alpha 2 alpha 3 alpha 4 etcetera any of these you know with a in n prime. So, to begin with it is always s going to something all the productions of s. Now, consider the you know non terminals of alpha 1 alpha 2 etcetera add them to n prime consider the terminal symbols of all this and add them to t prime. Here I must stress that the grammar g that we consider here is the one in which we have applied the production rather the algorithm which is presented here that is we have removed some of the useless symbols already. So, this is repeated until there is no change in n prime and t prime and then we accumulate in p prime the productions all of the symbols are in n prime union t prime. So, this is what we did here you know once we did a start from s we include c and a and those productions and those are the only ones which will be included in the set of systems. So, let us stop here and in the next lecture we will consider recursive descent parsing. Thank you.