Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3130: Formal languages and automata theory Tutorial 6

Similar presentations


Presentation on theme: "CSCI 3130: Formal languages and automata theory Tutorial 6"— Presentation transcript:

1 CSCI 3130: Formal languages and automata theory Tutorial 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

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

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

6 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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. state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift

7 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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 a state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a

8 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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. state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a E  a• a•+b+c Reduce

9 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a E  a• a•+b+c Reduce E

10 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• E  •E+T E  •a < fail T  •b < fail T  •c < fail E  E•+T E  a• T  •b T  •c E state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T

11 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• The valid item is: E  E•+T No complete items, shift. state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift

12 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• Read an input and push it in the stack. a+•b+c E  E•+T E  E+•T + state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift 3 E+ a+•b+c

13 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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. state stack valid items reading action 2 a E  a• a•+b+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift 3 E+ E  E+•T, T  •b, T  •c a+•b+c

14 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• Read an input and push it in the stack. a+b•+c E  E+•T < fail T  •b T  •c < fail T  b• b state stack valid items reading action 3 E+ E  E+•T, T  •b, T  •c a+•b+c Shift 4 E+b

15 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• The valid item is T  b• When you see exactly one item and it is complete, reduce. state stack valid items reading action 3 E+ E  E+•T, T  •b, T  •c a+•b+c Shift 4 E+b T  b• a+b•+c Reduce

16 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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’) state stack valid items reading action 3 E+ E  E+•T, T  •b, T  •c a+•b+c Shift 4 E+b T  b• a+b•+c Reduce E+T

17 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• E  E+•T T  •b < fail T  •c < fail E  E+T• T  •b < fail T  •c < fail T state stack valid items reading action 4 E+b T  b• a+b•+c Reduce E+ E  E+•T, T  •b, T  •c E+T E  E+T•

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

19 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 4 E+T E  E+T• Reduce E

20 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• E  •E+T E  •a < fail T  •b < fail T  •c < fail E  E•+T E  a• T  •b T  •c E state stack valid items reading action 4 E+T E  E+T• a+b•+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift

21 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• The valid item is: E  E•+T No complete items, shift. state stack valid items reading action 4 E+T E  E+T• a+b•+c Reduce e E  •E+T, E  •a, T  •b, T  •c E E  E•+T Shift

22 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• Read an input and push it in the stack. a+b+•c E  E•+T T  E+•T + state stack valid items reading action E E  E•+T a+b•+c Shift 5 E+ a+b+•c

23 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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. state stack valid items reading action E E  E•+T a+b•+c Shift 5 E+ E  E+•T, T  •b, T  •c a+b+•c

24 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• Read an input and push it in the stack. a+b+c• E  E+•T < fail T  •b < fail T  •c T  c• c state stack valid items reading action 4 E E  E•+T a+b•+c Shift 5 E+ E  E+•T, T  •b, T  •c a+b+•c

25 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• The valid item is T  c• When you see exactly one item and it is complete, reduce. state stack valid items reading action 5 E+ E  E+•T, T  •b, T  •c a+b+•c Shift 6 E+c T  •c a+b+c• Reduce

26 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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’) state stack valid items reading action 6 E+c T  •c a+b+c• Reduce E+ E  E+•T, T  •b, T  •c E+T

27 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• E  E+•T T  •b < fail T  •c < fail E  E+T• T  •b < fail T  •c < fail T state stack valid items reading action 6 E+c T  •c a+b+c• Reduce E+ E  E+•T, T  •b, T  •c E+T

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

29 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 6 E+ E  E+•T, T  •b, T  •c E+T E  E+T• Reduce

30 LR(0) parsing T  •c E  •a T  c• E  a• T  •b T  b• state stack
valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 2 a E  a• a•+b+c Reduce E E  E•+T 3 E+ E  E+•T, T  •b, T  •c a+•b+c 4 E+b T  b• a+b•+c E+T E  E+T• 5 a+b+•c 6 E+c T  •c a+b+c• E  •a E  a• T  •c T  c• T  •b T  b•

31 LR(0) parsing E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c
T  •b T  b• 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’) state stack valid items reading action 1 e E  •E+T, E  •a, T  •b, T  •c •a+b+c Shift 6 E+ E  E+•T, T  •b, T  •c E+T E  E+T• Reduce

32 LR(0) parsing: NFA representation
a, b: terminals A, B, C: variables a, b, d: mixed strings X: terminal or variable e notation 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
E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •c T  c• T  •b T  b• Each of these items is a state E  •E+T E  E•+T E  E+•T E  E+T• 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 e notation 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 E  •E+T E  E•+T E  E+•T E  E+T• E  •a E  a• T  •b T  b• T  •c T  c• q0 e E + T a b c

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

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. E  •E+T E  •a T  •b T  •c E  E•+T E  E+•T E  E+T• E  a• T  b• T  c• E + T a b c 1 2 3 4 5 6 7

39 LR(0) parsing: remember state in stack
E  •E+T E  •a T  •b T  •c E  E•+T E  E+•T E  E+T• E  a• T  b• T  c• E + T a b c 1 2 3 4 5 6 7 Stack State Reading Action 1 •a+b+c S 2 a•+b+c R 3 13 6 a+b•+c 136 4 7 a+b+•c 5 a+b+c•

40 End Questions?


Download ppt "CSCI 3130: Formal languages and automata theory Tutorial 6"

Similar presentations


Ads by Google