1 / 57

Bottom-Up Syntax Analysis

Bottom-Up Syntax Analysis. Mooly Sagiv html://www.math.tau.ac.il/~msagiv/courses/wcc03.html Textbook:Modern Compiler Design Chapter 2.2.5. 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.

leann
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 html://www.math.tau.ac.il/~msagiv/courses/wcc03.html Textbook:Modern Compiler Design Chapter 2.2.5

  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 parser tokens Efficient Parsers bison “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) Bottom-up parsing (given a parser table) Constructing the parser table Interesting non LR grammars Plan

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

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

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

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

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

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

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

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

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

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

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

  17. Handles • Identify the leftmost node (nonterminal) that has not been constructed but all whose children have been constructed • A  

  18. Identifying Handles • Create a finite state automaton over grammar symbols • Accepting states identify handles • Report if the grammar is inadequate • Push automaton states onto parser stack • Use the automaton states to decide • Shift/Reduce

  19. Example Finite State Automaton

  20. Example Control Table

  21. Example Control Table (2)

  22. stack input stack shift 5 s0($) i + i $

  23. stack input s5 (i) s0 ($) reduce T  i + i $

  24. stack input s6 (T) s0 ($) + i $ reduce E  T

  25. stack input shift 3 s1(E) s0 ($) + i $

  26. stack input shift 5 s3 (+) s1(E) s0 ($) i $

  27. input s5 (i) s3 (+) s1(E) s0($) reduce T  i $

  28. input stack reduce E  E + T s4 (T) s3 (+) s1(E) s0($) $

  29. stack input s1 (E) s0 ($) $ shift 2

  30. stack input s2 ($) s1 (E) s0 ($) reduce Z  E $

  31. input stack shift 7 s0($) ((i) $

  32. stack input shift 7 s7(() s0($) (i) $

  33. stack s7 (() s7(() s0($) input shift 5 i) $

  34. stack s5 (i) s7 (() s7(() s0($) input reduce T  i ) $

  35. stack s6 (T) s7 (() s7(() s0($) input reduce E T ) $

  36. stack s8 (E) s7 (() s7(() s0($) input shift 9 ) $

  37. s9 ()) s8 (E) s7 (() s7(() s0($) stack input reduce T  ( E ) $

  38. stack s6 (T) s7(() s0($) input reduce E  T $

  39. stack s8 (E) s7(() s0($) input err $

  40. Grammar Hierarchy Non-ambiguous CFG CLR(1) LL(1) LALR(1) SLR(1) LR(0)

  41. Constructing LR(0) parsing table • Add a production S’  S$ • Construct a finite automaton accepting “valid stack symbols” • States are set of items A  • The states of the automaton becomes the states of parsing-table • Determine shift operations • Determine goto operations • Determine reduce operations

  42. Example Finite State Automaton

  43. Constructing a Finite Automaton • NFA • For X  X1 X2 … Xn • X  X1 X2 …XiXi+1 … Xn • “prefixes of rhs (handles)” • X1 X2 … Xi is at the top of the stack and we expect Xi+1 … Xn • The initial state S’ S$ • (X  X1…XiXi+1 … Xn, Xi+1 )= X  X1 …XiXi+1… Xn • For every production Xi+1   (X  X1 X2 …XXi+1 … Xn,  ) = Xi+1   • Convert into DFA

  44. T  i  T  (E )  i E  T  T  i ) T ( T  ( E ) T  (E ) T  (E ) E   T E S   E $ E E   E + T E  E + T E  E+ T E  E+T  + S E $ E T $ S E $  S  E$ E  T | E + T T  i | ( E )

  45. Example Finite State Automaton

  46. Filling Parsing Table • A state si • reduce A • A    si • Shift • A  t   si • Goto(si, X) = sj • A   X   si • (si, X) = sj • When conflicts occurs the grammar is not LR(0)

  47. Example Finite State Automaton

  48. Example Control Table

  49. 4 EE+  E EE+E E  i 0 2 SE$ EE+E E  i SE$ EE+E + E + $ E i 5 3 EE+ E EE+E 1 E  i S E $ i Example S  E $ E  E + E | i

  50. 0 SE$ EE+E E  i 4 EE+  E EE+E E  i 2 SE$ EE+E + E + $ i E 5 3 1 EE+ E EE+E E  i S E $ i

More Related