THE POLI SH NOTATI ON Arithmetic and Logical Expressions ! - - PDF document

the poli sh notati on arithmetic and logical expressions
SMART_READER_LITE
LIVE PREVIEW

THE POLI SH NOTATI ON Arithmetic and Logical Expressions ! - - PDF document

THE POLI SH NOTATI ON Arithmetic and Logical Expressions ! repeatedly scan through the expression ! take parentheses and priorities of operators into account a + b + c * d - e / g a + b + ( c * d ) - ( e / g ) a + (( b + c ) * d - e ) / g a +


slide-1
SLIDE 1

THE POLI SH NOTATI ON

slide-2
SLIDE 2
  • S. Prasitjutrakul 1994

! repeatedly scan through the expression ! take parentheses and priorities of operators into

account

Arithmetic and Logical Expressions

a + b + c * d - e / g a + b + ( c * d ) - ( e / g ) a + (( b + c ) * d - e ) / g a + b <= c && a + b <= d ( a + b <= c ) || ( a + b <= d )

slide-3
SLIDE 3
  • S. Prasitjutrakul 1994

The Polish Notations

Q : How can a compiler accept an expression and produce correct code ? A : Tranforming the expression into a form called Polish notation

Infix form

a * b a + b * c (a + b) * c

Prefix form

* a b + a * b c * + a b c

Postfix form

a b * a b c * + a b + c *

Reverse Polish notation

slide-4
SLIDE 4
  • S. Prasitjutrakul 1994

Expression Evaluations : Stacks

5 * ( ( ( 9 + 8 ) + ( 4 * 6 ) ) - 7 ) Postfix form : 5 9 8 + 4 6 * + 7 - * Push( 5 ) Push( 9 ) Push( 8 ) Push( Pop() + Pop() ) Push( 4 ) Push( 6 ) Push( Pop() * Pop() ) Push( Pop() + Pop() ) Push( 7 ) Push( Pop() - Pop() ) Push( Pop() * Pop() )

slide-5
SLIDE 5
  • S. Prasitjutrakul 1994

Expression Evaluations : Stacks

5 * ( ( ( 9 + 8 ) + ( 4 * 6 ) ) - 7 ) Postfix form : 5 9 8 + 4 6 * + 7 - * 5 9 5 8 9 5 17 5 4 17 5 6 4 17 5 24 17 5 41 5 7 41 5 34 5 170

slide-6
SLIDE 6
  • S. Prasitjutrakul 1994

I nfix Form → Postfix Form

A / B ? C + D * E - A * C ( ( ( A / ( B ? C ) ) + ( D * E ) ) - ( A * C ) ) A B C ? / D E * + A C * -

slide-7
SLIDE 7
  • S. Prasitjutrakul 1994

I nfix Form → Postfix Form

A + B * C A + B * C A + B * C + A A A B + A + B * C A B * + A + B * C A B C * + A + B * C A B C * + A + B * C A B C * +

slide-8
SLIDE 8
  • S. Prasitjutrakul 1994

I nfix Form → Postfix Form

(A+B)*C (A+B)*C + ( A A ( A B + ( A B + A B + * A B + C (A+B)*C (A+B)*C (A+B)*C (A+B)*C (A+B)*C ( * A B + C * (A+B)*C

slide-9
SLIDE 9
  • S. Prasitjutrakul 1994

I nfix Form → Postfix Form

X-(A+B)*C X-(A+B)*C + (

  • X A

X A (

  • X A B

+ (

  • X A B +

X A B + *

  • X A B + C

X-(A+B)*C X-(A+B)*C X-(A+B)*C X-(A+B)*C X-(A+B)*C

  • X A B + C * -

X-(A+B)*C X X-(A+B)*C X-(A+B)*C X (

  • X
  • *
slide-10
SLIDE 10
  • S. Prasitjutrakul 1994

Operator Priorities

Symbol In-Stack Priority In-Coming Priority ) - - ? 3 4 *, / 2 2 +, - 1 1 ( 0 4

Operators are taken out of the stack as long as the in-stack priority is greater than or equal to the in-coming priority of the new operator.

? * input : a*b?2 + 3

  • utput: ab2

: ab2?∗