infix, postfix and stacks

Post on 05-Jan-2016

79 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

CSIT 301 (Blum) 1

Infix, Postfix and Stacks

CSIT 301 (Blum) 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

CSIT 301 (Blum) 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

CSIT 301 (Blum) 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)

CSIT 301 (Blum) 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.

CSIT 301 (Blum) 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)))

CSIT 301 (Blum) 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 * * + )

CSIT 301 (Blum) 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.

CSIT 301 (Blum) 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).

CSIT 301 (Blum) 10

Stack

1 1

2

1

3

1

3

4

1

2 is pushed onto the stack

2 is popped off of the stack

CSIT 301 (Blum) 11

Stack Pointer: don’t move all the data just change the pointer

X

X

X

X

X

1

X

X

X

X

2

1

X

X

X

X

2

1

X

X

X

X

3

1

X

X

X

4

3

1

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.

CSIT 301 (Blum) 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

CSIT 301 (Blum) 13

Evaluating a postfix expression using a stack (1)

Enter the postfix expression and click Step

CSIT 301 (Blum) 14

Evaluating a postfix expression using a stack (2)

CSIT 301 (Blum) 15

Evaluating a postfix expression using a stack (3)

CSIT 301 (Blum) 16

Evaluating a postfix expression using a stack (4)

CSIT 301 (Blum) 17

Evaluating a postfix expression using a stack (5)

CSIT 301 (Blum) 18

Evaluating a postfix expression using a stack (6)

CSIT 301 (Blum) 19

Evaluating a postfix expression using a stack (7)

CSIT 301 (Blum) 20

Evaluating a postfix expression using a stack (8)

CSIT 301 (Blum) 21

Evaluating a postfix expression using a stack (9)

CSIT 301 (Blum) 22

Evaluating a postfix expression using a stack (10)

CSIT 301 (Blum) 23

Evaluating a postfix expression using a stack (11)

CSIT 301 (Blum) 24

Evaluating a postfix expression using a stack (12)

CSIT 301 (Blum) 25

Evaluating a postfix expression using a stack (13)

CSIT 301 (Blum) 26

Evaluating a postfix expression using a stack (14)

CSIT 301 (Blum) 27

Evaluating a postfix expression using a stack (15)

CSIT 301 (Blum) 28

Evaluating a postfix expression using a stack (16)

CSIT 301 (Blum) 29

Evaluating a postfix expression using a stack (17)

CSIT 301 (Blum) 30

Evaluating a postfix expression using a stack (18)

CSIT 301 (Blum) 31

Evaluating a postfix expression using a stack (19)

CSIT 301 (Blum) 32

Evaluating a postfix expression using a stack (20)

CSIT 301 (Blum) 33

Evaluating a postfix expression using a stack (21)

CSIT 301 (Blum) 34

Evaluating a postfix expression using a stack (22)

CSIT 301 (Blum) 35

Evaluating a postfix expression using a stack (23)

CSIT 301 (Blum) 36

Evaluating a postfix expression using a stack (24)

CSIT 301 (Blum) 37

Evaluating a postfix expression using a stack (25)

CSIT 301 (Blum) 38

Evaluating a postfix expression using a stack (26)

CSIT 301 (Blum) 39

Evaluating a postfix expression using a stack (27)

CSIT 301 (Blum) 40

Evaluating a postfix expression using a stack (28)

CSIT 301 (Blum) 41

Evaluating a postfix expression using a stack (29)

CSIT 301 (Blum) 42

Evaluating a postfix expression using a stack (30)

CSIT 301 (Blum) 43

Evaluating a postfix expression using a stack (31)

CSIT 301 (Blum) 44

Evaluating a postfix expression using a stack (32)

CSIT 301 (Blum) 45

Evaluating a postfix expression using a stack (33)

CSIT 301 (Blum) 46

Evaluating a postfix expression using a stack (34)

CSIT 301 (Blum) 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.

CSIT 301 (Blum) 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 + + * +

CSIT 301 (Blum) 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)))

CSIT 301 (Blum) 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.

CSIT 301 (Blum) 51

Infix to Postfix (Approach 2)

• This approach involves a stack used for operators. • Proceed through the expression from left to right. • Operands (numbers) are automatically added to

the postfix expression we are generating (i.e. not put on the operator stack).

• What happens to a new operator depends on its precedence compared to that of the operator on the top of the stack.

CSIT 301 (Blum) 52

Infix to Postfix (Approach 2, Cont.)

• If the new operator has a higher precedence than the one on the top of the stack, it is pushed onto the stack.

• If the top of the stack is an open parenthesis, then the new operator is pushed onto the stack.

• If the operator has an equal or lower precedence than the one on the top of the stack, then the top of the stack is popped and added to the postfix expression until this is no longer the case. Then the current operator is pushed onto the stack.

