What is OPG?


OPG stands for Operator Precedence Grammar. Grammar with the later property is known as operator precedence grammar. It is ε −free Operator Grammar in which precedence relation <., =. and .> are disjoint, i.e., If a . > b exists, then b .> a will not exist.

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.

Example2 − Check whether the following Grammar is OPG or not

E → E + E|E ∗ E|(E)|id

Solution

Comparing R.H.S of production E → E + E with α a A β

ΑAAβ
E+Eε

& further, A → γ b $ is compared with E → E + E.

A →Γb$
E →E+E

So, applying the rule (2), we get a <.b which means.

+ <. +……………………………………….(1)

Comparing E → E + E with α A b β

ΑΑbβ
EE+E

& further A → γ a $ is compared with E → E + E.

A →Γa$
E →E+E

So, applying rule (3), we get a .> b which means

+ . > +………………………………(2)

From Relation (1), we have + <. +

From Relation (2), we have + . > +

∴ Precedence relations are ambiguous and not unique. Therefore given Operator Grammar is not OPG.

Example3 − Check whether the following Grammar is OPG or not

E → E + T|T

T → T ∗ F|F

F → (E)|id

Solution

Comparing E + T with α a A β.

ΑaAβ
E+Tε

∴ a = +, A = T & further comparison A → γ b $ with T → T * F

A →γb$
T →T*F

∴ b = *

So, applying the rule (2), we get a <.b which means.

+ <. +……………………………………….(1)

Substituting E → T in Production E → E + T, we get

E → T + T

Comparing T + T with α A b β

ΑAbβ
ΕT+T

∴ b = +, A = T

Further comparing A → γ a $ with T → T * F.

Α →γa$
Ε →T*F

So, applying the rule (3), we get a .> b that means

+ . > +………………………………(2)

From Relation (1),                                     we have + <. +

From Relation (2),                                      we have + . > +

So, both relations show that the multiply operator (*) has more precedence than the plus operator (+).

Therefore, there is no ambiguity.

Hence, Grammar is OPG.

Updated on: 30-Oct-2021

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements