1 / 34

STACK APPLICATION 2 CONVERTING FROM INFIX TO POSTFIX

STACK APPLICATION 2 CONVERTING FROM INFIX TO POSTFIX. INFIX NOTATION: AN OPERATOR IS PLACED BETWEEN ITS OPERANDS a + b c – d + (e * f – g * h) / i. OLD COMPILERS: INFIX MACHINE LANGUAGE THIS GETS MESSY BECAUSE OF PARENTHESES NEWER COMPILERS:

Download Presentation

STACK APPLICATION 2 CONVERTING FROM INFIX TO POSTFIX

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. STACK APPLICATION 2 CONVERTING FROM INFIX TO POSTFIX

  2. INFIX NOTATION: AN OPERATOR IS PLACED BETWEEN ITS OPERANDS a + b c – d + (e * f – g * h) / i

  3. OLD COMPILERS: INFIX MACHINE LANGUAGE THIS GETS MESSY BECAUSE OF PARENTHESES NEWER COMPILERS: INFIX POSTFIX MACHINE LANGUAGE

  4. IN POSTFIX NOTATION, AN OPERATOR IS PLACED IMMEDIATELY AFTER ITS OPERANDS. INFIX POSTFIX a + b ab+ a + b * c abc*+ a * b + c ab*c+ (a + b) * c ab+c* PARENTHESES NOT NEEDED – AND NOT USED – IN POSTFIX

  5. LET’S CONVERT AN INFIX STRING TO A POSTFIX STRING.    x – y * z

  6. POSTFIX PRESERVES THE ORDER OF OPERANDS, SO AN OPERAND CAN BE APPENDED TO POSTFIX AS SOON AS THAT OPERAND IS ENCOUNTERED IN INFIX.

  7. INFIX POSTFIX x – y * z x

  8. INFIX POSTFIX x – y * z x THE OPERANDS FOR ‘-’ ARE NOT YET IN POSTFIX, SO ‘-’ MUST BE TEMPORARILY SAVED SOMEWHERE.

  9. INFIX POSTFIX x – y * z xy

  10. INFIX POSTFIX x – y * z xy THE OPERANDS FOR ‘*’ ARE NOT YET IN POSTFIX, SO ‘*’ MUST BE TEMPORARILY SAVED SOMEWHERE, AND RESTORED BEFORE ‘-’.

  11. INFIX POSTFIX x – y * z xyz

  12. INFIX POSTFIX x – y * z xyz* –

  13. SUPPOSE, INSTEAD, WE STARTED WITH x*y-z AFTER MOVING x TO POSTFIX, * IS TEMPORARILY SAVED, AND THEN y IS MOVED TO POSTFIX. WHAT HAPPENS WITH ‘-’? INFIX POSTFIX x * y – z xy

  14. THE ‘*’ MUST BE MOVED TO POSTFIX NOW, BECAUSE BOTH OF THE OPERANDS FOR ‘*’ ARE ON POSTFIX. THEN THE ‘-’ MUST BE SAVED TEMPORARILY. AFTER ‘z’ IS MOVED TO POSTFIX, ‘-’ IS MOVED TO POSTFIX, AND WE ARE DONE. INFIX POSTFIX x * y – z xy*z–

  15. THE TEMPORARY STORAGE FACILITY IS A STACK. FOR EACH OPERATOR IN INFIX: LOOP UNTIL OPERATOR PUSHED: IF OPERATOR STACK IS EMPTY, PUSH ELSE IF INFIX OPERATOR HAS GREATER PRECEDENCE THAN TOP OPERATOR ON STACK, PUSH ELSE POP AND APPEND TO POSTFIX

  16. INFIX GREATER PUSH

  17. INFIX POSTFIX a + b * c / d - e

  18. INFIX POSTFIX a + b * c / d – e abc*d/+e – - / * + OPERATOR STACK

  19. CONVERT TO POSTFIX: x * (y + z)

  20. Top character on operator stack Action Taken ( +, - *, / empty Append to postfix Append to postfix Append to postfix Append to postfix I n f I x C h a r a c t e r Identifier Pop to postfix Pop to postfix Error ) Pop; pitch ‘(‘ Push Push Push Push ( Push Pop to postfix Pop to postfix Push +, - Push Push Pop to postfix Push *, / Error Pop to postfix Pop to postfix Done empty

  21. Token A token is the smallest meaningful unit in a program.

  22. Infix to Postfix Applet http://www.cs.lafayette.edu/~collinsw/infixapp/infix.html

More Related