Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.

Similar presentations


Presentation on theme: "Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The."— Presentation transcript:

1 Bottom-Up Parsing David Woolbright

2 The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The most common bottom-up parsing algorithms are in the LR family L – Read the input left to right R – Trace out a rightmost parse tree

3 The Parsing Problem Given a right sentential form, , determine what substring of  is the right-hand side of the rule in the grammar that must be reduced to produce the previous sentential form in the right derivation The correct RHS is called the handle

4 A Small example E  E + T | T T  T * F | F F  ( E ) | id A Rightmost Derivation: E  E + T  E + T * F  E + T * id  E + F * id  E + id * id  T + id * id  F + id * id  id + id * id

5 Definition –Def:  is the handle of the right sentential form  =  w if and only if S =>*rm  Aw =>rm  w E  E + T Let  =  w be  E + T * F E + F * id  E + T * id What is  ?  E + F * id What is w ? What is  ? What is A?

6 Definition –Def:  is a phrase of the right sentential form  if and only if S =>*  =  1 A  2 =>+  1  2 E  E + T Let  =  1 A  2 be  E + T * F E + T  E + T * id Let A be T. What is  1 ?  2 ? What can  be?

7 Definition –Def:  is a simple phrase of the right sentential form  if and only if S =>*  =  1 A  2 =>  1  2 E  E + T Let  =  1 A  2 be  E + T * F E + T  E + T * id Let A be T. What is  1 ?  2 ? What can  be? The handle of any rightmost sentential form is its leftmost simple phrase

8 Think Parse Tree! Three internal nodes = three phrases in the sentential form E + T * id Phrases: Id T * id E + T * id E T F E + T * id T F Id id

9 LR Parsers –The handle of a right sentential form is its leftmost simple phrase –Given a parse tree, it is now easy to find the handle –Parsing can be thought of as handle pruning

10 LR Parsers Use Shift-Reduce Shift-Reduce Algorithms –Reduce: replace the handle on the top of the parse stack with its corresponding LHS –Shift: move the next token to the top of the parse stack

11 LR Parsers Advantages of LR parsers: –Work for most grammars that describe programming languages. –Detect syntax errors as soon as it is possible. –The LR class of grammars is a superset of the class of grammars parsable by LL parsers.

12 The LR Algorithm An LR configuration stores the state of an LR parser (S 0 X 1 S 1 X 2 S 2 …X m S m, a i a i +1…a n $) S 0 = Start state X m = symbol m S m = state m a i = input symbol i

13 Sebesta’s LR Diagram

14 Sebesta’s LR Table


Download ppt "Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The."

Similar presentations


Ads by Google