CSIT 301 (Blum) 53

Infix to Postfix (Approach 2, Cont.)

• Left (opening parentheses) are always added to the stack.

• Right (closing parentheses) lead to everything up to and including the corresponding left parenthesis to be popped from the stack.

CSIT 301 (Blum) 54

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack Postfix Expression

9

Comment: Add 9 to expression.

CSIT 301 (Blum) 55

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

Postfix Expression

9

Comment: Push + onto operator stack.

CSIT 301 (Blum) 56

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

(

+

Postfix Expression

9

Comment: Push ( onto operator stack.

CSIT 301 (Blum) 57

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

(

+

Postfix Expression

98

Comment: Add 8 to postfix expression.

CSIT 301 (Blum) 58

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

(

+

Postfix Expression

98

Comment: Push + onto operator stack.

CSIT 301 (Blum) 59

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

(

+

Postfix Expression

987

Comment: Add 7 to postfix expression.

CSIT 301 (Blum) 60

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

Postfix Expression

987+

Comment: Pop + and ( from operator stack, add + to postfix expression.

CSIT 301 (Blum) 61

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

*

+

Postfix Expression

987+

Comment: Push * onto operator stack.

CSIT 301 (Blum) 62

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

*

+

Postfix Expression

987+6

Comment: Add 6 to postfix expression.

CSIT 301 (Blum) 63

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

Postfix Expression

987+6*+

Comment: Pop * and + off the stack and add them to the postfix expression, then push the + onto the stack.

CSIT 301 (Blum) 64

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

Postfix Expression

987+6*+5

Comment: Add 5 to the postfix expression.

CSIT 301 (Blum) 65

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

*

+

Postfix Expression

987+6*+5

Comment: Push * onto the operator stack.

CSIT 301 (Blum) 66

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

(

*

+

Postfix Expression

987+6*+5

Comment: Push ( onto the operator stack.

CSIT 301 (Blum) 67

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

(

*

+

Postfix Expression

987+6*+54

Comment: Add 4 to the postfix expression.

CSIT 301 (Blum) 68

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

(

*

+

Postfix Expression

987+6*+54

Comment: Push + onto the operator stack.

CSIT 301 (Blum) 69

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

(

+

(

*

+

Postfix Expression

987+6*+54

Comment: Push ( onto the operator stack.

CSIT 301 (Blum) 70

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

(

+

(

*

+

Postfix Expression

987+6*+543

Comment: Add 3 to the postfix expression.

CSIT 301 (Blum) 71

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

*

(

+

(

*

+

Postfix Expression

987+6*+543

Comment: Push * onto the operator stack.

CSIT 301 (Blum) 72

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

*

(

+

(

*

+

Postfix Expression

987+6*+5432

Comment: Add 2 to the postfix expression.

CSIT 301 (Blum) 73

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

(

+

(

*

+

Postfix Expression

987+6*+5432*

Comment: Pop * from the operator stack and add it to the postfix expression, then push + onto operator stack.

CSIT 301 (Blum) 74

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

(

+

(

*

+

Postfix Expression

987+6*+5432*1

Comment: Add 1 to the postfix expression.

CSIT 301 (Blum) 75

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

+

(

*

+

Postfix Expression

987+6*+5432*1+

Comment: Pop the + and ( from the operator stack, add + to the postfix expression.

CSIT 301 (Blum) 76

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack

*

+

Postfix Expression

987+6*+5432*1++

Comment: Pop the + and ( from the operator stack, add + to the postfix expression.

CSIT 301 (Blum) 77

9 + (8 + 7) * 6 + 5 * (4 + (3 * 2 + 1))

Operator Stack Postfix Expression

987+6*+5432*1++*+

Comment: Pop the * and then the + from the operator stack, add them the postfix expression.

CSIT 301 (Blum) 78

Another use for a stack

• We have seen a stack used for translating an infix expression into postfix.

• We have also seen a stack used to calculate a postfix expression.

• Another use of a stack is in subroutine and/or function calls.

CSIT 301 (Blum) 79

Input port 1 Accumulator

ALU FlagsInput port 2

Prog. counter

Mem.Add.Reg.

Memory

MDR

Instr. Reg.

Control

C

B

TMP

Output port 3

Output port 4

Display

Keyboard encoder

Bus

CSIT 301 (Blum) 80

Jump

• A processor generally assumes that the next statement to be executed is the next line of the code stored in memory. – Recall part of the fetch-execute cycle is to

increment the program counter (PC).

• A jump or goto alters this usual plan. A new value is placed into the program counter.

CSIT 301 (Blum) 81

Conditional Jump

• A conditional jump will change the PC or leave it unchanged based on the state of one of the Flag registers. – The ALU can perform some comparison and place the

result in a flag register, then the flag’s value could eventually be fed to the PC load control. So the PC is loaded (changed) or not based on the flag which came from the comparison.

• Conditional jumping gives the programmer ifs and loops.

CSIT 301 (Blum) 82

Calling a subroutine

• A subroutine call involves a jump. This “passes the control” to the subroutine. But a subroutine call differs from an ordinary jump in that one wants to return. – One reason for having subroutines is to reduce

repeated code. The subroutine may be called from any number of locations, so the return address is not known until the program is running.

CSIT 301 (Blum) 83

Storing the PC

• Prior to executing the jump portion of a call, the PC holds the address of the line of code immediately following the call instruction. And this is where one wants to be upon returning from the subroutine. Thus we have to store the value of the program counter (somewhere) and then change the value of the PC (i.e. execute the jump)

CSIT 301 (Blum) 84

A subroutine can call another subroutine that can call another subroutine that ….

• There cannot be a simple register for storing the PC (return address) because a subroutine can call another subroutine requiring two stored values.

CSIT 301 (Blum) 85

Calling and Returning

• Calling: If Main calls RoutineA, we store the MainReturnAddress. Then if RoutineA calls RoutineB, we store RoutineAReturnAddress.

• Returning: RoutineB returns control to RoutineA so we first need the last return address stored (RoutineAReturnAddress). Then RoutineA returns the control to Main and we need MainReturnAddress.

• The last return address stored is the first one needed, this is the protocol of a stack. (LIFO)

CSIT 301 (Blum) 86

Call is to Push as Return is to Pop

• A subroutine call thus requires pushing the value of the PC onto a stack (and then changing the value of the PC).

• A return from a subroutine requires popping the return address from the stack (and then placing that value in the PC).

CSIT 301 (Blum) 87

More to it

• Subroutines and functions typically have arguments/parameters. This information is not known until run-time and must be passed to the subroutine/function.

• There must be memory locations (where are they?) to serve as the variables in the subroutine. And any passed information must be written to the appropriate location.

CSIT 301 (Blum) 88

By reference or by value

• The arguments of the subroutine call can be actual numbers– Y = sin(3)

• Or they could be variables associated with the caller. – Y = sin(x)

• There are two possible scenarios in the later case, by value and by reference.

CSIT 301 (Blum) 89

By value

• Passing a parameter by value means that the memory location associated with the subroutine will hold a value and that that value will be written into the memory location when the function is called.

• Any changes to that memory location have no effect of the memory location associated with the caller.

CSIT 301 (Blum) 90

By reference

• Passing a parameter by reference means that the memory location associated with the subroutine will hold the address of the variable in the caller.

• All actions on the variable in the subroutine are of the indirect variety (recall the different addressing modes). One goes to this location to find an address, then goes to that address, ….

• The caller and the called write values to the same memory location.

CSIT 301 (Blum) 91

Return value

• In addition to the passed parameters and any local variables required, a function also has a memory location associated with the value it will send back.

CSIT 301 (Blum) 92

Allowing more than one version of a subroutine to be active

• Where are all of these memory locations associated with subroutines?

• The answer can depend on the how many active versions of a given subroutine one allows. – A subroutine is considered active if it has

started executing but hasn’t finished executing. That doesn’t imply it has control as it may have called some other routine before finishing.

CSIT 301 (Blum) 93

Activation Record

• All of the information required for an active subroutine – Return address – Passed parameters– Local variables– Return value

constitute what is called the activation record.

CSIT 301 (Blum) 94

Multiprocessing and recursion

• One way in which a CPU may end up with more than one active versions of a subroutine is if it is multiprocessing, running more than one program at once (actually swapping between programs to give the illusion of running more than one.)

• But even a single process may require multiple active versions of a subroutine if the subroutine is recursive.

CSIT 301 (Blum) 95

One can’t know ahead of time

• If one allows for recursive subroutines, then one cannot know beforehand (at compile time) how much memory will be used by a subroutine since each active version requires separate memory. – Recursion programs can be very memory intensive.

• A common approach is that when a subroutine is called, an activation record is placed on the run-time stack. – For this reason, activation records are sometimes called

stack frames.

CSIT 301 (Blum) 96

One more thing to remember• Recall that the accumulator and other registers

serve as a scratch pad area for the ALU. • The ALU may be in the middle of some calculation

when the subroutine is called and it may need to remember those temporary, scratch-pad values. – z = sin(x) + sqrt(y)– We cannot lose our result for sin(x) when we evaluate

sqrt(y)

• Thus the stack frame may also include a copy of various register values.

CSIT 301 (Blum) 97

References

• Computer Architecture, Nichols Carter

• Modern Programming Languages, Adam Brooks Webber

• Computers Systems: Organization and Architecture, John Carpinelli

• http://web.engr.oregonstate.edu/~minoura/cs261/javaProgs/stack/Polish.html

top related