1 / 14

Bottom-Up Parsing

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

egunn
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 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 =>*  = 1A2 =>+ 12 E  E + T Let  = 1A2 be  E + T * FE + 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 =>*  = 1A2 => 12 E  E + T Let  = 1A2 be  E + T * FE + 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! E T F E + T * id T F Id id • Three internal nodes = three phrases in the sentential form E + T * id Phrases: Id T * id E + T * 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 (S0X1S1X2S2…XmSm, aiai+1…an$) S0 = Start state Xm = symbol m Sm = state m ai = input symbol i

  13. Sebesta’s LR Diagram

  14. Sebesta’s LR Table

More Related