- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain Operator grammar and precedence parser in TOC
If the grammar satisfies the following two conditions, then we can say that type of grammar is called as operator precedence grammar.
If ε is on its RHS then there exists no production rule.
If two non-terminals are adjacent to each other on its RHS then there exists no production rule.
Operator Grammars have the property that no production right side is empty or has two adjacent non-terminals.
E-> E A E | id
A-> + | *
The above grammar is not an operator grammar but we can convert that grammar into operator grammar like −
E-> E + E | E * E | id
There are three precedence relations, which are given below −
|a ⋖ b||a yields precedence to b|
|a = · b||a has the same precedence as b|
|a ⋗ b||atakes precedence over b|
The input string is as follows −
id1 + id2 * id3
After inserting precedence relations is−
$ <· id1 ·> + <· id2 ·> * <· id3 ·> $
Scan the string from left until seeing ·> and put a pointer.
Scan backwards the string from right to left until seeing <·
Everything between the two relations <· and ·> forms the handle.
Replace handle with the head of the production.
Operator Precedence Parsing Algorithm
The algorithm is as follows −
Initialize: Set P to point to the first symbol of the input string w$
Repeat − Let b be the top stack symbol, a is the input symbol pointed to by P.
if (a is $ and b is $)
if a ·> b or a =· b then
push a onto the stack
advance P to the next input symbol
else if a <· b then
c -> pop the stack
until (c .> stack-top)
Construct a graph using the algorithm. This graph is as follows −
By seeing this, we can extract the precedence function like −
- What are Precedence Relations in Operator Grammar?
- Explain Type-0 grammar in TOC
- Explain Type-1 grammar in TOC
- Explain the relationship between grammar and language in TOC
- Explain Type-2 and Type-3 Grammar in TOC?
- Explain the concept of grammar in TOC
- What are LEADING and TRAILING operation of an operator precedence grammar?
- Explain about left linear regular grammar in TOC
- Explain the simplification of context free grammar in TOC
- Operator Precedence and Associativity in C
- PHP Operator Precedence
- What is unambiguous grammar in TOC?
- According to Java Operator precedence, which operator has the highest precedence?
- What is C Operator Precedence and Associativity?
- What do you mean by grammar and production in TOC?