- 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 Algorithm in compiler design?
Any string of Grammar can be parsed by using stack implementation, as in shift Reduce parsing. But in operator precedence parsing shifting and reducing is done based on Precedence Relation between symbol at the top of stack & current input symbol of the input string to be parsed.
The operator precedence parsing algorithm is as follows −
Input − The precedence relations from some operator precedence grammar and an input string of terminals from that grammar.
Output − There is no output but it can construct a skeletal parse tree as we parse, with one non-terminal labeling all interior nodes and the use of single productions not shown. Alternatively, the sequence of shift-reduce steps can be considered the output.
Method − Let the input string be a1a2 … … . an.$Initially, the stack contains $.
- Repeat forever
- If only $ is on the stack and only $ is on the input then accept and break else
- begin
- let a be the topmost terminal symbol on the stack and let b be the current input symbols.
- If a <. b or a =. b then shift b onto the stack /*Shift*/
- else if a . >b then /*reduce*/
- repeat pop the stack
- until the top stack terminal is related by <. to the terminal most recently popped.
- else call the error-correcting routine end.
Example1 − Construct the Precedence Relation table for the Grammar.
E → E + E|E − E|E ∗ E|E⁄E|E ↑ E|(E)| − E|id
Using Assumptions
Operators | Precedence | Association |
---|---|---|
↑ | Highest | Right Associative |
* and / | Next Highest | Left Associative |
+ and − | Lowest | Left Associative |
Solution
Operator Precedence Relations
+ | − | * | / | ↑ | id | ( | ) | $ | |
+ | .> | .> | <. | <. | <. | <. | <. | .> | .> |
− | .> | .> | <. | <. | <. | <. | <. | .> | .> |
* | .> | .> | .> | .> | <. | <. | <. | .> | .> |
/ | .> | .> | .> | .> | <. | <. | <. | .> | .> |
↑ | .> | .> | .> | .> | <. | <. | <. | .> | .> |
id | .> | .> | .> | .> | .> | .> | .> | ||
( | <. | <. | <. | <. | <. | <. | <. | = | |
) | .> | .> | .> | .> | .> | .> | .> | ||
$ | <. | <. | <. | <. | <. | <. | <. |
Example2 − Find out all precedence relations between various operators & symbols in the following Grammar & show them using the precedence table.
E → E + T|T
T → T ∗ F|F
F → (E)|id
Solution
+ | * | ( | ) | id | $ | |
+ | .> | .< | <. | .> | <. | .> |
* | .> | .> | <. | .> | <. | .> |
( | <. | <. | <. | =. | <. | |
) | .> | .> | .> | .> | ||
id | .> | .> | .> | .> | ||
$ | <. | <. | <. | <. |
- Related Articles
- What is Operator Precedence Parsing?
- What are Precedence Functions in compiler design?
- What are Parsing Techniques in Compiler Design?
- What is Top-Down Parsing with Backtracking in compiler design?
- What is Top-Down Parsing Without Backtracking in Compiler Design?
- What is Stack Implementation of Shift Reduce Parsing in compiler design?
- What is Compiler Design?
- What is Design of Lexical Analysis in Compiler Design?
- What is the operator precedence in C#?
- What is Chomsky Hierarchy in compiler design?
- What is error handling in compiler design?
- What is Input Buffering in Compiler Design?
- What is Finite Automata in Compiler Design?
- What is C Operator Precedence and Associativity?
- What is Language Processing Systems in Compiler Design?
