1 / 76

Bottom-Up Syntax Analysis

Bottom-Up Syntax Analysis. Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/wcc11-12.html Textbook:Modern Compiler Design Chapter 2.2.5 (modified). Pushdown automata Deterministic Report an error as soon as the input is not a prefix of a valid program

wanda-todd
Download Presentation

Bottom-Up Syntax Analysis

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 Syntax Analysis Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/wcc11-12.html Textbook:Modern Compiler Design Chapter 2.2.5 (modified)

  2. Pushdown automata Deterministic Report an error as soon as the input is not a prefix of a valid program Not usable for all context free grammars context free grammar tokens parser Efficient Parsers cup “Ambiguity errors” parse tree

  3. Top-Down (Predictive Parsing) LL Construct parse tree in a top-down matter Find the leftmost derivation For every non-terminal and token predict the next production Bottom-Up LR Construct parse tree in a bottom-up manner Find the rightmost derivation in a reverse order For every potential right hand side and token decide when a production is found Kinds of Parsers

  4. Input A context free grammar A stream of tokens Output A syntax tree or error Method Construct parse tree in a bottom-up manner Find the rightmost derivation in (reversed order) For every potential right hand side and token decide when a production is found Report an error as soon as the input is not a prefix of valid program Bottom-Up Syntax Analysis

  5. Pushdown automata Bottom-up parsing (informal) Non-deterministic bottom-up parsing Deterministic bottom-up parsing Interesting non LR grammars Plan

  6. Pushdown Automaton input u t w $ V control parser-table $ stack

  7. stack tree input $ i + i $ stack tree input + i $ i$ i Informal Example(1) S  E$E  T | E + TT i|(E) shift

  8. stack tree input T$ T + i $ i i Informal Example(2) S  E$E  T | E+TT i|( E) stack tree input i$ + i $ reduceT i

  9. T stack tree input E E$ + i $ T i i Informal Example(3) S  E$E  T | E+TT i | ( E) stack tree input T$ + i $ reduce E  T

  10. E stack tree input +E$ E i $ T T i i + Informal Example(4) S  E$E  T| E+TT i|(E) stack tree input E$ + i $ shift

  11. E stack tree input E i+E$ $ T T + i i i + Informal Example(5) S  E$E  T| E+TT i|(E) stack tree input +E$ i $ shift

  12. stack tree input E T+E$ $ T T T i i + + i i Informal Example(6) S  E$E  T| E+TT i|(E) stack tree input E i+E$ $ reduce T i

  13. stack tree input E $ E$ E T T T i i i + + Informal Example(7) S  E$E  T| E+TT i|(E) stack tree input E $ T T+E$ reduce E E+T i

  14. stack tree input E $E $ E T T T i i i + + Informal Example(8) S  E$E  T| E+TT i|(E) stack tree input E $ E$ E T shift i

  15. T i + Informal Example(9) S  E$E  T| E+TT i|(E) stack tree input E $ $E $ E T i reduce S  E$

  16. Informal Example reduce S  E$ reduce E  E+T reduce T i reduce E  T reduce T  i

  17. The Problem • Deciding between shift and reduce

  18. stack tree input E $ E$ E T T T i i i + + Informal Example(7) S  E$ E  T | E+T T i| (E) stack tree input E $ T T+E$ reduce E E + T i

  19. E E T T E i i i + + Informal Example(7’) S  E$ E  T | E+T T i|( E) stack tree input E $ T T+E$ reduce E  T stack tree input $ E+E$ 

  20. still need to be matched already matched regularexpression input input T· expecting to match  already matched  Bottom-UP LR(0) Items

  21. S  E$ E  T E  E+ T T i T (E)

  22. stack input 6: E E+T 1: S E$ i + i $ Formal Example(1) S  E$ E  T | E+TT i| (E) stack input 1: S E$ i + i $ -move 6

  23. stack stack input input 4: E T 6: E E+T 1: S E$ 6: E E+T 1: S E$ i + i $ i + i $ Formal Example(2) S  E$ E  T | E+T T i|(E) -move 4

  24. stack stack input input 10: T  i 4: E T 6: E E+T 1: S E$ 4: ET 6: E E+T 1: S E$ i + i $ i + i $ Formal Example(3) S  E$ E  T | E+TT i |(E) -move 10

  25. stack input 11: T i 10: T  i 4: E T 6: E E+T 1: S E$ + i $ Formal Example(4) S  E$ E  T | E+T T i|(E) stack input 10: T  i 4: E T 6: E E+T 1: S E$ i + i $ shift 11

  26. stack input 5: E  T 4: E T 6: E E+T 1: S E$ + i $ Formal Example(5) S  E$ E T | E+ TT i|(E) stack input 11: T i 10: T  i 4: E T 6: E E+T 1: S E$ + i $ reduce T i

  27. stack stack input input 7: E  E+T 6: E E+T 1: S E$ 5: E  T 4: E T 6: E E+T 1: S E$ + i $ + i $ Formal Example(6) S  E$ E  T | E+ T T i|(E) reduce E  T

  28. stack stack input input 8: E  E+T 7: E  E+T 6: E E+T 1: S E$ 7: E  E+T 6: E E+T 1: S E$ i $ + i $ Formal Example(7) S  E$ E  T| E+TT i| (E) shift 8

  29. stack input 10: T i 8: E  E+T 7: E  E+T 6: E E+T 1: S E$ i $ stack input 8: E  E+T 7: E  E+T 6: E E+T 1: S E$ i $ Formal Example(8) S  E$ E  T | E+T T i| (E) -move 10

  30. stack input 11: T i  10: T i 8: E  E+T 7: E  E+T 6: E E+T 1: S E$ stack input $ 10: T i 8: E  E+T 7: E  E+T 6: E E+T 1: S E$ i $ Formal Example(9) S  E$ E  T | E+T T i| (E) shift 11

  31. stack input 9: E  E+T  8: E  E+T 7: E  E+T 6: E E+T 1: S E$ $ Formal Example(10) S  E$ E  T| E+ T T i|( E) stack input 11: T i  10: T i 8: E  E+T 7: E  E+T 6: E E+T 1: S E$ $ reduce T i

  32. stack input 2: S E $ 1: S E$ $ Formal Example(11) S  E$ E  T | E+T T i|(E) stack input 9: E  E+T  8: E  E+T 7: E  E+T 6: E E+T 1: S E$ $ reduce E  E+T

  33. stack input 3: S E $  2: S E $ 1: S E$ Formal Example(12) S  E$ E  T |E+TT i| (E) stack input 2: S E $ 1: S E$ $ shift 3

  34. Formal Example(13) S  E$ E  T | E+T T i|(E) stack input 3: S E $  2: S E $ 1: S E$ reduce S  E$

  35. But how can this be done efficiently? Deterministic Pushdown Automaton

  36. 3 2 1 Handles • Identify the leftmost node (nonterminal) that has not been constructed but all whose children have been constructed input t1 t2 t4 t5 t6 t7 t8

  37. Identifying Handles • Create a deteteministic finite state automaton over grammar symbols • Sets of LR(0) items • Accepting states identify handles • Use automaton to build parser tables • reduce For items A on every token • shift For itemsA  t on token t • When conflicts occur the grammar is not LR(0) • When no conflicts occur use a DPDA which pushes states on the stack

  38. A Trivial Example • S  A B $ • A  a • B  a

  39. S  E$ E  T E  E+ T T i T (E)

  40. 1 1: S E$ 4: E T 6: E E +T 10: T i 12: T  (E) 2: S E $ 7: E E +T 0 T 6 E 5: E T $ i 2 5 3: S E $  11: T i + ( i 7 13: T (E) 4: E T 6: E E +T 10: T i 12: T  (E) 3 8: E E +T 10: T i 12: T  (E) ( i ( E T 8 + 14: T (E ) 7: E E +T 9: E E +T  ) 4 15: T (E)  9

  41. Example Control Table

  42. input stack shift 5 0($) i + i $

  43. stack input 5 (i) 0 ($) reduceT i + i $

  44. stack input 6 (T) 0 ($) + i $ reduce E  T

  45. stack input shift 3 1(E) 0 ($) + i $

  46. stack input shift 5 3 (+) 1(E) 0 ($) i $

  47. stack input 5 (i) 3 (+) 1(E) 0($) reduce T i $

  48. input stack reduce E  E+T 4 (T) 3 (+) 1(E) 0($) $

  49. stack input 1 (E) 0 ($) $ shift 2

  50. stack input 2 ($) 1 (E) 0 ($) accept

More Related