Shift-Reduce Parsing Example 1 Mr. Lupoli F2012
Mechanics Goal Shift Reduce trace that the given input can be reduced BACK to the original start symbol Shift marker that moves along the input from left to right what is on left needs to be reduced match with production table Reduce apply the inverse production
What’s Given E → int E E + (E) Original Production Syntax to be checked int + (int) + (int)
E → int E E + (E) | int + (int) + (int)
E → int E E + (E) int | + ( int ) + ( int ) E | + ( int ) + ( int ) we have a match!! Reduce E | + ( int ) + ( int )
E → int E E + (E) E + | ( int ) + ( int ) no match shift
E → int E E + (E) E + ( | int ) + ( int ) no match shift
E → int E E + (E) E + ( int | ) + ( int ) E + ( E | ) + ( int ) match !!! Reduce E + ( E | ) + ( int )
E → int E E + (E) E + ( E ) | + ( int ) match!! Reduce E | + ( int )
E → int E E + (E) E + | ( int ) no match, shift
E → int E E + (E) E + ( | int ) no match, shift
E → int E E + (E) E + ( int | ) found a match!! Reduce!! E + ( E | )
E → int E E + (E) E + ( E ) | found a match!! Reduce!! E
E → int E E + (E) E starting production!!! ACCEPT!!!
In total int + (int) + (int)$ shift int + (int) + (int)$ red. E int E + (int) + (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 $ accept
Sources CS 671 – University of Virginia