# GATE | GATE-CS-2004 | Question 90

Last Updated : 28 Jun, 2021

Assume that the operators +, -, Ã— are left associative and ^ is right associative. The order of precedence (from highest to lowest) is ^, x , +, -. The postfix expression corresponding to the infix expression a + b Ã— c – d ^ e ^ f is

(A)

abc Ã— + def ^ ^ –

(B)

abc Ã— + de ^ f ^ –

(C)

ab + c Ã— d – e ^ f ^

(D)

– + a Ã— bc ^ ^ def

Explanation:

Start scanning from left to right.
Use a stack to keep track of operators.
If the scanned symbol is an operand (in this case, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’), append it to the postfix expression.
If the scanned symbol is an operator, pop and output all operators from the stack which have equal or higher precedence than the current operator. Then push the current operator onto the stack.
If the scanned symbol is an opening parenthesis ‘(‘, push it onto the stack.
If the scanned symbol is a closing parenthesis ‘)’, pop and output operators from the stack until an opening parenthesis ‘(‘ is encountered. Pop ‘(‘ from the stack (but don’t output it).
Repeat steps 3-6 until all symbols are scanned.
Given the infix expression a + b Ã— c – d ^ e ^ f, the corresponding postfix expression is:

a b c x + d e f ^ ^ –

Quiz of this Question
Please comment below if you find anything wrong in the above post

Previous
Next