580 likes | 592 Views
s m. 温故知新. $. action. goto. …. a i. …. a n. a 1. 输入. X m. s m -1. LR 分析程序. 输出. 栈. X m -1. …. s 0. LR 分析器的模型. 1 。句柄与某个产生式的右部符号串相同 2 。句柄是句型的一个子串 3 。把句柄归约成非终结符代表了最右推导逆过程的一步. 上下文无关文法. !. 最左推导. 最右推导. 自上而下. 自下而上. 句柄. 归约. 移进 - 归约冲突. 移进 - 归约分析. 递归下降预测分析. 归约 - 归约冲突.
E N D
sm 温故知新 $ action goto … ai … an a1 输入 Xm sm-1 LR分析程序 输出 栈 Xm-1 … s0 LR分析器的模型 1。句柄与某个产生式的右部符号串相同 2。句柄是句型的一个子串 3。把句柄归约成非终结符代表了最右推导逆过程的一步 上下文无关文法 ! 最左推导 最右推导 自上而下 自下而上 句柄 归约 移进-归约冲突 移进-归约分析 递归下降预测分析 归约-归约冲突 LL(1)文法 LR文法 非递归的预测分析 2个函数 活前缀 右句型的前缀,该前缀不超过最右句柄的右端 • 简单的LR方法(SLR) • 规范的LR方法 • 向前看的LR方法(LALR) 1/41
3.5LR Parser 3.5.3 Constructing SLR Parse Table LR(0)item(item for short) a production of G with a dot at some position of the body Ex:AXYZ has four items A ·XYZ A X·YZ A XY·Z A XYZ· Ex:Ahas only one item A · Intuitively , an item indicates ho w much of a production we have seen at a given point in the parsing process.
S aABe A Abc | b B d a b b c d e $ $ S0 S ·aABe
S aABe A Abc | b B d Two possibilities b b c d e $ $ a S0 S1 A ·Abc S a ·ABe A ·b S a ·ABe A ·Abc A ·b action(1,b)=S2
S aABe A Abc | b B d b c d e $ $ a b S0 S1 S2 S2 achieved by shifting b from S1 S1 S a ·ABe A ·Abc A ·b A · bbecomes A b · S2 A b · Next, reduce
S aABe A Abc | b B d b c d e $ $ a A S0 S1 S3 Check: from S1, after seeing A, where to go? S a A·Be Two possibilities S1 S a ·ABe A ·Abc A ·b A A·bc S3 S a A ·Be A A·bc B ·d
S aABe A Abc | b B d c d e $ $ a A b S0 S1 S3 S4 After shifting b becomes S3 S a A ·Be A A·bc B ·d S4 A Ab ·c
S aABe A Abc | b B d d e $ $ a A b c S0 S1 S3 S4 S5 becomes S4 A Ab ·c Next, reduce S5 A Ab c ·
S aABe A Abc | b B d d e $ $ a A S0 S1 S3 becomes Next? S3 S a A ·Be A A·bc B ·d
S aABe A Abc | b B d e $ $ a A d S0 S1 S3 S6 S2 S a A·Be A A·bc B · d becomes S6 B d · Next, reduce
S aABe A Abc | b B d e $ $ a A B S0 S1 S3 S7 S3 S a A·Be A A·bc B · d becomes S7 S a AB ·e
S aABe A Abc | b B d $ $ a A B e S0 S1 S3 S7 S7 S a AB ·e becomes Next, reduce S8 S a AB e ·
S aABe A Abc | b B d $ $ S S0 OK Accept
3.5LRParser Two steps of constructing SLR Parse table 1. Construct a DFA to recognize the viable prefixes 2. Construct the parse table with respect to the DFA
3.5LRParser Constructing the DFA 1. Augmented Grammar E E E E + T | T T T * F | F F ( E ) | id acceptance occurs when and only when the parser is about to reduce b y E E id + id F + id T+id E+id E+F E+T E E rmE+T rmE+F rm E+id rm T+id rm F+id rm id+id
3.5LRParser Constructing the DFA 2. Construct the canonical LR(0) collection I0: E ·E E E E E + T | T T T * F | F F ( E ) | id closure(I) 1、 add every item in I to closure(I) 2、If Aα·Bβis inclosure(I), and Bγ is a production, then add B ·γ to closure(I)if it’s not there E ·E + T E ·T T ·T * F T ·F F ·(E) F ·id 5/41
3.5LRParser Constructing the DFA 2. Construct the canonical LR(0) collection I0: E ·E (Kernel items) E ·E + T E ·T (Nonkernel items, T ·T *F could be obtained by T ·F computing the closure F ·(E) function of kernel items) F ·id E E E E + T | T T T * F | F F ( E ) | id E’·E and those items in which dot not at the left end
3.5LRParser Constructing the DFA 2. Construct the canonical LR(0) collection I0: I1: E ·E E E· E ·E + T E E· + T E ·T T ·T *F T ·F F ·(E) F ·id E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser Constructing the DFA 2. Construct the canonical LR(0) collection I0: I1: E ·E E E· E ·E + T E E· + T E ·T T ·T *F I1 := goto ( I0, E ) T ·F F ·(E) F ·id E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser Constructing the DFA 2. Construct the canonical LR(0) collection I0: I1: E ·E E E· E ·E + T E E· + T E ·T T ·T *F I2: T ·F E T· F ·(E) T T· *F F ·id E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser Constructing the DFA 2. Construct the canonical LR(0) collection I0: I1: E ·E E E· E ·E + T E E· + T E ·T T ·T *F I2: T ·F E T· F ·(E) T T· *F F ·id I3: T F· E E E E + T | T T T * F | F F ( E ) | id 10/41
3.5LRParser I0: I4: E ·E F (·E ) E ·E + T E ·E + T E ·T E ·T T ·T *F T ·T *F T ·F T ·F F ·(E) F ·(E) F ·id F ·id E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser I0: I4: E ·E F (·E ) E ·E + T E ·E + T E ·T E ·T T ·T *F T ·T *F T ·F T ·F F ·(E) F ·(E) F ·id F ·id I5: F id· E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 E E E E + T | T T T * F | F F ( E ) | id I1: E E· E E·+ T
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 E E E E + T | T T T * F | F F ( E ) | id I1: E E· E E·+ T I6: EE + ·T T ·T * F T ·F F ·(E) F ·id 15/41
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 E E E E + T | T T T * F | F F ( E ) | id I2: E T· TT·*F I7: TT*·F F ·(E) F ·id
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 E E E E + T | T T T * F | F F ( E ) | id I3: T F·
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 E E E E + T | T T T * F | F F ( E ) | id I4: F (·E ) E ·E + T E ·T T ·T *F T ·F F ·( E ) F ·id
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 I4: I8: F (·E ) F (E·) E ·E + T E E·+ T E ·T T ·T *F T ·F F ·( E ) F ·id E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 I4: I8: F (·E ) F (E·) E ·E + T E E·+ T E ·T T ·T *F I2: T ·F E T· F ·( E ) TT·*F F ·id E E E E + T | T T T * F | F F ( E ) | id 20/41
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 I4: I8: F (·E ) F (E·) E ·E + T E E·+ T E ·T T ·T *F I2: T ·F E T· F ·( E ) TT·*F F ·id I3: TF· E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 I4: I8: F (·E ) F (E·) E ·E + T E E·+ T E ·T T ·T *F I2: T ·F E T· F ·( E ) TT·*F F ·id I3: TF· I4: F (·E ) . . . E E E E + T | T T T * F | F F ( E ) | id
3.5LRParser E E E E E + T | T T T * F | F F ( E ) | id I1 I0 T I2 F I3 ( I4 id I5 I4: I8: F (·E ) F (E·) E ·E + T E E·+ T E ·T T ·T *F I2: T ·F E T· F ·( E ) TT·*F F ·id I3: TF· I4:I5: F (·E )Fid· . . .
3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5 E E E E + T | T T T * F | F F ( E ) | id I5: Fid·
3.5LRParser E + E E E E + T | T T T * F | F F ( E ) | id I1 I0 I6 T * I7 I2 F I3 ( E I4 I8 T ( 指向I2 F 指向I3 id id 25/47 I5
3.5LRParser E + T * I1 I0 I9 To I7 I6 F To I3 ( To I4 id To I5 T * F I10 I7 I2 ( F To I4 I3 id To I5 ( E ) I11 I4 I8 + To I6 T ( To I2 F To I3 id id I5
3.5LRParser Properties of the constructed DFA Definition:valid item If S*rm Aw rm12w,we say item A1·2is valid for a viable prefix 1 In general, an item will b e v alid for man y viable prefixes S*rm AAw rmA12w rm 1212w For any viable prefix 1, suppose A1·2is valid If 2 , should shift If 2 = , should reduce by A1
3.5LRParser Properties of the constructed DFA Definition:valid item If S*rm Aw rm12w,we say item A1·2is valid for a viable prefix 1 In general, an item will b e v alid for man y viable prefixes A viable prefix may have multiple valid items The valid items of a viable prefix is the item set (state) reached by the path
3.5LRParser ExE + T* is a viable prefix. After reading it, DFAreaches I7 I7: TT *·F, F ·(E ), F ·id E E E EE E E+T E+T E+T E+T* F E+T* F E+T* F E+T*id E+T*(E ) E+T*id E+T* F *id Definition:Valid item If S*rmAw rm12w,we say item A1·2is valid for viable prefix 1
3.5LRParser Two steps of constructing SLR Parse table 1. Construct a DFA to recognize the viable prefixes 2. Construct the parse table with respect to the DFA 30/41
3.5LRParser Construct SLR Parse table from DFA For state i from Ii,its actionfunction defined as: If [A·a]in Ii,and goto(Ii, a ) = Ij,then action[i, a]is sj。 If [A·]in Ii,then for all a in FOLLOW(A),place action[i, a]with rj, where jis the id of A If [SS·]in Ii,action[ i, $ ]is acc。 If conflict occurs, the grammar is not SLR(1)
3.5LRParser Construct SLRParse table State Iconstructed from Ii, its action function defined as follows . . . Construct gotofunction as follows: For all nonterminalA, ifgoto(Ii, A) = Ij, then goto[i, A] = j。
3.5LRParser Construct SLRParse table State Iconstructed from Ii, its action function defined as follows . . . Construct gotofunction as follows: Set other entries as error
3.5LRParser Construct SLRParse table State Iconstructed from Ii, its action function defined as follows . . . Construct gotofunction as follows: Set other entries as error The state that contains [S·S]is the initial state
3.5LRParser 例 I2: E T· T T· *F 因为FOLLOW(E) = {$, +, )},所以 action[2, $]=action[2, +]=action[2, )]=r2 action[2, *] = s7 E E E E + T | T T T * F | F F ( E ) | id 35/41
3.5LRParser ExLimitation of SLR(1) 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 I2: S V ·= E E V· V
3.5LRParser ExLimitation of SLR(1) 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 I2: S V ·= E E V· V I2the first item: action[2, = ] = s6
3.5LRParser ExLimitation of SLR(1) 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 I2: S V ·= E E V· V I2the first item: action[2, = ] = s6 I2the second item: action[2, = ] = r5 Reduce by EV In that = belongs to FOLLW(E)
3.5LRParser ExLimitation of SLR(1) 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 I2: S V ·= E E V· V I2the first item: action[2, = ] = s6 I2the second item: action[2, = ] = r5 Reduce by EV In that = belongs to FOLLW(E)