Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is Handle?
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.
∴ $ <. id>1. > +<. id>2 >*<. id>3 > $
Apply 3 steps as explained above on the string.
| String | Handle | Production Used |
|---|---|---|
| <. id>1. > +<. id>2. >*<. id>3. > $ | <. id>1. > | E → id |
| $ E+<. id>2. >*<. id>3. > $ | <. id>2. > | E → id |
| $ E + E *<. id>3. > $ | <. id>3. > | E → id |
| $ E + E * E $ | Remove all Non-Terminals | |
| $ + * $ | Insert precedence relation between operators | |
| $ <.> $ | <.> i. e. , E * E | E → E * E |
| $ <.> $ | <.> i. e. , E + E | E → 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-Terminal | FIRST Terminal | LAST 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 String | Description | |
|---|---|---|---|
| $ | <.> | id + id * id $ | $ <. id shift> |
| $ <. id> | .> | +id * id $ | is Handle, id . > +, Reduce id, F → id1 |
| $F | <.> | +id * id $ | $ <. shift> |
| $F + | <.> | id * id $ | + <. id shift> |
| $F+<. id> | .> | id $ | <. id .> is Handle, Reduce id to F, F → id |
| $F + F | <.> | id $ | + <. shift> |
| $F + F * | <.> | id $ | * <. id shift> |
| $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 |
