L Rparser Edos

458 views

Published on

Aho, Ullman

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
458
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

L Rparser Edos

  1. 1. LR Parsers Canonical Collection LR
  2. 2. LR Parser 1 <ul><li>Tokens </li></ul><ul><li>Driver Routine </li></ul>S T A C K Prg Ejem1 ; Real X2 Fig. 6.2 Aho, Ullman. “Principles of Compiler Design” pág. 199 Parsing Table eof …
  3. 3. Driver Routine <ul><li>Determines S n , the state on top of stack </li></ul><ul><li>Uses the current token a i </li></ul><ul><li>Consults Table[S n , a i ]  Edo . </li></ul><ul><ul><li>Shift s </li></ul></ul><ul><ul><li>Reduce </li></ul></ul><ul><ul><li>Accept </li></ul></ul><ul><ul><li>Error </li></ul></ul>
  4. 4. Shift <ul><li>The parser executes a shift move: </li></ul><ul><ul><li>Stack  Edo </li></ul></ul><ul><ul><li>Shifts the current token </li></ul></ul>
  5. 5. Reduce <ul><li>Consults the specific production: A  ß </li></ul><ul><li>If n is the lenght of ß </li></ul><ul><li>Pop n symbols off the stack </li></ul><ul><li>Push A onto the stack </li></ul><ul><li>Note : the current token is not changed in a reduce move </li></ul>
  6. 6. Accept <ul><li>IF Table[S n , a i ] = accept, parsing is completed </li></ul>
  7. 7. Error <ul><li>IF Table[S n , a i ] = error, the parser has discovered an error and calls an error recovery routine </li></ul>
  8. 8. How to construct a simple LR 2 <ul><li>Procedure Closure(P) </li></ul><ul><li>Begin </li></ul><ul><li>repeat </li></ul><ul><li>for each item A  @♥Bß in P and each production </li></ul><ul><li>B  ¥ in G such that B  ♥ ¥ is not in P </li></ul><ul><li>do add B  ♥ ¥ to P </li></ul><ul><li>until no more items can be aded to P; </li></ul><ul><li>return P; </li></ul><ul><li>end </li></ul>IBID. Fig. 6.5 pág. 207
  9. 9. How to construct a simple LR 2 <ul><li>Procedure ITEMS(G’) </li></ul><ul><li>Begin </li></ul><ul><li>C := {CLOSURE(S’  ♥S)}; </li></ul><ul><li>repeat </li></ul><ul><li>for each set of items P in C and each grammar symbol X </li></ul><ul><li>such that GOTO(P,X) is not empty and is not in C </li></ul><ul><li>do add GOTO(P,X) to C </li></ul><ul><li>until no more sets of items can be added to C </li></ul><ul><li>end </li></ul>IBID. Fig. 6.5 pág. 207
  10. 10. Example: G is a grammar with start symbol E E  E + T E  T T  T/F T  F F  (E) F  id
  11. 11. G’ is the augmented grammar with a new start symbol E’ E’  E E  E + T E  T T  T/F T  F F  (E) F  id Blue nucleous
  12. 12. Closure(E’) E’  ♥E Blue nucleous
  13. 13. Closure(E) E’  ♥E E  ♥ E + T E  ♥ T
  14. 14. Closure(T) E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F
  15. 15. Closure(F) E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id
  16. 16. State 1 E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id
  17. 17. State 2=GoTo(1,E) E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T
  18. 18. State 3 =GoTo(1,T) E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F
  19. 19. State 4=GoTo(1,F) E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F T  F ♥
  20. 20. State 5: from state 1 with ( E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F T  F ♥ F  (♥E) E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id
  21. 21. State 6:from state1 with id E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F T  F ♥ F  (♥E) E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id F  id ♥
  22. 22. States 1,2 ,3,4,5,6 E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F T  F ♥ F  (♥E) E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id F  id ♥
  23. 23. States 7,8,9 E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F T  F ♥ F  (♥E) E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id F  id ♥ E’  E+♥T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id T’  T/♥F F  ♥ (E) F  ♥ id F  (E♥) E  E♥+T
  24. 24. States 10,11,12 E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F T  F ♥ F  (♥E) E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id F  id ♥ E’  E+♥T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id T’  T/♥F F  ♥ (E) F  ♥ id F  (E♥) E  E♥+T E’  E+T♥ T  T ♥ /F T’  T/F♥ F  (E)♥
  25. 25. States 1,2,3,4,... 12 E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id E’  E♥ E  E♥ + T E’  T♥ T  T♥ /F T  F ♥ F  (♥E) E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id F  id ♥ E’  E+♥T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id T’  T/♥F F  ♥ (E) F  ♥ id F  (E♥) E  E♥+T E’  E+T♥ T  T ♥ /F T’  T/F♥ F  (E)♥
  26. 26. END Canonical Collection LR

×