Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages

Similar presentations


Presentation on theme: "Programming Languages"— Presentation transcript:

1 Programming Languages
Meeting 5 September 27/28, 2016

2 Planning Ahead Hand in Date Project: Part 1 Short exam
Please give yourselves plenty of room by moving to an empty table. We’ll try to do one person per table. If you finish the exam early, you may slip out quietly. Class will reconvene at 7:15 pm.

3 FOR Loop Start with FOR I := E1 to E2 DO S or equivalently FOR (I:=E1; I<=E2; I := I+1) S

4 FOR Loop (2) Define [FOR I := E1 to E2 DO S] = [I := E1; while (I <= E2) do (S; I := I + 1)]

5 FOR Loop (3) Issues with the loop variable I
Where is its defining point? Case 1: In the closest containing block of the FOR loop Case 2: Implicitly in the structure (I is local to FOR) Case 3: Explicitly in the structure (I is local to FOR)

6 Example: Loop Variable
Consider the program fragment I := 28; K := 2; for I := K+1 to 10 do A[I] := I;

7 Example: Loop Variable
Note the three points to check Env and Store I := 28; K := 2; for I := K+1 to 10 do A[I] := I; A B C

8 Example (2) At A we have:

9 Example (3) At B we have:

10 Example (4) At C we have:

11 More FOR loop details What control variable types are allowed?
Ans: In general, any type supporting a next function What is the value of the control variable on exit? Ans: One of several possibilities

12 More (2) Is premature loop exit allowed?
If so, what happens to the control variable? May the control variable be threatened? by reassigning its value in the loop

13 More (3) When are the initial and final value expressions evaluated?
When do the test and increment occur?

14 Example (C) Consider the FOR statement in C to be a function of four variables, e1, an expression that initializes the control variable e2, a boolean-valued test of the control variable e3, an incrementing function for the control variable s, the body of the loop

15 Example (C2) Its program function is given by [for (e1;e2;e3) s] = [e1; while(e2) {s;e3}] which looks very similar to [I := E1; while (I <= E2) do (S; I := I + 1)]

16 Example (C3) From this example, we may infer
The control variable is global The control variable on exit has the first value for which e2 is false Initial expression is evaluated on entry, final expression is evaluated on each iteration Increment occurs after body and before test

17 Example (C4) Specifications for C allow any of e1, e2, e3 in any combination to be missing. There are 8 possibilities. Your turn Check the program function for each to verify validity of our semantic description. e1; e2; e3 e1; e2; e1; ; e3 ; e2; e3 e1; ; ; e2; ; ; e3 ; ; ;

18 FOR loop in Python for x in <indexset> What are the possible index sets? What properties must an index set have? for <exprlist> in <testlist> : <stmtblock> [ else : <stmtblock>] <testlist> is a set of values for the control variable, expressed as a list, or more generally as an iterator. It can be the output of the range function, which is not a list.

19 FOR loop in ChucK Uses the structure of the FOR loop in C
SinOsc s => dac; 5::second => now: Add between the first and second statements: Std.rand2(400, 1000) => s.freq Program 2 SinOsc s => dac; 120 => int playfreq; for ( 1 => int i; i < 6; i++) { playfreq => s.freq; 2::second => now; 2 *=> playfreq; // semicolon is a statement terminator } // an end-block terminates a for statement

20 next Functions Your turn:
In some languages there are shorthand designations for next functions. Using program functions, give the meaning for i++; ++i; j := i++; j := ++i; 4 => int p; <<< p >>>; <<< p++ >>>; <<< ++p >>>; p++ => int q; <<< p, q >>>; ++p => q; <<< p, q>>>; yields 4 6 7 6 8 8

21 next Functions (2) Program functions may not capture the subtleties of the ++ operator. Use the various semantic domains, supplemented by a temporary value store (perhaps implemented as a register rather than a memory location), as a different approach. Your answer should predict the output of the ChucK program in the notes of the previous slide.

22 FOR loop in Macsyma FOR <variable> [IN < > | FROM < > | : <exp>] [STEP <increment> | NEXT <exp>] [THRU <limit> | WHILE <cond> | UNLESS <cond> ] DO <body> Default initialization: <variable> = 1 Default step: value = 1 Termination conditions can be multiple, assumed to be joined with AND

23 Dates Project – Part 2 See handout

24 Plan for Next Weeks October 5: No formal class meeting
Project discussion Language experiments following from today’s notes October 12: Fall Break October 19: Dates Project – Part 2 due No formal class meeting October 26: Formal class meetings resume


Download ppt "Programming Languages"

Similar presentations


Ads by Google