Explanation: We are adapted to the writing of expression into infix notation. It has some precedence of operators. Example (3+2*4) is an infix notation having some precedence of operators. Postfix expression does not have precedence of operators. Post fix expression can be evaluated using a stack. Algorithm for evaluation of a post fixes expression.
1.Repeat the process until the expression is exhausted.
{ 2.Get next token from the expression.
3.If it is operand, push it on stack.
4.Else if it is operator then
{Pop the stack for the right hand operand.
Pop the stack for the left hand operand.
Perform the operation. Push the result into stack. }
}
5.When expression is exhausted,
Result is on the top of stack. Example.
Example ,Evaluate post fix expression 34+56-*.
Push operand 3. Then push operand 4. Stack has element (3,4(top)) .+ is an operator so pop two operand 4 and 3 and perform operation. (3+4) results is 7 and it is pushed in the stack. Stack has element (7) Next 5 is pushed followed by 6. Stack is (7,5,6(top)). Next token is -, so right operand is popped as 6. Then left operand 5 is popped, now operation (5-6 ) is done and result -1 is pushed to stack. Stack has two element (7,-1(top)). Next operator is * so pop two and perform the operation and push it into stack. So stack is (-7). String is exhausted and result is on the top of stack.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.