You are on page 1of 44

ICS 121 DS

Expression conversions with Stack


Notation

• Infix notation: operators placed between operands:


A+B*C <operand><operator><operand>
• Postfix notation: operands appear before their operators:-
ABC*+ <operand><operand><operator>
• Prefix notation: operators appear before operands:
*+ABC <operator><operand> <operand>
Fundamentals
• Arithmetic expression: B * C
Infix notation since the operator is in between the operands which working on it.
• Infix example, A + B * C? The expression seems unclear.
• The only thing that can change that order is the presence of parentheses.
• Let’s interpret the troublesome expression A + B * C using operator precedence.
B and C are multiplied first, and A is then added to that result.
(A + B) * C would force the addition of A and B to be done first before the multiplication.
In expression A + B + C, by precedence (via associativity), the leftmost + would be done first.
Sr.No. Operator Precedence Associativity
1 Exponentiation ^ Highest Right Associative
2 Multiplication ( ∗ ) Second Highest Left Associative
& Division ( / )
3 Addition ( + ) & Lowest Left Associative
Subtraction ( − )
Contd.,
• Fully parenthesized expression which guarantees there will be no confusion with respect to the
order of operations.
A + B * C + D can be rewritten as ((A + (B * C)) + D) here multiplication happens first.
A + B + C + D can be written as (((A + B) + C) + D) here addition operations associate from
left to right.
• Based on infix expression A + B. There are two other very important expression formats can be
formed.
• First What would happen if we moved the operator before the two operands? The resulting
expression would be + A B.
• Likewise, we could move the operator to the end. We would get A B +.
Contd.,
• These changes to the position of the operator with respect to the operands create two new
expression formats, prefix and postfix.
• Prefix expression notation requires that all operators precede the two operands that they work
on.
A + B * C would be written as + * B C A in prefix
• Postfix, on the other hand, requires that its operators come after the corresponding operands.
A + B * C would be written as A B C * +.
• Infix expression (A + B) * C: when A + B was written in prefix, it is + A B, then * + A B C.
• Likewise, in postfix A B +, then A B + C *.
Notation

• Infix: operators placed between operands:


A+B*C <operand><operator><operand>
• Postfix: operands appear before their operators:-
ABC*+ <operand><operand><operator>
• Prefix: operators appear before operands:
*+ABC <operator><operand> <operand>
Precedence and associativity table (highest to lowest)
Sr.No. Operator Precedence Associativity

1 Exponentiation ^ Highest Right Associative

2 Multiplication ( ∗ ) Second Highest Left Associative


& Division ( / )
3 Addition ( + ) & Lowest Left Associative
Subtraction ( − )

An Expression with Parentheses

Infix Expression Prefix Expression Postfix Expression


Additional Examples of Infix, Prefix, and Postfix
(A + B) * C *+ABC AB+C*
Infix Expression Prefix Expression Postfix Expression
A+B*C+D ++A*BCD ABC*+D+

(A + B) * (C + D) *+AB+CD AB+CD+*

A*B+C*D +*AB*CD AB*CD*+

A+B+C+D +++ABCD AB+C+D+


Evaluation of Infix expressions
• Infix notation is commonly used in arithmetic formula or statements, the operators are written in-
between their operands.

• Let’s assume the below


Operands are real numbers.
Permitted operators: +,-, *, /, ^(exponentiation)
Blanks are permitted in expression.

Parenthesis are permitted


