 Hello everyone, I am Mrs. Sunita Do working as an assistant professor in Computer Science and Engineering Department of Vajjan Institute of Technology, Swalapur. Topic covered here is top-down passing. Top-down passing can be viewed as an attempt to find a left-most derivation for an input string. Equivalently, it can be viewed as an attempt to construct a pass tree for the input, starting from the root and creating the node of the pass tree in pre-order. In this video, I am going to introduce the basic behind the top-down passing and show how to construct an efficient non-backtracking form of the top-down passer called Predictive Passer. Learning outcome. At the end of this session, students will be able to identify the recursive descent passer and pass the string using the Predictive Passer. In top-down passers, the pass tree is created top-to-bottom, following are the examples of the top-down passers, recursive descent passers, predictive passers and the non-recursive predictive passers. In this video, I am going to cover the recursive descent passers and the predictive passers. Recursive descent passers. Backtracking is needed, that is, if a choice of a production rule does not work, we backtrack to try other alternatives. It tries to find the left-most derivation. It is the general passing technique, but not widely used. Example, S derives CAD, A derives AB or A and the input string is W is equal to CAD. To construct a pass tree for this string top-down, we initially create a tree consisting of a single node label S, an input pointer point to C, the first symbol of W. So we use the first production for S to expand the tree and obtain the tree of figure E. The left-most leaf label C matches the first symbol of W, so we now advance the input pointer to E, the second symbol of W and consider the next leaf label E. We can then expand the E using the first alternative for E to obtain the tree of figure B. Now we have a match for the second input symbol, so we advance the input pointer to D, the third symbol of the input and compare D against the next leaf label B. Since B does not match D, we report failure and go back to C whether there is another alternative for E that we have not tried but that might produce a match. In going back to A, we must toward the input pointer in a local variable. We now try the second alternative for E to obtain the tree of figure C. The leaf E matches the second symbol of W and the leaf D matches the third symbol. Since we have produced a parse tree for W, we halt and announce the successful completion of the parsing. Till now we consider the top-down parser recursive descent parser. Now pause this video and reflect on this question for a minute or two minutes and write your response. Once you return the answer to this question then you can restart playing this video. The question is, construct a recursive descent parser with backtracking for the grammar. I hope all of you have completed this activity. So the question was, construct a recursive descent parser with backtracking for the grammar sderize a s b s b s a s or epsilon and the input string w is equal to a b a b. To construct a parse tree for the string top-down, we initially create a tree consisting of single node label s. An input pointer points to A, the first symbol of W. We then use the first production for S to expand the tree and obtain the tree of figure A. The leftmost leaf label A matches the first symbol of W. So we now advance the input pointer to B, the second symbol of W and consider the next leaf label S. We can then expand S using the first alternative for S that is sderize a s b s to obtain the tree of figure B. In input string, second input symbol is B. So compare B against the next leaf label A. Since A does not match with B, we report the failure and go back to see whether there is another alternative for S that we have not tried but that might produce the match. In going back to S, we must store the input pointer in a local variable. We now try the second alternative for S to obtain the tree of figure C. The leaf B matches the second symbol of B. We repeat the same procedure for the second and third symbol of the input string W and we can produce a pass tree as shown in figure D. So we halt and announce the successful completion of passing. Predictive Passer. In many cases, by carefully writing a grammar, eliminating left recursion from it and left factoring the resulting grammar, we can obtain a grammar that can be passed by a recursive descent passer that needs no backtracking that is a predictive passer. To construct a predictive passer, we must know, given the current input symbol A and the non-terminal A to be expanded, which one of the alternative of the production A derives alpha 1 or alpha 2 and so on up to alpha k is the unique alternative that derives a string beginning with A. That is, the proper alternative must be detectable by looking at only the first symbol it derives. Flow of control construct in most programming language with their distinguishing keywords are usually detectable in this way. For example, if we have the following production statement derives if expression then statement as statement or while expression do statement or begin statement list end, then the keyword if, while and begin tell us which alternative is the only one that could possibly succeed if we are to find a statement. Transition diagram for the predictive passer. In passer, there is one diagram for each non-terminal. The level of edges are token and non-terminals. A transition on a token means we should take that transition if that token is the next input symbol. If a transition on non-terminal A is a call of procedure for A. To construct a transition diagram of a predictive passive from a grammar, first eliminate the left recursion from the grammar and then left factor the grammar. Then for each non-terminal A do the following. Create an initial and final state. For each production A derives x1, x2 up to xn create a path from the initial top the final state with the edges x1, x2 up to xn. Predictive passer behaves as follows. It begins in the start state from the start symbol. If after some action it is in state s with an edge level by a terminal A to the state t and if the next input symbol is A, then the passer moves the input cursor one position right and goes to the state t. If an edge is level by a non-terminal A then passer goes to the start state for A without moving the input cursor. If it ever reaches the final state for A, it immediately goes back to the state t in effect having read A from the input during the time it moved from state s to state t. If there is an edge from s to t level epsilon then from the state s the passer immediately goes to the state without advancing the input. A predictive passer program based on the transition diagram attempts to match the terminal symbol against the input and makes a potential recursive procedure call whenever it has to follow an edge level by a non-terminal. This contains a transition diagram for a given grammar. The only ambiguity concern whether or not to take an epsilon edge. If we interpret the edge out of the initial state for e dash as saying take the transition on plus whenever that is the next input and take the transition on epsilon otherwise and make the analogous assumption for t dash then the ambiguity is removed. And we can write a predictive passing program for the grammar. Transition diagram can be simplified by substituting the diagrams in one another. For example in figure A the call of the e dash on itself has been replaced by a jump to the beginning of the diagram for e dash. Figure B shows an equivalent transition diagram for e dash. We may then substitute the diagram of figure B for the transition on e dash in the transition diagram for e yielding the diagram of figure C. Observe that the first and the third node in the figure C are equivalent and we merge them resulting in figure D. The same technique apply through the diagram for t and the t dash. The complete set of resulting diagram is shown in the following figure. The input string using the predictive passer. Here the input string is id plus id. Step one, first input symbol is id hence the input cursor points to id. So, consider the first transition diagram that is for the non-terminal e. There is a transition from 0 to 3 on the non-terminal t. So, passer goes to the star state for the t without moving the input cursor. In transition diagram for t there is a transition from state 7 to 8 on the non-terminal f. So, the passer goes to the star state for f without moving the input cursor. In the transition diagram for f there is a state 14 with an h leveled by a terminal id to the state 17 and the input symbol is also id. So, the passer moves the input cursor to the next input symbol plus and goes to the final state 17. Now, passer goes back from the state 17 to 8 and from 8 to the final state 13 on input symbol epsilon. From the state 13, passer goes back to the 3. Step two, now input cursor is pointing to plus hence passer goes from the state 3 to 0 on the input symbol plus. Now, the passer moves the input cursor to the next input symbol id. Step three, passer repeat the step one for the input symbol id and finally goes to the final state 6 from the state 3 on epsilon. This is the reference. Thank you.