1.02k likes | 1.07k Views
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.
E N D
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 • 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
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)
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.
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)))
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 * * + )
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.
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).
Stack 2 is pushed onto the stack 2 is popped off of the stack 4 2 3 3 1 1 1 1 1
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.
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
Evaluating a postfix expression using a stack (1) Enter the postfix expression and click Step
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.
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 + + * +
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)))
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.