400 likes | 432 Views
Learn about LR(0) parsing, including item validity, reducing techniques, address R/R and S/R conflicts using examples. Master LR(0) parsing efficiently.
E N D
CSCI 3130: Formal languagesand automata theoryTutorial 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 aA•b Or expanding the ‘A’ (think of aaAbb) • A •aAb • A a•Ab
LR(0) parsing • E E+T | a • T b | c
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•
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•
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
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•
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•
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
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•
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• +
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•
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
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•
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•
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
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•
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•
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
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•
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• +
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•
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
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•
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•
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
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•
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•
LR(0) parsing T •c T c• E •a E a• T •b T b•
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•
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
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•
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•
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•
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
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•
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•
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•
End • Questions?