What is Operator Precedence Parsing?


Operator Precedence Parsing is also a type of Bottom-Up Parsing that can be used to a class of Grammars known as Operator Grammar.

A Grammar G is Operator Grammar if it has the following properties −

  • Production should not contain ϵ on its right side.

  • There should not be two adjacent non-terminals at the right side of production.

Example1 − Verify whether the following Grammar is operator Grammar or not.

E → E A E |(E)|id

A → +| − | ∗

Solution

No, it is not an operator Grammar as it does not satisfy property 2 of operator Grammar.

As it contains two adjacent Non-terminals on R.H.S of production E → E A E.

We can convert it into the operator Grammar by substituting the value of A in E → E A E.

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

Operator Precedence Relations

Three precedence relations exist between the pair of terminals.

                  Relation                        Meaning
                    p <. qp has less precedence than q.
                   p >. qp has more precedence than q.
                   p =. qp has equal precedence than q.

Depending upon these precedence Relations, we can decide which operations will be executed or parsed first.

Association and Precedence Rules

  • If operators have different precedence

Since * has higher precedence than +

Example

In a statement a + b * c

∴ + <. *

In statement a * b + c

∴ ∗ . > +

  • If operators have Equal precedence, then use Association rules.

(a) Example minus; In statement a + b + c here + operators are having equal precedence.

As '+' is left Associative in a + b + c

∴ (a + b) will be computed first, and then it will be added to c.

i.e., (a + b) + c

+ .> +

Similarly, '*' is left Associative in a * b * c

(b) Example − In a statement a ↑ b ↑ c here, ↑ is the Right Associative operator

∴ It will become a ↑ (b ↑ c)

∴ (b ↑ c) will be computed first.

∴ ↑<. ↑

  • Identifier has more precedence then all operators and symbols.
∴ θ <. id           $ <. id
id . > θ            id . > $
id . >)
(<. id.
  • $ has less precedence than all other operators and symbols.
$ <.           ( id . > $
$ <. +         ). > $
$ <.*

Example2 − Construct the Precedence Relation table for the Grammar.

E → E + E | E ∗ E/id

Solution

Operator-Precedence Relations


Id+*$
Id
.>.>.>
+<..><..>
*<..>.>.>
$<.<.<.

Advantages of Operator Precedence Parsing

  • It is accessible to execute.

Disadvantages of Operator Precedence Parsing

  • Operator Like minus can be unary or binary. So, this operator can have different precedence’s in different statements.

  • Operator Precedence Parsing applies to only a small class of Grammars.

Ginni
Ginni

e

Updated on: 29-Oct-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements