Stacks: (Continuation) Applications

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

Stacks

(Continuation)
Applications
Parse Parentheses
Infix to Postfix Transformation
• Infix to Postfix transformation is a very
important part of preprocessing of the
source codes that are written in high-level
languages.
• This transformation separates operands
from operators
Infix to Postfix Transformation
• A * B  AB*
• Which transformation is correct?
• A*B+C  ? ABC*+
• A*B+C  ?  AB*C+
• To ensure a correct representation, a
precedence rule must be taken into
account
• Thus, the second representation is correct
Infix to Postfix Transformation
• The simplest visible solution is to push all the
operators into the stack and then to pop the
stack.
• However, this will work only for the simplest
expression with the two operands A*B  AB*
• This will not work for the expression with more
than two operands A+B*C ≠ ABC+* because a
precedence rule is not taken into account in this
solution.
• ABC+*  A*(B+C), but we need to represent
A+B*C, that is we need to obtain the
representation ABC*+
Infix to Postfix Transformation
• Let us obtain A+B*C  ABC*+
Expression Stack

Copy operand A to output expression. A


Push operator + into stack. A +
Copy operand B to output expression. AB +
Push operator * into stack (Priority of * is higher than +) AB *+
Copy operand C to output expression. ABC *+
Pop operator * and copy to output expression. ABC* +
Pop operator + and copy to output expression. ABC*+
Infix to Postfix Transformation
• Final algorithm (the most general case):

Copy the ith operand to output expression.


Push the ith operator into stack.
Copy the next (i+1st) operand to output expression.
If priority of the next operator is higher than priority of the ith operator
then push it into stack, otherwise pop the ith operator, copy it to output
expression and then push the next operator into stack.
Repeat the previous steps until the last operand will be copied to
output expression.
Pop the operators reminded and copy them to output expression.
Infix to Postfix Transformation
• Priorities:
• 2: * /
• 1: + -
• 0: (
Backtracking
• Backtracking is a method to find a proper
path to some certain goal, from a number
of the ones. This method is widely used in
decision analysis, expert systems and
computer gaming.
• Goal seeking is a typical backtracking
algorithm of finding the unique path to a
desired goal.
Backtracking
Stacks and Subroutines
• All the parameters are transferred from
(to) a calling program to (from) a
subroutine through a stack
• All the necessary system data (the content
of processor registers, the content of the
processor status word (PSW), the
returning address, etc.) must also be
pushed in the stack before a subroutine
will be called
Stacks and Subroutines
• A stack frame is a subspace within the stack that
is assigned for a particular subroutine.
• The stack frame contains the following elements:
1) the parameters to be processed by the called
algorithm; 2) the system data (register and PSW
content) in the calling algorithm; 3) the return
address in the calling algorithm; 4) the
expression that is to receive the return value (if
any, in the case of function-subroutine).
Parameter Passing
• It is also necessary to take into account
that general purpose processor registers
can be used by the calling program and by
the subroutine separately. This means that
their contents must be preserved before
calling the subroutine.
Stack Frame
• A private work space for the subroutine inside the stack,
created at the time the subroutine is entered and freed
up when the subroutine returns control to the calling
program is called a stack frame.
• To save the memory space and to simplify access to
data used by the subroutine, the local memory variables
used by the subroutine can also be placed in the stack
frame.
• The frame pointer (FP) is a pointer register which is
used for access to the parameters passed to the
subroutine and to the local memory variables used by
the subroutine.
Problem-Example 1
• Suppose we need to allocate the
subroutine in the memory. This subroutine
will use registers R0, R1,, and 4
parameters PAR1, PAR2, PAR3, and
PAR4 that will be passed from the calling
program
Problem-Example 1
Stack Comment
• PAR1 • Paramenter PAR1
• PAR2 • Paramenter PAR2
• PAR3 • Paramenter PAR3
• PAR4 • Paramenter PAR4
• (R0) • The old contents of R0
• (R1) • The old contents of R1
• RETURN ADDRESS • Address for return to the
calling program
Problem-Example 2
• Suppose we need to allocate two
subroutines in the memory: SUB1 and
SUB2.
• SUB1 will use register R0, and parameter
A passed from the calling program
• SUB2 will be called from SUB1 and will
use registers R0, R1, and parameters
B,C,D passed from SUB1
Problem-Example 2
Stack Comment
• D • Paramenter D
• C • Parameter C
• B • Parameter B
• (R0) • R0 from SUB1
• (R1) • The old contents of R1
• SUB 2 RETURN ADDR. • Return address for SUB2
• A • Parameter A
• (R0) • The old contents of R0
• SUB 1 RETURN ADDR. • Return address for SUB1
Recursive function (subroutine)
and stack
Factorial (3):
Decomposition and solution
Homework
• P.140 Exercises 3 a, b, c (only postfix, try
both manual and algorithmic methods)
• P.140 Exercise 4 a, b
• P. 140 Exercise 5

You might also like