What is Handle Pruning?
The handle is the substring that matches the body of a production whose reduction represents one step along with the reverse of a Rightmost derivation.
The handle of the right sequential form Y is the production of Y where the string S may be found and replaced by A to produce the previous right sequential form in RMD(Right Most Derivation) of Y.
Sentential form: S => a here, ‘a’ is called sentential form, ‘a’ can be a mix of terminals and nonterminals.
Consider Grammar : S -> aSa | bSb | ε Derivation: S => aSa => abSba => abbSbba => abbbba
Left Sentential and Right Sentential Form:
- A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.
- A right-sentential form is a sentential form that occurs in the rightmost derivation of some sentence.
Handle contains two things:
S -> aABe A -> Abc | b B -> d
abbcde : γ = abbcde , A->b; Handle = b aAbcde : γ = RHS = aAbcde , A->Abc; Handle = Abc aAde : γ = aAde , B->d; Handle = d aABe : γ = aABe, S-> aABe; Handle = aABe
Note- Handles are underlined in the right-sentential forms.
Is the leftmost substring always handled?
No, choosing the leftmost substring as the handle always, may not give correct SR(Shift-Reduce) Parsing.
Removing the children of the left-hand side non-terminal from the parse tree is called Handle Pruning.
A rightmost derivation in reverse can be obtained by handle pruning.
Steps to Follow:
- Start with a string of terminals ‘w’ that is to be parsed.
- Let w = γn, where γn is the nth right sequential form of an unknown RMD.
- To reconstruct the RMD in reverse, locate handle βn in γn. Replace βn with LHS of some An ⇢ βn to get (n-1)th RSF γn-1. Repeat.
|Right Sequential Form||Handle||Reducing Production|
|id + id * id||id||E ⇒ id|
|E + id * id||id||E ⇒ id|
|E + E * id||id||E ⇒ id|
|E + E * E||E + E||E ⇒ E + E|
|E * E||E * E||E ⇒ E * E|
|Right Sequential Form||Handle||Production|
|id + id + id||id||E ⇒ id|
|E + id + id||id||E ⇒ id|
|E + E + id||id||E ⇒ id|
|E + E + E||E + E||E ⇒ E + E|
|E + E||E + E||E ⇒ E + E|