Operator precedence parsing is a computer programming approach for parsing and evaluating phrases including operators with varying levels of precedence. Each operator is allocated a precedence level in this approach, which reflects its relative priority in comparison to other operators.
The method scans an input phrase from left to right, one token at a time, and keeps a stack of operators and operands. When a token is received, based on its precedence level and the current state of the stack, it is either pushed into the stack as an operand or combined with the top of the stack as an operator. If the current token has larger precedence than the top of the stack, it is pushed onto the stack.
Purpose: When evaluating an expression, the fundamental purpose of operator precedence parsing is to find the right sequence of operations.
Stack: Stack is a data structure where insertion or deletion can be done only one way, or from one end called Top End. Stack follows the LIFO or FILO principle. This makes stacks useful for a variety of tasks, such as parsing expressions, implementing recursive algorithms, and undoing actions in software applications.
In the stack, there are operations like push, pop, display, and peep. Some stacks may also have a maximum capacity, beyond which they cannot store any additional elements.
Let us see the operator's precedence first from higher precedence to lower precedence.
1. ( )
3. /, * (same precedence)
4. +, - (same precedence)
Along with the precedence we also need to focus on associativity.
It is either from Left to Right or Right to Left.
The above operator's associativity is given below:
1. ^ (Right to Left)
2. /, * (Left to Right)
3. +, - (Left to Right)
Follow the below points to check the precedence and associativity of a given expression:
- If the precedence is the same, then check for associativity if it is also the same then pop out the operator which is at the top of the stack.
- If the precedence is the same, then check for associativity if it is different then, push the operator into the stack.
- If the precedence is higher than the existing operator, then push the operator into the stack.
- If the precedence is lower than the existing operator, then pop out the previous operator from the stack.
- Repeat the same process until the total expression is evaluated.
- Coming to the operands they will be in the output and according to the operators parsing the operation is performed and the result is obtained.
Let us understand with an Example: An expression is given below we need to find out the final output by using operator precedence and parsing.
Q) 6 3 / 1 – 5 2 * +
Solution: The output is 11and the steps are listed below in detail.
Step1: Push 6 into the stack
Step 2: Push 3 into the stack
Step 3: Perform division operation on 6 and 3 as per stack 6/3=2
Step 4: Now push 2 into the stack as the operation is performed on 6 and 3, stack is empty so push the result obtained into the stack which is 2
Step 5: Push 1 into the stack
Step 6: Perform subtraction operation on 2 and 1 as per stack 2-1=1
Step 7: Push 5 into the stack
Step 8: Push 2 into the stack
Step 9: The operands present in the stack are 1,5,2 as per the FILO principle.
Step 9: Perform multiplication operation on 2 and 5 as per stack 2*5=10
Step 10: Push 10 into the stack and the operands present in the stack are 10,1 as per the FILO principle.
Step 11: Perform addition operation on 10 and 1 as per stack 10+1=11
Step 12: As there are no operands and operators left 11 is the output for the given expression.
Let us take another example
An expression is given below we need to find out the final output by using operator precedence and parsing.
Q) (A + B / C * (D + C) - F)
Solution: For better understanding let us insert a table with suitable columns and rows.
Here A, B, C, D, E, and F are the operands
+, /, *, - are the operators
By using the operator precedence and associativity as per the above-described rules we will evaluate the expression and obtain the final result.
Evaluating the given expression using operator precedence and stack
|/||( + /||AB|
|C||( + /||ABC|
So, after evaluating the whole given expression the obtained result is
ABC/DE+*+F- as per the rules of operator precedence and associativity.
Conclusion: Overall, operator precedence parsing is a critical approach in computer science and software development. This can assist in enhancing application speed while also making code writing and maintenance easier. Also using operator precedence, we can easily evaluate the expressions.