1 / 40

CSCI 3130: Formal languages and automata theory Tutorial 6

CSCI 3130: Formal languages and automata theory Tutorial 6. Chin. Reminder. Homework 4 is due next thursday. LR(0) parsing. A  a • Ab After reading an ‘a’, the next possible symbol is A  a A • b Or expanding the ‘A’ (think of a aAb b) A  • aAb A  a • Ab. LR(0) parsing.

laymon
Télécharger la présentation

CSCI 3130: Formal languages and automata theory Tutorial 6

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. CSCI 3130: Formal languagesand automata theoryTutorial 6 Chin

  2. Reminder • Homework 4 is due next thursday.

  3. LR(0) parsing • A  a•Ab • After reading an ‘a’, the next possible symbol is • A  aA•b Or expanding the ‘A’ (think of aaAbb) • A  •aAb • A  a•Ab

  4. LR(0) parsing • E  E+T | a • T  b | c

  5. LR(0) parsing E  E+T | a T  b | c Put a dot • in any possible positions These are valid items. The bottom ones are complete items. When you see complete items, reduce. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  6. LR(0) parsing Suppose we are given a+b+c Before reading anything, the valid items are: E  •E+T, E  •a, T  •b, T  •c No complete items, shift. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  7. LR(0) parsing Read an input and push it in the stack. a•+b+c E  •E+T < fail E  •a T  •b < fail T  •c < fail E  •E+T E  a• T  •b T  •c T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• a

  8. LR(0) parsing The valid item is E  a• When you see exactly one item and it is complete, reduce. * If there are more than 1 complete items, R/R conflict. * If there is another item, but it is not complete, S/R conflict. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  9. LR(0) parsing How to reduce: Look at the corresponding complete item (i.e. E  a•) R.H.S. has k symbols, pop k symbols out of the stack (i.e. pop ‘a’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  10. LR(0) parsing E  •E+T E  •a < fail T  •b < fail T  •c < fail E  E•+T E  a• T  •b T  •c T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• E

  11. LR(0) parsing The valid item is: E  E•+T No complete items, shift. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  12. LR(0) parsing Read an input and push it in the stack. a+•b+c E  E•+T E  E+•T T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• +

  13. LR(0) parsing The valid items are E  E+•T T  •b <- This comes from expanding T  •b T  •c <- This comes from expanding T  •c No complete items, shift. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  14. LR(0) parsing Read an input and push it in the stack. a+b•+c E  E+•T < fail T  •b T  •c < fail T  b• T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• b

  15. LR(0) parsing The valid item is T  b• When you see exactly one item and it is complete, reduce. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  16. LR(0) parsing How to reduce: Look at the corresponding complete item (i.e. T  b•) R.H.S. has k symbols, pop k symbols out of the stack (i.e. pop ‘b’) Consider the valid items in corresponding state (i.e. items in state 3) Then push the symbol on the L.H.S. to the stack (i.e. push ‘T’) T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  17. LR(0) parsing E  E+•T T  •b < fail T  •c < fail E  E+T• T  •b < fail T  •c < fail T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• T

  18. LR(0) parsing The valid item is E  E+T• When you see exactly one item and it is complete, reduce. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  19. LR(0) parsing How to reduce: Look at the corresponding complete item (i.e. E  E+T•) R.H.S. has k symbols, pop k symbols out of the stack (i.e. pop ‘E+T’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  20. LR(0) parsing E  •E+T E  •a < fail T  •b < fail T  •c < fail E  E•+T E  a• T  •b T  •c T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• E

  21. LR(0) parsing The valid item is: E  E•+T No complete items, shift. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  22. LR(0) parsing Read an input and push it in the stack. a+b+•c E  E•+T T  E+•T T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• +

  23. LR(0) parsing The valid items are E  E+•T T  •b <- This comes from expanding T  •b T  •c <- This comes from expanding T  •c No complete items, shift. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  24. LR(0) parsing Read an input and push it in the stack. a+b+c• E  E+•T < fail T  •b < fail T  •c T  c• T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• c

  25. LR(0) parsing The valid item is T  c• When you see exactly one item and it is complete, reduce. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  26. LR(0) parsing How to reduce: Look at the corresponding complete item (i.e. T  c•) R.H.S. has k symbols, pop k symbols out of the stack. (i.e. pop ‘c’) Consider the valid items in corresponding state (i.e. items in state 5) Then push the symbol on the L.H.S. to the stack (i.e. push ‘T’) T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  27. LR(0) parsing E  E+•T T  •b < fail T  •c < fail E  E+T• T  •b < fail T  •c < fail T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• T

  28. LR(0) parsing The valid item is E  E+T• When you see exactly one item and it is complete, reduce. T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  29. LR(0) parsing How to reduce: Look at the corresponding complete item (i.e. E  E+T•) R.H.S. has k symbols, pop k symbols out of the stack. (i.e. pop ‘E+T’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  30. LR(0) parsing T  •c T  c• E  •a E  a• T  •b T  b•

  31. LR(0) parsing How to reduce: Look at the corresponding complete item (i.e. E  E+T•) R.H.S. has k symbols, pop k symbols out of the stack. (i.e. pop ‘E+T’) Consider the valid items in corresponding state (i.e. items in state 1) Then push the symbol on the L.H.S. to the stack (i.e. push ‘E’) T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b•

  32. LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable notation e q0 S  •a For every item S  •a X A  •X A  X• For every item A  •X e A  •C C  •d For every pair of items A  •C, C  •d

  33. LR(0) parsing: NFA representation • Each of these items is a state E  •E+T E  E•+T E  E+•T E  E+T• T  •c T  c• E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• E  •a E  a• q0 T  •c T  c• T  •b T  b•

  34. LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable notation e q0 S  •a For every item S  •a E  •E+T E  E•+T E  E+•T E  E+T• e E  •a E  a• e q0 e T  •b T  b• e T  •c T  c•

  35. LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable notation X A  •X A  X• For every item A  •X E + T E  •E+T E  E•+T E  E+•T E  E+T• e a E  •a E  a• e q0 e b T  •b T  b• e c T  •c T  c•

  36. LR(0) parsing: NFA representation a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable notation e A  •C C  •d For every pair of items A  •C, C  •d a E  •a E  a• e e E + T E  E+T• E  E+•T E  •E+T E  E•+T e e e q0 e b T  b• T  •b e c T  c• T  •c

  37. LR(0) parsing: NFA representation • Convert the NFA to DFA 2 E  a• a 6 E  E+•T T  •b T  •c 7 3 E  •E+T E  •a T  •b T  •c 1 + T E  E•+T E  E+T• E b b 4 T  b• c c 5 T  c•

  38. LR(0) parsing: remember state in stack • When we reduce, we have to consider the valid items in the corresponding state in the past. How? • Remember the states in the stack instead. 2 E  a• a 6 E  E+•T T  •b T  •c 7 3 E  •E+T E  •a T  •b T  •c 1 + T E  E•+T E  E+T• E b b 4 T  b• c c 5 T  c•

  39. LR(0) parsing: remember state in stack 2 E  a• a 6 E  E+•T T  •b T  •c 7 3 E  •E+T E  •a T  •b T  •c 1 + T E  E•+T E  E+T• E b b 4 T  b• c c 5 T  c•

  40. End • Questions?

More Related