Precedence and Associativty (1)
Precedence and Associativty (1)
+ 3
expression
Infix Expressiono
Stac
py if it’s only a n operand d
Prefix
2
c
k
2 add to
* stack
* 2 cop
5 pr ecedence
* pop from y
+ stack
25
+ 2
Infix to Prefix: using
Infix Stack Stac Prefi
Expression k x
3 + 25*
25*3 3
+
revers
e
+3*5
2
Infix to Postfix:
Ex. 1 Precedence:
3+5
operator * has
greater
precedence
than +
Infix * 2Stac
expression
Posfi
Expression k x
3 copy if it’s
only an
3 operand
+ add to
stack + 3
+ 35
*
5 +* push to
35
2 stack
35
Infix to Postfix: using
Stack 3 + 5
original
Infix * 2Stack
expression
Postfi
Expression x
2 +*pop * 35
2
first
352
then pop
352 +
*
Infix to Prefix: Ex.
2
(8 - 3) * (4 - 2)
Precedence: parenthesis has a
-83 * -42
before
the
operands
*-83-42
Infix to Prefix: using
Stack
(8 - 3) * (4 )2 - 4( * )3
- 2) Stac - 8(
original expression reversed
) k )
Infix Expression Prefix
2 ) 2
- ) 2
4 - 24
( ) 24
-
Infix to Prefix: using
Stack
Infix Expression Stack Prefix
( 24-
* * 24-
) *) 24-
3 *) 24-3
- *)- 24-3
8 *)- 24-
38
Infix to Prefix: using
Stack
Infix Expression Prefix
Stack
24-
( * 38-
24-
38-*
reverse
*-83-
Infix to Postfix: Ex.
2
(8 - 3) * (4 - 2)
Precedence: parenthesis has a
83- * 42-
after
the
operands
83-42-*
Infix to Postfix: using
Stack (8 - 3) * (4
Infix Expression -Stack
2) Postfi
original expression
x
( (
8 ( 8
- (- 8
3 (- 83
) 83-
Infix to Postfix: using
Stack
Infix Expression Stack Postfix
* * 83-
( *( 83-
4 *( 83-4
- *(- 83-4
2 *(- 83-
42
) *( 83-
42-
Infix to Postfix: using
Stack
Infix Expression Postfix
Stack
83-
) 42-
*
83-
42-*
Infix to Prefix: Ex.
3
5-3 Associativity: since + and - has
the same precedence,
+-
Infix to
Postfix:
(5-3) Associativity: since + and - has
53- +2
to right
the
Postfix: Operators are written
operands
after
53-2
Infix to Postfix: using
Stack (5 - 3)
Infix Expression + 2
Stack Postfi
otriginal expression
x
( (
5 ( 5
- (- 5
3 (- 53
) 53-
Infix to Postfix: using
Infix Stack Stac Postfi
Expression k x
+ + 53-
+ 53-
2 2
53-
2+