310 likes | 318 Views
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.
E N D
Revision Leftmost Derivation F T T E * F T id id ? Rightmost Derivation ETE E + TE | TFT 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
Revision F T T E * F T id id E ETE E + TE | TFT 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 TrmF T ’rm F * F T ’ rm F * F rm F * id rm id*id
Rightmost Derivation 温故知新 Leftmost Derivation Context Free Grammar Top Down Bottom Up Recursive Descent LL(1) Non recursive 2Functions
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
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 rmaABermaAde rmaAbcdermabbcde 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
3.4Bottom Up Parsing 3.4.2Handle S rmaABermaAde 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+ id3rm E *id2+ id3 rmid1* id2 + id3rmid1* id2 + id3 In E * E + id3, handle not unique
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
3.4Bottom Up Parsing 3.4.3 Shift-Reduce with Stack Example: Use Shift-Reduce analyzer to parseinput id1* id2 + id3
3.4Bottom Up Parsing 30/75
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
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 … $
3.4Bottom Up Parsing Reduce-Reduce Conflict stmt id (parameter_list) | expr := expr parameter_listparameter_list, parameter | parameter parameter id expr id (expr_list) | id expr_listexpr_list, expr | expr Sentential Form Starting with A(I, J) StackInput … id ( id , id )…
3.4Bottom Up Parsing Reduce-Reduce Conflict stmt procid (parameter_list) | expr := expr parameter_listparameter_list, parameter | parameter parameter id expr id (expr_list) | id expr_listexpr_list, expr | expr stackinput … procid ( id , id )…
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
3.5LRParser 3.5.1LRParser Structure action sm goto $ … ai … an a1 Input Xm sm-1 LRParsing Program Output Stack Xm-1 … s0 LRParser
3.5LR Parser E E + T | T T T * F |F F (E ) | id
3.5LR Parser 3.5.2 LR文法和LR分析方法的特点 概念 活前缀:右句型的前缀,该前缀不超过最右句柄的右端 S*rm A wrm w 的任何前缀(包括和本身)都是一个活前缀。 一个符号串的前缀是指从第一个符号开始的连续的若干个符号构成的子串。 55/75
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
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.
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
3.5LRParser 例 E E + T | E T T T * F |T E F (E ) | F id 60/75
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
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
3.5LRParser Comparison between LR(1) and LL(1)
3.5LRParser Comparison between LR(1) and LL(1) Suppose the last step of derivation is Al S rm … rm A b w rm l b w LR(1) LL(1)
3.5LRParser Comparison between LR(1) and LL(1)
3.5LR分析器 Comparison between LR(1) and LL(1)
习 题 3.15 75/75