compiler development cmpsc 401
play

Compiler Development (CMPSC 401) Syntax Analysis Bottom-Up Parsing - PowerPoint PPT Presentation

Compiler Development (CMPSC 401) Syntax Analysis Bottom-Up Parsing Janyl Jumadinova March 5, 2019 Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 1 / 41 Bottom Up Parsing Idea : Apply productions in reverse to convert the


  1. Compiler Development (CMPSC 401) Syntax Analysis Bottom-Up Parsing Janyl Jumadinova March 5, 2019 Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 1 / 41

  2. Bottom Up Parsing Idea : Apply productions in reverse to convert the user’s program to the start symbol. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 2 / 41

  3. Bottom Up Parsing Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 3 / 41

  4. Bottom Up Parsing Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 4 / 41

  5. Sentential Form If S → ∗ α , the string α is called a sentential form of the grammar. In the derivation S → β 1 → β 2 → ... → β n , each of the β i are sentential forms. A sentential form in a rightmost derivation is called a right-sentential form (similarly for leftmost and left-sentential). Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 5 / 41

  6. Bottom-Up Parsing The handle of the right-sentential form is a substring corresponding to the right-hand side of the production that produced it from the previous step in the rightmost derivation. Handle can also be represented as the production and the position of the last symbol being replaced. A left-to-right, bottom-up parse works by iteratively searching for a handle, then reducing the handle. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 6 / 41

  7. Finding Handles Where do we look for handles? How do we search for possible handles? - Once we know where to search, how do we identify candidate handles? How do we recognize handles? - Once we have found a candidate handle, how do we check that it really is the handle? Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 7 / 41

  8. Shift/Reduce Algorithm The bottom-up parsers we will consider are called shift/reduce parsers. Idea: Split the input into two parts: - Left substring is our work area; all handles must be here. - Right substring is input we have not yet processed; consists purely of terminals. At each point, decide whether to: - Move a terminal across the split ( shift ) - Reduce a handle ( reduce ) Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 8 / 41

  9. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 9 / 41

  10. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 10 / 41

  11. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 11 / 41

  12. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 12 / 41

  13. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 13 / 41

  14. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 14 / 41

  15. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 15 / 41

  16. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 16 / 41

  17. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 17 / 41

  18. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 18 / 41

  19. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 19 / 41

  20. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 20 / 41

  21. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 21 / 41

  22. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 22 / 41

  23. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 23 / 41

  24. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 24 / 41

  25. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 25 / 41

  26. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 26 / 41

  27. Shift/Reduce Algorithm Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 27 / 41

  28. Observations Since reductions are always at the right side of the left area, we never need to shift from the left to the right. No need to “uncover” something to do a reduction. Consequently, shift/reduce parsing means Shift: Move a terminal from the right to the left area. Reduce: Replace some number of symbols at the right side of the left area. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 28 / 41

  29. Observations All activity in a shift/reduce parser is at the far right end of the left area. Idea: Represent the left area as a stack. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 29 / 41

  30. Observations All activity in a shift/reduce parser is at the far right end of the left area. Idea: Represent the left area as a stack. Shift: Push the next terminal onto the stack. Reduce: Pop some number of symbols from the stack, then push the appropriate nonterminal. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 29 / 41

  31. Finding Handles Where do we look for handles? Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 30 / 41

  32. Finding Handles Where do we look for handles? - At the top of the stack . How do we search for possible handles? - Once we know where to search, how do we identify candidate handles? How do we recognize handles? - Once we have found a candidate handle, how do we check that it really is the handle? Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 30 / 41

  33. Searching for Handles When using a shift/reduce parser, we must decide whether to shift or reduce at each point. We only want to reduce when we know we have a handle. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 31 / 41

  34. Finding Handles Where do we look for handles? - At the top of the stack . How do we search for possible handles? Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 32 / 41

  35. Finding Handles Where do we look for handles? - At the top of the stack . How do we search for possible handles? - Build a handle-finding automaton. How do we recognize handles? - Once we have found a candidate handle, how do we check that it really is the handle? Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 32 / 41

  36. Handle Recognition Our automaton will tell us all places where a handle might be. However, if the automaton says that there might be a handle at a given point, we need a way to confirm this. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 33 / 41

  37. Handle Recognition Our automaton will tell us all places where a handle might be. However, if the automaton says that there might be a handle at a given point, we need a way to confirm this. We will thus use predictive bottom-up parsing: Have a deterministic procedure for guessing where handles are. There are many predictive algorithms, each of which recognize different grammars. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 33 / 41

  38. LR(1) Bottom-up predictive parsing with: L:Left-to-right scan R:Rightmost derivation (1): One token lookahead Tries to intelligently find handles by using a lookahead token at each step. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 34 / 41

  39. A Deterministic Automaton Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 35 / 41

  40. LR(1) Guess which series of productions we are reversing. Use this information to maintain information about what lookahead to expect. When deciding whether to shift or reduce, use lookahead to disambiguate. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 36 / 41

  41. LR(1) How do we know what lookahead to expect at each state? Observation: - There are only finitely many productions we can be in at any point. - There are only finitely many positions we can be in each production. - There are only finitely many lookahead sets at each point. Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 37 / 41

  42. LR(1) How do we know what lookahead to expect at each state? Observation: - There are only finitely many productions we can be in at any point. - There are only finitely many positions we can be in each production. - There are only finitely many lookahead sets at each point. - Construct an automaton to track lookaheads! Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 37 / 41

  43. A Deterministic LR(1) Automata Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 38 / 41

  44. Representing LR(1) Automata LR(1) parsers are usually represented via two tables: an action table and a goto table. The action table maps each state to an action: - shift, which shifts the next terminal, and - reduce A → ω , which performs reduction Janyl Jumadinova Compiler Development (CMPSC 401) March 5, 2019 39 / 41

Recommend


More recommend