expressions

12
Expressions Programming Language Design and Implementatio n (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.1-8.2

Upload: sydnee

Post on 05-Jan-2016

14 views

Category:

Documents


0 download

DESCRIPTION

Expressions. Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 8.1-8.2. Execution-time representation. Machine code sequence Tree representation Pre-fix or post-fix. Tree structure rules. Slow  software interpreter - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Expressions

Expressions

Programming Language Design and Implementation (4th Edition)

by T. Pratt and M. ZelkowitzPrentice Hall, 2001

Section 8.1-8.2

Page 2: Expressions

Expressions 2

Execution-time representation

Machine code sequence Tree representation Pre-fix or post-fix

Page 3: Expressions

Expressions 3

Tree structure rules

Slow software interpreter– parse tree

Problem – Uniform evaluation rules

• Eager evaluation ? Lazy evaluation (LISP, PROLOG)• Z+(Y==0 ? X : X/Y)

– Side effect• a * fun(x) + a

– Error conditions• Optimization, …

– Short-circuit boolean expression• if((A==0) || (B/A)>C) {…}• while((I<=UB) && (V[I]>0)) {…}

Page 4: Expressions

Expressions 4

PostfixInfix notation: Operator appears between operands:

2 + 3 53 + 6 9

Implied precedence: 2 + 3 * 4 2 + (3 * 4 ), not (2 + 3 ) * 4

Prefix notation: Operator precedes operands:+ 2 3 5+ 2 * 3 5 (+ 2 ( * 3 5 ) ) + 2 15 17

Postfix notation: Operator follows operands:2 3 + 52 3 * 5 + (( 2 3 * 5 +) 6 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.

Page 5: Expressions

Expressions 5

Evaluation of postfix

1. If argument is an operand, stack it.2. If argument is an n-ary operator, then the n argumen

ts are already onthe stack. Pop the n arguments from the stack and replace by the value of the operator applied to the arguments.

Example: 2 3 4 + 5 * +1. 2 - stack2. 3 - stack3. 4 - stack4. + - replace 3 and 4 on stack by 75. 5 - stack6. * - replace 5 and 7 on stack by 357. + - replace 35 and 2 on stack by 37

Page 6: Expressions

Expressions 6

Importance of Postfix to Compilers

Code generation same as expression evaluation.To generate code for 2 3 4 + 5 * +, do:1. 2 - stack L-value of 22. 3 - stack L-value of 33. 4 - stack L-value of 44. + - 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

5. 5 - stack L-value of 56. * - 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

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

Page 7: Expressions

Expressions 7

Forth - A language based on postfix

Postfix source language leads to an efficient execution model, even though generally interpreted. System runs on two stacks - a subroutine return stack and an expression evaluation stack. Run-time model very small making it useful on small embedded computers.

Forth was developed by Charles Moore around 1970. The name was a contraction of “Fourth Generation Programming Language” with the program name limited to five characters. The language was a replacement for FORTRAN on small minicomputers in the 1970s where space was at a premium and the only input-output device was often a very slow and cumbersome paper tape. Having a resident translator/interpreter made for easy program development on the target system.

Page 8: Expressions

Expressions 8

Example Forth program

Program to compute: 12+22+ ... +92+102$[Notation: a,b,c is expression stack.

c is stack(top)]: SQR DUP * ; (Defines square by: n n,n (n*n)): DOSUM SWAP 1 + SWAP OVER SQR + ; Execution:( N,S N+1,S+(N+1)2)( N,S S,N S,(N+1) (N+1),S

(N+1),S, N+1) (N+1),S, N+1)2 (N+1),S+(N+1)2)

3 6 DOSUM . . 22 4 ok (Period (.) prints stack(top).Output is 22 = 42+6)

0 0 10 0 DO DOSUM LOOP . 385 ok (Apply DOSUM from 0 to 9 (Stop at

10)) }}

Page 9: Expressions

Expressions 9

Postscript

Postscript is Forth with painting commands added.1: %Same as Forth program 2: /Helvetica findfont3: 20 scalefont4: setfont5: 200 400 moveto6: /formatit {10 10 string cvrs show} def7: /sqr {dup mul} def8: /dosum {exch 1 add exch 1 index sqr add} def9: 3 6 dosum 2 copy formatit ( ) show formatit10: clear11: 200 375 moveto12: 0 0 0 1 9 {pop dosum} for formatit13: showpage

Page 10: Expressions

Expressions 10

Postscript painting commands

14: % Lets draw a truck15: /box {newpath 0 0 moveto 0 1 lineto 3 1 lineto 3 0 lineto16: closepath} def17: .1 setlinewidth 0 setgray18: gsave19: 72 72 scale20: 2 5 translate box stroke21: 3.2 0 translate .5 .5 scale box fill22: 0 1 translate .6 .6 scale box fill23: grestore24: /tire {newpath 1 0 moveto 0 0 1 0 360 arc closepath} def25: .5 setlinewidth 10 10 scale26: 16 34 translate tire stroke27: 3 0 translate tire stroke28: 17 0 translate tire stroke29: 3 0 translate tire stroke30: 8 0 translate tire stroke13: showpage

Page 11: Expressions

Expressions 11

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)

Page 12: Expressions

Expressions 12

C precedence levels

Precedence Operators Operator names17 tokens, a[k], f()Literals, subscripting, function call .,-> Selection16 ++, -- Postfix increment/decrement15* ++, -- Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection14 typename Casts13 *, /, % Multiplicative operators12 +,- Additive operators11 <<, >> Shift10 <,>,<=, >= Relational9 ==, != Equality8 & Bitwise and 7 Bitwise xor6 | Bitwise or5 && Logical and4 || Logical or3 ?: Conditional2 =, +=, -=, *=, Assignment /=, %=, <<=, >>=, &=, =, |= 1 , Sequential evaluation