Download presentation
Presentation is loading. Please wait.
1
Simple, efficient;limitated
Revision Simple, efficient;limitated LR Parser Input a1 … ai … an $ LR Parser 栈 sm Xm sm-1 Xm-1 … s0 Output Simple LR (SLR) Canonical LR (LR) LookAheadLR (LALR) action goto LR Parse Table 10/46
2
3.5 LR Parser 3.5.4 Construct Canonical LR Parsing table
LR(0) Item [A·] Left of the dot indicates history information; right of the dot indicates what we expect from input 3.5.4 Construct Canonical LR Parsing table LR(1) Item set: It is possible to carry more information in the state that will allow us to rule out some of these in valid reductions The extra information is incorporated in to the state b y redefining items to include a terminal symbol as a second component. The general form of an item becomes [A·, a]
3
3.5 LR Parser 3.5.4 Construct Canonical LR Parsing table
LR(1) item set: The general form of an item becomes [A·, a] LR(1) item [A·, a] is valid for a viable prefix : if there exists a derivation S *rm Aw rm w, where: = ; a is the first symbol of w, or w is and a is $。
4
3.5 LR Parser Ex S BB B bB | a Derivation S *rm bbBba rm bbbBba:
[Bb·B, b] is valid for = bbb LR(1) item [A·, a] is valid for viable prefix : if exists S *rm Aw rm w, where: = ; a is the first symbol of w, or w is anda is$。
5
3.5 LR Parser Ex S BB B bB | a
From S *rm bbBba rm bbbBba we observe: [Bb·B, b] is valid for = bbb For item [A·, a], If is not empty, should shift (same as LR(0)); If is empty,decide the reduction choice by a, rather than FOLLOW(A) (different from LR(0)). Usually a is subset of FOLLOW(A)
6
3.5 LR Parser Construct Canonical LR Parsing table
Constructing LR(1) Sets of Items to recognize viable prefixes of G 1、Augmented grammar S S S BB B bB B a 15/46
7
3.5 LR Parser Construct Canonical LR Parsing table
S S S BB B bB B a Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G 2、Construct LR(1) Item set I0: S .S, $ S .BB, $ B .bB, b/a B .a, b/a closure(I) 1、Place every item in I into closure(I) 2、If [Aα·Bβ, a] is in closure(I), and Bη is a production,then add [B·η,b ] into closure(I) if it does not belongs to closure(I) (b in FIRST(βa))
8
3.5 LR Parser Construct Canonical LR Parsing table
S S S BB B bB B a Construct Canonical LR Parsing table Constructing LR(1) Sets of Items to recognize viable prefixes of G 2、Construct LR(1) Item set I0: S .S, $ (kernel item) S .BB, $ (nonkernel item, B .bB, b/a which could be B .a, b/a obtained from closure)
9
3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S S
10
3.5 LR Parser S S ·S, $ I0 S S·, $ I1 S ·BB, $ B ·bB, b/a
B ·a, b/a S S S·, $ I1 S B·B, $ B ·bB, $ B ·a, $ I2 B b B b·B, b/a B ·bB, b/a B ·a, b/a I3 a B a·, b/a I4
11
3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a
B ·a, $ I2 S B B b·B, b/a B ·a, b/a I3 B a·, b/a I4 a b S BB·, $ I5 B b·B, $ B ·a, $ I6 B bB·, $ I9 B a·, $ I7 B bB·, b/a I8 20/46
12
3.5 LR Parser Construct Canonical LR Parsing table
Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: If[A ·a, b] in Ii,and goto(Ii, a) = Ij ,then set action[i, a] sj。 If [A · , a] in Ii,and A S,then set action[i, a] rj . If [SS·, $] Ii,then set action[i, $] = acc。 If conflict occurs, the grammar is not LR(1)
13
3.5 LR Parser Construct Canonical LR Parsing table
Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: . . . Goto function for state i : if goto(Ii, A) = Ij, then goto[i, A] = j
14
3.5 LR Parser Construct Canonical LR Parsing table
Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: . . . Goto function for state i : Undefined entries are set as error。
15
3.5 LR Parser Construct Canonical LR Parsing table
Constructing LR(1) Sets of Items to recognize viable prefixes of G For state i from Ii,its action function defined as: . . . Goto function for state i : Undefined entries are set as error。 The state that contains [S·S, $] is the start state
16
3.5 LR Parser 3.5.5 Constructing LALR Parse table Property:
Parse tables of LALR and SLR have same number of states, meanwhile that of LR is much larger The power of LALR lies between SLR and LR 25/46
17
3.5 LR Parser Merged LR(1) item set:
Omit the character a in [A · , a], and merge the item sets
18
3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a
B ·a, $ I2 S B B b·B, b/a B ·a, b/a I3 B a·, b/a I4 a b S BB·, $ I5 B b·B, $ B ·a, $ I6 B bB·, $ I9 B a·, $ I7 B bB·, b/a I8
19
3.5 LR Parser S ·S, $ I0 S ·BB, $ B ·bB, b/a B ·a, b/a S
S BB·, $ I5 B S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89
20
3.5 LR Parser stack input 0 bba$ S ·S, $ I0 S ·BB, $ B ·bB, b/a
B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input bba$ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89 30/46
21
3.5 LR Parser stack input 0b36 ba$ S ·S, $ I0 S ·BB, $ B ·bB, b/a
B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b ba$ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89
22
3.5 LR Parser stack input 0b36b36 a$ S ·S, $ I0 S ·BB, $
B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36b a$ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89
23
3.5 LR Parser stack input 0b36b36a47 $ S ·S, $ I0 S ·BB, $
B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36b36a $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89
24
3.5 LR Parser stack input 0b36b36B89 $ S ·S, $ I0 S ·BB, $
B ·bB, b/a B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36b36B $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89
25
3.5 LR Parser stack input 0b36B89 $ S ·S, $ I0 S ·BB, $ B ·bB, b/a
B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0b36B $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89 35/46
26
3.5 LR Parser stack input 0B2 $ S ·S, $ I0 S ·BB, $ B ·bB, b/a
B ·a, b/a S S S·, $ I1 S BB·, $ I5 B stack input 0B $ S B·B, $ B ·bB, $ B ·a, $ I2 B b b B b·B, b/a/$ B ·bB, b/a/$ B ·a, b/a/$ I36 a b a B a B a·, b/a/$ I47 B bB·, b/a/$ I89
27
3.5 LR Parser Merged item set could lead to conflicts
No new shift-reduce conflict [A·, a] [B·a, b] [B·a, c] [A·, d]
28
3.5 LR Parser Merged item set could lead to conflicts
No new shift-reduce conflict New reduce-reduce conflict is possible
29
3.5 LR Parser Merged item set could lead to conflicts
No new shift-reduce conflict New reduce-reduce conflict is possible S S S aAd | bBd | aBe | bAe A c B c
30
3.5 LR Parser Merged item set could lead to conflicts
No new shift-reduce conflict New reduce-reduce conflict is possible S S S aAd | bBd | aBe | bAe A c B c valid for ac A c ·, d B c ·, e 40/46
31
3.5 LR Parser Merged item set could lead to conflicts
No new shift-reduce conflict New reduce-reduce conflict is possible S S S aAd | bBd | aBe | bAe A c B c valid for ac valid for bc A c ·, d B c ·, e A c ·, e B c ·, d
32
3.5 LR Parser 合并同心项目集可能会引起冲突 同心集的合并不会引起新的移进归约冲突 同心集的合并有可能产生新的归约归约冲突
S S S aAd | bBd | aBe | bAe A c B c 对ac有效的项目集 对bc有效的项目集 A c ·, d B c ·, e A c ·, e B c ·, d 合并同心集后 A c ·, d/e B c ·, d/e
33
3.5 LR Parser Merged item set could lead to conflicts
No new shift-reduce conflict New reduce-reduce conflict is possible S S S aAd | bBd | aBe | bAe A c B c valid ac valid for bc A c ·, d B c ·, e A c ·, e B c ·, d merged LR(1) but not LALR(1) A c ·, d/e B c ·, d/e
34
3.5 LR Parser Construct LALR(1) Parsing table
Construct LR(1) item set C = {I0, I1, …, In}。 Search for LR(1) item sets that could be merged Construct parsing table as LR(1)
35
3.7 Parser Generator 3.7.1 Parser Generator Yacc
Lexer generated by Lex Lex compiler Lex source lex.l lex.yy.c C a.out input Token stream Yacc compiler Yacc source translate.y y.tab.c C a.out Token stream output 1/36
36
3.7 Parser Generator Structure of Yacc source code Declaration %%
Translation rules Auxiliary functions
37
3.7 Parser Generator Ex: Declaration %{
EE + T | T TT * F | F F(E) | digit Ex: Declaration %{ #include <ctype.h> /* constant, variable*/ %} %token DIGIT %%
38
3.7 Parser Generator Ex: Translation rules
EE + T | T TT * F | F F(E) | digit Ex: Translation rules line : expr ‘\n’ {printf(“%d\n”, $1);} ; expr : expr ‘+’ term {$$ = $1+$3;} | term term : term ‘*’ factor {$$ = $1 * $3;} | factor factor : ’(’ expr ‘)’ {$$ = $2;} | DIGIT %%
39
3.7 Parser Generator Ex: Auxiliary function yylex(){ int c;
EE + T | T TT * F | F F(E) | digit Ex: Auxiliary function yylex(){ int c; c = getchar(); if (isdigit(c)){ yylval = c – ‘0’; return DIGIT; } return c; 5/36
40
3.7 Parser Generator 3.7.2 Use Yacc to process Ambiguous Grammar
To default rules to resolve conflicts: For reduce-reduce conflicts, choose the first reduce rule For shift-reduce conflicts, choose shift rules
41
3.7 Parser Generator 3.7.2 Use Yacc to process Ambiguous Grammar
Ex: Calculator Calculate the expression from input。 lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number
42
3.7 Parser Generator %{ # include <ctype .h>
# include <stdio.h > # define YYSTYPE double /* 将栈定义为double类型 */ %} %token NUMBER %left ‘+’ ‘’ %left ‘*’ ‘/ ’ %right UMINUS %% lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number
43
3.7 Parser Generator lines : lines expr ‘\ n’ {printf ( “%g \ n”, $2 ) } | lines ‘\ n’ | /* */ ; expr : expr ‘+’ expr {$$ = $1 + $3; } | expr ‘’ expr {$$ = $1 $3; } | expr ‘*’ expr {$$ = $1 * $3; } | expr ‘/ ’ expr {$$ = $1 / $3; } | ‘(’ expr ‘)’ {$$ = $2; } | ‘’ expr %prec UMINUS {$$ = $2; } | NUMBER %% lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number
44
3.7 Parser Generator yylex ( ) { int c;
while ( ( c = getchar ( ) ) = = ‘ ’ ); if ( ( c = = ‘.’ ) | | (isdigit (c) ) ) { ungetc (c, stdin); scanf ( “% lf ”, &yylval); return NUMBER; } return c; lines lines expr ‘\n’ | lines ‘\n’ | e E E + E | E – E | E * E | E / E | (E) | -E | number 10/36
45
课堂练习 S V = E S E V * E V id E V I0 : S · S, $
Construct the DFA based on LR(1) items S V = E S E V * E V id E V I0 : S · S, $ S ·V = E, $ S · E, $ V · * E, =/$ V · id, =/$ E · V, $ V I2 : S V · = E, $ E V ·, $ 45/46
46
参考答案 S’. S, $ S . V=E, $ S. E, $ V.*E, =/$ V. id, =/$ E. V, $ S
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.