 So, far we have discussed what is a contestry grammar and the languages generated by contestry grammars that is contestry languages and certain examples to get the familiarity with the contestry grammars. Let me give you one more example, if you consider the set of strings of the form a power m, b power n say c power m plus n such that you consider this language. You can give a contestry grammar to generate this language by taking the clue of the sum of previous examples. For example, if you recollect the grammar for this language set of 0 power n 1 power n such that n greater than equal to 0, the grammar that we introduced to generate this is 0 s 1 and s can be epsilon. These two rules generates the language 0 power n 1 power n n greater than equal to 0. Now, if you look at a typical string in this that is a power n b power m c power m plus n what we have to generate the number of a s followed by the number of b s is equal to the following number of c s. So, now what you can do is if we first generate so many a s corresponding to those many c s for example, you have generated here k s generating k c s that you can do using this type of rules. And since n can be greater than or equal to 0 if n is 0 in which case it is the language the string is precisely of the form a power m c power m that is the strings in this language of that form or what you can do you can continue to produce. Now, b s in between these two certain number of b s and corresponding to which you can produce again this kind of using this kind of rules certain number of c s, but here we have to keep one thing in mind that after producing a s once we start producing b s we should not again go back to produce a s here. For example, if I just simply introduce s goes to a s c and s goes to b s c this will not do the business the reason why after producing using first rule for example, or second rule here you I do not have any restriction that once I have s here at a given point of time I may produce b c or after producing b c once again using this s I may produce a c in between. So, that way there may be a mixture of a s and b s, but we have a restriction that a s followed by b s then c s. So, we have to choose one more non terminal symbol which restricts after producing s first you want to produce as many as you produce using the first symbol here you may call for example, here some s and then you have to give a connection to another non terminal symbol once we go to the non terminal symbol new non terminal symbol there you can introduce this kind of thing producing b s and c s, but non terminal symbol will not give you back to come to produce producing s. So, now, let us use this information to define the grammar as just mentioned you can take this rule s goes to a s c this produces as many s as as many c's on the both the sides and then you can terminate because we are asked m n greater than or equal to 0. Now, I give you a non terminal symbol for example, s goes to a and this non terminal symbol a will produce b s and once we go to the non terminal symbol we do not have a connection back to coming to s. Now, this a goes to say b a c and now a you can terminate again now the order is very clear that this s is connecting to a for example, if this m is 0 we need not produce any s then the string is of the form b power n c power n. So, in which case you can directly choose this rule come to a and produce this b's and c's and terminate the derivation or in case if you have to produce only a's and c's where n is 0 then you will not come to this rule to produce b's and thus these rules will precisely give you the a language l. So, now you can prove that take it as an exercise if I call the grammar g which is having these production rules prove that language generated by this grammar is as desired. Similarly, you can take one more that say a power m now in between b power m plus n c power n such that m n greater than equal to 0. You can try a context to grammar which generates this language to show that this language is context free. Now, I will introduce a concept called a derivation tree which is useful to understand more about this context free languages. Because the context free languages are generated by context free grammars and this context free grammars using a context free grammar any string in the language you are generating through a derivation and to understand more properties about the derivations this derivation trees are very much useful. In this context let me consider the following example. Consider a context free grammar which are having precisely these production rules s goes to s star s or s plus s or bracket s or little a or little b. What is the language generated by this grammar? If you do a little homework you can quickly understand that arithmetic expressions with the operations star and plus over symbols a b can be generated using these production rules. That means the grammar this context free grammar generates arithmetic expressions over a b with the operations plus and addition and multiplication. Now, what do we do? Let us observe the following three derivations for the string a plus b star a. Using these rules first derivation a plus b star a this gives s gives s star s then this s I am terminating with a because s goes to a is a symbol. Now, you consider for this s s plus s and then you terminate this s by a and then this s you terminate it by b. So, using these production rules I have one derivation which produces a plus b star a similarly, I have the derivation two here and derivation three here. There are three different derivations you can clearly see that although first rule here is same, but here the second in the second step it is differing and in the first step itself this is differing with any of these two. So, that way these three derivations are different, but there are some common features that you can understand between the derivations one and two. What are the common features that you can understand here? What is in a particular step if you find a non-terminal symbol at a particular place the terminal string produced by that particular non-terminal symbol is same as the sub string of this produced by that non-terminal symbol. For example, in this two derivations one and two the second s if you consider here in the second step this is producing a the term the sub string a of a plus b star a and there is a contrast to feature between one and two with three that is this second s in the step one the non-terminal symbol s it is producing b star a these two are producing sub string a whereas, this second non-terminal symbol is producing b star a now, so this is the contrast to feature that I have explained. Now, to talk about this kind of the equivalences between derivations the common features that we have mentioned are contrast to feature to distinguish this kind of feature we make use of the notion called derivation tree. Now, let me define formally the notion of derivation tree let g be a c f g as given here with v is an union sigma. Now, you consider a derivation a gives alpha infinitely many steps for alpha in v star. Now, a derivation tree or a parse tree of the derivation given here is defined iteratively as follows for the tree we take this non-terminal symbol as the root and there are finitely many rules that you have applied to produce alpha here. So, if you consider the rule b goes to x 1 x 2 x k if this is applied in the derivation what do we do then new nodes with the labels x 1 x 2 x k are created and made children to the node b from left to right in that order. That means, for example, a a b we have applied and once again I have applied this a a b b and applying the rule if you consider this derivation here the rules I am using in this rules if I consider this derivation what is that I we have introduced what is we have defined a is a root of the tree this a is root of the tree and now we have applied the production rule a gives a a b this production rule. So, we will create these three as labels of the children of a like this and create the connections like this and then once again I have applied the rule a a b. So, this non term now for this we have these three children and then the rule a goes to epsilon is introduced now in the same order we are making this children from left to right because if you apply the rule b goes to x 1 x 2 x k in the derivation at a particular step for b we make x 1 x 2 x k as children and we keep them in the same order as just explained with s a here the derivation tree for that is here. And if you consider this derivation s goes to a b the rule is applied and then in the next step we have applied the rule a goes to a a b capital a b and then once again we have applied the rule this and if you consider this derivation the corresponding you can verify that this is the derivation tree for this derivation. Now, using the concept derivation tree or parse tree we will talk about equivalence between derivations. Now, we say two derivations are said to be equivalent if their derivation trees are same precisely again I recollect these derivations. Now, if you draw the derivation trees for these three derivations you can see that for first two derivations for first two derivations these two trees are exactly same. And for the third derivation the tree is different from 1 and 2 and thus you can understand that these two derivations that derivation number 1 and 2 are equivalent whereas, the third one is not equivalent to any of the first one and 2. Now, this concept related to equivalence you know we can capture through the notion left most derivations. So, that means essentially here what we have understood in the previous case to talk about the equivalence two derivations are equivalent if they have same derivation tree and that means the concept of derivation tree is uniquely assigned between equivalent derivations. Now, can we have any derivation a canonical representation of a derivation which is unique in some sense for equivalent derivations. In that context we are introducing this notion called left most derivation. So, this left most derivation is defined as as follows if you consider derivation a gives x x is a terminal string infinitely many steps through a contrast free grammar. Now, we say this is left most derivation a derivation a goes a gives x we say it is left most derivation if the production rule applied in each step is on the left most non-terminal symbol of the sentential form when you are getting a derivation you have applied rules in each step. Now, what is the condition here for example, this derivation when you look at of course, here in each sentential form you have only one non-terminal symbol you have only one non-terminal symbol. So, thus whatever the production rule that I am applying that is on the left most non-terminal symbol, but if you consider this example if you consider this example in this derivation you have choice there are two non-terminal symbols in this step a and b. So, if to say a derivation is left most derivation the production rule that you apply at a particular step you have to apply in the non-terminal symbol in the non-terminal symbol that is occurring first in the sentential form first from left side the left most non-terminal symbol. For example, in this step we have applied on the first non-terminal symbol a. So, this step is satisfying that and if you continue like this now you see there is a non-terminal symbol a here and the non-terminal symbol b here in this step. So, in this step we have not applied production rule on this non-terminal symbol whereas, we have applied production rule on this non-terminal symbol b. And here also you can observe that on b we have applied the production rule to produce this b b c. That means, this step or this step is violating the condition of left most derivation because in the left most derivation as we have mentioned that in the at each step when you are applying a production rule we have to apply the production rule production rule on the left most non-terminal symbol then we say that is a left most derivation. So, in that sense this is not a left most derivation. So, a derivation if we say that is left most derivation that is denoted by a subscript l for example, as I have explained in the previous case also this derivation 2 is a left most derivation because this is s star s we have applied production rule on this s you have got s plus s here. Then this s is terminated and then this s is terminated here then this s is terminated. And derivation 3 is also left most derivation because we have applied production rule on the first s here and then after this this is the non-terminal symbol. So, on this non-terminal symbol we have applied the production rule and now here is a non-terminal symbol on that we have applied. So, this derivation both the derivation 2 and 3 in the previous example both are left most derivation whereas, the derivation 1 which was not left most derivation. And now a similar concept called right most derivations can be introduced that means a derivation a gives x infinitely many steps a terminal string x we say this is right most derivation if each production rule if the production rule applied in each step it is on the right most non-terminal symbol of the sentential form. In which case we say it is right most derivation we see the similarity between left most derivation and right most derivation in case of left most derivation we are applying production rule on left most non-terminal symbol in each step. And in case of right most derivation we are applying production rule on the right most non-terminal symbol in each step you see the similarity between these 2 and because of the similarity between these 2 what are the results that we give for left most derivation we can one can similarly produce for right most derivations also. So, in the following we simply consider only left most derivations and a similar results can be produced as an exercise for right most derivations as well. Here is a notation similar to left most derivations if it is a right most derivation we use the subscript r for the derivation for the for the derivation. Now, let me give you a theorem that every derivation every derivation that produces a terminal string is equivalent to a left most can have left most equivalent left most derivation. What I mean here if you take a derivation a gives x where x is a terminal string then you can produce x through a left most derivation starting from a and conversely of course, you can quickly understand that this if part is straight forward because every left most derivation is anyway derivation therefore, if you can produce a terminal string x from a infinitely many steps through a left most derivation of course, you have produced x infinitely many steps and thus if part is straight forward. Now, let me look at only if part. So, what we do you consider the derivation a gives x infinitely many steps let me call a is alpha naught and that gives alpha 1 in first step and in the second step may be alpha 2 and so on in n steps you are producing x. Let me consider the derivation as this what we do we have to show that there is an equivalent left most derivation there is an equivalent left most derivation for this that means which produces x using in each step on the left most non terminal symbol that we are applying production rule. If what is the derivation that you have consider in which you have this kind of property that means on the left most non terminal symbol only you have applied the production rule in a particular in each step then it is a left most derivation then there is nothing we have to prove otherwise at a particular step we have not applied production rule on left most non terminal symbol and produce alpha i plus 1 that means there is an i alpha i produces alpha i plus 1, but not through the production rule applied on the left most non terminal symbol that is this. Now, what do you consider let k be the least for which this is satisfied that means till k we have alpha i produces alpha i plus 1 through that which is a part of you know applied on left most non terminal symbol in a sentential form alpha i and there after particularly at this place we have the condition failed. Now, what do we do we will show you a mechanism which can make which can give you a derivation for which this k plus 1 this is k plus 1 step because in the first step you are getting alpha 1 in the k plus 1 step you are getting alpha k plus 1. So, in the k plus 1 step also how to make this derivation which is applied on the left most non terminal symbol. So, if you if I can give you a mechanism which can convert the k plus 1 step also as a left most as a part of left most derivation then you can apply induction to produce an equivalent derivation that means a derivation which produces x in n steps. That means what do we give we give a conversion of this derivation an equivalent one to produce this alpha n with this alpha k plus 1 the sorry k plus 1 step you have through left most applied on left most non terminal symbol there after some strings of v star that is alpha dash k plus 1 alpha dash k plus 2 and so on alpha dash n minus 1 alpha n that is what is x. So, what do we have in the information alpha k gives alpha k plus 1, but it is not obtained by applying production rule on the left most non terminal symbol that means alpha k is of the form some terminal string y and a 1 is the first non terminal symbol in the sentential form that is a left most non terminal symbol is a 1 and after that you have some beta 1 that is an element of v star some non terminal symbol a 2 and some beta 2 that is an element of v star. I can see here two non terminal symbols just to mention that in alpha k a 1 is the left most non terminal symbol whereas, you have applied production rules to get alpha k plus 1 on the non terminal symbol a 2. This does not mean a 2 is a second non terminal symbol from left side there may be non terminal symbol in beta 2 as well. Now, why is a terminal string and a 1 is a this thing and a 1 a 2 are non terminal symbols beta 1 beta 2 are elements of v star and you have applied production rule say a 2 goes to gamma 2 to get alpha k plus 1. Now, what we have to understand here in this derivation eventually we are getting a terminal string x alpha n is terminal string. Thus at a particular step in the derivation you have to apply a production rule on a 1 because a 1 is a non terminal symbol to convert it into a terminal string you have to apply somewhere a production rule. So, let me assume that is on the p'th step thus and in the p'th step suppose you are applying the production rule a 1 goes to gamma 1. Now, what do we do? So, in this situation the derivation looks as follows alpha naught that is a gives alpha 1 in the first step and so on till alpha k these are all applied on left motion non terminal symbol and yet k plus 1 step you have this situation and so on you are producing x. This is the form of the derivation that we have with the situation that alpha k is of this form as I have explained and you have applied production rule on a 2 to replace gamma 2 there that is what is alpha k plus 1. Let me call this as psi 1 this beta 1 gamma 2 beta 2 let me call psi 1 and as I had mentioned this is a terminal string y you are not going to apply anything a 1 is a first non terminal symbol and that as I had mentioned that we are applying production rule on a 1 in this step only in the p'th step only. So, that way the production rules in between these steps that you have applied that you have applied on psi 1 to produce say for example psi 2 and then psi 3 and so on at this stage you have got psi if you you know enumerate the indices you can quickly understand that this is psi p minus k minus 1 the p step that you have and now on a 1 you have applied the rule a goes to gamma 1 and the p step to get this alpha p there after whatever you have that is alpha p plus 1 and so on. Now, what do we do I give you a mechanism to give l here that means whatever the whatever the string alpha k plus 1 here that I want to call now alpha k alpha k plus 1 dash that is this rule will be preponed in this step to get l here. Let me now set alpha k plus 1 dash is y gamma 1 beta 1 a 2 beta 2 that means I have applied production rule a 1 goes to gamma 1 in this step k'th step itself k plus 1 step itself and then what do I do this I will set now alpha k plus 2 dash to be I will apply in place of a 2 gamma 2. So, that means this step is postponed here and this step is preponed to this place and there after whatever is there I will adjust that means anyway this is psi 1 alpha dash k plus 3 the next step from psi 1 I am getting psi 2 that you will get here and then if you continue here I have already got gamma 1. So, what I call alpha p minus 1 dash to be y gamma 1 and whatever is here psi p k minus 2 because 1 that is preponed and now alpha p dash is this two are same because we have just preponed within these steps what are the number of steps the number of steps are same. So, that is what is alpha p now alpha p plus 1 dash is I will continue with the same that is alpha p plus 1 and I will continue here after what are the strings that are there that is alpha n dash to be alpha n itself that is x. So, if I set these strings and you can clearly see that alpha k produces alpha k plus 1 dash through a production rule applied on the leftmost non terminal symbol. Now, if I replace this alpha k plus 1 dash in place of this I can give l here that means I have k plus 1 steps which are the production rules applied in each step on the leftmost non terminal symbol. So, I will move this string here to get this derivation and as we understand through this derivation alpha k plus 1 dash produces alpha k plus 2 dash and alpha k plus 2 dash produces alpha k plus 3 dash and so on and there after it continues. So, what do I have is this alpha k plus 1 dash I can move it here and alpha k plus 2 and the other things. So, that I have a derivation in which I have applied leftmost production rules on the leftmost non terminal symbol till k plus 1 steps. So, that means this mechanism by pre poning this step I could produce from till k plus k steps if you have applied leftmost production rules on the leftmost non terminal symbol to k plus 1 steps how to do that I have explained. So, now you can apply induction to extend this to k plus 2 k plus 2 steps and here you one more point you have to understand that the number of steps is not increased or decreased the same number of steps are there. So, thus what will happen till n steps one after another you can produce and this bound is fixed and thus within this limit that means next you will do for k plus 2 the same mechanism and then k plus 3 and so on till n these are the fixed number of steps by applying this means inductively you can produce a derivation that is a left most derivation to produce x from a. Thus we have the theorem that means any derivation which is of the form a gives x infinitely many steps corresponding to which we can give a left most derivation which produces x that is same terminal strength from a and conversely of course. Now, let me give you one more a small result that is 2 equivalent left most derivations are identical. So, that means here what we have to show that equivalent left most derivation means you have same derivation tree and their identical means you have to say that in each step whatever is the production rule they are applying in the first derivation the same thing that you are applying the second derivation. It is easy to understand because these two are considered two derivations left most derivation d 1 and d 2 since they are equivalent you have a common tree let me call that as t. Now, understand that in the common tree tree is same understand the children of each node represents a production rule applied in d 1 and d 2. So, since they are since we have common derivation tree the production rules which you have used in d 1 and d 2 they are essentially same and now the production rule applied in the both derivations are same because of this. Now, you understand the order of application of the production rule is also same the reason is both are left most derivations. And on each in each step you have to apply production rule on the left most non terminal symbol and the production rules that you have applied they are same. And thus you understand that the order of application of production rules in d 1 d 2 are also same and hence they are identical. So, two equivalent left most derivations are identical. Now, I use this result to give this following important theorem what we are looking for every derivation tree whose yield is a terminal string represents a unique left most derivation. So, that means the derivation tree concept when the equivalent between derivations is defined through derivation tree and the canonical form in derivations is a left most derivation as I had mentioned that is expressed through this result. That means the unique left most derivation that you will have for each derivation tree which produces a terminal string of course. The proof for this suppose t is a derivation tree as a derivation a gives x infinitely many steps. Now, we can find an equivalent left most derivation because the theorem one what I have explained now for every derivation you can have an equivalent left most derivation. Now, if you have two equivalent left most derivation they are identical that is what we have observed through theorem 2. So, by theorem 2 we have unique left most derivation represented by this t thus what you have every derivation tree which produces a terminal string represents a unique left most derivation. Now, I will introduce a new concept called ambiguity this is an important application for left most derivation the concept left most derivation. And the concept of ambiguity has very much practical importance particularly in the context of compiler design. Because in the compiler design the grammars that one expects is an unambiguous grammar context free grammar. Let me define the concept ambiguity of a context free grammar a C F G G is said to be ambiguous if G has two different left most derivation for same for some string in L of G otherwise we say the grammar is unambiguous. That means for every string there has to be unique left most derivation there cannot be two left most derivations for a string in the language to say an ambiguous. So, we have expressed this definition through the concept left most derivation as I had mentioned one can equivalently say a grammar is ambiguous if it has two different right most derivation or derivation trees. Because the concept of derivation tree and unique left can be you know captured through left most derivation. So, that way one can see equivalently if a string has two different right most derivations or two different derivation trees then we say the grammar is ambiguous this is an equivalent formulation. Now, let me give you an example let us recall the grammar that produces arithmetic expressions. Now, as you understand as we have already mentioned that a plus b star a this string we have two different left most derivations and thus the grammar given here the grammar given here is ambiguous. If for one string if you have two different left most derivations then we say that it is ambiguous. Now, the grammar given for a power n b power n n greater than equal to 0 for this the production rules that are given to you are this. Now, since there is only one non terminal symbol on right hand side of this production rule what is the derivation that you have here which is of this form after k number of steps may finitely many steps you can produce like this. And then when you are terminating after finitely many steps you may whatever you want a power n b power n for some n. So, in the k step you will have this and finitely many steps and you observe the sentential form in this derivation it is a power k s b power k and you have only one non terminal symbol. And if the derivation is continuing that is a because of this rule one only and the rule two is only used to terminate the derivation. So, the typical sentential form in any derivation of this grammar is of the form a power k s b power k and you have only one non terminal symbol and on which only we will be applying production rule this. Thus this is a left most derivation and this is only derivation possible for a power n b power n a string of this language. And thus here you can understand that this grammar is unambiguous grammar and now one can think about an unambiguous grammar to produce arithmetic expressions. Because here the syntax the grammar is given in this way, but the problem where it is coming this is a plus b star a we have you know two different derivations first producing this s star s or s plus s. So, this operator whatever that we are getting here star n plus we do not have any precedence here you can produce this first or this first and we have interchanged and the because of this both are left most derivations either this rule or this rule we have applied either of this rule and to get two different left most derivations. Now, if you insist on the operators so called operator precedence what is the operation that you have to apply first that is how in programming languages one considers that without when I write this is a plus b star a if you look at the semantics of this the meaning of that if you add a and b then you multiply with this whatever is the resultant value the meaning it will differ from when you take first multiply b and a then you add a that means a plus b star a. So, these two meanings they differ so that way in this syntax if you can take care of that situation that means when the two different one expression if it cannot have two different meanings if it has a same syntax then such a grammar will the ambiguity can be avoided. So, if you can give such a grammar that means operator precedence can be followed, but in the syntax we cannot say that this is to this has to be applied first or this has to be applied next that you can do only by introducing certain parenthesis. So, here you can produce that kind of arithmetic expressions also for example using the same grammar you can produce a plus then in the bracket b star a, but the operator precedence if you want to introduce everywhere you should have the brackets. So, thus one can try to give a contested grammar by choosing this concept operator precedence, but of course here you have to introduce brackets to give an unambiguous grammar to produce arithmetic expressions. So, you can take it as an exercise that construct unambiguous c f g that produces that generates arithmetic expressions over two elements set say a b of course with the binary operations with the operations multiplication and addition one can consider that is with the operations plus and star you can try this as an exercise. Now, you know already regular expressions in the context of regular languages these regular expressions are introduced to you they are similar to arithmetic expressions. You can give a contested grammar that produces regular expressions over some alphabet and operations there are you have union, cleanly star and concatenation. So, give a grammar so another exercise is that generates regular expressions that generates regular expressions over some alphabet may be I consider the same a b and in this context you can look for a c f g which is ambiguous and further you can look for a c f g that is unambiguous grammar c f g that is unambiguous to produce regular expressions try both the versions if possible the versions which is ambiguous unambiguous. In case of arithmetic expressions what you understand that we have an ambiguous grammar, but we can also produce an unambiguous grammar that gives arithmetic expressions, but what is the situation unfortunately there are certain context languages for which you may not be able to have any context grammar that is unambiguous. So, now that the notion inherently ambiguous language is essentially the one for which you cannot have any unambiguous c f g these are called inherently ambiguous language context languages. I give you an example without proof at this point of time that is if you consider this context language a power m b power n c power n d power n for m n greater than equal to 1 union a power m b power n c power n d power m for m n greater than equal to 1 you can quickly understand that this is a context language you can give a context grammar for this. Because we have already discussed languages of this type now you have two components here. So, consider this component and you know how to produce this component because using the philosophy that the production rules given for a power n b power n are in the beginning of this lecture. You can give production rules of context we type to produce this component and similarly this and understand that this is a context language. And what is the grammar that you have produced you can try understanding that it is ambiguous grammar as well because as I had stated here that it is inherently ambiguous this language is inherently ambiguous language, but proving that this is inherently ambiguous we require you know little more details that will be further introducing. So, at that point of time you will see why this is inherently ambiguous, but for the time being you just take this as an example and as an inherently ambiguous language.