Handles in Right-Sentential Forms for Grammar
In bottom-up (shift-reduce) parsing, the parser repeatedly reduces a substring that is a handle of the current right-sentential form . Intuitively, a handle is exactly the piece of the current sentential form that was produced most recently by a rightmost derivation; reducing it simulates reversing that last derivation step. This “rightmost derivation in reverse” viewpoint is standard in LR parsing theory. In LR parsing, the notion is formalized by splitting a right-sentential form into , where is the handle and is the viable prefix context.
For the grammar:
and the input string:
we will find the handles of the right sentential forms that appear during the reduction sequence for that specific string. The key idea is: each reduction replaces the RHS by its LHS in one step, so the handle is the that matches some production .
Footnotes
-
LR parser - Wikipedia - Discusses right-sentential forms, viable prefixes, and defines the handle β within LR parsing theory. ↩ ↩2
Handles and Viable Prefixes | Easiest Explanation | Compiler Design (GATE 2005) | Parser | Part-1
Keyword anchors
- handle
- right-sentential form
- rightmost derivation
- viable prefix
These terms are tightly connected: LR parsing uses viable prefixes built from right-sentential forms, and a reduction occurs when the handle (RHS of a production) can be reduced to the corresponding nonterminal.
Footnotes
-
LR parser - Wikipedia - Discusses right-sentential forms, viable prefixes, and defines the handle β within LR parsing theory. ↩
Compute the reduction sequence for
A right-sentential form is reduced by choosing a substring such that is the RHS of some production , and replacing with in . The handle is precisely that .
We will use the natural operator structure implied by the grammar reductions for the specific input:
- forms one
- then forms another
We list the right sentential forms during the bottom-up reduction, from terminals toward the start symbol . (This is the reverse of a rightmost derivation.) The handles are marked at each step.
Footnotes
-
LR parser - Wikipedia - Discusses right-sentential forms, viable prefixes, and defines the handle β within LR parsing theory. ↩
Finding handles by reverse rightmost reductions
- 1Step 1
Current right-sentential form is .
- 2Step 2
content: "In the substring , the production matches after previously reducing ’s to (which are done in the next steps if needed). For handle identification on the current right-sentential form, the next reducible RHS instance is the that must become ; thus we start by reducing each as a handle of the form .
- 3Step 3
content: "Right-sentential form at this moment contains an which matches , so the handle is and the replacement is .
- 4Step 4
Again use : handle is , replace it with .
- 5Step 5
After both tokens participating in have been reduced, the substring matching is the handle. Replace .
- 6Step 6
Finally, with the entire expression shaped as , the substring is the handle. Replace .
The handles (explicitly stated)
A clean way to present handles is to write a concrete bottom-up reduction chain (each step reduces one handle to its ):
Consider the following reduction sequence (each “” is one reduction step):
-
- handle: using production
- result:
-
- handle: (the left operand of ) using
- result:
-
- handle: (the right operand of ) using
- result:
-
- handle: using
- result:
-
- handle: using
- result:
Thus, the handles of the right sentential forms during the reduction are, in order:
This matches the LR “reduce the handle” idea: at each step, the handle is the RHS of a production that can be reduced in reverse of a rightmost derivation.
Footnotes
-
LR parser - Wikipedia - Discusses right-sentential forms, viable prefixes, and defines the handle β within LR parsing theory. ↩
type="tip" title="Pro Tip: How to spot a handle quickly" content="On a current right-sentential form, scan for a substring that is exactly the RHS of one production (here: , , or ). That RHS instance (at the correct position) is the handle reduced in one step in the bottom-up process. Handles are defined relative to right-sentential forms, not arbitrary substrings. "
Footnotes
-
LR parser - Wikipedia - Discusses right-sentential forms, viable prefixes, and defines the handle β within LR parsing theory. ↩
type="warning" title="Warning: Handles are not “any reducible-looking RHS”" content="Even if a substring resembles or , it must correspond to a single reverse step of a rightmost derivation in that specific right-sentential form. LR parsing formalizes this via viable prefixes and the handle definition within right-sentential forms. "
Footnotes
-
LR parser - Wikipedia - Discusses right-sentential forms, viable prefixes, and defines the handle β within LR parsing theory. ↩
Reduction roadmap for
id + id * id
Form 1Handle reduced: → ."
E + id * id
Form 2Handle reduced: → ."
E + E * id
Form 3Handle reduced: → ."
E + E * E
Form 4Handle reduced: → ."
E + E
Form 5Handle reduced: → (accept)."
Handles encountered in reduction order
Each bar corresponds to one reduction step's handle β
Quick conceptual checks
Handles & right-sentential forms (self-test)
Knowledge Check
In LR parsing, the handle of a right-sentential form is (roughly) the substring that corresponds to:
Explore Related Topics
Syntax-Directed Translation: Infix to Prefix Notation
The module shows how a syntax‑directed translation scheme using only synthesized attributes can convert infix arithmetic expressions into prefix (Polish) notation while preserving operator precedence and left‑associativity.
- Grammar: E → E + T | E - T | T; T → T * F | F; F → digit, enforcing precedence ( * > + / - ).
- Semantic actions compute a
valstring for each non‑terminal, concatenating the operator before its operand strings. - Example results:
9 - 5 + 2→+ - 9 5 2;9 - 5 * 2→- 9 * 5 2. - Synthesized (S‑attributed) attributes allow immediate bottom‑up evaluation during LR‑style parsing.
- Left‑recursive rules enable left‑associativity; to use LL parsers the grammar must be transformed and inherited attributes introduced.
Finding the Key (Candidate Key) for Relation \(R(E,F,G,H,I,J,K,L,M,N)\)
Ambiguous Grammars in Formal Language Theory: Choosing the Correct Option