CSE 452: Programming Languages Lecture 8 9/18/2003.

42
CSE 452: Programming Languages Lecture 8 9/18/2003
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of CSE 452: Programming Languages Lecture 8 9/18/2003.

Page 1: CSE 452: Programming Languages Lecture 8 9/18/2003.

CSE 452: Programming Languages

Lecture 8

9/18/2003

Page 2: CSE 452: Programming Languages Lecture 8 9/18/2003.

2Organization of Programming Languages-Cheng (Fall 2004)

Outline

Control StructuresSelection StatementsIterative StatementsUnconditional Branches

Subprograms and Procedures

Page 3: CSE 452: Programming Languages Lecture 8 9/18/2003.

3Organization of Programming Languages-Cheng (Fall 2004)

Iterative Statements

Repeated execution of a statement or compound statement accomplished either by iteration or recursion; here we look at iteration, because recursion is a unit-

level control (e.g., using functions)

General design issues for iteration control statements:1. How is the iteration controlled?

Counter-controlled vs logical-controlled

2. Where should the control mechanism appear in the loop? pretest (before loop body is executed) vs posttest (after loop

body is executed)

Page 4: CSE 452: Programming Languages Lecture 8 9/18/2003.

4Organization of Programming Languages-Cheng (Fall 2004)

Iterative Statements

Counter-Controlled Loopsfor (i = init_value; i < terminal_value; i+=stepsize) {

…}

Design Issues:1. What are the type and scope of the loop variable?2. What is the value of the loop variable at loop termination?3. Should it be legal for the loop variable or loop parameters to be

changed in the loop body, and if so, does the change affect loop control?

4. Should the loop parameters be evaluated only once, or once for every iteration?

Loop parametersLoop variable

Page 5: CSE 452: Programming Languages Lecture 8 9/18/2003.

5Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

FORTRAN 95 Syntax: Do label var = initial, terminal [, stepsize]

stepsize can be any value but zero parameters can be expressions

Design choices:

1. Loop var must be integer; loop parameters can be expressions

2. The loop var cannot be changed in the loop, but loop parameters can because they are evaluated only once, it does not affect loop control

3. Single entry structure – loop can be entered through Do statement

Page 6: CSE 452: Programming Languages Lecture 8 9/18/2003.

6Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

Fortran 95:Do label var = init_expression, terminal_expression [, step_expression]

Operational Semantics for Fortran 95 Do statementinit_value = init_expressionterminal_value = terminal_expressionstep_value = step_expressiondo_var = init_valueiteration_count = max(int((terminal_value–

init_value+step_value)/step_value), 0)loop:

if iteration_count 0 goto out[loop body]do_var = do_var + step_valueiteration_count = interation_count – 1goto loop

out: …

Page 7: CSE 452: Programming Languages Lecture 8 9/18/2003.

7Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

Another form of Do statement for FORTRAN 95

[name:] DO variable = initial, terminal [, stepsize]

END DO [name]

Uses a special word for closing: END DO

Page 8: CSE 452: Programming Languages Lecture 8 9/18/2003.

8Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

Ada Syntax:

for var in [reverse] discrete_range loop

...

end loop;

reverse indicates that values of discrete range are assigned in reverse order

Step size is always one (or next element in discrete_range)

Page 9: CSE 452: Programming Languages Lecture 8 9/18/2003.

9Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

Ada Design choices:1. Type of the loop var is that of the discrete range

discrete range is subrange of integer or enumeration type, such as 1..10 or Monday..Friday

2. Scope of loop var is the loop body (it is implicitly declared); loop var does not exist outside the loop

Count : Float := 1.35;for Count in 1..10 loopSum := Sum + Count;

end loop;… Count gets the value of 1.35

3. The loop var cannot be changed in the loop, but the discrete range can; it does not affect loop control

4. The discrete range is evaluated just once

Page 10: CSE 452: Programming Languages Lecture 8 9/18/2003.

10Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

C-based languages Syntax:

for ([expr_1] ; [expr_2] ; [expr_3])

loop body Loop body can be single, compound, or null statement

Expressions can be whole statements, or even statement sequences, with the statements separated by commas

