Presentation is loading. Please wait.

Presentation is loading. Please wait.

BOTTOM UP PARSING Lecture 16.

Similar presentations


Presentation on theme: "BOTTOM UP PARSING Lecture 16."— 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 Right sentential form Handle Production Rule
id1 + id2 * id3 id1 E  id E + id2 * id3 id2 E + E * id3 id3 E + E * E E * E E  E * E E + E E  E + E E

11 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.

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

13 Decision 2 : Reduce the symbol at the top of the stack
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.

14 String id1 + id2 * id3 STACK INPUT ACTION 1. $ id1 + id2 * id3$ Shift
Reduce(E  id) 3. $E 4. $E + id2 * id3 $ 5. $E + id2 * id3 $ Reduce (E  id) 6. $E + E 7. $E + E * id3 $ 8. $E + E * id3 $ 9. $E + E * E Reduce (E  E * E) 10.$E + E Reduce (E  E + E) 11.$E Accept

15 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

16 OPERATOR PRECENDENCE PARSER

17 OPERATOR GRAMMAR No epsilon No two non-terminals side by side

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

19 Precedence Table + * id $ > < . . . . . . . . . . . . . . . .

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

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

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

23 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

24 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

25 <.id1.>+<.id2.>*<.id3.>$ Shift
Stack Input Action 1 $ <.id1.>+<.id2.>*<.id3.>$ Shift 2 $<.id1 .>+ <.id2.>*<.id3.>$ Reduce (E  id) 3 $<.E +<.id2.>*<.id3.>$ 4 $<.E+<.id2 .>*<.id3.>$ 5 $<.E+<.E *<.id3.>$ 6 $<.E+<.E*<.id3 .>$ 7 $<.E+<.E*E Reduce (E  E*E) 8 $<.E+E Reduce (E  E+E) 9 $E Accept Prepared by Rishabh Singh 04CS1015


Download ppt "BOTTOM UP PARSING Lecture 16."

Similar presentations


Ads by Google