1 / 25

BOTTOM UP PARSING

BOTTOM UP PARSING. Lecture 16. In Bottom-up parsing, we start with the string and try to apply the production rules in reverse, in order to finish up with the start symbol S of the grammar. This corresponds to starting at the leaves of the parse tree, and working back to the root.

olaf
Télécharger la présentation

BOTTOM UP PARSING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BOTTOM UP PARSING Lecture 16

  2. In Bottom-up parsing, we start with the string and try to apply the production rules in reverse, in order to finish up with the start symbol S of the grammar.

  3. This corresponds to starting at the leaves of the parse tree, and working back to the root. Bottom-up parsing is also known as shift-reduce parsing

  4. Suppose we have a grammar S aABe A Abc | b B d

  5. And the input string is abbcde Then an instance of bottom-up parsing can be given as abbcde aAbcde aAde S aABe

  6. Thus this process of bottom-up parsing is like tracing out the rightmost derivations in reverse.

  7. RIGHT DERVATION Expanding the rightmost non-terminal in each step of derivation. For the example, it can be shown S aABe aAde aAbcde abbcde

  8. HANDLE A handle is a substring that matches the right hand side of a production and replacing RHS by LHS must be a step in the reverse rightmost derivation that ultimately leads to the start symbol S. In the example b, Abc, d, aABe are all handles

  9. Consider the Grammar E  E + E E * E id | |

  10. Here it is apparent that the string appearing to the right of a handle contains only terminal symbols Since here the grammar is ambiguous the choices for the handles can be different depending upon right dervations used.

  11. This process can be made algorithmic using a stack implementation Decision 1 : Shift the next symbol onto the top of the stack

  12. Decision 2 : Reduce the symbol at the top of the stack. Here the parser knows that the right end of the handle is at the top of the stack. It must then locate the left end of the handle within the stack and replace it with the non-terminal.

  13. String id1 + id2 * id3

  14. Note in 6., E + E could have been reduced to E instead of shifting ( a shift-reduce conflict), but we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

  15. OPERATOR PRECENDENCE PARSER

  16. OPERATOR GRAMMAR • No epsilon • No two non-terminals side by side

  17. . a > b means a has higher precedence than b Also a > b need not imply b < a . .

  18. Precedence Table . . . . . . . . . . . . . . . .

  19. Consider the input string id + id * id The string with the precedence relations inserted from the above table is $ <. id .> + <. Id .> * <.id .> $

  20. The handles can be found as • If <. Push in stack • If .> Pop till a <. is found and reduce

  21. $ <. id .> + <. id .> * <. id.>$ $ <. E.+ <. id .> * <. id.>$ $ <. E + <. id .> * <. id.>$ $ <. E + <. id .> * <. id.>$ $ <. E + <. E * <. id.>$ $ <. E + <. E * E .>$ $ <. E + E .>$ $ <. E .> $

  22. Algorithm • If the front of input has $ and top of stack both have $S, it’s done Else 2.Compare front of input b with .> if b!=‘.>’ then push b Scan the next input symbol

  23. 3. If b==‘.>’ Then pop till <. and store it in a string S pop <. also reduce the popped string If (top of stack) <. (Front of input) then push <. S If (top of stack) .> (Front of input) then push S and goto 3

  24. Prepared by Rishabh Singh 04CS1015

More Related