- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 β
Α | A | A | β |
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 | β |
E | E | + | 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 β.
Α | a | A | β |
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 β
Α | A | b | β |
Ε | 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.