• Example:
A*(B+C)/D
2 * (5 + 3) / 4
Output: 4
Evaluation of Infix Expressions
• Approach: Use Stacks
• We will use two stacks
Operand stack: This stack will be used to keep track of numbers.
Operator stack: This stack will be used to keep operations (+, -, *, /, ^)
• Order of precedence of operations–
^ (Exponential)
/*
+–
• Note: brackets ( ) are used to override these rules.
Evaluation of Infix Expressions
Algorithm: Iterate through given expression, one character at a time
1. If the character is an operand, push it to the operand stack.
2. If the character is an operator,
1. If the operator stack is empty then push it to the operator stack.
2. Else If the operator stack is not empty,
1. If the character’s precedence is greater than or equal to the precedence of the stack top
of the operator stack, then push the character to the operator stack.
2. If the character’s precedence is less than the precedence of the stack top of the operator
stack then do Process (as explained above) until character’s precedence is less or stack is
not empty.
3. If the character is “(“, then push it onto the operator stack.
4. If the character is “)”, then do Process (as explained above) until the corresponding “(” is
encountered in operator stack. Now just pop out the “(“.
Infix expression: 2*(5*(3+6))/15-2
Postfix
• The postfix expression as the name suggests has the operator placed right after the two
operands.
• It is of the form <operand><operand><operator>
• Postfix expression itself determines the precedence of operators: the operator which occurs first
operates on the operand.
• E.g. PQ-C/, here – operation is done on P and Q and then / is applied on C and the previous
result.
• A postfix expression is parenthesis-free expression. For evaluation, we evaluate it from left-to-
right.

Infix expression Postfix expression


(P+Q)*(M-N) PQ+MN-*
(P+Q) / (M-N) - (A*B) PQ+MN-/AB*-
Evaluation of Postfix expression
• Algorithm for evaluating postfix expression:
Let P is an expression written in postfix expression.
1) STACK=empty stack.
2) Scan P from left to right and repeat step 3 and 4 for each symbol in P until end
of expression.
3) If an operand is encountered, push it on STACK.
4) If an operator x encountered then;
a) Operand 2= pop (STACK).
b) Operand 1= pop (STACK).
c) Value= operand1 x operand 2.
d) Push value on STACK.
5) Return the value at top of the STACK.
6) Exit.
Postfix evaluation: 623+-382/+*2^3+
Token Operand 1 Operand 2 Value Stack
6 6
2 2, 6
3 3, 2, 6
+ 3 2 5 6, 5
- 6 5 1 1
3 3, 1
8 8, 3, 1
2 2, 8, 3, 1
/ 2 8 4 4, 3, 1
+ 4 3 7 7, 1
* 7 1 7 7
2 2, 7
^ 7 2 49
3 3, 49
+ 49 3 52
Total 52
Prefix
• The prefix expression as the name suggests has the operator placed before the operand is
specified.
• It is of the form < operator > < operand > < operand >.
• It works entirely in same manner as the postfix expression, but in reverse order.
• While evaluating a prefix expression, the operators are applied to the operands immediately on
the right of the operator.
• For evaluation, we evaluate it from left-to-right.

Infix expression Postfix expression

(P+Q)*(M-N) *+PQ-MN

(P+Q) / (M-N) - (A*B) -/+PQ-MN*AB


Evaluating a Prefix expression
• Algorithm for evaluating postfix expression:
Let P is an expression written in postfix notation.
Reverse the given expression and Iterate through it, one character at a time
1) STACK=empty stack.
2) Scan P from left to right and repeat step 3 and 4 for each symbol in P until end
of expression.
3) If an operand is encountered, push it on STACK.
4) If an operator x encountered then;
a) Operand 2= pop (STACK).
b) Operand 1= pop (STACK).
c) Value= operand1 x operand 2.
d) Push value on STACK.
5) Return the value at top of the STACK.
6) Exit.
Prefix: Expression: -/*2*5+3652
Reversed Prefix Expression: 2563+5*2*/-
Token Action Stack
2 Push 2 to stack 2
5 Push 5 to stack 5, 2
6 Push 6 to stack 6, 5, 2
3 Push 3 to stack 3, 6, 5, 2
+ Pop 3 from stack 6, 5, 2
Pop 6 from stack 5, 2
Push 3+6 = 9 to stack 9, 5, 2
5 Push 5 to stack 5, 9, 5, 2
Pop 5 from stack 9, 5, 2
* Pop 9 from stack 5, 2
Push 5*9 = 45 to stack 45, 5, 2
2 Push 2 to stack 2, 45, 5, 2
* Pop 2 from stack 45, 5, 2
Pop 45 from stack 5, 2
Push 2*45 = 90 to stack 90, 5, 2
/ Pop 90 from stack 5, 2
Pop 5 from stack 2
Push 90/5 = 18 to stack 18, 2
- Pop 18 from stack 2
Pop 2 from stack []
Push 18-2= 16 to stack 16
Result = 16
Evaluation of Prefix Expressions
• Algorithm:
Reverse the given expression and Iterate through it, one character at a time
1.If the character is an operand, push it to the operand stack.
2.If the character is an operator,
1. pop the operand from the stack, say it’s s1.
2. pop another operand from the stack, say it’s s2.
3. perform (s1 operator s2) and push it to stack.
3.Once the expression iteration is completed, The stack will have the final result.
pop from the stack and return the result.
Prefix expression: -/*2*5+3652
Stacks can be used for checking balancing of symbols

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.

Example Valid? Description


(A+B)+ (C-D) Yes Balanced expression
((A+B)+(C+D) No Closing brace missing
((A+B)+[C-D]) Yes Balanced expression
((A+B)+[C-D]} No Last closing brace doesn’t
match
Stacks can be used for checking balancing of symbols
• Algorithm:

1. Create a stack.

2. While (end of input is not reached) {

a. If the character read is not a symbol to be balanced, ignore it.

b. If the character is an opening symbol like (, [, {, push it onto the 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.}

3. At end of input, if the stack is not empty report an error.


Check the parenthesis balanced or not: () (() [()])
Input Symbol, A[i] Operation Stack Output
( Push ( (
) Pop (
Test if ( and A[i] match? Yes
( Push ( (
( Push ( ((
) Pop ( (
Test if ( and A[i] match? Yes
[ Push [ ([
( Push ( ([(
) Pop ( ([
Test if ( and A[i] match? Yes
] Pop ] (
Test if ] and A[i] match? Yes
) Pop )
Test if ) and A[i] match? Yes
Test if stack is empty? Yes True
Problems
• Infix: 4*2+3-5/5

• Infix: 1+2*3-4/2^2

• Postfix: 123*+45*6+2*+

• Postfix: 5,2,7,^,*,39,13,/,-,9, 11,*, +

• Prefix: -, *, 3, +, 16, 2, /, 12, 6

• 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.

1. Push “(“onto Stack, and add “)” to the end of Q.


2. Scan Q from left to right and repeat Step 3 to 6 for each element of Q until the Stack is empty.
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to P each operator (on the top of Stack) which has the
same precedence as or higher precedence than operator.
2. Add operator to Stack.
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to P each operator (on the top of Stack) until a left
parenthesis is encountered.
2. Remove the left Parenthesis.
7. END.
Scanned Stack Postfix Description
(
A ( A
- (- A
B (- AB
/ (-/ AB
Infix notation: ( (-/( AB
A-B /(C*D^E) C (-/( ABC
* (-/(* ABC
What is the Postfix Notation?
D (-/(* ABCD
^ (-/(*^ ABCD
E (-/(*^ ABCDE
) (-/ ABCDE^* Pop from top of
the stack ^, *

) ABCDE^*/- Pop from top of


