1 / 125

CS 2130

Revised Version. CS 2130. Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing. Warning: The precedence table given for the Wff grammar is in error. Top-down Parsing 1. Root node  leaves 2. Abstract  concrete 3. Uses grammar left  right 4. Works by "guessing". Bottom-up Parsing

Download Presentation

CS 2130

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. Revised Version CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.

  2. Top-down Parsing 1. Root node  leaves 2. Abstract  concrete 3. Uses grammar left  right 4. Works by "guessing" Bottom-up Parsing 1. Leaves  root node 2. Concrete  abstract 3. Uses grammar right  left 4. Works by "pattern matching" Parsing • Parsing -- Syntax/Semantic Analysis

  3. Introduction • Top down parsing • Scan across the string to be parsed • Attempt to find patterns that match the right hand side of a rule • Reduce them to the left hand side of the rule • If the eventual result is reduction to the start symbol then parse is successful

  4. Imagine... • We are parsing 1 + 2 * 3 • or num + num * num • We need some way to make sure that we don't turn the num + num into<expr> + <term> and reduce it to <expr> • Can num + num be reduced to <expr> ? • Why is it a problem?

  5. Problem... • We cannot reduce <expr> * num • What we need is a way of recognizing that we must reduce first num + num * num

  6. Recall our expression grammar <expr> ::= <expr> + <term> | <term> <term> ::= <term> * <factor> | <factor> <factor> ::= '(' <expr> ')' | num | id • It would suggest that what follows a + must be a term. • It would also suggest that if a num is followed by a * then we will somehow need to find a factor to perform <term> ::= <term> * <factor>

  7. Bottom Up Parsing • Bottom up parsing tries to group tokens into things it can reduce (based on a rule in the grammar) in the correct sequence • This group of symbols is known as a handle. • Handles are indicated using special symbols known as Wirth-Weber operators • These symbols function like parentheses which can be used to indicate precedence 1 + (2 * 3) • We will determine where to put these symbols by examining the grammar and developing additional information to assist us

  8. Wirth-Weber Operators x <• y y has higher precedence than x (We expect y will be involved in a reduction before x) x = y x and y have equal precedence (We expect x and y will be involved in a reduction together) x •> y x has higher precedence than y (We expect y will be involved in a reduction before x)

  9. Bottom Up Parsing • Two Things must be understood: • Given the ability to determine precedence between symbols how can we use this to parse a string? • How do we determine this precedence between symbols/tokens? • We deliberately choose to explain in this order and we'll use a very simple grammar to explain

  10. Recall Well Formed Formulae <wff> ::= p | q | r | s<wff> ::= N <wff><wff> ::= ( C | A | K | E ) <wff> <wff> Suppose we wish to parse CANpqp

  11. Bottom Up Parsing C A N p q p

  12. Bottom Up Parsing <• C A N p q p • We can assume that the string has a leading less than precedence operator

  13. Bottom Up Parsing <• C <• A N p q p • We move from left to right (and in fact in reality we would normally proceed by asking a lexical scanner for the next token • As we get to each token or symbol we get its precedence from a precedence table that we'll present later

  14. Bottom Up Parsing <• C <• A <• N p q p • We continue in this fashion as long as we place the < and = operators

  15. Bottom Up Parsing <• C <• A <• N <• p q p • We continue in this fashion as long as we place the < and = operators

  16. Bottom Up Parsing <• C <• A <• N <• p q p • We continue in this fashion as long as we place <• and = operators • We are postponing the discussion on the precedence table because this part of the algorithm must be clear to be able to understand where the precedence table comes from!

  17. Bottom Up Parsing <• C <• A <• N <• p •> q p • When we place a > operator we have found a handle or something that we should be able to reduce • We examine the rules of the grammer to see if there is a rule to match this handle

  18. Bottom Up Parsing <• C <• A <• N <• p •> q p • We find <wff> ::= p • Note: If no rule is found we have a parse error

  19. Bottom Up Parsing <• C <• A <• N<wff> q p • Note that we have removed the entire handle and replaced it with the appropriate symbol from the grammar. We "backup" to examine the relationship between N and <wff>

  20. Bottom Up Parsing <• C <• A <• N = <wff> q p • We continue

  21. Bottom Up Parsing <• C <• A <• N = <wff> •> q p • We continue, again, until we find a handle

  22. Bottom Up Parsing <• C <• A <wff> q p • We can reduce this using the rule <wff> ::= N <wff>

  23. Bottom Up Parsing <• C <• A = <wff> q p • We continue

  24. Bottom Up Parsing <• C <• A = <wff> <• q p • We continue

  25. Bottom Up Parsing <• C <• A = <wff> <• q •> p • We can reduce this one also

  26. Bottom Up Parsing <• C <• A = <wff> <wff> p • Once again backtracking

  27. Bottom Up Parsing <• C <• A = <wff> = <wff> p • Once again backtracking

  28. Bottom Up Parsing <• C <• A = <wff> = <wff> •> p • Continuing

  29. Bottom Up Parsing <• C <wff> p • Continuing

  30. Bottom Up Parsing <• C = <wff> p • Continuing

  31. Bottom Up Parsing <• C = <wff> <• p • Continuing

  32. Bottom Up Parsing <• C = <wff> <• p •> • A greater than precedence symbol is assumed after the last symbol in the input.

  33. Bottom Up Parsing <• C = <wff> <wff> • Continuing

  34. Bottom Up Parsing <• C = <wff>= <wff> • Continuing

  35. Bottom Up Parsing <• C = <wff> = <wff> > • Again a trailing greater than can be added

  36. Bottom Up Parsing <wff> • Since <wff> is our start symbol • (and we have nothing left over) • Successful Parse!

  37. Bottom Up Parsing • What kind of algorithm? • Stack based • Known as semantic stack or shift/reduce algorithm • We won't code this algorithm but understanding this parsing technique will make some concepts found in yacc clearer

  38. Example Our stream of tokens C A N p q p

  39. Example Stack Our stream of tokens C A N p q p

  40. Example Stack Our stream of tokens C A N p q p • Color Commentary • Welcome to Monday Night Parsing

  41. Example Stack Our stream of tokens A N p q p <• C We will place the Wirth-Weber operator and following token on the stack. Encountering the end of a handle •> will initiate additional processing

  42. Example Stack Our stream of tokens N p q p <• A <• C Working

  43. Example Stack Our stream of tokens p q p <• N <• A <• C Working

  44. Example Stack Our stream of tokens q p <• p <• N <• A <• C Working

  45. Example Stack Our stream of tokens q p <• p <• N <• A <• C Now, between the next token in the stream (q) and the symbol on top of the stack, we find a greater than precedence >• indicating we have the end of a handle. We must now go down the stack and search for the beginning

  46. Example Stack Our stream of tokens q p <• N <• A <• C We can remove the p and looking at the grammar determine it can be reduced to be a <wff>. We then examine the <wff> in relation to the top of the stack

  47. Example Stack Our stream of tokens q p = <wff> <• N <• A <• C We can remove the p and looking at the grammar determine it can be reduced to be a <wff>. We then examine the <wff> in relation to the top of the stack

  48. Example Stack Our stream of tokens q p = <wff> <• N <• A <• C Looking at the <wff> followed bt the q we again find a greater than precedence relationship. We find that we can reduce the N <wff> to a <wff>.

  49. Example Stack Our stream of tokens q p <• A <• C Now have <wff> from previous reduction. Compare it with A

  50. Example Stack Our stream of tokens q p = <wff> <• A <• C Now have <wff> from previous reduction. Compare it with A

More Related