Open In App

GATE | GATE-CS-2004 | Question 90

Last Updated : 28 Jun, 2021
Like Article
Like
Save
Share
Report

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



Answer: (A)

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads