1 / 31

Revision

This article discusses the leftmost and rightmost derivation in context-free grammars, as well as the LR parsing method and its properties. It explores shift-reduce conflicts and viable prefixes, providing insights into parsing strategies.

witcher
Download Presentation

Revision

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. Revision Leftmost Derivation  F  T T E * F T id id ? Rightmost Derivation ETE  E  + TE  |  TFT  T  * FT |  F (E) | id Context Free Grammar E Input:id*id Top Down Bottom Up Recursive Descent LL(1) Non recursive 2Functions E lmTE ’ lmFT ’E ’lmid T ’E’lm id * F T ’ E’ lm id * id T ’ E’ lm id * id E’ lm id*id 1/75

  2. Revision  F T  T E * F T id id E ETE  E  + TE  |  TFT  T  * FT |  F (E) | id Input:id*id Input scanned left to right, hard to determine the parsing strategy with rightmost derivation. Rightmost derivation not suitable for top-down parsing E rmTE ’ rm TrmF T ’rm F * F T ’ rm F * F rm F * id rm id*id

  3. Rightmost Derivation 温故知新 Leftmost Derivation Context Free Grammar Top Down Bottom Up Recursive Descent LL(1) Non recursive 2Functions

  4. 3.4Bottom Up Parsing 3.4.1Reduction Ex.S aABe A Abc | b B d A B S A a e d b c Input:abbcde abbcde b aAbcde aAde aABe S S rm aABe rm aAde rm aAbcde rm abbcde

  5. 3.4Bottom Up Parsing 3.4.2Handle A handle is a substring that matches the body of a production, and whose reduction represents one step along the reverse of a rightmost derivation S aABe A Abc | b B d S rmaABermaAde rmaAbcdermabbcde Matches the body of a production Substring of the sentential form reduction of a handle represents one step along the reverse of a rightmost derivation 5/75

  6. 3.4Bottom Up Parsing 3.4.2Handle S rmaABermaAde rmaAbcde rmabbcde To the right of the handle contains only terminal symbols If a grammar is unambiguous, then every right-sentential form of the grammar has exactly one handle Example E  E + E | E * E | (E ) | id E rmE * E E rmE + E rm E *E + E rm E +id3 rm E * E +id3 rmE * E+ id3 rm E *id2+ id3rm E *id2+ id3 rmid1* id2 + id3rmid1* id2 + id3 In E * E + id3, handle not unique

  7. 3.4Bottom Up Parsing 3.4.3 Shift-Reduce Parsing Four Actions: ShiftShift the next input symbol on top of the stack ReduceThe right end of the string to be reduced must be at the top of the stack. Locate the left end of the string within the stack, and decide with what nonterminal to replace the string AcceptAnnounce successful completion of parsing ErrorDiscover a syntax error, and call an error recovery routine X a + b $ Input Y Z Shift Reduce Program $ Output Stack Parsing Table M

  8. 3.4Bottom Up Parsing 3.4.3 Shift-Reduce with Stack Example: Use Shift-Reduce analyzer to parseinput id1* id2 + id3

  9. 3.4Bottom Up Parsing 30/75

  10. 3.4Bottom Up Parsing Two important issues, even when we know reduction should be performed Decide the substring to be reduced in the right-sentential form Decide which production to choose

  11. 3.4Bottom Up Parsing 3.4.4Conflicts Shift-Reduce Conflict Example stmt if expr then stmt | if expr then stmt else stmt | other Suppose the following parser status StackInput … if expr then stmt else … $

  12. 3.4Bottom Up Parsing Reduce-Reduce Conflict stmt  id (parameter_list) | expr := expr parameter_listparameter_list, parameter | parameter parameter id expr id (expr_list) | id expr_listexpr_list, expr | expr Sentential Form Starting with A(I, J) StackInput … id ( id , id )…

  13. 3.4Bottom Up Parsing Reduce-Reduce Conflict stmt  procid (parameter_list) | expr := expr parameter_listparameter_list, parameter | parameter parameter id expr id (expr_list) | id expr_listexpr_list, expr | expr stackinput … procid ( id , id )…

  14. 3.5LRParser LR(k)Parsing Characteristics Recognize virtually all programming language constructs Efficient Three types of LRParsing Simple LR(SLR) Canonical LR Lookahead LR方法(LALR) 35/75

  15. 3.5LRParser 3.5.1LRParser Structure action sm goto $ … ai … an a1 Input Xm sm-1 LRParsing Program Output Stack Xm-1 … s0 LRParser

  16. 3.5LR Parser E E + T | T T T * F |F F  (E ) | id

  17. 3.5LR Parser

  18. 3.5LR Parser 3.5.2 LR文法和LR分析方法的特点 概念 活前缀:右句型的前缀,该前缀不超过最右句柄的右端 S*rm  A wrm  w 的任何前缀(包括和本身)都是一个活前缀。 一个符号串的前缀是指从第一个符号开始的连续的若干个符号构成的子串。 55/75

  19. 3.5LRParser 3.5.2 Properties of LR Parser Viable Prefix The prefixes of right sentential forms that can app ear on the stack of a shift- reduce parser are called viable prefixes. LR Grammar:a language can be generated by an LR(k) grammar if and only if it is deterministic [and context-free] Donald Knuth

  20. 3.5LR分析器 3.5.2 Properties of LR Parser Everything to the left of the stack top has already been fully reduced, and forms a viable prefix.

  21. 3.5LRParser

  22. 3.5LRParser 3.5.2 Properties of LR Parser Everything to the left of the stack top has already been fully reduced, and forms a viable prefix. GOTO Function is in essence a DFA recognizing viable prefixes

  23. 3.5LRParser 例 E E + T | E T T T * F |T E F  (E ) | F  id 60/75

  24. 3.5LRParser 3.5.2 Properties of LR Parser Everything to the left of the stack top has already been fully reduced, and forms a viable prefix. GOTO Function is in essence a DFA recognizing viable prefixes Only the top of stack is necessary for determining the handles. The LR-parsing method is the most general non backtracking shift-reduce parsing method known 65/75

  25. 3.5LRParser 3.5.2 Properties of LR Parser The class of grammars that can b e parsed using LR methods is a proper superset of the class of grammars that can b e parsed with predictive or LL methods. An LR parser can detect a syntactic error as so on as it is possible to do so on a left-to-right scan of the input Drawback: Hard to implement by hand 65/75

  26. 3.5LRParser Comparison between LR(1) and LL(1)

  27. 3.5LRParser Comparison between LR(1) and LL(1) Suppose the last step of derivation is Al S rm … rm A b w rm l  b w LR(1) LL(1)

  28. 3.5LRParser Comparison between LR(1) and LL(1)

  29. 3.5LR分析器 Comparison between LR(1) and LL(1)

  30. 习 题 3.15 75/75

More Related