Download presentation
Presentation is loading. Please wait.
Published byTabitha Elliott Modified over 5 years ago
1
PZ07A - Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000
2
Postfix Infix notation: Operator appears between operands: 2 + 3 5
3 + 6 9 Implied precedence: * 4 2 + (3 * 4 ), not (2 + 3 ) * 4 Prefix notation: Operator precedes operands: + 2 3 5 + 2 * 3 5 (+ 2 ( * 3 5 ) ) 17 Postfix notation: Operator follows operands: 2 3 + 5 2 3 * 5 + (( 2 3 * 5 +) 11 Called Polish postfix since few could pronounce Polish mathematician Lukasiewicz, who invented it. An interesting, but unimportant mathematical curiosity when presented in 1920s. Only became important in 1950s when Burroughs rediscovered it for their ALGOL compiler. PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000
3
Evaluation of postfix 1. If argument is an operand, stack it.
2. If argument is an n-ary operator, then the n arguments are already onthe stack. Pop the n arguments from the stack and replace by the value of the operator applied to the arguments. Example: * + stack stack stack replace 3 and 4 on stack by 7 stack 6. * - replace 5 and 7 on stack by 35 replace 35 and 2 on stack by 37 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000
4
Importance of Postfix to Compilers
Code generation same as expression evaluation. To generate code for * +, do: stack L-value of 2 stack L-value of 3 stack L-value of 4 generate code to take R-value of top stack element (L-value of 4) and add to R-value of next stack element (L-value of 3) and place L-value of result on stack stack L-value of 5 6. * - generate code to take R-value of top stack element (L-value of 5) and multiply to R-value of next stack element (L-value of 7) and place L-value of result on stack generate code to take R-value of top stack element (L-value of 35) and add to R-value of next stack element (L-value of 2) and place L-value of result (37) on stack PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000
5
Precedence of operators
Assumed order of evaluation for arithmetic expressions: 2*3+4*5 assumed to be 26 since assumed ordering is (2*3)+(4*5). This is specified by precedence of operators. In any expression, the highest precedence operations are evaluated first, and so on. Most languages have an implied precedence. APL and Smalltalk do not. Neither language emphasizes arithmetic data, so it is not clear what precedence means in this case. C has 17 levels of precedence (given next) PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000
6
C precedence levels Precedence Operators Operator names
tokens, a[k], f()Literals, subscripting, function call .,-> Selection , Postfix increment/decrement 15* , Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection typename Casts *, /, % Multiplicative operators , Additive operators <<, >> Shift <,>,<=, >= Relational ==, != Equality & Bitwise and Bitwise xor | Bitwise or && Logical and || Logical or ?: Conditional =, +=, -=, *=, Assignment /=, %=, <<=, >>=, &=, =, |= , Sequential evaluation PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.