In: Computer Science
USING C++:
Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest):
5 | *, /, % |
4 | +, - |
3 | <, <=, >, >=, !=, == |
2 | not |
1 | and |
0 | or |
1. Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the postfix expression and the result.
The Given Infix expression is:
(19 + 4) * 2 < (32 * 2 + 1) AND (16 / 4) == 4
To convert Infix to Postfix expression, we use stack, called 'Operator stack'. For coversion, we use precedence and associatiativity rules of operators.
Infix Expression is: A operand B
Potfix Expresion is: A B operand
As precedence is already given in quetion, following is generally followed associativity:
Precedence | Operator | Associativity |
5 | *, /, % | Left to right |
4 | +, - | Left to right |
3 | <, <=, >, >=, !=, == | Left to right |
2 | not | Left to right |
1 | and | Left to right |
0 | or | Left to right |
Rules:
now, for conversion: see the image
The postfix expression is:
19 4 + 2 * 32 2 * 1 + < 16 4 / 4 == AND
Postfix Evaluation
For evaluation, we use stack called, operand stack.
Rules:
Now, for evaluation: see image