Software Layers 2 Introduction to unix 2


Application of stacks – evaluation of arithmetic expressions



Download 0.58 Mb.
Page20/26
Date28.01.2017
Size0.58 Mb.
#10070
1   ...   16   17   18   19   20   21   22   23   ...   26

Application of stacks – evaluation of arithmetic expressions


  • Stacks are useful for backtracking problems (eg: navigating a maze)  previous states are recorded so that algorithmic progress may occur from them at a later time

  • Stacks are the implementation technique used for embodying backtracking

  • some compilers convert the infix expressions used in programming to postfix expressions, and generate machine code to evaluate them.

    • In infix the operator is written between the operands:
      A * B + (C - D / E)

    • In postfix the operator is written after the operands:
      A B * C D E / - +

    • In postfix notation, ()s are needed. ie. the evaluation is unambiguous and does not rely on operator precedence.




  • Converting infix to postfix:

    • By hand, add ()s to all subexpressions, then move the operators to the position of the corresponding ), and delete (s. eg.

A * B + (C - D / E)

((A * B) + (C - (D / E)))

((A B* (C (D E/-+

A B* C D E/-+




    • Here is an algorithm that uses backtracking via a stack for converting infix to postfix:

Initialise stack

Read symbol ch

While not end of expression

If ch is an operand

Append it to the postfix

If ch is (

Push ch

If ch is )

Pop stack

While popped value is not (

Append popped value to postfix

Pop stack

If ch is an operator

While stack not empty, and

top of stack != (, and

priority of top of stack >= priority of ch

Pop stack

Append popped value to postfix

Push ch

Read symbol ch

While stack not empty

Pop stack

Append popped value to postfix


    • eg run: (top of the stack is the leftmost element)

Infix

Stack

Postfix

A * B + (C - D / E)

Empty




* B + (C - D / E)

Empty

A

B + (C - D / E)

*

A

+ (C - D / E)

*

AB

(C - D / E)

+

AB*

C - D / E)

(+

AB*

- D / E)

(+

AB*C

D / E)

-(+

AB*C

/ E)

-(+

AB*CD

E)

/-(+

AB*CD

)

/-(+

AB*CDE

Empty

+

AB*CDE/-

Empty

Empty

AB*CDE/-+

 

  • Evaluating postfix:

    • once an expressions has been parsed from infix to postfix, another backtracking algorithm is used to evaluate the expression

    • the contents of the stack here are: variable names & values

Read symbol ch

While not end of expression

If ch is an operand

Push ch

Else Pop RHS

Pop LHS

Compute LHS ch RHS

Push result

Read symbol ch

Pop stack and print result




    • eg: A = 5, B = 3, C = 6, D = 8, E = 2

      Postfix

      Stack

      AB*CDE/-+

      Empty

      B*CDE/-+

      A

      *CDE/-+

      BA

      CDE/-+

      15

      DE/-+

      C 15

      E/-+

      DC 15

      /-+

      EDC 15

      -+

      4 C 15

      +

      2 15

      Empty

      17

    • And the answer is 17.




Download 0.58 Mb.

Share with your friends:
1   ...   16   17   18   19   20   21   22   23   ...   26




The database is protected by copyright ©ininet.org 2024
send message

    Main page