the stack /, -
Scanned Stack Postfix expression Description
(
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
Infix expression: C (+(* ABC
- (+(- ABC* ‘*’ Has higher
A+ (B*C-(D/E^F)*G)*H precedence than ‘-‘

( (+(-( 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+-+

So the prefix notation will be: +-+A**BC+*M^NPTGH


Infix Expression
A+ (B*C-(D/E^F)*G)*H
Prefix to Infix
Let P is an expression written in prefix notation.

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))

Infix expression: ((A-(B/C)) *((D/E)-F))


Prefix: -*+ABC/DE
Iterate right-to-left or reverse the prefix notation: ED/CBA+*-
Token Action Stack Description
E Push E to stack E
D Push D to stack D, E
Pop D from stack E Pop D, E
/ Pop E from stack [] Perform D/E
Push D/E to stack (D/E) Push (D/E)
C Push C to stack C, (D/E)
B Push B to stack B, C, (D/E)
Prefix: -*+ABC/DE A Push A to stack A, B, C, (D/E)
Infix: (((A+B)*C)-(D/E)) Pop A from stack B, C, (D/E) Pop A, B
+ Pop B from stack C, (D/E) Perform A+B
Push (A+B) to stack (A+B), C, (D/E) Push (A+B)

Pop (A+B) C, (D/E) Pop (A+B), C


Pop C (D/E) Perform (A+B)*C
* Push ((A+B) *C) ((A+B) *C), (D/E) Push ((A+B)*C)

Pop ((A+B) *C) (D/E) Pop ((A+B) *C), (D/E)


Pop (D/E) [] Perform ((A+B) *C)-(D/E)
- Push (((A+B) *C)- (((A+B) *C)- (D/E)) Push (((A+B) *C)- (D/E))
(D/E))
Infix expression: (((A+B) *C)- (D/E))
Prefix to Postfix

• Prefix expression: + A B
• Postfix expression: A B +
• Algorithm:

1. Let P is an expression written in prefix notation.

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:

1. Let P is an expression written in postfix notation.

2. Scan POSTFIX expression 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 before the 2 OPERANDs.
For example, if the scanned character is “/” and s1, s2 are operands popped from the stack then
Push string “/” “+s2+” “+s1+” to Stack
4. Repeat step 3 until all the characters of prefix scanned.
Postfix: ABC/-DE/F-*
Token Action Stack Description
A Push A to stack A
B Push B to stack B, A
C Push C to stack C, B, A
Pop C from stack B, A Pop operands B, C
/ Pop B from stack A Perform /BC
Push /BC to stack /BC, A Push /BC
Pop /BC from stack A Pop /BC, A
- Pop A from stack [] Perform -A/BC

Postfix: ABC/-DE/F-* Push -A/BC -A/BC Push -A/BC


D Push D to stack D, -A/BC
E Push E to stack E, D, -A/BC
Prefix: *-A/BC-/DEF Pop E from stack D, -A/BC Pop E, D
/ Pop D from stack -A/BC Perform /DE
Push /DE to stack /DE, -A/BC Push /DE
F Push F to stack F, /DE, -A/BC
Pop F from stack /DE, -A/BC Pop F, /DE
- Pop /DE from stack -A/BC Perform -/DEF
Push -/DEF -/DEF, -A/BC Push -/DEF
Pop -/DEF -A/BC Pop -/DEF, -A/BC
* Pop -A/BC [] Perform *-A/BC-/DEF
*-A/BC-/DEF
Push *-A/BC-/DEF Push *-A/BC-/DEF
Postfix to Infix
• Postfix expression: A B +

• Infix expression: A + B

• Algorithm:

Let P is an expression written in postfix notation.

1. Scan POSTFIX expression 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 between the 2 OPERANDs.
4. 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 “(“+s2+” “/” “+s1+”)” to Stack
4. Repeat step 3 until all the characters of prefix scanned.
Postfix: ABC/-DE/F-*
Token Action Stack Description
A Push A to stack A
B Push B to stack B, A
C Push C to stack C, B, A
Pop C from stack B, A Pop C, B
/ Pop B from stack A Perform B/C
Push B/C to stack (B/C), A Push (B/C)
Pop (B/C) from stack A Pop (B/C), A
- Pop A from stack [] Perform A-(B/C)
Push (A-(B/C)) Push (A-(B/C))
(A-(B/C))
D Push D to stack D, (A-(B/C))
E Push E to stack E, D, (A-(B/C))
Pop E from stack D, (A-(B/C)) Pop E, D
/ Pop D from stack (A-(B/C)) Perform
Push (D/E) to stack (D/E), (A-(B/C)) D/E
Push (D/E)
F Push F to stack F, (D/E), (A-(B/C))
Pop F from stack (D/E), (A-(B/C)) Pop F, (D/E)
- Pop (D/E) from stack (A-(B/C)) Perform (D/E)-F
Push ((D/E)-F) to stack ((D/E)-F), (A-(B/C)) Push ((D/E)-F)
Pop ((D/E)-F) (A-(B/C)) Pop ((D/E)-F), (A-(B/C))
* Pop (A-(B/C)) [] 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))
Assignment 1
• Infix Expression: ( A+ ( B * C ) ) ;
Postfix Expression:
Prefix Expression:
• Infix Expression: ( A+ ( B * C ) ) / ( D - E );
Postfix Expression:
Prefix Expression:
• Infix Expression: ( ( A + B ) * ( C + E ) ) ;
Postfix Expression:
Prefix Expression:
• Infix Expression: ( A * ( B * ( ( ( C + A ) + B ) * C ) ) ) ;
Postfix Expression:
Prefix Expression:
• Infix Expression: ( ( H * ( ( ( ( A + ( ( B + C ) * D ) ) * F ) * G ) * E ) ) + J ) ;
Postfix Expression:
Prefix Expression

You might also like