The value of a multiple-statement expression is the value of the last statement in the expression

for (i = 0, j = 10; j == i; i++)

… All expressions of C’s for statement are optional

If expr_2 is absent, it is an infinite loop

Page 11: CSE 452: Programming Languages Lecture 8 9/18/2003.

11Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

for ([expr_1] ; [expr_2] ; [expr_3])

loop body Operational Semantics:

expr_1 % initialization (evaluate once)

loop:

if expr_2 = 0 goto out % loop control (each iter)

[loop_body]

expr_3 % increment loop counter?

goto loop

out: …

Page 12: CSE 452: Programming Languages Lecture 8 9/18/2003.

12Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

Design choices for C:

1. There is no explicit loop variable or loop parameters

All involved variables can be changed in the loop body

2. It is legal to branch into loop body

3. The first expression is evaluated once, but the other two are evaluated with each iteration

Page 13: CSE 452: Programming Languages Lecture 8 9/18/2003.

13Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

#include <stdio.h>

main(){ int count, i = 2;

count = 2; /* count = 5 */

if (count % 2 == 0) { goto loop; }

for (i=0; i<5; i++) {loop: printf("i=%d\n", i); }}

count = 2:

i = 2;i = 3;i = 4;

count = 5:

i = 0;i = 1;i = 2;i = 3;i = 4;

Page 14: CSE 452: Programming Languages Lecture 8 9/18/2003.

14Organization of Programming Languages-Cheng (Fall 2004)

Counter-Controlled Loops

C++ Differs from C in two ways:

1. The control expression can also be Boolean

2. The initial expression can include variable definitions (scope is from the definition to the end of the loop body)

for (int i=0; i < len; i++) { … }

Java Differs from C++ in that the control expression must be

Boolean

Page 15: CSE 452: Programming Languages Lecture 8 9/18/2003.

15Organization of Programming Languages-Cheng (Fall 2004)

Logically-Controlled Loops

Repetition control is based on a Boolean expression, rather than a counter

More general than counter-controlled loops Every counting loop can be built with a logical loop, but

not vice-versa

Design Issues:

1. Pretest or postest?

2. Should logically controlled loop be a special form of counting loop statement or a separate statement?

Page 16: CSE 452: Programming Languages Lecture 8 9/18/2003.

16Organization of Programming Languages-Cheng (Fall 2004)

Logically-Controlled Loops

Pascal has separate pretest and posttest logical loop statements (while-do and repeat-until)

C and C++ also have bothwhile (count > 0) { …}

do {…

} while (count > 0);

Legal to branch into both while and do loop bodies

Page 17: CSE 452: Programming Languages Lecture 8 9/18/2003.

17Organization of Programming Languages-Cheng (Fall 2004)

Logically-Controlled Loops

Ada has a pretest version, but no posttest

FORTRAN 77, 90, and 95 have neither

Perl has two pretest logical loops, while and until, but no posttest logical loop

while (count > 0) {

…}

until (count == 0) {…

}

Page 18: CSE 452: Programming Languages Lecture 8 9/18/2003.

18Organization of Programming Languages-Cheng (Fall 2004)

User-Located Loop Control

User modify the control flow of program

Design issues:1. Should the conditional mechanism be an integral

part of the exit?

2. Should only one loop body be exited or can enclosing loops also be exited?

Page 19: CSE 452: Programming Languages Lecture 8 9/18/2003.

19Organization of Programming Languages-Cheng (Fall 2004)

User-Located Loop Control

Exit statement: Unconditional unlabeled exit: break (C, C++)

for (index=0; index<10; index++) {…

if (value < 0) break;}

