Expression Conversion With Stack
Expression Conversion With Stack
(A + B) * (C + D) *+AB+CD AB+CD+*
(P+Q)*(M-N) *+PQ-MN
Very useful in compilers, each time the parser reads one character at a time.
If the character is an opening delimiter such as (, {, or [- then it is push to the stack.
When a closing delimiter is encountered like ), }, or ]-the stack is popped.
1. Create a stack.
c. If it is a closing symbol like ),],}, then if the stack is empty report an error.
Otherwise pop the stack.
d. If the symbol popped is not the corresponding opening symbol, report an error.}
• Infix: 1+2*3-4/2^2
• Postfix: 123*+45*6+2*+
• Postfix: 823^/23*+51*- So what are the Top two elements of the stack after the first * operator is
evaluated?
Postfix: 823^/23*+51*-
Token Action Stack
8 Push 8 to stack 8
2 Push 2 to stack 2, 8
3 Push 3 to stack 3, 2, 8
Pop 3 from stack 2, 8
Postfix: 823^/23*+51*- ^ Pop 2 from stack 8
So what are the Top two
elements of the stack after Push 2^3 = 8 8, 8
the first * operator is Pop 8 from stack 8
evaluated? / Pop 8 from stack
Push 8/8 = 1 to 1
stack
2 Push 2 to stack 2, 1
3 Push 3 to stack 3, 2, 1
Pop 3 from stack 2, 1
* Pop 2 from stack 1
Push 3*2 = 6 to 6, 1
stack
Result = 6, 1
Prefix: Expression: -, *, 3, +, 16, 2, /, 12, 6
Reversed Prefix Expression: 6, 12, /, 2, 16, +, 3, *, -
Token Action Stack
6 Push 6 to stack 6
12 Push 12 to stack 12, 6
Pop 12 to stack 6
Prefix: -, *, 3, +, 16, 2, /, 12, 6 / Pop 6 from stack
Push 12/6= 2 to stack 2
2 Push 2 to stack 2, 2
16 Push 16 to stack 16, 2, 2
Pop 16 from stack 2, 2
+ Pop 2 from stack 2
Push 16+2 = 18 to stack 18, 2
3 Push 3 to stack 3, 18, 2
Pop 3 from stack 18, 2
* Pop 18 from stack 2
Push 3*18 = 54 to stack 54, 2
Pop 54 from stack 2
- Pop 2 from stack []
Push 54-2= 52 to stack 52
Result = 52
Postfix:
5, 2, 7, ^, *, 39, 13, /, -, 9, 11, *, +
Infix to Postfix Conversion
• One of the applications of Stack is in the conversion of arithmetic expressions in high-level
programming languages into machine readable form.
• Computer system can only understand and work on a binary language, it assumes that an
arithmetic operation can take place in two operands only e.g., A+B, C*D,D/A etc.
• But in our usual form an arithmetic expression may consist of more than one operator and two
operands e.g. (A+B)*C(D/(J+D)).
• These complex arithmetic operations can be converted into postfix expression using stacks which
then can be executed in two operands and an operator form.
• Infix Expression: <operand><operator><operand>. Infix expression. E.g., A+B
• Postfix Expression: <operand><operand><operator>. Postfix expression E.g., AB+
Algorithm to Convert Infix To Postfix
• Let, Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix
expression P.
( (+(-( ABC*
What is Postfix expression? D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/ Pop from top of the
stack ^, /
* (+(-* ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*- Pop from top of the
stack *, -
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) empty ABC*DEF^/G*-H*+ END
Conversion of Infix expression to Prefix:
Assume Q is an infix expression. Consider there are two stacks S1 and S2 exist.
Algorithm:
Step 1: Add left parenthesis ‘(‘ at the beginning of the expression Q
Step 2: Push ‘)’ onto stack S1
Step 3: Repeatedly scan Q from right-to-left order, until stack S1 is empty
Step 3.1: If Q[I] is an operand, then push it into stack S2
Step 3.2: Else if Q[I] is ‘)’, then push it onto stack S1
Step 3.3: Else if Q[I] is an operator (OP) then
Step 3.3.1: Set X: = Pop (S1)
Step 3.3.2: Repeat, While X is an Operator AND (Precedence(X)>Precedence (OP))
Push (X) onto Stack S2
Set X: = POP (S1)
[End of While – Step 3.3.2]
Step 3.3.3: Push (X) onto Stack S1
Step 3.3.4: Push (OP) onto stack S1
Contd.,
Step 3.4: Else if Q [I] is ‘(‘, then
Step 3.4.1: Set X = pop (S1)
Step 3.4.2: Repeat, While (X! =’)’) [Until right parenthesis found]
Step 3.4.2.1: Push (X) onto Stack S2
Step 3.4.2.2: Set X= Pop (S1)
[End of While – step 3.4.2]
[End of IF – step 3.1]
End of Loop – step 3]
Step 4: Repeat, while Stack S2 is not empty
Step 4.1: Set X = POP (S2)
Step 4.2: Display X
[End of while – step 4]
Step 5: Exit
Symbol scanned from Stack S1 Stack S2
Q
)
H ) H
+ )+ H
G )+ HG
- )+- HG
) )+-) HG
T )+-) HGT
+ )+-)+ HGT
Q = A+B*C*(M*N^P+T)-G+H
P )+-)+ HGTP
H+G-)T+P^N*M(*C*B+A ^ )+-)+^ HGTP
N )+-)+^ HGTPN
* )+-)+* HGTPN^ ^ higher than *
M )+-)+* HGTPN^M
( )+- HGTPN^M*+ * higher than +
* )+-* HGTPN^M*+
C )+-* HGTPN^M*+C
* )+-** HGTPN^M*+C
B )+-** HGTPN^M*+CB
+ )+-+ HGTPN^M*+CB**
A )+-+ HGTPN^M*+CB**A
( HGTPN^M*+CB**A+-+
1. Scan PREFIX expression from Right To Left Or Reverse the PREFIX expression and scan it from
LEFT to RIGHT.
2. IF the incoming symbol is a OPERAND, PUSH it onto the Stack
3. IF the incoming symbol is a OPERATOR, POP 2 OPERANDs from the Stack, ADD this incoming
OPERATOR in between the 2 OPERANDs,
• ADD ‘(‘ & ‘)’ to the whole expression & PUSH this whole new expression back into the
Stack.
For example, if the scanned character is “/” and s1, s2 are operands popped from the stack
then Push string “(“+s1+”/” +s2+”)” to Stack
4. Repeat step 3 until all the characters of prefix scanned.
Prefix expression: *-A/BC-/DEF
Iterate right to left or Reverse the prefix expression: FED/-CB/A-*
Token Action Stack Description
F Push F to stack F
E Push E to stack E, F
D Push D to stack D, E, F
Pop D from stack E, F Pop operands D, E
/ Pop E from stack F Perform D/E
Push (D/E) to stack (D/E), F Push (D/E) to stack
Pop (D/E) from stack F Pop operands (D/E), F
- Pop F from stack [] Perform (D/E)-F
Prefix expression:
Push ((D/E)-F) to stack ((D/E)-F) Push ((D/E)-F) to stack
*-A/BC-/DEF
Infix expression: C Push C to stack C, ((D/E)-F)
B Push B to stack B, C, ((D/E)-F)
((A-(B/C))*((D/E)-F)) Pop B from stack C, ((D/E)-F) Pop operands B, C
/ Pop C from stack ((D/E)-F) Perform B/C
Push B/C to stack (B/C), ((D/E)-F) Push (B/C) to stack
A Push A to stack A, (B/C), ((D/E)-F)
Pop A from stack (B/C), ((D/E)-F) Pop A, B/C
- Pop (B/C) from stack ((D/E)-F) Perform A-(B/C)
Push (A-(B/C)) to stack (A-(B/C)), ((D/E)-F) Push (A-(B/C)) to stack
Pop (A-(B/C)) from stack ((D/E)-F) Pop (A-(B/C)), ((D/E)-F)
* Pop ((D/E)-F) from stack [] Perform (A-(B/C)) *((D/E)-F)
Push ((A-(B/C)) *((D/E)-F)) ((A-(B/C )) *((D/E)-F)) Push ((A-(B/C)) *((D/E)-F))
• Prefix expression: + A B
• Postfix expression: A B +
• Algorithm:
2. Scan PREFIX expression from Right To Left Or Reverse the PREFIX expression and scan it from LEFT
to RIGHT.
3. IF the incoming symbol is a OPERAND, PUSH it onto the Stack
4. IF the incoming symbol is a OPERATOR, POP 2 OPERANDs from the Stack, ADD this incoming
OPERATOR after the 2 OPERANDs.
For example, if the scanned character is “/” and s1, s2 are operands popped from the stack then
Push string “+s1+” “+s2+” “/” to Stack
4. Repeat step 3 until all the characters of prefix scanned.
Prefix: *-A/BC-/DEF
Iterate right to left or reverse the given prefix: FED/-CB-*
Token Action Stack Description
F Push F to stack F
E Push E to stack E, F
D Push D to stack D, E, F
Pop D from stack E, F Pop operands D, E
/ Pop E from stack F Perform DE/
Push DE/ to stack DE/, F Push DE/ to stack
Pop DE/ from stack F Pop operands DE/, F
- Pop F from stack [] Perform DE/F-
Prefix: *-A/BC-/DEF Push DE/F- to stack DE/F- Push DE/F- to stack
C Push C to stack C, DE/F-
Postfix: ABC/-DE/F-* B Push B to stack B, C, DE/F-
Pop B from stack C, DE/F- Pop operands B, C
/ Pop C from stack DE/F- Perform BC/
Push BC/ to stack BC/, DE/F- Push BC/
A Push A to stack A, BC/, DE/F-
Pop A from stack BC/, DE/F- Pop operands A, BC/
- Pop BC/ from stack DE/F- Perform ABC/-
Push ABC/- ABC/-, DE/F- Push ABC/-
Pop ABC/- DE/F- Pop operands
* Pop DE/F- [] Perform
Push ABC/-DE/F-* ABC/-DE/F-* Push ABC/-DE/F- to stack
Postfix to Prefix
• Postfix expression: A B +
• Prefix expression- + A B
• Algorithm:
• Infix expression: A + B
• Algorithm: