1 / 58

温故知新

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 。把句柄归约成非终结符代表了最右推导逆过程的一步. 上下文无关文法. !. 最左推导. 最右推导. 自上而下. 自下而上. 句柄. 归约. 移进 - 归约冲突. 移进 - 归约分析. 递归下降预测分析. 归约 - 归约冲突.

iegan
Download Presentation

温故知新

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. 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

  2. 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:AXYZ has four items A  ·XYZ A  X·YZ A  XY·Z A  XYZ· Ex:Ahas 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.

  3. S  aABe A  Abc | b B  d a b b c d e $ $ S0 S ·aABe

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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 ·

  9. 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

  10. 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

  11. 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

  12. 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 ·

  13. S  aABe A  Abc | b B  d $ $ S S0 OK Accept

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 3.5LRParser E I1 I0 T I2 F I3 ( I4 id I5

  25. 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

  26. 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: EE + ·T T ·T * F T ·F F ·(E) F ·id 15/41

  27. 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· TT·*F I7: TT*·F F ·(E) F ·id

  28. 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·

  29. 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

  30. 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

  31. 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 ) TT·*F F  ·id E   E E  E + T | T T T * F | F F ( E ) | id 20/41

  32. 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 ) TT·*F F  ·id I3: TF· E   E E  E + T | T T T * F | F F ( E ) | id

  33. 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 ) TT·*F F  ·id I3: TF· I4: F (·E ) . . . E   E E  E + T | T T T * F | F F ( E ) | id

  34. 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 ) TT·*F F  ·id I3: TF· I4:I5: F (·E )Fid· . . .

  35. 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: Fid·

  36. 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

  37. 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

  38. 3.5LRParser Properties of the constructed DFA Definition:valid item If S*rm Aw rm12w,we say item A1·2is valid for a viable prefix 1 In general, an item will b e v alid for man y viable prefixes S*rm AAw rmA12w rm 1212w For any viable prefix 1, suppose A1·2is valid If 2 , should shift If 2 = , should reduce by A1

  39. 3.5LRParser Properties of the constructed DFA Definition:valid item If S*rm Aw rm12w,we say item A1·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 

  40. 3.5LRParser ExE + T* is a viable prefix. After reading it, DFAreaches I7 I7: TT *·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*rmAw rm12w,we say item A1·2is valid for viable prefix 1

  41. 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

  42. 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 [SS·]in Ii,action[ i, $ ]is acc。 If conflict occurs, the grammar is not SLR(1)

  43. 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。

  44. 3.5LRParser Construct SLRParse table State Iconstructed from Ii, its action function defined as follows . . . Construct gotofunction as follows: Set other entries as error

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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 EV In that = belongs to FOLLW(E)

  50. 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 EV In that = belongs to FOLLW(E)

More Related