Two Applications ofStacks 1. Function Calls 2. Reverse Polish Notation (RPN)
3.
Use of Stacksin Function Calls Whenever a function begins execution (i.e is activated), an activation record (or stack frame) is created to store the current environment for that function which includes such things as: Parameters Caller’s state information (e.g contents of registers, return address) Local Variables Temporary Storage
4.
Use of Stacksin Function Calls (Cont…) Functions may call other functions and thus interrupt their own execution. A data structure is required to store the activation records of such functions so they can be recovered and the system reset when a function resumes execution. Such functions must have LIFO behavior, because when a function terminates, the function with which to resume execution is the last function whose activation record was saved. A stack is therefore the appropriate structure, and since it is manipulated at run time, it is called run time stack
5.
Use of Stacksin Function Calls (Cont…) When a function is called the following events occur A copy of its activation record is pushed onto the run-time stack Arguments are copied into the parameter spaces Control is transferred to the starting address of the body of the function Thus, the top activation record in the run-time stack is always that of the function currently being executed
6.
Use of Stacksin Function Calls (Cont…) When a function terminates, the run time stack is popped, which removes the activation record of the terminated function and exposes the activation record of the function that was previously executing and was interrupted. This activation record can then be used to restore the environment of the interrupted function so it can resume execution
7.
The taskof compiler is to generator the machine instructions. In most programming languages, arithmetic expressions are written in infix notation like a*b+c Many compilers first transform these infix expressions into postfix notations or prefix notations and then generate machine instructions to evaluate these expressions Application of Stacks to RPN
8.
INFIX, POSTFIX andPREFIX Infix: A+B-C Postfix (Reverse Polish Notation): AB+C- Prefix: -+ABC Order of Precedence of Operators Parentheses Exponentiation Multiplication/Division Addition/Subtraction Application of Stacks to RPN
9.
Infix: ( (A+B)*C-(D-E)) $ (F+G) Conversion to Postfix Expression ( (AB+)*C-(DE-) ) $ (FG+) ( (AB+C*)-(DE-) ) $ (FG+) (AB+C*DE--) $ (FG+) AB+C*DE- -FG+$ Exercise: Convert the following to Postfix ( A + B ) * ( C – D) A $ B * C – D + E / F / (G + H)
10.
Conversion to PrefixExpression The precedence rules for converting an expression from infix to prefix are identical. The only change from postfix is that the operator is placed before the operands rather than after them. Evaluating a Postfix Expression Each operator in a postfix string refers to the previous two operands in the string.
11.
Algorithm to EvaluateRPN Expressions INPUT: An RPN expression OUTPUT: The value of the RPN expression (unless an error occurred) Algorithm: 1. Initialize an empty stack 2. Repeat the following until the end of the expression is encountered: a. Get the next token (constant, variable, arithmetic operator) in the RPN expression b. If the token is an operand, push it onto the stack. If it is an operator, then do the following: i. Pop the top two values from the stack. (If the stack does not contain any two items, an error due to malformed RPN expression has occurred and evaluation is terminated) ii. Apply the operator to these two values iii. Push the resulting value back onto the stack. 3. When the end of the expression is encountered, its value is on top of the stack (and in fact, must be the only value in the stack)
12.
Conversion of InfixExpression to postfix 1. Expression Trees 2. Fully Parenthesize-move-erase method 3. Use of Stacks to convert infix to RPN
13.
Expression Trees Treesare pictured using circular nodes that store data, and these are connected by line segments called edges. One of the nodes, called the root, has no edges coming into it. Every other node is reachable from the root by a unique path, which is a sequence of connected edges. The root node is usually placed at the top of the tree diagram To illustrate, consider the following expression tree for the expression a*b: * a b
Conversion to RPNfrom Expression Tree Traverse the tree in Left-Right-Parent order For each node, before we can visit it we must first visit its left child and then its right child For the infix expression a*b+c, this traversal produces ab*c+ + c * a b
16.
Fully Parenthesis-move-erase method 1.Fully parenthesis the expression 2. Replace each right parenthesi by the corresponding operator 3. Erase all left parentheses Examples Three steps for converting a*b+c are: 1. ((a*b)+c) 2. ( ( a * b ) + c ) ((ab*c+ 3. ab*c+
17.
Fully Parenthesis-move-erase method Three steps for converting a*(b+c) are: 1. (a*(b+c)) 2. ( a * (b + c ) ) ((abc+* 3. abc+* Three steps for converting (a+b)*(c/(d-e)) are: 1. ((a+b)*(c/(d-e))) 2. ( ( a + b ) * ( c / ( d – e ) ) ) ((ab+(c(de-/* 3. ab+cde-/*
18.
Use of Stacksto Convert Infix to Postfix: Algorithm INPUT: An infix Expression OUTPUT: The RPN expression Algorithm: 1. Initialize an empty stack of operators 2. While no error has occurred and the end of the infix expression has not been reached, do the following: a. Get the next input token (constant, variable, arithmetic operator, left parenthesis, right parenthesis) in the infix expression. b. If token is i. A left parenthesis Push it onto the stack ii. A right parenthesis Pop and display stack elements until a left parenthesis is encountered, but do not display it. (It is an error if the stack becomes empty with no left parenthesis found.)
19.
Use of Stacksto Convert Infix to Postfix: Algorithm iii. An operator iv. An operand Display it. 3. When the end of the infix expression is reached, pop and display stack items until the stack is empty If the stack is empty or token has a higher priority than the top stack element, push token onto the stack. Otherwise, pop and display the top stack element; then repeat the comparison of token with the new top stack item Note: A left parenthesis in the stack is assumed to have a lower priority than that of operators.