1 / 28

Stacks

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.

leone
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks (Continuation) Applications

  2. Parse Parentheses

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

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

  5. 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*+

  6. 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*+

  7. 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 ithoperator, 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.

  8. Infix to Postfix Transformation • Priorities: • 2: * / • 1: + - • 0: (

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

  10. Backtracking

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

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

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

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

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

  16. Problem-Example 1 Stack Comment Paramenter PAR1 Paramenter PAR2 Paramenter PAR3 Paramenter PAR4 The old contents of R0 The old contents of R1 Address for return to the calling program • PAR1 • PAR2 • PAR3 • PAR4 • (R0) • (R1) • RETURN ADDRESS

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

  18. Problem-Example 2 Stack Comment Paramenter D Parameter C Parameter B R0 from SUB1 The old contents of R1 Return address for SUB2 Parameter A The old contents of R0 Return address for SUB1 • D • C • B • (R0) • (R1) • SUB 2 RETURN ADDR. • A • (R0) • SUB 1 RETURN ADDR.

  19. Recursive function (subroutine) and stack

  20. Factorial (3): Decomposition and solution

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

More Related