Presentation is loading. Please wait.

Presentation is loading. Please wait.

PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ07A - Expressions Programming Language Design and Implementation.

Similar presentations


Presentation on theme: "PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ07A - Expressions Programming Language Design and Implementation."— Presentation transcript:

1 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ07A - Expressions Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Chapter 8

2 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: 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 +  5 2 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.

3 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: 2 3 4 + 5 * + 1. 2 - stack 2. 3 - stack 3. 4 - stack 4. + - replace 3 and 4 on stack by 7 5. 5 - stack 6. * - replace 5 and 7 on stack by 35 7. + - replace 35 and 2 on stack by 37

4 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 2 3 4 + 5 * +, do: 1. 2 - stack L-value of 2 2. 3 - stack L-value of 3 3. 4 - stack L-value of 4 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 5. 5 - 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 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

5 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 5 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.

6 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 6 Example Forth program Program to compute: 1 2 +2 2 +... +9 2 +10 2 $ [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 = 4 2 +6) 0 0 10 0 DO DOSUM LOOP. 385 ok (Apply DOSUM from 0 to 9 (Stop at 10)) }}

7 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 7 Postscript Postscript is Forth with painting commands added. 1: %Same as Forth program 2: /Helvetica findfont 3: 20 scalefont 4: setfont 5: 200 400 moveto 6: /formatit {10 10 string cvrs show} def 7: /sqr {dup mul} def 8: /dosum {exch 1 add exch 1 index sqr add} def 9: 3 6 dosum 2 copy formatit ( ) show formatit 10: clear 11: 200 375 moveto 12: 0 0 0 1 9 {pop dosum} for formatit 13: showpage

8 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 8 Postscript painting commands 14: % Lets draw a truck 15: /box {newpath 0 0 moveto 0 1 lineto 3 1 lineto 3 0 lineto 16: closepath} def 17:.1 setlinewidth 0 setgray 18: gsave 19: 72 72 scale 20: 2 5 translate box stroke 21: 3.2 0 translate.5.5 scale box fill 22: 0 1 translate.6.6 scale box fill 23: grestore 24: /tire {newpath 1 0 moveto 0 0 1 0 360 arc closepath} def 25:.5 setlinewidth 10 10 scale 26: 16 34 translate tire stroke 27: 3 0 translate tire stroke 28: 17 0 translate tire stroke 29: 3 0 translate tire stroke 30: 8 0 translate tire stroke 13: showpage

9 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 9 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)

10 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 10 C precedence levels Precedence Operators Operator names 17 tokens, a[k], f()Literals, subscripting, function call.,-> Selection 16 ++, -- Postfix increment/decrement 15* ++, -- Prefix inc/dec , -, sizeof Unary operators, storage !,&,* Logical negation, indirection 14 typename Casts 13 *, /, % Multiplicative operators 12 +,- Additive operators 11 > Shift 10, = Relational 9 ==, != Equality 8 & Bitwise and 7  Bitwise xor 6 | Bitwise or 5 && Logical and 4 || Logical or 3 ?: Conditional 2 =, +=, -=, *=, Assignment /=, %=, >=, &=,  =, |= 1, Sequential evaluation

11 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 11 Assignment Basic attribute for imperative languages, although syntax differs: A := B Pascal, Ada A = B C, FORTRAN, PL/I, Prolog, ML, SNOBOL4 MOVE B TO A COBOL A  B APL (SETQ A B) LISP Note that LISP, an applicative language, has an assignment. Most LISP programmers do not write purely applicative programs. C has multiple ways to assign: A=B, A++,... How many C assignment operations can you identify?

12 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 12 Explicit sequence controls Early languages used labels on statements and explicitly determined location of branches (early FORTRAN): IF (A.GT.7) GOTO 100... 100 CONTINUE  Explicit control This is the “goto” controversy of the early 1970s. Languages do not need explicit control and concept is not used or needed even if present. C includes a break statement for exit out of a loop. It is a limited form of goto, that also has its problems with correctness.

13 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 13 Control structures Basic control structures: Programs need 4 basic structures: A sequencing between statements A conditional for choosing alternatives A Loop construct A function call for calling subroutines. (We consider subprograms elsewhere in this course.)

14 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 14 Sequencing Compound statement: Typical syntax: begin statement1; statement2;... end; Execute each statement in sequence. Sometimes (e.g., C) {... } used instead of begin... end

15 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 15 Conditional The if statement is typical: if expression then statement1 else statement2 if expression then statement1 The expression is evaluated and if true, the then clause is executed, otherwise the else clause is executed. If there is no else clause, then the next statement is executed. Since a begin statement is a statement, the then or else clause can be an arbitrary number of commands. As with everything else in C, its austere syntax for if is: if (expression) statement1 else statement2;

16 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 16 Iteration Four iterations are most common: while expression do statement; - Evaluate expression and if true execute statement. Then repeat process. Repeat statement until expression; - Execute statement and then evaluate expression. Quit if expression is now true. For loop - Specify a count of the number of times to execute a loop: for I=1 to 10 do statement; for(I=0;I<10; I++) statement; perform statement 10 times; Indefinite iterations (Ada): loop exit when condition end loop; foreach $X(@arrayitem){statement} - Perl

17 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 17 Case statement A form of multiway branch (similar to if): case Tag is when 0 => begin statement0 end; when 1 => begin statement1 end; when 2 => begin statement2 end; when others => begin statement3 end; end case

18 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 18 Implementation of case

19 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 19 Understanding control structures Earlier discussion on control structures seemed somewhat ad hoc. Is there a theory to describe control structures? Do we have the right control structures? Roy Maddux in 1975 developed the concept of a prime program as a mechanism for analysing these questions.

20 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 20 Spaghetti code

21 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 21 Control structures represented as flowcharts

22 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 22

23 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 23 Structured programming Issue in 1970s: Does using only single-entry, single- exit structures limit what programs can be written? Resolved by Structure Theorem of Böhm-Jacobini. Here is a graph version of theorem originally developed by Harlan Mills:

24 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 24 Structure theorem Outline of proof: 1. Label each arc with 1 being the entry arc and 0 the exit arc. 2. Let I be a new variable not used in program. 3. For each node in program construct the following:

25 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 25 Structure theorem (continued) 4. Create the following new flowchart on right. If F is the original function, what is the new function?

26 PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 26 Prime programs - Summary Structure theorem does not say: Given any “spaghetti program” convert it into a structured program. What it does say is that there is no loss of functionality in using only those control structures. It is still up to the programmer to choose the best algorithm to solve problem.


Download ppt "PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ07A - Expressions Programming Language Design and Implementation."

Similar presentations


Ads by Google