Bottom Up Parsing Shift Reduce CS 671 January
Bottom Up Parsing – Shift Reduce CS 671 January 31, 2008
1 Bottom Up Parsing – Shift Reduce
Shift-Reduce Notation Split input into two substrings • Right substring (a string of terminals) is as yet unexamined by parser • Left substring has terminals and non-terminals The dividing point is marked by a • The is not part of the string Initially, all input is unexamined: x 1 x 2. . . xn 2 CS 671 – Spring 2008
Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift and Reduce Shift: Move one place to the right • Shifts a terminal to the left string E + ( int ) E + (int ) Reduce: Apply an inverse production at the right end of the left string • If E E + ( E ) is a production, then E + ( E ) ) E +(E ) 3 CS 671 – Spring 2008
Shift-Reduce Example int + (int)$ shift, since no match E int E E + (E) int + ( int ) + ( 4 CS 671 – Spring 2008 int )
Shift-Reduce Example int + (int)$ shift int + (int)$ red. E int E E + (E) *** red. == reduce *** int + ( int ) + ( 5 CS 671 – Spring 2008 int )
Shift-Reduce Example int + (int)$ shift int + (int)$ red. E int E + (int)$ shifts 3 times E int E E + (E) E int + ( int ) + ( 6 CS 671 – Spring 2008 int )
Shift-Reduce Example int + (int)$ shift int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E E + (E) E int + ( int ) + ( 7 CS 671 – Spring 2008 int )
Shift-Reduce Example int + (int)$ shift int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E + (E ) + (int)$ shift E int E E + (E) E E int + ( int ) + ( 8 CS 671 – Spring 2008 int )
Shift-Reduce Example E int E E + (E) int + (int)$ shift int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E + (E ) + (int)$ shift E + (E) + (int)$ red. E E + (E) E E int + ( int ) + ( 9 CS 671 – Spring 2008 int )
Shift-Reduce Example E int E E + (E) int + (int)$ shift int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E + (E ) + (int)$ shift E + (E) + (int)$ red. E E + (E) E + (int)$ E shift 3 times E E int + ( int ) + ( 10 CS 671 – Spring 2008 int )
Shift-Reduce Example E int E E + (E) int + (int)$ shift int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E + (E ) + (int)$ shift E + (E) + (int)$ red. E E + (E) E + (int)$ E + (int )$ E shift 3 times red. E int E E int + ( int ) + ( 11 CS 671 – Spring 2008 int )
Shift-Reduce Example E int E E + (E) int + (int)$ shift int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E + (E ) + (int)$ shift E + (E) + (int)$ red. E E + (E) E + (int)$ E shift 3 times E + (int )$ red. E int E + (E )$ shift E E int + ( int ) + ( 12 CS 671 – Spring 2008 E int )
Shift-Reduce Example E int E E + (E) int + (int)$ shift int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E + (E ) + (int)$ shift E + (E) + (int)$ red. E E + (E) E + (int)$ E shift 3 times E + (int )$ red. E int E + (E )$ shift E + (E) $ red. E E + (E) E E int + ( int ) + ( 13 CS 671 – Spring 2008 E int )
Shift-Reduce Example E int E E + (E) int + (int)$ shift E int + (int)$ red. E int E + (int)$ shift 3 times E + (int ) + (int)$ red. E int E + (E ) + (int)$ shift E + (E) + (int)$ red. E E + (E) E + (int)$ shift 3 times E + (int )$ red. E int E + (E )$ shift E + (E) $ red. E E + (E) E $ 14 E accept E E int + ( int ) + ( CS 671 – Spring 2008 E int )
How do we keep track? Left part string implemented as a stack • Top of the stack is the • Shift: – Pushes a terminal on the stack • Reduce: – Pops 0 or more symbols off of the stack – Symbols are right-hand side of a production – Pushes a non-terminal on the stack (production LHS) 15 CS 671 – Spring 2008
Implementation: LR Parsing Table input (terminal) symbols state next action state next state Action table Goto table Used at every step to decide whether to shift or reduce Used only when reducing, to determine next state a 16 non-terminal symbols X ▪ CS 671 – Spring 2008 X
Shift-Reduce Parsing Table terminal symbols non-terminal symbols state Action table next actions next state on red’n 1. shift and goto state n 2. reduce using X → γ – pop symbols γ off stack – using state label of top (end) of stack, look up X in goto table and goto that state • DFA + stack = push-down automaton (PDA) 17 CS 671 – Spring 2008
List Grammar Parsing Table ( 1 s 3 2 S→id 3 s 3 ) id , s 2 S→id S S→id s 2 g 7 accept 5 s 6 s 8 6 S→(L) S→(L) 7 L→S L→S L→S 8 s 3 9 L→L, S s 2 L→L, S L g 4 4 18 $ g 9 L→L, S CS 671 – Spring 2008 L→L, S g 5
- Slides: 19