+ All Categories
Home > Documents > BOTTOM UP PARSING

BOTTOM UP PARSING

Date post: 19-Mar-2016
Category:
Upload: olaf
View: 63 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
25
BOTTOM UP PARSING Lecture 16
Transcript
Page 1: BOTTOM UP PARSING

BOTTOM UP PARSING

Lecture 16

Page 2: BOTTOM UP PARSING

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.

Page 3: BOTTOM UP PARSING

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

Page 4: BOTTOM UP PARSING

Suppose we have a grammar

S aABe A Abc | b B d

Page 5: BOTTOM UP PARSING

And the input string is abbcdeThen an instance of bottom-up

parsing can be given asabbcde aAbcde aAde S aABe

Page 6: BOTTOM UP PARSING

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

Page 7: BOTTOM UP PARSING

RIGHT DERVATION

Expanding the rightmost non-terminal in each step of derivation.

For the example, it can be shown

S aABe aAde aAbcde abbcde

Page 8: BOTTOM UP PARSING

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

Page 9: BOTTOM UP PARSING

Consider the Grammar

E E + E E * E id | |

Page 10: BOTTOM UP PARSING

Right sentential form

Handle Production Rule

id1 + id2 * id3 id1 E idE + id2 * id3 id2 E idE + E * id3 id3 E idE + E * E E * E E E * EE + E E + E E E + EE

Page 11: BOTTOM UP PARSING

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.

Page 12: BOTTOM UP PARSING

This process can be made algorithmic using a stack implementation

Decision 1 : Shift the next symbol onto the top of the stack

Page 13: BOTTOM UP PARSING

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.

Page 14: BOTTOM UP PARSING

String id1 + id2 * id3

STACK INPUT ACTION1. $ id1 + id2 * id3$ Shift 2. $id3 + id2 * id3 $ Reduce(E id) 3. $E + id2 * id3 $ Shift 4. $E + id2 * id3 $ Shift 5. $E + id2 * id3 $ Reduce (E id) 6. $E + E * id3 $ Shift 7. $E + E * id3 $ Shift 8. $E + E * id3 $ Reduce (E id) 9. $E + E * E $ Reduce (E E * E) 10.$E + E $ Reduce (E E + E) 11.$E $ Accept

Page 15: BOTTOM UP PARSING

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

Page 16: BOTTOM UP PARSING

OPERATOR PRECENDENCE PARSER

Page 17: BOTTOM UP PARSING

OPERATOR GRAMMAR

• No epsilon• No two non-terminals side by side

Page 18: BOTTOM UP PARSING

a > b means a has higher precedence than b

Also a > b need not imply b < a

.

. .

Page 19: BOTTOM UP PARSING

Precedence Table

+ * id $

+ > < < >

* > > < >

id > > > >

$ < < < <

.

.

.

.

.

. .

.

.

.

. . .

. .

.

Page 20: BOTTOM UP PARSING

Consider the input string id + id * id

The string with the precedence relations inserted from the above table is

$ <. id .> + <. Id .> * <.id .> $

Page 21: BOTTOM UP PARSING

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

Page 22: BOTTOM UP PARSING

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

Page 23: BOTTOM UP PARSING

Algorithm

1. If the front of input has $ and top of stack both have $S, it’s done

Else2.Compare front of input b with .> if b!=‘.>’ then push b Scan the next input symbol

Page 24: BOTTOM UP PARSING

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

Page 25: BOTTOM UP PARSING

Stack Input Action

1 $ <.id1.>+<.id2.>*<.id3.>$ Shift

2 $<.id1 .>+ <.id2.>*<.id3.>$ Reduce (E id)

3 $<.E +<.id2.>*<.id3.>$ Shift

4 $<.E+<.id2 .>*<.id3.>$ Reduce (E id)

5 $<.E+<.E *<.id3.>$ Shift

6 $<.E+<.E*<.id3 .>$ Reduce (E id)

7 $<.E+<.E*E .>$ Reduce (E E*E)

8 $<.E+E .>$ Reduce (E E+E)

9 $E $ Accept

Prepared by

Rishabh Singh 04CS1015


Recommended