Unconditional labeled exit: break (Java, C#), last (Perl)C#: outerloop: for (row=0; row<numRows; row++)

for (col = 0; col < numCols; col++) {

sum += matrix[row][col]; if (sum > 1000)

break outerLoop; }

Perl: LINE: while (<STDIN>) { last LINE if /^$/;

... }

Page 20: CSE 452: Programming Languages Lecture 8 9/18/2003.

20Organization of Programming Languages-Cheng (Fall 2004)

User-Located Loop Control

Skip the rest of loop body:C/C++ have unlabeled control statement

(continue)while (sum < 1000) {

value = getNextValue();

if (value < 0) continue;

sum += value;

}

Java, Perl, and C# have statements similar to continue, except they can include labels that specify which loop is continued

Page 21: CSE 452: Programming Languages Lecture 8 9/18/2003.

21Organization of Programming Languages-Cheng (Fall 2004)

Iteration Based on Data Structures

Loops are controlled by number of elements in a data structure Perl, Javascript, PHP, and C# have such statements

Perl: @values = (1, 2, 3, 4, 5);foreach $value (@values) {print “Value is $value\n”;}

C#: String[] strList = {“Bob”, “John”, “Carol” };foreach (String name in strList) …

More general approach uses a user-defined data structure and a user-defined function

(called an iterator) to go through the structure’s elements Java has a Collection interface that contains two methods:

boolean add(Object obj) - adds elements to collectionIterator iterator() - used to visit the elements in the collection one by one.

Page 22: CSE 452: Programming Languages Lecture 8 9/18/2003.

22Organization of Programming Languages-Cheng (Fall 2004)

Unconditional Branching

Transfers execution control to a specified location in the program

goto label

Problem: readability Some languages do not have them: e.g., Java

Loop exit statements are restricted and somewhat camouflaged goto’s

Label forms:

1. Unsigned int constants: Pascal (with colon) FORTRAN (no colon)

2. Identifiers with colons: ALGOL 60, C

3. Variables as labels: PL/I

Page 23: CSE 452: Programming Languages Lecture 8 9/18/2003.

23Organization of Programming Languages-Cheng (Fall 2004)

Subprograms

Two fundamental abstraction facilities in programming language: Process abstraction – represented by subprograms Data abstraction

General characteristics of subprograms:

1. A subprogram has a single entry point

2. The caller is suspended during execution of the called subprogram

3. Control always returns to the caller when the called subprogram’s execution terminates

Page 24: CSE 452: Programming Languages Lecture 8 9/18/2003.

24Organization of Programming Languages-Cheng (Fall 2004)

Subprograms

A subprogram definition is a description of the actions of the subprogram abstraction

A subprogram call is an explicit request that the subprogram be executed A subprogram is active if, after being called, it has begun

execution but has not yet completed that execution

A subprogram header is the first line of the definition Specifies that the following syntactic unit is a subprogram

of some particular kind - using a special word (function, procedure, etc)

Provides name of subprogram Specifies the list of formal parameters

Fortran: Subroutine Adder(parameters) Ada: procedure Adder(parameters)

Page 25: CSE 452: Programming Languages Lecture 8 9/18/2003.

25Organization of Programming Languages-Cheng (Fall 2004)

Subprograms

The parameter profile (signature) of a subprogram is the number, order, and types of its parameters

The protocol of a subprogram is its parameter profile plus, if it is a function, its return type

Subprograms can have declarations as well as definitions

Subprogram declaration provides the subprogram’s protocol but do not include their bodies Function declarations in C/C++ are called prototypes

Page 26: CSE 452: Programming Languages Lecture 8 9/18/2003.

26Organization of Programming Languages-Cheng (Fall 2004)

Parameters

A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram

An actual parameter represents a value or address used in the subprogram call statement

void doNothing (int formal_param) {…

}main() {

int actual_param;doNothing(actual_param);

}

Page 27: CSE 452: Programming Languages Lecture 8 9/18/2003.

27Organization of Programming Languages-Cheng (Fall 2004)

Parameters

Actual/Formal Parameter Correspondence Binding of actual to formal parameters1. Positional parameters

First actual param bound to first formal param, etc

2. Keyword parameters Name of formal param to which actual param is bound is

specified with actual paramAda: Sumer( Length => My_Length,

List => My_Array,Sum => My_Sum );

Advantage: order is irrelevant Disadvantage: user must know the formal parameter’s names

Page 28: CSE 452: Programming Languages Lecture 8 9/18/2003.

28Organization of Programming Languages-Cheng (Fall 2004)

Parameters

Default values of formal parameters Allowed by C++, Fortran 95, Ada and PHP Default value is used if no actual parameter is passed to the

formal parameterAda: function Compute_Pay( Income : Float; Exemptions : Integer := 1;

Tax_Rate : Float ) return Float pay := Compute_Pay (20000.00, Tax_Rate => 0.15);

C# allows methods to accept variable number of params of the same typepublic void DisplayList(params int[] list ) { foreach (int nextValue in list) {

Console.WriteLine(“Next value {0}”, nextValue); }}

