![]() |
VOOZH | about |
In this, we will cover the overview of Operator Precedence Parser and mainly focus on the role of Operator Precedence Parser. And will also cover the algorithm for the construction of the Precedence function and finally will discuss error recovery in operator precedence parsing. Let's discuss it one by one.
Introduction :
Operator Precedence Parser constructed for operator precedence grammar. Operator precedence grammar is a grammar that doesn't contain epsilon productions and does not contain two adjacent non-terminals on R.H.S. of any production. Operator precedence grammar is provided with precedence rules. Operator Precedence grammar could be either ambiguous or unambiguous.
Operator Precedence Parser Algorithm :
1. If the front of input $ and top of stack both have $, it's done else 2. compare front of input b with ⋗ if b! = '⋗' then push b scan the next input symbol 3. if b == '⋗' then pop till ⋖ and store it in a string S pop ⋖ also reduce the popped string if (top of stack) ⋖ (front of input) then push ⋖ S if (top of stack) ⋗ (front of input) then push S and goto 3
Components Operator Diagram :
Example -
Let's take an example to understand the role of operator precedence as follows.
E-> E+T/T T-> T*V/V V->a/b/c/d string= "a+b*c*d"
Implementation of the above algorithm for the string "a+b*c*d" as follows.
| Stack | Input | Stack Top | Current Input | Action |
|---|---|---|---|---|
| $ | a+b*c*d$ | $ | a | shift a |
| $a | +b*c*d$ | a | + | reduce using V->a |
| $V | +b*c*d$ | V | + | reduce using T->V |
| $T | +b*c*d$ | T | + | reduce using E->T |
| $E | +b*c*d$ | E | + | shift + |
| $E+ | b*c*d$ | b | * | reduce using V->b |
| $E+V | b*c*d$ | V | * | reduce using T->V |
| $E+T | *c*d$ | T | * | shift * |
| $E+T* | c*d$ | * | c | shift c |
| $E+T*c | *d$ | c | * | reduce using V->c |
| $E+T*V | *d$ | V | * | reduce using T->T*V |
| $E+T | *d$ | T | * | shift * |
| $E+T* | d$ | * | d | shift d |
| $E+T*d | $ | d | $ | reduce using V->d |
| $E+T*V | $ | V | $ | reduce using T->T*v |
| $E+T | $ | T | $ | reduce using E->E+T |
| $T | $ | E | $ | accept |
Algorithm for construction of Precedence function :
Example -
Let's take an example to understand the construction of precedence function as follows.
E -> E + E/E * E/( E )/id
Here, you will see the Operator precedence relation table and Precedence Relations Graph diagram. Let's have a look.
As we can see no cycle there is no cycle in the graph, we can make this function table as follows.
Columns Representation function :
Columns represent function Ya and Rows represent function Xa -
It is calculated by taking the longest path from Xid to X$ and Yid to Y$.
fid -> g* -> f+ ->g+ -> f$ gid -> f* -> g* ->f+ -> g+ ->f$
Error Recovery in Operator-Precedence Parsing :