The Wayback Machine - https://web.archive.org/web/20240810124927/https://www.geeksforgeeks.org/role-of-operator-precedence-parser/
Open In App

Role of Operator Precedence Parser

Last Updated : 08 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this, we will cover the overview of Operator Precedence Parser and mainly focus on the role of Operator Precedence Parser. And will also cover the algorithm for the construction of the Precedence function and finally will discuss error recovery in operator precedence parsing. Let’s discuss it one by one.

Introduction :
Operator Precedence Parser constructed for operator precedence grammar.  Operator precedence grammar is a grammar that doesn’t contain epsilon productions and does not contain two adjacent non-terminals on R.H.S. of any production. Operator precedence grammar is provided with precedence rules. Operator Precedence grammar could be either ambiguous or unambiguous.

Operator Precedence Parser Algorithm :

1. If the front of input $ and top of stack both have $, it's done
  else
2. compare front of input b with ⋗
      if b! = '⋗'
         then push b
      scan the next input symbol
3. if b == '⋗'
       then pop till ⋖ and store it in a string S
       pop ⋖ also
       reduce the popped string
       if (top of stack) ⋖ (front of input)
            then push ⋖ S
       if (top of stack) ⋗ (front of input)
            then push S and goto 3 

Components Operator Diagram :

Components Operator

Example –
Let’s take an example to understand the role of operator precedence as follows.

E-> E+T/T
T-> T*V/V
V->a/b/c/d
string= "a+b*c*d"

Implementation of the above algorithm for the string “a+b*c*d” as follows.

Stack Input Stack Top Current Input Action
$ a+b*c*d$ $ a shift a
$a +b*c*d$ a + reduce using V->a
$V +b*c*d$ V + reduce using T->V
$T +b*c*d$ T + reduce using E->T
$E +b*c*d$ E + shift +
$E+ b*c*d$ b * reduce using V->b
$E+V b*c*d$ V * reduce using T->V
$E+T *c*d$ T * shift *
$E+T* c*d$ * c shift c
$E+T*c *d$ c * reduce using V->c
$E+T*V *d$ V * reduce using T->T*V
$E+T *d$ T * shift *
$E+T* d$ * d shift d
$E+T*d $ d $ reduce using V->d
$E+T*V $ V $ reduce using T->T*v
$E+T $ T $ reduce using E->E+T
$T $ E $ accept

Algorithm for construction of Precedence function :

  1. Generate a function Xa for each grammar terminal a and for the end of the string symbol.
  2. Partition the symbol in groups so that Xa and Yb are the same groups if a ≐ b.
  3. Generate a directed graph whose nodes are in the groups, for each symbol a and b, do place an edge from the group of Yb to the group of Xa if a ⋖ b, otherwise if a ⋗ b place an edge from the group of Xa to that of Yb.
  4. If the constructed graph has a cycle then no procedure functions exist. When there are no cycles collect the length of the longest paths from the groups of Xa and yb respectively.

Example –
Let’s take an example to understand the construction of precedence function as follows.

E -> E + E/E * E/( E )/id

Here, you will see the Operator precedence relation table and Precedence Relations Graph diagram. Let’s have a look.

Operator precedence relation table

Precedence Relations Graph

As we can see no cycle there is no cycle in the graph, we can make this function table as follows.

Function table

Columns Representation function :

Columns represent function Ya and Rows represent function Xa –
It is calculated by taking the longest path from Xid to X$ and Yid to Y$.

fid -> g* -> f+ ->g+ -> f$
gid -> f* -> g* ->f+ -> g+ ->f$ 
  • The disadvantage of the operator precedence relation table is that if there are ‘n’ numbers of symbols then we require a table of n*n to store them. On other hand, by using the operator function table, to accommodate n number of symbols we require a table of 2*n. Operator Precedence Grammar cannot decide the unary minus(lexical analyzer should handle the unary minus).
  • The advantage of using Operator Precedence Grammar is simple and enough powerful for expression in programming languages.

