The Wayback Machine - https://web.archive.org/web/20230816065643/https://www.geeksforgeeks.org/what-is-handle-pruning/
Skip to content
Related Articles
Open In App
Related Articles

What is Handle Pruning?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

Example:

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:

  • Production
  • Position

Example:  

S -> aABe
A -> Abc | b
B -> d

Steps:

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.

Handle Pruning:

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.

Example 1:

Right Sequential FormHandleReducing Production
id + id * ididE ⇒ id
E + id * ididE ⇒ id
E + E * ididE ⇒ id
E + E * EE + EE ⇒ E + E
E * EE * EE ⇒ E * E
E (Root)  

Example 2:

Right Sequential FormHandleProduction
id + id + ididE ⇒ id
E + id + ididE ⇒ id
E + E + ididE ⇒ id
E + E + EE + EE ⇒ E + E
E + EE + EE ⇒ E + E
E (Root)  
Last Updated : 18 Jan, 2022
Like Article
Save Article
Similar Reads
Related Tutorials