Download presentation
Presentation is loading. Please wait.
Published byHeather Green Modified over 6 years ago
1
Revision ? E TE E + TE | T FT T * FT |
F (E) | id Context Free Grammar E Input:id*id T E Leftmost Derivation Rightmost ? Top Down Bottom Up F T Recursive Descent id * T F LL(1) id Non recursive 2 Functions E lm TE ’ lm FT ’E ’lm id T ’E’ lm id * F T ’ E’ lm id * id T ’ E’ lm id * id E’ lm id*id 1/75
2
Revision E rm TE ’ rm Trm F T ’ rm F * F T ’
E TE E + TE | T FT T * FT | F (E) | id T E F T id Input:id*id * T F Input scanned left to right, hard to determine the parsing strategy with rightmost derivation. Rightmost derivation not suitable for top-down parsing id E rm TE ’ rm Trm F T ’ rm F * F T ’ rm F * F rm F * id rm id*id
3
温故知新 Context Free Grammar Leftmost Derivation Rightmost Top Down
Bottom Up Recursive Descent LL(1) Non recursive 2 Functions
4
3.4 Bottom Up Parsing 3.4.1 Reduction Ex. S aABe A Abc | b B d
Input:abbcde abbcde b aAbcde aAde aABe S S rm aABe rm aAde rm aAbcde rm abbcde
5
3.4 Bottom Up Parsing 3.4.2 Handle
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 3.4.2 Handle 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 rm aABe rm aAde rm aAbcde rm abbcde 5/75
6
3.4 Bottom Up Parsing 3.4.2 Handle
S rm aABe rm aAde rm aAbcde rm abbcde 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 rm E * E E rm E + E rm E * E + E rm E + id3 rm E * E + id3 rm E * E + id3 rm E * id2 + id3 rm E * id2 + id3 rm id1 * id2 + id3 rm id1 * id2 + id3 In E * E + id3, handle not unique
7
3.4 Bottom Up Parsing 3.4.3 Shift-Reduce Parsing Four Actions:
Shift Shift the next input symbol on top of the stack Reduce The 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 Accept Announce successful completion of parsing Error Discover a syntax error, and call an error recovery routine a + b $ Input Shift Reduce Program Parsing Table M Output X Y Z Stack
8
3.4 Bottom Up Parsing 3.4.3 Shift-Reduce with Stack
Example: Use Shift-Reduce analyzer to parse input id1 * id2 + id3
9
3.4 Bottom Up Parsing Stack Input Action $ id1 * id2 + id3$ Shift
Reduce E id $E * id2 + id3$ Shift $E* id2 + id3$ Shift $E* id2 + id3$ Reduce E id $E*E + id3$ Shift $E*E+ id3$ Shift $E*E+id3 $ Reduce E id $E*E+E $ Reduce E E+E $E*E $ Reduce E E*E 30/75 $E $ Acc
10
3.4 Bottom 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.4 Bottom Up Parsing 3.4.4 Conflicts Shift-Reduce Conflict Example
stmt if expr then stmt | if expr then stmt else stmt | other Suppose the following parser status Stack Input … if expr then stmt else … $
12
3.4 Bottom 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) Stack Input … id ( id , id )…
13
3.4 Bottom 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 stack input … procid ( id , id )…
14
3.5 LR Parser LR(k) Parsing Characteristics
Recognize virtually all programming language constructs Efficient Three types of LR Parsing Simple LR(SLR) Canonical LR Lookahead LR方法(LALR) 35/75
15
3.5 LR Parser 3.5.1 LR Parser Structure Input Output Stack LR Parser
LR Parsing Program Output Stack LR Parser action goto sm Xm sm-1 Xm-1 … s0 a1 ai an $
16
3.5 LR Parser E E + T | T T T * F | F F (E ) | id Status Action
goto id * ( ) $ E T F s s4 1 s acc 2 r2 s r2 r2 3 r4 r r4 r4 4
17
3.5 LR Parser Stack Input Action id * id + id $ shift 0 id 5
id * id + id $ shift 0 id 5 * id + id $ Reduce by F id 0 F 3 * id + id $ Reduce by T F 0 T 2 * id + id $ shift 0 T 2 *7 id + id $ shift 0 T 2 *7 id 5 + id $ Reduce by F id 0 T 2 *7 F 10 + id $ Reduce byT T*F . . . 0 E 1 $ Accept
18
一个符号串的前缀是指从第一个符号开始的连续的若干个符号构成的子串。
3.5 LR Parser 一个符号串的前缀是指从第一个符号开始的连续的若干个符号构成的子串。 3.5.2 LR文法和LR分析方法的特点 概念 活前缀:右句型的前缀,该前缀不超过最右句柄的右端 S *rm A w rm w 的任何前缀(包括和 本身)都是一个活前缀。 55/75
19
3.5 LR Parser 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.5 LR分析器 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.5 LR Parser Stack Input Action id * id + id $ shift 0 id 5
id * id + id $ shift 0 id 5 * id + id $ Reduce F id 0 F 3 Reduce T F 0 T 2 Shift 0 T 2 *7 id + id $ 0 T 2 *7 id 5 + id $ 0 T 2 *7 F 10 Reduce T T*F . . . 0 E 1 $ Accept
22
3.5 LR Parser 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.5 LR Parser 例 E E + T | E T T T * F | T E F (E ) | F id
状态 动 作 转 移 id * ( ) $ E T F s s4 1 s acc 2 r2 s r2 r2 3 r4 r r4 r4 4 60/75
24
3.5 LR Parser 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.5 LR Parser 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
Reduction of Derivation
3.5 LR Parser Comparison between LR(1) and LL(1) LR(1) LL(1) Way of tree building Bottom Up Top Down Reduction of Derivation Reduction Derivation When to determine the choice of the production Seeing the whole body of the production Seeing the first terminal of the body
27
3.5 LR Parser 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
No left recursion, no left factoring
3.5 LR Parser Comparison between LR(1) and LL(1) LR(1)方 法 LL(1)方 法 Restrictions No No left recursion, no left factoring Parse table |states| ×|symbol| Large |Nonterminal|× |Terminal| Small Stack Status Symbol
29
Determined by the top of the stack and the next symbol
3.5 LR分析器 Comparison between LR(1) and LL(1) LR(1) LL(1) Handle Determined by the top of the stack and the next symbol No handle Syntactic Error No symbol into the stack after errors No symbols into the stack after errors
30
习 题 3.15 75/75
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.