Back to the 2023 paper

Module 2: Syntax Analysis (Parser)

20235m

What is handle? Consider the grammar:
E→E+E∣E∗E∣idE \to E + E | E * E | id
Find the handles of the right sentential forms of the reduction for the string id+id∗idid + id * id.

Worked SolutionAI Assisted

Handle; reduction of id + id * id with grammar E→E+E∣E∗E∣idE\to E+E \mid E*E \mid \text{id}

What is a handle?

A handle of a (right) sentential form is a substring that matches the right-hand side of some production, and whose reduction to the left-hand-side nonterminal is a step in a rightmost (reverse) derivation — that is, the rightmost handle that, when reduced, produces a valid step toward the start symbol.

In shift–reduce parsing we repeatedly prune handles: shift symbols onto the stack until the top stack symbols match a production RHS, then reduce (replace the handle by its nonterminal).

Find the handles for id + id * id

Grammar: E→E+E∣E∗E∣idE\to E+E \mid E*E \mid id. The (unique, rightmost) reduction sequence:

Stack (top shown last) Input Action Handle reduced
id + id * id shift id —
E + id * id reduce id → E handle id
E + id * id shift + —
E + id * id shift id —
E + E * id reduce handle id (2nd)
E + E * id shift * —
E + E * id (empty) shift —
E + E * E reduce handle id (3rd)
E + E reduce E*E → E handle E*E
E reduce E+E → E handle E+E

Handles (in reduction order), with the actual substrings:

  • id (first id on the left)
  • id (the middle id)
  • id (the id after *)
  • E * E (the redex E*E → E)
  • E + E (the redex E+E → E)

Written against the original string, the reduction is a reverse of the rightmost derivation:
id+id∗id  →reduce idE+id∗id→reduce idE+E∗id→E+E∗E→E+E→Eid + id * id \;\xrightarrow{\text{reduce id}} E + id*id \xrightarrow{\text{reduce id}} E+E*id \xrightarrow{} E+E*E \xrightarrow{E+E} \xrightarrow{E}
The seeds/handles reduced, going left-to-right on the string: the *ids**, then EE, then E + E`.

Similar questions

Student FinanceWeb Platform

Pocketly — Personal Finance for Students

Keep your hostel and college expenses under control. Track accounts, log spending in seconds, and stay within your monthly budget.