COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 7, 10/09/2003 Prof. Roy Levow
Bottom-up Parsing Almost always automatic generation Because algorithm is complex Uses Push-down Automata Basic principles are can be seen with LR(0) though it is of little practical value “handle” is complete sequence of children that can be “reduced”
LR(0) Using dotted notation A shift item has dot anywhere but end A reduce item has dot at end Item sets are sets of items that can occur in a state
Sample Grammar for LR(0)
Transition Diagram
LR(0) PDA
Example LR(0) Parse
LR Parsing with Lookahead There are a number of variants for LR parsing with lookahead Problem is that tables for direct approaches can be very large 1M for LR(1) 10K for SLR(1) 1K for LALR(1) Smaller size reduces class of languages that can be parsed
Example for LR(1)
LALR(1) Automaton, fig. 2.100
Conflicts & Error Recovery Grammars that are not LR(1) exhibit Shift-Reduce Conflicts Reduce-Reduce Conflicts Adding actions often generates conflicts Error recovery Add symbol in input Pop items from stack and input to recovery point