Upcoming SlideShare
×

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.
Standard text messaging rates apply

# L Rparser Edos

262

Published on

Aho, Ullman …

Aho, Ullman

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
262
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
13
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. LR Parsers Canonical Collection LR
• 2. LR Parser 1
• Tokens
• Driver Routine
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
• Determines S n , the state on top of stack
• Uses the current token a i
• Consults Table[S n , a i ]  Edo .
• Shift s
• Reduce
• Accept
• Error
• 4. Shift
• The parser executes a shift move:
• Stack  Edo
• Shifts the current token
• 5. Reduce
• Consults the specific production: A  ß
• If n is the lenght of ß
• Pop n symbols off the stack
• Push A onto the stack
• Note : the current token is not changed in a reduce move
• 6. Accept
• IF Table[S n , a i ] = accept, parsing is completed
• 7. Error
• IF Table[S n , a i ] = error, the parser has discovered an error and calls an error recovery routine
• 8. How to construct a simple LR 2
• Procedure Closure(P)
• Begin
• repeat
• for each item A  @♥Bß in P and each production
• B  ¥ in G such that B  ♥ ¥ is not in P
• do add B  ♥ ¥ to P
• until no more items can be aded to P;
• return P;
• end
IBID. Fig. 6.5 pág. 207
• 9. How to construct a simple LR 2
• Procedure ITEMS(G’)
• Begin
• C := {CLOSURE(S’  ♥S)};
• repeat
• for each set of items P in C and each grammar symbol X
• such that GOTO(P,X) is not empty and is not in C
• do add GOTO(P,X) to C
• until no more sets of items can be added to C
• end
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