1 / 97

Infix, Postfix and Stacks

Infix, Postfix and Stacks. Ordering of opcodes and operands. Another example of syntax is the ordering of opcode and operand(s). Postfix: operand(s) then opcode 4 5 + Works well with stacks Prefix: opcode then operand(s) + 4 5 Infix: operand opcode operand 4 + 5. Precedence.

cai
Download Presentation

Infix, Postfix and 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. Infix, Postfix and Stacks

  2. Ordering of opcodes and operands • Another example of syntax is the ordering of opcode and operand(s). • Postfix: operand(s) then opcode • 4 5 + • Works well with stacks • Prefix: opcode then operand(s) • + 4 5 • Infix: operand opcode operand • 4 + 5

  3. Precedence • Precedence is the order in which operations occur when an expression contains more than one operation. • Operations with higher precedence are performed before operators with lower precedence. • 1 + 2 * 3 - 4 • 1 + 6 - 4 (multiplication has higher precedence) • 7 - 4 (start on the left when operators have the same precedence) • 3

  4. Infix to postfix • To convert 1+2*3-4, put in parentheses even though they’re not strictly necessary for this expression • ((1+(2*3))-4) • Convert the innermost parentheses to postfix: 2*3 becomes 2 3 * • ((1+(2 3 *))-4) • Once a group is in postfix order, it should be thought of as a unit (in particular as a single operand (data)), nothing should come in between any of the parts of the group • Convert the next set of parentheses • ((1 2 3 * +)-4)

  5. Infix to postfix • The last step eliminated the innermost set of parentheses. Continue to convert from infix to postfix from the innermost to outermost parentheses. • (1 2 3 * + 4 -) • Note there is one overall set of parentheses that can be thrown away. Also note that the order of the numbers has not changed.

  6. Another example • 1+ (2+3) * 4 + (5 + 6) * ((7 + 8) * 9) • Add parentheses • 1+ ((2+3) * 4) + ((5 + 6) * ((7 + 8) * 9)) • Add parentheses • (1+ ((2+3) * 4)) + ((5 + 6) * ((7 + 8) * 9)) • Add parentheses • ((1+ ((2+3) * 4)) + ((5 + 6) * ((7 + 8) * 9))) • Convert innermost to postfix • ((1+ ((2 3 +) * 4)) + ((5 6 +) * ((7 8 +) * 9)))

  7. Another Example (Cont.) • ((1+ ((2 3 +) * 4)) + ((5 6 +) * ((7 8 +) * 9))) • ((1+ (2 3 + 4 * )) + ((5 6 +) * (7 8 + 9 * ))) • ((1 2 3 + 4 * +) + (5 6 +7 8 + 9 * *)) • ( 1 2 3 + 4 * + 5 6 +7 8 + 9 * * + )

  8. Postfix good for Hardware • Postfix order is better suited for hardware since one must prepare the inputs (a.k.a. the data, a.k.a. the operands) before operating on them to get an output. • Postfix is particularly well suited for architectures that use a stack to perform computations.

  9. The stack • A stack is a data structure (which may be implemented in hardware or in software) that holds a sequence of data but limits the way in which data is accessed. • A stack obeys the Last-In-First-Out (LIFO) protocol, the last item written (pushed) is the first item to be read (popped).

  10. Stack 2 is pushed onto the stack 2 is popped off of the stack 4 2 3 3 1 1 1 1 1

  11. Stack Pointer: don’t move all the data just change the pointer The stack pointer is pointing to the next available location in the stack. When it’s pointing at the 2, the 2 is no longer on the stack.

  12. Infix Evaluation • 1+ (2+3) * 4 + (5 + 6) * ((7 + 8) * 9) • 1+(5)*4 + (11)*((15)*9) • 1 + 20 + 11*135 • 1 + 20 + 1485 • 21 + 1485 • 1506

  13. Evaluating a postfix expression using a stack (1) Enter the postfix expression and click Step

  14. Evaluating a postfix expression using a stack (2)

  15. Evaluating a postfix expression using a stack (3)

  16. Evaluating a postfix expression using a stack (4)

  17. Evaluating a postfix expression using a stack (5)

  18. Evaluating a postfix expression using a stack (6)

  19. Evaluating a postfix expression using a stack (7)

  20. Evaluating a postfix expression using a stack (8)

  21. Evaluating a postfix expression using a stack (9)

  22. Evaluating a postfix expression using a stack (10)

  23. Evaluating a postfix expression using a stack (11)

  24. Evaluating a postfix expression using a stack (12)

  25. Evaluating a postfix expression using a stack (13)

  26. Evaluating a postfix expression using a stack (14)

  27. Evaluating a postfix expression using a stack (15)

  28. Evaluating a postfix expression using a stack (16)

  29. Evaluating a postfix expression using a stack (17)

  30. Evaluating a postfix expression using a stack (18)

  31. Evaluating a postfix expression using a stack (19)

  32. Evaluating a postfix expression using a stack (20)

  33. Evaluating a postfix expression using a stack (21)

  34. Evaluating a postfix expression using a stack (22)

  35. Evaluating a postfix expression using a stack (23)

  36. Evaluating a postfix expression using a stack (24)

  37. Evaluating a postfix expression using a stack (25)

  38. Evaluating a postfix expression using a stack (26)

  39. Evaluating a postfix expression using a stack (27)

  40. Evaluating a postfix expression using a stack (28)

  41. Evaluating a postfix expression using a stack (29)

  42. Evaluating a postfix expression using a stack (30)

  43. Evaluating a postfix expression using a stack (31)

  44. Evaluating a postfix expression using a stack (32)

  45. Evaluating a postfix expression using a stack (33)

  46. Evaluating a postfix expression using a stack (34)

  47. Infix to Postfix (Approach 1) • 9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1)) • Introduce parentheses that do not change the order of operations • 9 + (8 + 7) * 6 + 5 * (4 + ((3 * 2) + 1)) • 9 + ((8 + 7) * 6) + (5 * (4 + ((3 * 2) + 1))) • (9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1))) • ((9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1)))) • Note that there are nine operands, eight operators, eight left parentheses and eight right parentheses.

  48. Infix to Postfix (Approach 1, Cont.) • ((9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1)))) • Convert the innermost parentheses to postfix • ((9 + ((8 7 +) * 6)) + (5 * (4 + ((3 2 *) + 1)))) • ((9 + ((8 7 +) 6 *)) + (5 * (4 + ((3 2 *) 1 +)))) • ((9 ((8 7 +) 6 *) +) + (5 * (4 ((3 2 *) 1 +) +))) • ((9 ((8 7 +) 6 *) +) + (5 (4 ((3 2 *) 1 +) +) *)) • ((9 ((8 7 +) 6 *) +) (5 (4 ((3 2 *) 1 +) +) *) +) • 9 8 7 + 6 * +5 4 3 2 * 1 + + * +

  49. Backwards • 9 8 7 + 6 * +5 4 3 2 * 1 + + * + • 9 (8 + 7) 6 * +5 4 3 2 * 1 + + * + • 9 ((8 + 7) * 6) +5 4 3 2 * 1 + + * + • (9 + ((8 + 7) * 6)) 5 4 3 2 * 1 + + * + • (9 + ((8 + 7) * 6)) 5 4 (3 * 2) 1 + + * + • (9 + ((8 + 7) * 6)) 5 4 ((3 * 2) + 1) + * + • (9 + ((8 + 7) * 6)) 5 (4 + ((3 * 2) + 1)) * + • (9 + ((8 + 7) * 6)) (5 * (4 + ((3 * 2) + 1))) + • (9 + ((8 + 7) * 6)) + (5 * (4 + ((3 * 2) + 1)))

  50. Infix to Postfix • The approach taken for converting infix to postfix does not make for a good algorithm as it requires too many passes. • One passes over the expression introducing parentheses • One pass over the expression converting inner parentheses to postfix • Fortunately there is a more efficient algorithm that requires only one pass through the expression.

More Related