 Hello everyone, I am Mrs. Sunita Role working as an assistant professor in Computer Science and Engineering Department of Vulture Institute of Technology, SolarPur. Topic covered here is writing a grammar topic of syntax analysis. In the previous video, I considered the transformation that are useful for rewriting the grammar so that they become suitable for top-down passing such as writing a grammar for given non-deterministic finite automata, eliminating an ambiguity from given grammar and identifying the non-context-free languages. In this video, I am going to cover the topic, eliminate left recursion from given grammar and left factor the grammar for given example. Learning outcome At the end of the session, students will be able to eliminate left recursion from a given grammar and left factor the grammar for the given example. Elimination of left recursion A grammar is left recursive if it has a non-terminal A such that there is a derivation A derives A alpha in one or more steps for some string alpha. In left recursive grammar, the leftmost symbol on the right side of the production is the same as the non-terminal on the left side of the production. Top-down passing method cannot handle the left recursive grammar, so a transformation that eliminate left recursion is needed. Immediate left recursion A left recursive production can be eliminated by rewriting the offending production. Consider a non-terminal A with two productions, A derives A alpha or the beta where beta does not start with A. Here we replace the A production by A derives beta A dash and A dash derives alpha A dash or the epsilon which is an equivalent grammar. No matter how many A productions there are, we can eliminate the immediate left recursion from them by the following technique. First we group the A productions as A derives A alpha 1 or A alpha 2 and so on up to A alpha m or beta 1 or beta 2 and so on up to beta n where beta 1, beta 2 up to beta n do not starts with A. Then we replace the A productions by A derives beta A 1 dash or beta 2 A dash and so on up to beta n A dash and A dash derives alpha 1 A dash or alpha 2 A dash and so on up to alpha m A dash or epsilon which is an equivalent grammar. Immediate left recursion example, so consider this example E derives E plus T or T, T derives T star F or F, F derives I D or in parenthesis E. Eliminating the immediate left recursion to the production for E and then for T we obtain the following grammar, E derives T E dash, E dash derives plus T E dash or epsilon, T derives F T dash, T dash derives star F T dash or epsilon and F derives I D or in the parenthesis E. A grammar cannot be immediately left recursive but it still can be left recursive. By just eliminating the immediate left recursion we may not get a grammar which is not a left recursive. Consider the grammar S derives A A or B, A derives S C or D. This grammar is not immediately left recursive but it is still left recursive. Consider the example S derives A E derives S C A or A derives S C derives A E C causes to a left recursion. So we have to eliminate all the left recursions from our grammar. Eliminating the left recursion algorithm input, grammar G with no cycles of the epsilon production output and equivalent grammar with no left recursion. So the method apply the following algorithm given below in the flow chart to the grammar G. The resulting non-left recursive grammar may have epsilon production. First step, arrange non-terminals in some order that is even A 2 up to A n for I is equal to 1 to n, do. Second for loop for J is equal to 1 to I minus 1, do. Replace each production of the form A i derives A j gamma by the production A i derives alpha 1 gamma or alpha 2 gamma and so on up to alpha k gamma where A j derives alpha 1 or alpha 2 and so on up to alpha k. Next end of the first for loop. Eliminate the immediate left recursion among the A i production and then end of the second for loop. Example S derives A A or B, A derives A C or S D or F. Now first step that is order of the non-terminal that is S and the A for S we do not enter the inner loop as there is no immediate left recursion in the S. For A replace A derives S D with A derives A A D or B D. So A derives A C or A A D or B D and after eliminating the immediate left recursion in A A derives B D A dash or F A dash and A dash derives C A dash or A D A dash or the epsilon. An equivalent grammar which is not left recursive is given below. Now we consider what is left recursive grammar and how to eliminate the left recursion from the given grammar. Now post this video and reflect on this question for a minute or two minute and write your response. Once you return the answer to this question then you can restart playing this video. The question is eliminate left recursion from the following grammar. I hope all of you have completed this activity. So the question was eliminate left recursion from the following grammar production S derives S A B or S C D or the E. So after eliminating the left recursion from the grammar we get S derives E S dash and S dash derives A B S dash or C D S dash or the epsilon. Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive passing. The basic idea is that when it is not clear which of the two alternative production to use to expand a non-terminal A we may be able to rewrite the A production to defer the decision until we have seen enough of the input to make the right choice. Consider the example that a statement derives if expression then statement else statement or if expression then statement. When we see if we cannot immediately tell which production rule to choose to rewrite the statement in the derivation. In general if A derives alpha beta 1 or the alpha beta 2 are the two productions and the input begins with a non-empty string derived from A we do not know whether to expand A to beta 1 or the beta 2. However we may defer the decision by expanding A to alpha A dash. Then after seeing the input derived from the A we expand A dash to beta 1 or to beta 2 that is the left factor the original production becomes A derives alpha A dash and A dash derives beta 1 or the beta 2. For each non-terminal A with the two or more production rule with the common non-empty prefix A derives alpha beta 1 or the alpha beta 2 and so on up to alpha beta n or the gamma 1 gamma 2 and so on up to gamma m convert it into A derives alpha A dash or gamma 1 or gamma 2 and so on up to gamma m and A dash derives beta 1 or beta 2 and so on up to beta n. Consider the example the following grammar abstract the dangling S problem statement derives if expression then statement else statement or if expression then statement. This grammar is written as S derives I E T S or I E T S E S or A and E derives P. Here I T and E stands for if then and else E and S for expression and statement. So left factor this grammar left factor this so the grammar becomes S derives I E T S S dash or A S dash derives ES or epsilon and E derives B. This is the second example of left factoring the grammar and same is explained here. This is the reference. Thank you.