Your SlideShare is downloading. ×
0
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
L Rparser Edos
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

L Rparser Edos

265

Published on

Aho, Ullman …

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
265
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. LR Parsers Canonical Collection LR
  • 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. 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. 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. 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. Accept <ul><li>IF Table[S n , a i ] = accept, parsing is completed </li></ul>
  • 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. 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. 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. Example: G is a grammar with start symbol E E  E + T E  T T  T/F T  F F  (E) F  id
  • 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. Closure(E’) E’  ♥E Blue nucleous
  • 13. Closure(E) E’  ♥E E  ♥ E + T E  ♥ T
  • 14. Closure(T) E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F
  • 15. Closure(F) E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id
  • 16. State 1 E’  ♥E E  ♥ E + T E  ♥ T T  ♥ T/F T  ♥ F F  ♥ (E) F  ♥ id
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. END Canonical Collection LR

×