Handle
. Handles always appear at the top of the stack and never inside it
. This makes stack a suitable data structure
. Consider two cases of right most derivation to verify the fact that handle appears on the top of the stack
- S
a Az
aß Byz
aßγyz
- S
a BxAz
a Bxyz
aγxyz
. Bottom up parsing is based on recognizing handles
There are two problems that must be solved if we are to parse by handle pruning. The first is to locate the substring to be reduced in a right sentential form, and the second is to determine what production to choose in case there is more than one production with that substring on the right side.
A convenient way to implement a shift-reduce parser is to use a stack to hold grammar symbols and an input buffer to hold the string w to be parsed. We use $ to mark the bottom of the stack and also the right end of the input. Initially, the stack is empty, and the string w is on the input, as follows:
The parser operates by shifting zero or more input symbols onto the stack until a handle ß is on top of the stack. The parser then reduces ß to the left side of the appropriate production. The parser repeats this cycle until it has detected an error or until the stack contains the start symbol and the input is empty:
After entering this configuration, the parser halts and announces successful completion of parsing.
There is an important fact that justifies the use of a stack in shift-reduce parsing: the handle will always appear on top of the stack, never inside. This fact becomes obvious when we consider the possible forms of two successive steps in any rightmost derivation. These two steps can be of the form:
S
a Az
aß Byz
aßγ yz
S
a BxAz
a Bxyz
aγ xyz