What is Handle?

Compiler DesignProgramming LanguagesComputer Programming

A handle is a substring that connects a right-hand side of the production rule in the grammar and whose reduction to the non-terminal on the left-hand side of that grammar rule is a step along with the reverse of a rightmost derivation.

Finding Handling at Each Step

Handles can be found by the following process −

  • It can scan the input string from left to right until first .> is encountered.

  • It can scan backward until <. is encountered.

  • The handle is a string between <. and .>

Example1 − Consider the Grammar

E → E + E|E * E|(E)id.

Find handles at each step for reducing the string id1 + id2 * id3 using operator Precedence Parsing.

Solution

First, Attach $ at starting and ending of string i.e., $id1 + id2 * id3 $.

Put Precedence relation between operators & symbols using Precedence Relation Table.

$ <. id1. > +<. id2 >*<. id3 > $

Apply 3 steps as explained above on the string.

StringHandleProduction Used
<. id1. > +<. id2. >*<. id3. > $<. id1. >E → id
$ E+<. id2. >*<. id3. > $<. id2. >E → id
$ E + E *<. id3. > $<. id3. >E → id
$ E + E * E $Remove all Non-Terminals
$ + * $Insert precedence relation between operators
$ <. +<.*. > $<.*. > i. e. , E * EE → E * E
$ <. +. > $<. +. > i. e. , E + EE → E + E
$

Example2 − Compute FIRST & LAST terminals of non-terminals E, T, and F in the following Grammar.

E → E + T| T

T → T * F | F

F → (E)| id

Solution

On seeing the production, we can judge

+ is the first terminal of E

* is the first terminal of T

(, id is the first terminals of F.

But E → T → F

∴ The First Terminal of F is contained in the First terminal of T and the First Terminal of T is contained in the First Terminal of E.

∴ First(F) = {(, id}

∴ First(T) =*∪ First (F) = {*, (, id}

First(E) = + ∪ First (T) = {+,*, (, id}

Similarly, the Last Terminals can be found.

Non-TerminalFIRST TerminalLAST Terminal
F(, id), id
T*, (, id*, ), id
E+,*, (, id+,*, ), id.

Example3 − Consider the Grammar

E → E + T|T

T → T ∗ F|F

F → (E)| id

Perform Stack Implementation for string id + id * id using operator precedence parsing.

Solution

Precedence rules <. , .> or =. holds between the top of the stack and the current input symbol. If the top of the stack is Non-terminal, then the terminal below the top of the stack will be compared with the current input symbol.

Stack
Input StringDescription
$<.id + id * id $$ <. id, shift id
$ <. id.>+id * id $< id > is Handle, id . > +, Reduce id, F → id1
$F<.+id * id $$ <. +, shift +
$F +<.id * id $+ <. id, shift id
$F+<. id.>id $<. id . > is Handle, Reduce id to F, F → id
$F + F<.id $+ <.*, shift *
$F + F *<.id $* <. id, shift id
$F + F *<. id.>$<. id . > is handle, reduce id to F, F → id
$F + F * F
$Remove all non-terminals. As they do not participate in precedence relations.
$ +*.>$Insert Precedence Relations
$ <. +<.*.>$* . > $, Reduce <.*. >
$ <. +.>$+ . > $, <. + . > is Handle, Reduce <. + . >
$
$Accept
raja
Published on 29-Oct-2021 11:50:55
Advertisements