Page 29: CSE 452: Programming Languages Lecture 8 9/18/2003.

29Organization of Programming Languages-Cheng (Fall 2004)

Procedures and Functions

Procedures provide user-defined statements

Functions provide user-defined operators Value produced by function is returned to the calling code,

effectively replacing the call itself

float power(float base, float exp)

result = 3.4 * power(10.0, x);

C-based languages have only functions (but they can behave like procedures) Can be defined to return no value if the return type is void

Page 30: CSE 452: Programming Languages Lecture 8 9/18/2003.

30Organization of Programming Languages-Cheng (Fall 2004)

Design Issues for Subprograms

1. What parameter passing methods are provided?

2. Are parameter types checked?

3. Are local variables static or dynamic?

4. What is the referencing environment of a passed subprogram?

5. Are parameter types in passed subprograms checked?

6. Can subprogram definitions be nested?

7. Can subprograms be overloaded?

8. Are subprograms allowed to be generic?

9. Is separate or independent compilation supported?

Page 31: CSE 452: Programming Languages Lecture 8 9/18/2003.

31Organization of Programming Languages-Cheng (Fall 2004)

Local Referencing Environments

Local variables: variables defined inside subprograms their scope is the body of subprogram in which they

are defined Stack-dynamic: bound to storage when subprogram begins

execution, unbound when its execution terminates Advantages:1. Support for recursion2. Storage for local variables of active subprogram can be shared

with local variables of inactive subprograms Disadvantages:1. Allocation/deallocation time 2. Indirect addressing (indirectness because the place in stack

where a particular local variable is stored can only be determined at run time)

3. Subprograms cannot be history sensitive Cannot retain data values of local variables between calls

Static: bound to storage at compile-time

Page 32: CSE 452: Programming Languages Lecture 8 9/18/2003.

32Organization of Programming Languages-Cheng (Fall 2004)

Parameter Passing: Semantic Models

Semantic models for formal parameters In mode – can receive data from corresponding

actual parameters Actual value is either copied to caller, or an access path is

transmitted

Out mode – can transmit data to actual parameters Inout mode – can do both receive/transmit data

Page 33: CSE 452: Programming Languages Lecture 8 9/18/2003.

33Organization of Programming Languages-Cheng (Fall 2004)

Parameter Passing: Implementation

Pass by value (in mode) Value of actual parameter is used to initialize formal parameter, which

acts as a local variable

void foo (int a) {a = a + 1;

}void main() {

int b = 2;foo(b);

}

Normally implemented by copying actual parameter to formal parameter

Can also be implemented by transmitting access path to the value of actual parameter as long as cell is write protected

Disadvantages: Requires more storage (duplicated space) Cost of the moves (if the parameter is large)

Page 34: CSE 452: Programming Languages Lecture 8 9/18/2003.

34Organization of Programming Languages-Cheng (Fall 2004)

Parameter Passing: Implementation

Pass by result (out mode) Local’s value is passed back to the caller No value transmitted to the subprogram Formal parameter acts as local variable, but just before

control is transferred back to caller, its value is transmitted to actual parameter

Disadvantages:1. If value is copied back (as opposed to access paths),

need extra time and space 2. Pass-by-result can create parameter collision

e.g. procedure sub1(y: int, z: int); ... sub1(x, x);

Value of x in the caller depends on order of assignments at the return

Page 35: CSE 452: Programming Languages Lecture 8 9/18/2003.

35Organization of Programming Languages-Cheng (Fall 2004)

Parameter Passing: Implementation

