0% found this document useful (0 votes)
22 views15 pages

Stack Appln

Stacks are commonly used to evaluate arithmetic expressions. Infix notation is converted to postfix notation and then evaluated using a stack. Java bytecode is also evaluated on a stack-based virtual processor. Infix expressions are converted to postfix and prefix notation using stacks by following precedence rules and scanning the expression from left to right or right to left. Postfix and prefix expressions are then evaluated using stacks by applying operators on operands popped from the stack.

Uploaded by

Badnaam Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views15 pages

Stack Appln

Stacks are commonly used to evaluate arithmetic expressions. Infix notation is converted to postfix notation and then evaluated using a stack. Java bytecode is also evaluated on a stack-based virtual processor. Infix expressions are converted to postfix and prefix notation using stacks by following precedence rules and scanning the expression from left to right or right to left. Postfix and prefix expressions are then evaluated using stacks by applying operators on operands popped from the stack.

Uploaded by

Badnaam Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Stack Application

Stack Application
• Arithmetic expression evaluation:
– An important application of stacks is in parsing.
– In high level languages, infix notation cannot be
used to evaluate expressions.
– A common technique is to convert an infix
notation into postfix notation, then evaluating it.
• Java bytecode is interpreted on (virtual) stack based
processor.
Infix to postfix
• Step 1 : Scan the Infix Expression from left to right.
• Step 2 : If the scanned character is an operand, append it with final Infix to Postfix
string.
• Step 3 : Else,
– Step 3.1 : If the precedence order of the scanned(incoming) operator is greater than the
precedence order of the operator in the stack (or the stack is empty or the stack contains a
Infix to Postfix conversion

‘(‘ or ‘[‘ or ‘{‘), push it on stack.


– Step 3.2 : If the precedence order of the scanned(incoming) operator is less than the
precedence order of the operator in the stack Pop all the operators from the stack which are
greater than or equal to in precedence than that of the scanned operator. After doing that
Push the scanned operator to the stack. (If you encounter parenthesis while popping then
stop there and push the scanned operator in the stack.)
– Step3.3 : the precedence order of the scanned(incoming) operator is equal to the
precedence order of the operator in the stack , go for Associativity.
• If R-L then push the operator
• If L-R then pop the stack top and then push the scanned operator
• Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it to the stack.
• Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and and output it
until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the parenthesis.
• Step 6 : Repeat steps 2-6 until infix expression is scanned.
• Step 7 : Print the output
• Step 8 : Pop and output from the stack until it is not empty.
Infix expression:(a-b)/c*(d+e)-f
1. ( operator push (

2. a operand append a
-
3. - operator push (

4. b operand append ab

5. ) operator pop till (, append ab- discard (,)


6. / operator push
/
7. c operand append ab-c

8. * operator with equal precedence and L-R *


associativity pop stacktop and push *
9. ( operator push ab-c/
(
*
:(a-b)/c*(d+e)-f

10. d operand append ab-c/d

11. + operator stack top is ‘(‘, so push +


(
ab-c/de
12. e operand append *

13. ) operatorpop till ‘(‘ ab-c/de+

14. – operator lower precedence than * *


ab-c/de+*
pop and append * ,push –
-
15. f operand append ab-c/de+*f

16. pop all elements from stack and append


ab-c/de+*f-
ab–c/de+*f-
Infix to Prefix conversion
• Write the infix notation in reverse order.
• Step 1 : Scan the Infix Expression from left to right.
• Step 2 : If the scanned character is an operand, append it with final Infix to
Postfix string.
• Step 3 : Else,
– Step 3.1 : If the precedence order of the scanned(incoming) operator is greater than the
precedence order of the operator in the stack (or the stack is empty or the stack contains
Infix to Prefix conversion

a ‘)‘ push it on stack.


– Step 3.2 : If the precedence order of the scanned(incoming) operator is less than the
precedence order of the operator in the stack Pop all the operators from the stack which
are greater than or equal to in precedence than that of the scanned operator. After doing
that Push the scanned operator to the stack. (If you encounter parenthesis while popping
then stop there and push the scanned operator in the stack.)
– Step3.3 : the precedence order of the scanned(incoming) operator is equal to the
precedence order of the operator in the stack , go for Associativity.
• If L-R then push the operator
• If R-L then pop the stack top and then push the scanned operator
• Step 4 : If the scanned character is an ‘)‘ push it to the stack.
• Step 5 : If the scanned character is an ‘(’ pop the stack and output it until a ‘(‘is
encountered, and discard both the parenthesis.
• Step 6 : Repeat steps 2-6 until infix expression is scanned.
• Step 7 : Print the output
• Step 8 : Pop and output from the stack until it is not empty.
Infix expression:(a-b)/c*(d+e)-f
Reverse : f-)e+d(*c/)b-a( -

1. f operand append f
)
2. - operator push -
+
3. ) operator push fe )
-
4. e operand append
5. + operator push fed
6. d operand append fed+ -

7. ( operator pop till ) and append


8. * operator with higher precedence push *
9. c operand append fed+c *
-
/)b-a(
10. / operand equal precedence, L-R fed+c push )

11. ) operator push /


*
12. b operand append -

13. - operatorpush fed+cb _


)
14. a operand append fed+cba /

15. ( operator pop and append till ) *


-
fed+cba-
16. Pop all the elements and append
17. Reverse the expression fed+cba-/*-
/
-*/-abc+def *
-
Evaluation of postfix expressions
ab–c/de+*f-
Evaluation of prefix expressions using stack
1. Scan for the given prefix expression from right to left, one
element at a time.
2. Check whether it is an operand or an operator.
If it is an operand then push the element in to stack.
If it is an operator then pop the two element from the stack.
Apply the given operator on them and then push back the
result in to the stack.
<operand 1> operator <operand 2>
3. Repeat the step 1 and 2 until the whole prefix expression
comes to the end.
4. Pop the element from the stack which should be the only
element in the stack and return it from the function as the
result of the expression
-* / - a b c + d e f
F push
E push
D push
+ pop, pop d+e then push
C push
B push
A push
- pop, pop, a-b then push
/ pop, pop, (a-b)/c then push
* pop, pop, (a-b)/c * (d+e) then push
- pop, pop, (a-b)/c * (d+e) –f then push
Evaluation of postfix expressions using stack
1. Scan for the given postfix expression from left to right, one
element at a time.
2. Check whether it is an operand or an operator.
If it is an operand then push the element in to stack
If it is an operator then pop the two element from the stack.
Apply the given operator on them and then push back the
result in to the stack.
<operand 2> operator <operand 1>
3. Repeat the step 1 to 2 until the whole postfix expression
comes to the end.
4. Pop the element from the stack which should be the only
element in the stack and return it from the function as the
result of the expression
ab–c/de+*f–
A push
B push
- Pop pop a-b
C push
/ pop pop (a-b)/c
D push
E push
+ pop pop d+e
* Pop pop (a-b)/c*(d+e)
F push
- Pop pop (a-b)/c*(d+e)-f

You might also like