0% found this document useful (0 votes)
5 views28 pages

Precedence and Associativty (1)

The document explains operator precedence and associativity in expressions, detailing how they determine the order of evaluation for operators. It provides examples of converting infix expressions to prefix and postfix notation using stacks, illustrating the process with various expressions. The document emphasizes the importance of understanding these concepts for correct expression evaluation.

Uploaded by

ellexbeige
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views28 pages

Precedence and Associativty (1)

The document explains operator precedence and associativity in expressions, detailing how they determine the order of evaluation for operators. It provides examples of converting infix expressions to prefix and postfix notation using stacks, illustrating the process with various expressions. The document emphasizes the importance of understanding these concepts for correct expression evaluation.

Uploaded by

ellexbeige
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

When there are several operators in an expression,

precedence (also known as operator precedence) dictates


the order in which they are evaluated. Higher precedence
operators are assessed before lower precedence operators.
The order in which operators with the same precedence
level are evaluated in an expression is determined by
associativity. It specifies whether evaluation occurs left-to-
right (left-associative) or right-to-left (right-associative).
Infix to Prefix:
Ex. 1 Precedence:
3+5
operator * has
greater
precedence
than +

* 2 Prefix: it moves the operator


before the evaluated operands

3+ Precedence: since the only


operator left to evaluate is +, 3
and 52* (considered as 1
*52 operand) will then be added

Prefix: it moves the operator

3+ before the evaluated operands


Infix to Prefix: using
Stack
3+5* 2
original
2* 5
reverse

+ 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 +

* 2 Postfix: it moves the operator


after the evaluated operands

3+ Precedence: since the only


operator left to evaluate is the
[+], 3 and *52 (considered as 1
52* operand) will then be added

Postfix: it moves the operator

3+ after the evaluated operands


Infix to Postfix: using
Stack 3 + 5
original

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

(8 - 3) * greater precedence than


any operator but is not
written in the prefix
-42 notation.
Prefix: Operators are written

-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

(8 - 3) * greater precedence than


any operator but is not
written in the postfix
42- notation.
Postfix: Operators are written

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,

+2 associativity comes into play.


Associativty is evaluated from left
to right

-53 + Prefix: Operators are written


before the operands
2
+-532
Infix to Prefix: using
Stack
(5-3) 2+)3
+2 Stac - 5 (
original expression reversed
k
Infix Expression Prefix
2 + 2
+
) + 2
3 ) 2
- + 23
)
Infix to Prefix: using
Infix Stack Stac Prefi
Expression k x
5 +)- 23
(
+ 5
235- 235
-
+
revers
e

+-
Infix to
Postfix:
(5-3) Associativity: since + and - has

+2 the same precedence,


associativity comes into play.
Associativty is evaluated from left

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+

You might also like