Pass by value-result (or pass-by-copy)Combination of pass-by-value and pass-by-resultFormal parameter acts as local variable in

subprogramActual parameter is copied to formal parameter

at subprogram entry and copied back at subprogram termination

Share disadvantages of pass-by-result and pass-by-value

Requires multiple storage for parametersRequires time for copying valuesProblems with parameter collision

Page 36: CSE 452: Programming Languages Lecture 8 9/18/2003.

36Organization of Programming Languages-Cheng (Fall 2004)

Parameter Passing: Implementation

Pass by reference (or pass-by-sharing) transmits an access path (e.g., address) to the called

subprogram Called subprogram is allowed to access actual

parameter in the calling program unit Advantage:

passing process is efficient (no copying and no duplicated storage)

Disadvantages: Slower accesses to formal parameters due to additional level

of indirect addressing Allows aliasing

void fun (int &first, int &second);…fun(total, total);

Page 37: CSE 452: Programming Languages Lecture 8 9/18/2003.

37Organization of Programming Languages-Cheng (Fall 2004)

Parameter Passing: Implementation

Pass-by-referenceCollisions due to array elements can also cause

aliasesvoid fun(int &first, int &second)

fun(list[i], list[j]); /* where i=j */void fun1(int &first, int *a);fun1(list[i], list);

Collisions between formal parameters and nonlocal variables that are visible

int *global; void sub(int *param) {void main() { extern int *global;

extern int *global; …… }sub(global);…

}

Page 38: CSE 452: Programming Languages Lecture 8 9/18/2003.

38Organization of Programming Languages-Cheng (Fall 2004)

Parameter Passing: Implementation

Pass by Name Another type of inout mode Actual parameter is textually substituted for the

corresponding formal parameters Actual binding of value and address is delayed until formal

parameter is assigned or referenced Advantage:

flexibility of late binding Disadvantage:

very expensive related to other parameter passing Not used in any widely used language

Another Example: Used at compile time by macros, and for generic subprograms in

C++

Page 39: CSE 452: Programming Languages Lecture 8 9/18/2003.

39Organization of Programming Languages-Cheng (Fall 2004)

Pass-by-value

int m=8, i=5;

foo(m);

print m; # prints 8

# since m is passed by-value...

proc foo (byval b) {

b = i + b;

# b is byval so it is essentially a local variable

# initialized to 8 (the value of the actual back in

# the calling environment)

# the assignment to b cannot change the value of m back

# in the main program

}

Page 40: CSE 452: Programming Languages Lecture 8 9/18/2003.

40Organization of Programming Languages-Cheng (Fall 2004)

Pass-by-reference

int m=8, i=5;

foo(m);

print m; # prints 13

# since m is passed by-reference...

proc foo (byref b) {

b = i + b;

# b is byref so it is a pointer back to the actual

# parameter back in the main program (containing 8 initially)

# the assignment to b actually changes the value in m back

# in the main program

# i accesses the variable in the main via scope rules

}

Page 41: CSE 452: Programming Languages Lecture 8 9/18/2003.

41Organization of Programming Languages-Cheng (Fall 2004)

Pass-by-value-result

int m=8, i=5;

foo(m);

print m; # prints 13

# since m is passed by-value-result...

proc foo (byvres b) {

b = i + b;

# b is byves so it copies value of the actual

# parameter (containing 8 initially)

# new value of b is copied back to actual parameter

# in the main program

# i accesses the variable in the main via scope rules

}

Page 42: CSE 452: Programming Languages Lecture 8 9/18/2003.

42Organization of Programming Languages-Cheng (Fall 2004)

Pass-by-name

array A [1..100] of int; array A [1..100] of int;

int i=5; int i=5;

foo(A[i],i); foo(A[i]);

print A[i]; # prints A[6] print A[i]; # prints A[5]

... # so prints 7 ... # not sure what

# good example # a problem here...proc foo (name B,name k) { proc foo (name B) { k = 6; int i = 2; B = 7; B = 7;} }

# text substitution does this proc foo { proc foo { i = 6; int i = 2; A[i] = 7; A[i] = 7;} }