- Trending Categories
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 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 <. q | p has less precedence than q. |
p >. q | p has more precedence than q. |
p =. q | p 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.
- Related Articles
- What is Operator Precedence Parsing Algorithm in compiler design?
- What is the operator precedence in C#?
- What is C Operator Precedence and Associativity?
- PHP Operator Precedence
- According to Java Operator precedence, which operator has the highest precedence?
- What are Precedence Relations in Operator Grammar?
- Operator Precedence and Associativity in C
- Can we change operator precedence in Python?
- What are LEADING and TRAILING operation of an operator precedence grammar?
- How Can MySQL operator precedence affect result set?
- Explain Operator grammar and precedence parser in TOC
- Evaluating a mathematical expression considering Operator Precedence in JavaScript
- What is Top-Down Parsing?
- What is Bottom-up Parsing?
- How does the precedence of || operator depend on PIPES_AS_CONCAT SQL mode?
