 a sort of an error in the past when people go through anti-corresponding to this situation. And we had a choice between the views and shift. And we found this path does not allow us to progress. But if we take this path, then we were able to progress. Now, obviously, which means that when I got this conflict, shift to this conflict, if suppose my past table did not have this view section, then I would have been fine. I could have proceeded. That means this entry somehow is an erroneous entity. And this should be removed. And then we said, why we are saying this is an erroneous entity? Because we are saying that there is no sentential form in this language which can start with R. So we are saying that if I now look at the sentential form of this grammar, then we'll find that it is giving me a sentential form which is R.equal ID and such a sentential form cannot occur. So what is wrong? How did I come to a situation where my past table had this entity? Ideally, it should not have had. So where is the problem? If you understand the problem, then you'll find that subsequent discussion will become very easy for everyone. Any ideas that normal could be? And just say something, think a lot. So let me give you a hint. And the hint is that we were saying there is something called a state symbol. So top of stack always contains a state symbol. And what is the state symbol? What is the information captured by the state symbol? In a bottom of parser, this we discussed right in the beginning. Then we started doing bottom of parser. What is the information which is captured by the state symbol? So state symbol is trying to capture the configuration of the stack. If it captures the configuration of the stack, then I'm fine. And because in this case, state symbol should have in search, it should have remembered that there is no sentential form that can come with starting with R and therefore this reduction is wrong. So whatever my earlier state was, it should not have come to this state. Now let me try to explain that in terms of the ground. Suppose my configuration is like this. I have this alpha, I have beta and I have some symbol. And I have a production which says A goes to beta. And then I have a symbol here, let's say A, some term it will say. And now when do I do this reduction? I'm saying that there's a production, beta is a handle. So when will I do the reduction of beta to A? When this symbol, look ahead symbol, is in follow of A. That was the rule we gave for construction of the parser table. Now suppose I do this reduction and this becomes my configuration of the stack. And I say now this A is in follow of A and therefore I could continue parsing. And the problem now is that if I look at the state now at the top, this does not remember that below A is something which is alpha. Like in this case, this state did not remember that below R is a symbol which is the beginning of the string. So if now look at a situation that A is in follow of A, but if I have a situation now where I say this symbol is in follow of A, but this symbol is not in follow of alpha A. Alpha A is my configuration after this reduction. Then I have a problem. And exactly this is what is happening here. That although this R is in, this equal sign is in follow of R, but is not in follow of when R is at the beginning of first string. That is where I got stuck. Which was giving me that wrong central form. So therefore if I can now remember the whole context, that means the state symbol I have is not popular. State symbol cannot remember the whole stack configuration. If I can make my state symbol more powerful, that it can remember the left context, then I would not have got into this problem. So now I am saying that I do not want to do this reduction only when I look at a symbol which is in follow of A, but I want to do this reduction only when this symbol is in follow of alpha A. Then I will be fine. Does it make sense? So this is how we make SLR part two technique more powerful. Now to make it more powerful I need to change certain definitions. So I had certain transformations in the grammar. I first started with the grammar, then I augmented it. Then I gave you a definition of closure. And then I gave you a definition of go to. And then I said using this information, we had all sets of LR0 items. And then we had a part three. So first thing I want to do now is, I want to change or introduce a new definition. And rather than saying I have this set of LR0 items, let me say I have now set of LR1 items. So I am introducing now more information so that I can remember this whole of the left context. And what is an LR1 item? LR1 item is nothing but an LR0 item with a lookahead symbol. So earlier what was the LR0 item? LR0 item was a symbol of a production where I put dot anywhere on the right-hand side of the production. But here I am saying let me also associate with this symbol. And this symbol is eventually is going to be used for reduction. So now I am going to carry extra information in every state. So typically what can happen is that if I have an LR0 item like this, I am saying that let me have some lookahead symbol. And what is the significance of this symbol? This symbol is basically saying that at some point of time when I keep on doing goto's on this, when I say that from this item I will go to an item on goto of y which is going to give me an LR0 item of a going to x, y, dot, z. And then I further do a goto on z which gives me a going to x, y, z, dot. And when I am ready for reduction, I will be able to reduce only on these symbols. I will remember the whole of left context. Now one thing that must immediately come to your mind is that if I am talking of this lookahead symbol, earlier I was doing a reduction on symbol which is following. So this symbol must still be following. But now it is a subset. So the way we are looking at it is that this symbol is in follow of R. But now I want this symbol to be in follow of R, beginning R. Or when I say that this symbol should be in follow of A, I want this symbol also to be in follow of L5. So this set is always going to be subset of what is follow of A. It cannot be anything else. It cannot be a superset. So this eventually will give me now extra information. So there is the first thing I introduce. Second thing I introduce is that when I look at my definition of closure set, earlier my closure set, so just to put you back on the track so that I can still remember. How did I confuse my closure sets? I said that if I have an LR0 item which is of this form, it says A goes to L5 dot B beta. And I have a production which is of this form. I have a production which says B going to gamma. Then how did I confuse closure? I said all these items are going to be in the closure and B going to dot gamma also is going to be in the closure. That is what was my definition of closure. Now I am changing this definition. Now I am saying that since I do not have these items anymore, I have LR1 items if not LR0 items and I have to define now closure on set of LR1 items. So LR1 item will be something like this. So now I say that if I have an LR1 item of the form which says A goes to L5 dot B beta and B going to gamma is a production, then what will be the new item I will add in this? I will say that I am going to add now B going to dot gamma and what will be the look ahead symbol? First of partially correct, not fully correct because what may happen is beta is null. So it will now be first of beta A. So this is now giving me extra information. Saying that when I do this, I can do this reduction only when I have a symbol which is in first of beta A. First of beta A is going to be a subset of follow of B. This is the only extra thing I am going to do and this now captures the complete stack configuration. So my new state is so powerful that rather than just saying that I will do a reduction on this symbol, this is saying I will do a reduction only if the symbol is in follow of whole of this stack. Make sense? Can there be? Could be. They could be null. First of beta A can contain multiple symbols. But this is going to be a subset of B, a subset of follow of B. So first conceptually is it clear what is happening? So we will understand why SLR parser has a limitation and how we are putting more information in this parser by enriching the information in LR0 item and calling it LR1 and putting some look ahead information and then saying now I can go to this new parsing method where my state is so powerful that not only it remembers what is below it but it remembers what is on the stack. So my earlier state was just able to remember what is below it but this new state will be able to remember what is on the whole of stack because now it will know that not only I know that this symbol has to be in follow of R or in follow of whatever was the reduction follow of L that is when this reduction happened but it should also be in follow of when R occurs at the beginning and if this configuration is not acceptable configuration then earlier reduction would not have taken place. Yes? Everyone is comfortable with this? Is conceptually clear? What is going on? So let's take again an example and try to construct now a new parser for this and this new parser I am going to call is canonical LR parser and in fact this is the most powerful LR parsing that is available to us today. So in contextually drama LR is a subject but what we are going to discuss canonical LR parser and let me also say with a look ahead of one so if I just want to do a look ahead of one this is the most powerful parsing method if a language cannot be parsed by this then we are going to say method is powerful enough is the language which is not in this case. In earlier case we said language was an ambiguous method was not powerful enough and we went for a new method. In this case we will have to say now we know that method knows everything method is the most powerful one and therefore we are not able to pass it. So let's try to construct now So let's try to construct now canonical LR parser and for which I am going to so let's keep these definitions for the time being let's clean this part of the board and let me pick up an example. So let's say I have this grammar and I will explain why we are taking this grammar and this grammar has something very interesting let's take this grammar now this grammar says what are the languages which are generated by or what are the strings which are generated by this grammar the language can be generated from this so look at the first rule look at the second rule it says A goes to KA and that's the right parsing rule or A goes to B so what is the string I can generate from that and if I say S goes to KA then I can generate A star B A star B or these are I am saying A star B A star B and I want in these kind of languages that in earlier method of parsing you would have found that the follow set of this and follow set of this are different now in earlier method what is the follow set of this A dollar and what is the follow set of this it's either A or B so in earlier method you would have noticed that this reduction would have been taking place on dollar also so if I had written a string like A star B it would have done this reduction and would have said I cannot proceed any further so the reason I am repeating this symbol is that the follow set although this symbol is same but because of the context in which it's occurring in this case follow set of this A is dollar so this whole thing can happen only in this dollar and this first part of the A can be reduced from whatever is the input only on A and B and not on dollar so that is going to separate out my things so let's start constructing now parser for this and again we have to go through the same thing we have to construct all sets of L R 1 items not L R 0 but L R 1 items in that way a pass table now I don't have to compute my follow sets because this information look ahead information will be carried as part of the L R 1 items so computation of follow set is gone so what happens now is that if I say I want to now construct this now my look ahead symbol is going to be what will be the look ahead symbol for this for this it will always be dollar so I can do this reduction only in dollar and let me call this as I 0 and now when I say that I want to take a closure a closure this is my initial item so closure is going to give me S going to A A there is only one production of A and what will be the look ahead symbol here look at this that's why I am keeping this definition here so if you just try to do a pattern match here I am coming from this item to this item this is where I am taking closure so look at this that if I say I have a non-terminal symbol immediately on the right of dot then I am looking at first of beta A now what is beta in this case what is beta in this case beta is not actually B in this what is beta no beta is null so I am looking at first of epsilon dollar that gives me dollar so my look ahead will be this now I have to take closure of this A right now I will say A goes to A A or A goes to dot B now what will be the look ahead here you know so first let's write that statement what will be the look ahead here how will I compute look ahead for this first of first of A dollar now what is first of A dollar A of B right so these are my look ahead statements okay what about this what will be look ahead symbol here same as what same as the earlier one because this closure is again coming from this alarm on item right so again I will have A now you can see something interesting just by looking at this first set of items that eventually somewhere I will do a transition from this state where I say go to a state where I will have a look ahead as B and that state is going to contain A going to B dot and now you can see that when I do a reduction I will do only on these symbols and not on dollar because now I say I don't want to do a reduction on follow of A but I want to do a reduction only when my look ahead symbol is this so I will be able to reduce this B to A only on these look ahead I will be able to reduce this do this reduction only on A B and not on dollar in some other state you will find that when I look at the second day and that set of states which is carried there I will be able to do reduction from dollars so this computation of first item clear are so this is my initial state always and from this now I am computing extra information okay so next state I say is go to of I0S and let me call that as I1 so first tell me what will it be so it will obviously do a transition on S so it will give me S dot first what will be the look ahead symbol first off have I changed the definition of my go to have I given you new definition of go to so why are you saying computation of first or anything that was only for closure my go to remains as it is that means this symbol has to be just carried I do a shift here and whatever is the look ahead just carry that don't do any computations remember that I only gave you a new definition closure I did not give you a definition of go to so my go to will still contain the same set of subset of LR0 item and whatever is the look ahead that gets copied then next thing I do is so I cannot do anything more on this now I say that I go to a state which will be corresponding go to of I0 on A let me call this state as I2 which is coming from here so this gives me S going to A dot A I just shift this dot this is the only LR1 item on which I can do a foundation on it and what will be the look ahead here I don't have to do any computation whatever is the look ahead here I just carry that so I carry that and this becomes now now if I take closure closure is going to give me A going to A and A going to dot B right what is the look ahead here how did I get dollar here first of epsilon dollar very good because I am taking closure because of this non-terminal therefore my beta if you just match this pattern beta is epsilon here so I am taking first of epsilon dollar and therefore my look ahead here is going to be dollar now you can see a difference between this and this here when I was having this state which said A goes to dot B my look ahead was AB and here when I am saying A goes to dot B my look ahead is dollar that means when I do a reduction from B to A in some cases I will do a reduction only on this look ahead and in some cases I will do a reduction only in this look ahead because now my parser is able to remember that whether this is the first part of A star B coming out of this or this is the second part of A star B coming out of this A perhaps separated these two states these two states if you had constructed an SLR parser then this was not being carried and whenever I had this situation which says A goes to B dot I said follow of A contains A B or dollar and I would have reduced that meaning to make sense so let's move forward and then we say I want to now compute go to of I0 and I have already done it for A so I now want to do it for small A and let me call this as I3 okay and this is going to give me A going to A dot A which is coming out of this and what will you look ahead here A or B I don't have to compute anything I just copy this which comes here now I take closure because of this so if I now take closure which gives me A going to dot A and A going to dot B ought to be the look ahead symbol here because now these closure items are coming because of this A so therefore I am taking first off epsilon and A or B so that gives me same thing as A or B so let's move forward from this now I can erase this part and now say let me go to on I0 B okay so I am doing now go to on this and let me call this as a new state called I4 and this gives me A going to B dot and what is the look ahead here here I am just copying it not computing anything okay and I cannot add anything go to this so this is where I stop all transitions of I0 okay now I come to I1 I cannot do any condition on I1 here then I come to I2 first thing I notice is I can do a transition on A okay so if I do a transition on A so let me call it as go to off I2 A and this gives me as going to A A dot okay and what is the look ahead now dot because I just need to carry it along I don't have to compute anything let me call this as I5 and then so I cannot add anything more to this then I do this transition which says go to off I2 on A right and this gives me what does it give me A going to A dot A have I already computed it somewhere isn't it same as I3 look ahead is different so it's not same as I3 very good okay so you are able to cache that so this says now I will have A going to A dot A so I am coming from there right and this is giving me a look ahead of dollar let me call this as I6 okay and this is not same as this state because look ahead are different okay and now I will take closure here so this closure is saying A goes to dot A and A going to dot B and what will be the look ahead symbols here dollar again right so I am looking at now first off so this gives me dollar very okay so from I6 if I move now from I2 and I now do a transition on B okay so this is going to give me go to I2 on B this gives me A going to B dot and A going to B dot although I have I4 okay but because my look ahead is different therefore I will call it as a new state and I call them as state number 7 okay and this is not add anything go to it and so I have exhausted everything in I2 okay now I come to I3 and in I3 I can do a transition on A okay so if I do now go to of I3 on A what does that give me A going to AA dot and what is the look ahead here look ahead will be same as this okay which is A or B okay now this state does not exist anywhere so let me call this state as I and if I now look at this particular state which says A goes to I3 on A what does that give me gives me A going to AA dot with the look ahead of A B and that is I3 itself right I have only computed that that is not a new state so this I taking it back to I2 okay if I now do a transition on B in this state okay so if I say go to of I3 B that gives me B dot with the look ahead of A B and that is I4 if I look at I4 I cannot do any transition if I look at I5 no more transitions possible if I look at I6 I can do a transition on A okay so now if I compute go to of I6 on upper case A that gives me A going to AA dot and what is the look ahead I just carry forward okay and this is this does not take this anywhere so let me call this state as I9 okay if I take closure doesn't that anything go to it okay and now if I so I am here if I do now a transition on this if I say now so let me keep this definition intact and if I now say go to of I6 on A okay that gives me I6 itself and if I do a transition on B if I say go to of I6 B what is that A going to B dot on dollar I7 so I have exhausted I6 I cannot do any transition on I7 cannot do a transition on A cannot do a transition on 9 lambda okay so this is how I construct all my set of so I am no longer computing but I am constructing and how do I construct my pass table now so my pass table rules still remain the same so what are the rules now that I can have only an accept state so accept state is going to come from this basically whenever you do this reduction okay and my look at symbol is dollar then I will accept okay but if I look at action part and go to part they are going to remain the same at least the shift part in action is the same as earlier one that if I have a state Ii and this state is containing an LR0 item of this form let's say I have alpha dot AB okay and then if my new state J it corresponds to A going to alpha A dot beta I have done a transition on this so what is it that I will do I will say if I am in this state then on A I am going to say shift to J that is the only thing I will do and what will happen in the go to part happen for a non-terminal in the go to part okay so only new thing that will happen is in reduction now okay so my earlier rule for reduction was in case of reduce action we said that if I have this particular LR0 item in some state so suppose my state I contains this then I was saying in this particular state if you find a symbol which is in follow of A then reduce this by this rule okay now I will change that and I will say if I am in this state okay and my look at symbol is A then I of A is going to be reduced by A going to alpha so instead of saying if this symbol is in follow okay I am carrying this information as part of my LR1 item and therefore I just do this reduction conceptually what was the limitation of SLR parser how we remove that limitation by carrying extra item in LR1 item and then controlling the reduction and then how do I compute all my sets of LR1 items is this clear both now of algorithm as well as the concept anyone who has a doubt on this if you don't ask questions then I ask questions they can get quiz immediately I can give you a grammar and ask you to construct all sets of LR1 items right away if you don't ask the questions you want to do that so I don't even check so it is now become a mechanical set okay okay okay without worrying about I just take any grammar and add one rule without saying do I have multiple productions of this right so I can put a case there saying that if my start symbol has only one production then I don't augment it I am making it simpler saying I am not even testing it condition just augment it okay see you want to take the same grammar which you had in the previous class in one to construct one parser for that right away as a quiz no not as a quiz assignments you won't do unless I grade it we tested that so I ask you to do a simple assignment which you did not do you did not bother to okay so it is an assignment to be submitted okay and submitted to model so assignment is to be done okay the same grammar construct canonical nr1 parser for that okay you have the grammar already with you okay so let's then move forward and what I do now is I will sort of try to capture this to take you to the remaining material quickly so this is what we started discussing that if I had a shift action if I had a review section that was taking me to an error state and if I did a shift that was taking me to the correct state and the limitation we saw was that this state symbol was not able to remember the whole of left context and therefore what we did was that there is no sentential form which can start with r equal where r is in the beginning and the review section was generating a sentential form of this form and therefore the review section is incorrect and in SLR parser and I say that I am doing reduction by the symbol the symbol may be in follow of a but may not be in follow of l5 what was being on the stack okay and therefore we said this symbol is invalid and problem is that the symbol state symbol does not capture the left context does not capture the full stack configuration just captures the symbol on the top and that was the limitation we wanted to remove and because of which we said in canonical lr parser we are going to carry extra information that whenever reduction by this rule happens we will try to rule out all the wrong reductions and wrong reductions are happening because of the symbol which is in follow of a so now I am carrying extra information which I call as look ahead symbol and I am going to do reduction so if I am in some state if I have some lr1 item which is of the form a goes to alpha.beta then I will carry extra information like a which I am going to call as lr1 item and reduction so this item lr1 item and the look ahead symbol in lr1 item is only going to effect my reductions and nothing else so now I am saying that if I have some item like this which says a goes to alpha.a then I call for reduction only if next input is a okay so this is now making sure that next input a although is in follow of a but I am not looking at all symbols which are in follow of a but I am looking at something which is the whole of state okay so that is what I captured here okay so it is a symbol which is in subset of follow of a okay and not just the full site which is follow of and closure is the only definition I changed so I said if I have this particular lr1 item which says a goes to alpha.beta and b going to gamma is a production then I am going to add another lr1 item to the closure it says b going to dot gamma with the look ahead of b where b is in the first of beta okay this is the only change here okay I did not change anything in go to so we looked at oh so I have slightly different gamma but basically same similar notation where I have a repeat and okay and then we constructed all sets of lr1 items for this and this is how we ended up constructing all these lr1 okay and if I now construct pass table all my rules remain the same except this rule where we said that I am going to call for reduction when my look ahead symbol is a my shift rule and go to rule which did not change okay they are same as the SLR pass and this is the kind of pass table I get okay so now let us look at something interesting okay and that interesting thing is that if I am in this state okay I am doing a rule by reduction by rule number 3 and this is my rule okay which says that B goes to A okay but if you see here if I am in this state then I am doing a reduction by rule number 3 when my look ahead symbol is dollar okay so it is not that every time I will be able to reduce by this rule which says A goes to B I have separated out these states in SLR pass and you would have noticed that this entry would have contained now say even on dollar to a reduction and reason was simple because dollar also is in follow okay but here I was able to separate out okay so here I am using a different symbol C so here I am able to separate out whether it is the first C or the second C and the follow of the first C contains only C or D and the follow of second C contains only the dollar so I am only doing reductions in this case on CD and in this case on dollar and in this case I am doing reductions by rule number 2 and my rule number 2 is this one depending upon whether it is the first part of the second part I am doing reductions on 2 different states okay so this is what we have just discussed now so let us look at some of the properties okay so this language which is specified by the grammar I had okay so in this case I am using slightly different symbols so let us upon this was slightly confusing because lower case and upper case C can create problem I should change the example correct okay so this is the string which is specified by the language and when reading I am reading this input which is CCD, CCD and so on okay of C star D, parser is going to shift all the C into stack and then goes into state 4 after reading the D okay and only here you will see that I will be able to do this reduction only if it is followed by C or D and not quite all okay those are 2 different cases and if dollars followed in the first D then we say that this input is correct, incorrect and parser goes into analysis in earlier case it would have just done a reduction okay so on an error this canonical LR parser and this is a very interesting property and makes it the most powerful method is that whenever there is a wrong input okay it will just refuse to do any over shift and reduce it will catch it right there it will not permit you to do any shift or any reduce it just catches it and therefore as far as error reporting and error recovery is concerned this is the most powerful part in that we know of as far as loss of LR parser LR from ground surface okay not permit any wrong input okay only problem is that canonical LR parser can be very very large now let me throw some numbers at you so suppose I had a language like C okay how many states parser will have SLR parser of C roughly somehow that is to it does not matter typically you will find that you will have about 7 to 800 states in a C parser SLR parser but suppose I go canonical parser the number of states may shoot to something at 24 there is an order of magnitude of tables 5% 10% there is an order of magnitude of difference because in the worst case what can happen is that I am looking at worst case scenario that if I look at follow set of each of the symbols for each of the symbols I can have a new entity so if I have n symbols in the follow set all these n symbols can give rise to one line in canonical LR right now that makes it something which is which is very large okay so computation is going to be computation of this particular parser that means creation of the parse table itself is going to be possible because I have to compute so many states so what do we do because we are not just worried about correctness we are also worried about efficiency issues okay I do not want to have such large parsing skills because maybe having by saying that I will do all these things okay I am going for an overgift right can I think of something better something which is a compromise between SLR which is the weakest parsing method found versus canonical LR which is the most powerful parsing method we have can I think of something better and what does that mean we can reduce the number of states which are done will that take me to SLR so proposal is that suppose we take all the states and merge them and on the symbols which they reduce so let me give a slightly more clear and technical notation to what you are saying that if I consider all my LR1 items so let me go back to the set of LR1 items okay to look at some of the LR1 items look at 4 okay 4 has saying C going to D dot on the look ahead of CD and 7 has C going to D dot on dollar okay now when I say this is an LR1 item I say this is my kernel of LR1 item which is basically LR0 item and it has a look ahead similarly if I look at say 6 and 3 kernel is the same look ahead is different if I look at 4 and 7 kernel is the same look ahead is different if I look at 8 and 9 kernel is the same and look ahead is different can I take all these states which has the same kernel and different look ahead and merge them is that what you are saying now what happens if I merge them we will get we will get closer to SLR we will not get SLR if I get closer to SLR is that more powerful than SLR but obviously less powerful than canonical LR perhaps you don't know okay so there is one proposal okay seems like good do-even proposal okay because all I am trying to do are reduce the number of states here so saying rather than having 10 states 0 to 9 can I reduce number of states so how many people want to merge these states and what are the implications see more important people what are the implications so first question if I merge all these states how big my parts are really is it going to be smaller than canonical LR and larger than SLR it will be somewhere in between or it will be of something other some other size so if I say I have SLR has some number of entries and what is being proposed is do some merge so let me call it merged parser for the time being okay number of states okay will it be what 10% lower than this 20% lower than this 10% higher than this 20% higher what will estimates or is it somewhere in the middle average see SLR in SLR what we do we do take these looker heads and merge looker heads and merge and we have kind of accumulated them in one okay everyone I agree with that that is an excellent observation in fact that the number of states I will get after doing this merger because terminals are the same that means I am now merging all the states with the same kernel in different looker heads and therefore number of states so I think that is very powerful method now because now I am saying that I can take canonical SLR parser and I want to reduce number of states and I reduce it to SLR issue remains by merging is it possible that I reach SLR parser table because if I say size is SLR power is also SLR then what I have gained nothing we have power is of canonical LR power will not reduce power will not reduce okay so it will power be the same as canonical LR so what we will do is we will break here today and tomorrow we will close this discussion and we will close parsing and we will want to move to type checking only few things are left and we will see what are the implications of doing this merger in between or I will get back to SLR and say I do not gain anything so let's break here today and tomorrow we will see in our discussion