Error Recovery in Operator-Precedence Parsing :

  • Error Cases –
    1. No relation holds between the terminal on the top of the stack and the next input symbol.
    2. A handle is found (reduction step), but there is no production with this handle as a right side.
     
  • Error recovery –
    1. Each empty entry is filled with a pointer to an error routine.
    2. Decides the popped handle “looks like” which right-hand side. And tries to recover from that situation.
     
  • Handling shift/ reduce errors –
    To take care of such types of errors we must modify the following.
    1. Stack
    2. Input
    3. or Both


Similar Reads

Difference between Recursive Predictive Descent Parser and Non-Recursive Predictive Descent Parser
Prerequisite - Recursive Descent Parser 1. Recursive Predictive Descent Parser : Recursive Descent Parser is a top-down method of syntax analysis in which a set of recursive procedures is used to process input. One procedure is associated with each non-terminal of a grammar. Here we consider a simple form of recursive descent parsing called Predict
4 min read
Precedence Graph in Operating System
Prerequisite - Process Synchronization Precedence Graph is a directed acyclic graph which is used to show the execution level of several processes in operating system. It consists of nodes and edges. Nodes represent the processes and the edges represent the flow of execution. Properties of Precedence Graph : Following are the properties of Preceden
2 min read
Precedence Graph for Testing Conflict Serializability in DBMS
A Precedence Graph or Serialization Graph is used commonly to test the Conflict Serializability of a schedule. It is a directed Graph (V, E) consisting of a set of nodes V = {T1, T2, T3..........Tn} and a set of directed edges E = {e1, e2, e3..................em}. The graph contains one node for each Transaction Ti. An edge ei is of the form Tj --
6 min read
Shift Reduce Parser in Compiler
Prerequisite – Parsing | Set 2 (Bottom Up or Shift Reduce Parsers) Shift Reduce parser attempts for the construction of parse in a similar manner as done in bottom-up parsing i.e. the parse tree is constructed from leaves(bottom) to the root(up). A more general form of the shift-reduce parser is the LR parser. This parser requires some data structu
8 min read
LR Parser
In this article, we will discuss LR parser, and it's overview and then will discuss the algorithm. Also, we will discuss the parsing table and LR parser working diagram. Let's discuss it one by one. LR parser :LR parser is a bottom-up parser for context-free grammar that is very generally used by computer programming language compiler and other ass
3 min read
Recursive Descent Parser
Prerequisite - Construction of LL(1) Parsing Table, Classification of top down parsers Parsing is the process to determine whether the start symbol can derive the program or not. If the Parsing is successful then the program is a valid program otherwise the program is invalid. There are generally two types of Parsers: Top-Down Parsers: In this Pars
4 min read
Difference between LL and LR parser
LL Parser includes both the recursive descent parser and non-recursive descent parser. Its one type uses backtracking while another one uses parsing table. Theses are top down parser. Example: Given grammar is S -> Ac A -> ab where S is start symbol, A is non-terminal and a, b, c are terminals. Input string: abc Parse tree generated by LL par
2 min read
Parsing ambiguous grammars using LR parser
LR parser can be used to parse ambiguous grammars. LR parser resolves the conflicts (shift/reduce or reduce/reduce) in parsing table of ambiguous grammars based on certain rules (precedence and/or associativity of operators) of the grammar. Example: Lets take the following ambiguous grammar: E -> E+E E -> E*E E -> id Lets assume, the prece
3 min read
Working of Bottom up parser
In this article, we are going to cover working of the bottom-up parser and will see how we can take input and parse it and also cover some basics of bottom-up parser. Pre-requisite - Parsing Bottom-up parser : It will start from string and proceed to start.In Bottom-up parser, Identifying the correct handle (substring) is always difficult.It will f
2 min read
Working of top down parser
In this article, we are going to cover working of top down parser and will see how we can take input and parse it and also cover some basics of top down. Pre-requisite - Parsing Top Down Parser : In top down technique parse tree constructs from top and input will read from left to right. In top down, In top down parser, It will start symbol from pr
2 min read
three90RightbarBannerImg