Top Banner
ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology
133

ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dec 29, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

ALGOL-60 GENERALITY AND

HIERARCHY

Programming Languages CourseComputer Engineering DepartmentSharif University of Technology

Page 2: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Outline2

History and Motivation

Structural Organization

Name Structures

Data Structures

Control Structures

Page 3: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Outline3

History and Motivation

Structural Organization

Name Structures

Data Structures

Control Structures

Page 4: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

4

The International Algebraic Language

Page 5: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

5

The International Algebraic Language

Designed by 8 representatives of ACM and GAMM

Combined experiences of algebraic language design and implementing pseudo-codes.

Essentially completed in 8 days Created a great stir Many dialects: NELIAC, JOVIAL

Page 6: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

6

Objectives of Algol

As close as possible to standard mathematical notation and readable with little further explanation.

possible to be used for the description for computing processes in publications.

Mechanically translatable into machine programs.

Page 7: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

7

Algol-60

Final report was published in May 1960, revised in 1962.

The original algol-60 report is a paradigm of brevity and clarity, remains a standard.

Syntax: BNF notation Semantics: clear, precise, unambiguous

English-language description.

Page 8: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

8

Outline

History and Motivation

Structural Organization

Name Structures

Data Structures

Control Structures

Page 9: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

9

Hierarchical Structure

begininteger N;read int (N);begin

real array Data[1:N];integer i;sum := 0;for i := 1 step 1 until N do

beginend…

endend

Page 10: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

10

Category of Construct

Declaratives: bind name to the objects Variables Procedures Switches

Imperative: do the work of computation Control-flow Computational (:=)

Page 11: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

11

The Familiar Control Structure

goto If-Then-Else For loop Procedure invocation

Page 12: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Compile-time, Run-time Distinction FORTRAN: static compilation process

Algol: not completely static Dynamic arrays Recursive procedures

12

Page 13: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

The Stack, the Central Run-Time Data Structure

Dynamic allocation and deallocation are achieved by pushing and poping activation records on the stack.

13

Page 14: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

14

Outline

History and Motivation

Structural Organization

Name Structures

Data Structures

Control Structures

Page 15: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

15

Blocks and Compound Statements

A compound statement is a group of statements used where one statement is permitted.

Blocks contain declarations, compound statements do not.

An example of regularity in language design.

Page 16: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Example

for i:=0 step 1 until N do

16

sum:=sum+Data[i]

for i:=0 step 1 until N dobegin

if Data[i]>10 then Data[i]:=10;

sum:=sum+Data[i];

Print Real(sum);

end

Page 17: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Example

for i:=0 step 1 until N do

17

for i:=0 step 1 until N dosum:=sum+Dat

a[i]

The body of a for loop is one

statement by default.

begin

if Data[i]>10 then Data[i]:=10;

sum:=sum+Data[i];

Print Real(sum);

end

Page 18: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Example

for i:=0 step 1 until N do

18

for i:=0 step 1 until N dosum:=sum+Dat

a[i]

begin

if Data[i]>10 then Data[i]:=10;

sum:=sum+Data[i];

Print Real(sum);

end

The compound statement is used

where one statement is permitted.

Page 19: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

19

Blocks Define Nested Scopes In FORTRAN scopes are nested in two

levels: global and subprogram-local. Algol-60 allows any number of scopes

nested to any depth. Each block defines a scope that extends

from the begin to the end. Name structures (such as blocks) restrict

the visibility of names to particular parts of the program.

Page 20: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

20

Example

begin real x,y; procedure f(y,z); integer y,z; begin real array A[1:y]; end begin integer array Count [0:99] endend

Page 21: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

21

Sample Contour Diagram

xyf

y

zA

count

Page 22: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

22

Algol use of nested scopes prevents the programmer from committing errors that were possible in using FORTRAN COMMON blocks.

Making errors impossible to commit is preferable to detecting them after

their commission.

Impossible Error Principle

Page 23: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

23

Static and Dynamic Scoping

Page 24: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

24

Static and Dynamic Scoping

Static scoping: a procedure is called in the environment of its definition.

Dynamic scoping: a procedure is called in the environment of its caller.

Algol uses static scoping exclusively.

Page 25: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

25

Example

a: begin integer m; procedure P m:=1; b: begin integer m; P end; P end

Page 26: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

26

a: begin integer m; procedure P m:=1; b: begin integer m; P end P end

Example

With dynamic scoping, the

value of this m is 1 when the

program finishes block b.

Page 27: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

27

Contour Diagram of Dynamic Scoping

mP

(a)

m(b)Call P

DLm:=1

(P)

Page 28: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

28

a: begin integer m; procedure P m:=1; b: begin integer m; P end; P end

Example

With static scoping, the

value of this m is 1 when the

program finishes block b.

Page 29: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

29

Contour Diagram of Static Scoping

mP

(a)

m(b)Call P

DLm:=1

(P)

Page 30: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dynamic Scoping (Advantages)

begin real procedure sum; begin real S,x; S:=0;

x:=0; for x:=x+0.01 while

x<=1 do S:=S+f(x); sum:=S/100; end …end

beginreal procedure f(x); value x; real x; f:=x 2 + 1;sumf:=sum;

end

30

To use the sum function it is necessary only to name the function to be summed f.

Page 31: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dynamic Scoping (Advantages)

begin real procedure sum; begin real S,x; S:=0;

x:=0; for x:=x+0.01 while

x<=1 do S:=S+f(x); sum:=S/100; end …end

beginreal procedure f(x); value x; real x; f:=x↑2 + 1;sumf:=sum;

end

31

Page 32: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dynamic Scoping (Advantages)

begin real procedure sum; begin real S,x; S:=0;

x:=0; for x:=x+0.01 while

x<=1 do S:=S+f(x); sum:=S/100; end …end

beginreal procedure f(x); value x; real x; f:=x 2 + 1;sumf:=sum;

end

32

One advantage of dynamic scoping : A general procedure that makes use of

variables and procedures supplied by the caller’s environment.

Page 33: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dynamic Scoping (Problems)33

begin real procedure discr(a,b,c) values a,b,c; real a,b,c; discr:=b↑2-4хaхc; procedure roots(a,b,c,r1,r2); value a,b,c; real a,b,c,r1,r2; begin …d:=discr(a,b,c); … end . . roots(c1,c2,c3,root1,root2); . .end

Page 34: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dynamic Scoping (Problems)34

begin real procedure discr(a,b,c); values a,b,c; real a,b,c; discr:=b↑2-4хaхc; procedure roots(a,b,c,r1,r2); value a,b,c; real a,b,c,r1,r2; begin …d:=discr(a,b,c); … end . . roots(c1,c2,c3,root1,root2); . .end

begin real procedure discr(x,y,z); value x,y,z; real x,y,z; discr:=sqrt(x ↑2); . . . roots(acoe,bcoe,ccoe,rt1,rt2); . . . end

Page 35: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dynamic Scoping (Problems)35

begin real procedure discr(x,y,z); value x,y,z; real x,y,z; discr:=sqrt(x ↑2); . . . roots(acoe,bcoe,ccoe,rt1,rt2); . . . end

begin real procedure discr(a,b,c); values a,b,c; real a,b,c; discr:=b↑2-4хaхc; procedure roots(a,b,c,r1,r2); value a,b,c; real a,b,c,r1,r2; begin …d:=discr(a,b,c); … end . . roots(c1,c2,c3,root1,root2); . .end

The roots procedure will use the wrong discr function to compute its result.

Page 36: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Dynamic Scoping (Problems)

begin real procedure discr(a,b,c); values a,b,c; real a,b,c; discr:=b↑2-4хaхc; procedure roots(a,b,c,r1,r2); value a,b,c; real a,b,c,r1,r2; begin …d:=discr(a,b,c); … end . . roots(c1,c2,c3,root1,root2); . .end

begin real procedure discr(x,y,z); value x,y,z; real x,y,z; discr:=sqrt(x ↑2); . . . roots(acoe,bcoe,ccoe,rt1,rt2); . . . end

36

This is called vulnerability.The root procedure is vulnerable to being called

from an environment in which its auxiliary procedure is not accessible.

Page 37: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

37

Static and Dynamic Scoping

In dynamic scoping, the meanings of statements are determined at run-time.

In static scoping, the meanings of statements are fixed.

Static scoping aids reliable programming.

Dynamic scoping is against the Structure Principle.

Page 38: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

38

Blocks and Efficient Storage Management

The value of a variable is retained In the scope of that variable. In a block or structure that returns to the

scope of the variable. Variables of disjoint blocks can never

coexist. Much more secure than FORTRAN

EQUIVALENCE. Implemented by a stack.

Page 39: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

39

Do not ask users what they want, find out what they need.

Responsible Design Principle

Page 40: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

40

Outline

History and Motivation

Structural Organization

Name Structures

Data Structures

Control Structures

Page 41: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

41

Primitives

The primitive data types are mathematical scalars: integer, real, Boolean.

Only one level of precision: real.

No double-precision types, they violate Portability: Doubles need word size and floating point

representation of the computer to be implemented.

Page 42: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

42

Primitives (cont.)

Complex numbers were omitted because: They are not primitive. The convenience and efficiency of adding them to

the language is not worth the complexity of it.

string data type: a second hand citizen of Algol-60. Can only be passed to procedures as arguments. Does not cause a loophole in the type system, as

it does in FORTRAN. Algol was the last major language without strings.

Page 43: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

43

Algol follows the Zero-One-Infinity principle.

Arrays are generalized in Algol: More than 3 dimensions. Lower bounds can be numbers other than 1.

The only reasonable numbers in a programming language design are

zero, one, and infinity.

Zero-One-Infinity Principle

Page 44: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

44

Dynamic Arrays

Arrays are dynamic in a limited fashion: The array’s dimension can be recomputed

each time its block is entered. Once the array is allocated, its dimension

remains fixed.

Stack allocation permits dynamic arrays. The array is part of the activation record. The array’s size is known at block entry

time.

Page 45: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

45

Strong Typing

Strong typing is where a language does not allow the programmer to treat blocks of memory defined as one type as another (casting).

Example: Adding numbers to strings, doing a floating point multiply on Boolean values.

This is different from legitimate conversions between types, i.e. converting reals to integers, which are machine independent operations.

Page 46: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Outline47

History and Motivation

Structural Organization

Name Structures

Data Structures

Control Structures

Page 47: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Primitive Computational Statement

The primitives from which control structures are build are those computational statements that do not affect the flow of control → ‘:=’

In Algol, function of control structures is to direct and manage the flow of control from one assignment statement to another

Page 48: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Control Structures, Generalization of FORTRAN’s

FORTRAN:

Algol:

if expression then statemtent1 else statement2

IF (logical expression) simple statement

Page 49: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Control Structures, Generalization of FORTRAN’s

Algol for-loop Includes the functions of a simple Do-loop

Has a variant similar to while-loop

for NewGuess := improve(oldGuess)while abs( NewGuess - oldGuess)>0.1

do oldGuess := NewGuess

for 1:=1 step 2 until M*N doinner[i] := outer [N*M-i];

for i:=i+1while i < 100

do A [i] := i;

Page 50: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Control Structures, Generalization of FORTRAN’s

Algol for-loop Includes the functions of a simple Do-loop

Has a variant similar to while-loop

for NewGuess := improve(oldGuess)while abs( NewGuess - oldGuess)>0.1

do oldGuess := NewGuess

for 1:=1 step 2 until M*N doinner[i] := outer [N*M-i];

while i < 100 dobegin

A [i] := i;i=i+1;

end

Page 51: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Control Structures, Generalization of FORTRAN’s

Algol-60 control structures are more: Regular Symmetric Powerful General

FORTRAN has many restrictions, because of:

Efficiency ( the array restriction) Compiler simplicity (the IF restriction)

Page 52: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Control Structures, Generalization of FORTRAN’s

the Algol designers’ attitude:

“Anything that you thing you ought to be able to do, you will be able to do.”

Restrictions are inexplicable for programmer.

Irregularity in a language, makes it hard to learn and remember.

Page 53: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Nested Statements

FORTRAN IF: if the consequent is more than 1 statement there are some problems:

Problem of maintainability Irregular syntax, source of error

FORTRAN DO-loop: begin and end of the loop is marked.

DO 20 I=1, Nstatement 1statement 2…statement m

20 CONTINUE

Page 54: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Nested Statements

the Algol designers’ realized:

All control structures should be allowed to govern an arbitrary number of

statements.

Page 55: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Nested Statements

Compound statement:

beginstatement 1 statement 2...statement n

end

Page 56: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Nested Statements

Begin-end brackets do double duty:1. Group statements into compound

statements.2. Delimit blocks, which define nested

scopes.

Page 57: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Nested Statements

Begin-end brackets do double duty:1. Group statements into compound

statements.2. Delimit blocks, which define nested

scopes.

Lack of orthogonality!

Page 58: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Hierarchical Structure of Compound Statements

Hierarchical structure is one of most important principles of language and program design.

Complex structures are built up by hierarchically combining single statements into larger compound statements.

Page 59: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Nested Led to Structured Programming

Many fewer GOTO-statements were needed in Algol-60.

Programs were much easier to read.

Edsger Dijkstra:

“Go To Statement Considered Harmful.”

Page 60: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Nested Led to Structured Programming

the static structure of the program should correspond in a simply way to

the dynamic structure of the corresponding computations.

The Structure Principle

Page 61: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Recursive Procedures

Example:

integer procedure fac (n);

value n; integer n;

fac := if n =0 then 1 else n * fac(n-1);

Page 62: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Recursive Procedures

There must be a memory location to hold the formal parameter!

Only one location for n? Each invocation of fac has its own instance of

the formal parameter n.

Creating new activation record for fac each time it is called, is instantiation.

Page 63: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Value

Example:

procedure switch(n);

value n, integer n;

n:=3;

Page 64: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Value

Example:

Procedure switch(n);

Value n, integer n;

n:=3;Useful only for

input parameters!

Page 65: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Value

Pass by value is very inefficient for arrays:

Real procedure avg (A , n);

value A, n;

real array A; integer n;

begin

.

.

end;

Page 66: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Name

Example:

Pass by name is based on substitution!

Procedure Inc (n);

integer n;

n := n+1;

Page 67: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Name

Implementation is not based on substitution!

The procedure can be replaced by its body with the actuals

substituted for the formals.

Copy Rule

Page 68: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Name

Example:

Pass by name is based on substitution!

Procedure Inc (n);

integer n;

n := n+1;Satisfies Algol’s need for output parameters!

Page 69: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Name

Pass by Name Is Powerful!Example:

real procedure Sum (k, l, u , ak);

value l, u;

integer k, l , u; real ak;

begin real S; S := 0;

for k:=1 step 1 until u do

S := S + ak;

Sum:=S;

end;

Page 70: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Name

Pass by Name Is Powerful!Example: X := Sum (i, 1, n, v [i])

real procedure Sum (k, l, u , ak);

value l, u;

integer k, l , u; real ak;

begin real S; S := 0;

for I := 1 step 1 until n do

S := S + v [i];

x := S;

end;

Page 71: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Name

How is it implemented? Following the Copy Rule? Passing the address of the code sequence!

A thunk is an invisible, parameterless, address-returning,

function used by a compiler to implement name parameters and similar constructs.

Page 72: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing by Name

Pass by Name is Dangerous and Expensive!Example: Swap (A[i], i) and Swap (i, A[i])

procedure swap (x, y);

integer x, y;

begin integer t;

t := x;

x := y;

y := t ;

end

Page 73: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Parameter Passing Summarized

1. Pass by value: At call time, the formal is bound to the value of the actual.

2. Pass by reference: At call time, the formal is bound to a reference to the actual.

3. Pass by name: At call time, the formal is bound to a thunk for the actual.

Page 74: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Good Conceptual Models

3 kind of conceptual models:1. Designer’s model2. System image3. User’s model;

Donald Norman (psychologist):

“A good conceptual model allows us to predict the effects of our

actions”

Page 75: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Expensive Out of Block GOTOs

An identifier declaration is visible if we are nested within the block in

which it occurs .

Algol Scope Rule

Page 76: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Expensive Out of Block GOTOs

Example:a : begin array X [1:100];

b : begin array Y [1:100];

goto exit;

end;

exit :

end

Page 77: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Expensive Out of Block GOTOs

Another example:

begin

procedure P (n);

value n; integer n;

if n=0 then goto out

else P(n-1);

P(25);

out:

end

Page 78: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Expensive Out of Block GOTOs

Another example:

begin

procedure P (n);

value n; integer n;

if n=0 then goto out

else P(n-1);

P(25);

out:

end

The goto-statement must be implemented as a call of a run-time routine that find the appropriate activation record

and deletes all the ones above it!

Page 79: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Future Interaction, a Difficult Design Problem

Algol visibility rules versus GOTO statement

Problem of Interaction!

Page 80: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Future Interaction, a Difficult Design Problem

Designer must develop a “nose” for spotting possible problem

areas!

Page 81: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Generality of the for-Loop

for var:=exp step exp' until exp" do stat

for var:=exp while exp ' do stat

for days:= 31, 28, 31, 30, do…

for days:=31,if mod (year , 4) =0 then 29else 28, 31, 30, do…

Page 82: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

The Baroque for-Loop

Example:

3 7 11 12 13 14 15 16 8 4 2 1 2 4 8 16 32

for i :=3, 7,

11 step 1 until 16,

i/2 while i>=1,

2 step i until 32

Do print (i)

Page 83: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

The Baroque for-Loop

Users should pay only for what they use; avoid distributed costs.

The Localized Cost Principle

Page 84: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

Handling Cases with the switch Example:

beginswitch marital status = single, married, divorced;…goto marital status [i];

single: … handle single casegoto done:

married: … handle married casegoto done:

divorced: … handled divorced casedone:end;

Page 85: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

The Baroque switch

Example:

beginswitch S=L, if i>0 then M else N, Q;goto S [j];

end

Page 86: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

The Baroque switch

Example:

beginswitch S=L, if i>0 then M else N, Q;goto S [j];

end

goto if i>0 then M else N;

Page 87: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

88

Handling Cases with the switch Example:

beginswitch marital status = single, married, divorced;…goto marital status [i];

single: … handle single casegoto done:

married: … handle married casegoto done:

divorced: … handled divorced casedone:end;

Page 88: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

89

The Baroque switch

Example:

beginswitch S=L, if i>0 then M else N, Q;goto S [j];

end

Page 89: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

90

The Baroque switch

Example:

beginswitch S=L, if i>0 then M else N, Q;goto S [j];

end

goto if i>0 then M else N;

Page 90: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

91

SYNTAX AND ELEGANCE:

ALGOL-60

Programming Languages CourseComputer Engineering DepartmentSharif University of Technology

Page 91: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

92

Outline92

Syntactic Structures

Descriptive tools: BNF

Elegance

Evaluation and Epilog

Page 92: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

93

Outline93

Syntactic Structures

Descriptive tools: BNF

Elegance

Evaluation and Epilog

Page 93: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

94

Software Portability

Avoid features or facilities that are dependent on a particular computer

or small class of computers.

Portability Principle

Page 94: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

95

Free Format

FORTRAN: fixed format

Algol: free format

Page 95: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

96

Free Format

FORTRAN: fixed format

Algol: free format

Programmers had to think about:

“How a program should be formatted?”

Page 96: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

97

Free Format

Example: FORTRAN style

for i:=1 step 1 until N dobegin real val;Read Real (val);Data [i] := val;end;for i:=1 step 1 until N dosum:= sum + Data [i];avg := sum/N;

Page 97: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

98

Free Format

Example: English style

for i:=1 step 1 until N do begin real val;Read Real (val); Data [i] := val; end; fori:=1 step 1 until N do sum:= sum + Data [i];avg := sum/N;

Page 98: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

99

Free Format

Example: English style

for i:=1 step 1 until N do begin real val;Read Real (val); Data [i] := val; end; fori:=1 step 1 until N do sum:= sum + Data [i];avg := sum/N;

Remember the Structure Principle!

Page 99: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

100

Free Format

Example:

for i:=1 step 1 until N dobegin

real val;Read Real (val);Data [i] := val;

end;for i:=1 step 1 until N do

sum:= sum + Data [i];avg := sum/N;

Page 100: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

101

Levels of Representation

No universal character set!

How to design the lexical structure? Using those symbols available in all char

sets. Design independent of particular char sets.

Page 101: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

102

Levels of Representation

Three levels of the language:1. Reference Language

2. Publication language

3. Hardware representations

a [i+1] := (a [i] + pi ˣ r↑2 ) / 6.021023

a (/i+1/) := (a (/i/) + pi * r**2 ) / 6,02e23

ai+1 ← { ai + π ˣ r2 } / 6.02 ˣ1023

Page 102: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

103

Problems of FORTRAN Lexics Example:

IF IF THEN THEN = 0;

ELSE; ELSE ELSE = 0;

Page 103: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

104

Problems of FORTRAN Lexics Example:

IF IF THEN THEN = 0;

ELSE; ELSE ELSE = 0;

Confusion!

Page 104: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

105

Problems of FORTRAN Lexics How does Algol solve the problem?

if procedure then until := until +1 else do := false;

Page 105: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

106

Lexical Conventions

Reserved words: reserved words cant be used by the

programmer

Keywords: used word by the language are marked in

some unambiguous way

Keywords in Context: used words by the language are keywords

in those context they are expected.

Page 106: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

107

Arbitrary Restrictions, Eliminated!

Number of chars in an identifier Subscript expression

Remember theZero-One-Infinity Principle!

Page 107: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

108

Dangling else

What does the above code mean?

if B then if C then S else T;

if B then begin if C then S else T end;

if B then begin if C then S end else T;

Page 108: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

109

Dangling else

What does the above code mean?

if B then if C then S else T;

Algol solved the problem:

“Consequent of an if-statement must be an unconditional statement”

Page 109: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

110

Algol’s lexical and syntactic structures became so popular that virtually all languages designed since have been

“Algol-like”.

Page 110: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

111

Outline111

Syntactic Structures

Descriptive tools: BNF

Elegance

Evaluation and Epilog

Page 111: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

112

Various Descriptive tools

Example: numeric denotations 1. Giving Examples:

Gives a clear idea, But not so precise.

Integers such as: ‘-273’Fractions such as: ‘3.76564’Scientific notations: ‘6.021023’

Page 112: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

113

Various Descriptive tools

Example: numeric denotations 2. English Description:

Precise, But not so clear!

1. A digit is either 0,1,2,…,9.2. An unsigned integer is a sequence of one or more digits.3. An integer is either a positive sign followed by an unsigned integer or … .4. A decimal fraction is a decimal point immediately followed by an unsigned integer.5. An exponent part is a subten symbol immediately followed by an integer6. A decimal number has one of three forms: … .7. A unsigned number has one of three forms: … .8. Finally, a number … .

Page 113: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

114

Various Descriptive tools

3. Backus-Naur Form (BNF)

Page 114: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

115

Backus Formal Syntactic Notation

How to combine perceptual clarity and precision?

Example: FORTRAN subscript expressions

cvv + c or v - cc * vc * v + c′ or c * v - c′

Page 115: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

116

Backus Formal Syntactic Notation

How to combine perceptual clarity and precision?

Example: FORTRAN subscript expressions

cvv + c or v - cc * vc * v + c′ or c * v - c′The formulas make use of

syntactic categories!

Page 116: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

117

Naur’s Adaptation of the Backus Notation

BNF represents: Particular symbols by themselves, And classes of strings (syntactic

categories) by phrases in angle brackets.

<decimal fraction> ::== .<unsigned integer>

Page 117: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

118

Describing Alternates in BNF

Example: alternative forms

<integer> ::== +<unsigned integer>| -<unsigned integer>| <unsigned integer>

Page 118: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

119

Describing Repetition in BNF

Example: recursive definition

<unsigned integer> ::== <digit> | <unsigned integer><digit>

Page 119: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

120

Extended BNF

How to directly express the idea “sequence of …”?

Kleene cross Kleene star

<unsigned integer> ::== <digit>+

<identifier> ::== <letter>{<letter>|<digit>}*

<integer> ::== [+/-]<unsigned integer>

Page 120: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

121

Extended BNF

How to directly express the idea “sequence of …”?

Kleene cross Kleene star

Why preferable? Remember the

Structure principle!

Page 121: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

122

Mathematical Theory of PLs

Chomsky type 0, or recursively enumerable languages

Chomsky type 1, or context-sensitive languages

Chomsky type 2, or context-free languages

Chomsky type 3, or regular languages

Page 122: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

123

Mathematical Theory of PLs

Languages described by BNF are context-free.

→Mathematical analysis of the syntax and grammar of PLs.

Page 123: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

124

Outline124

Syntactic Structures

Descriptive tools: BNF

Elegance

Evaluation and Epilog

Page 124: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

125

Design Has Three DimensionsEfficiency

Scientific

Economy

Social

Elegance

Symbolic

Page 125: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

126

Efficiency

Efficiency seeks to minimize resources used.

Resources : Memory usage Processing time Compile time Programmer typing time

Page 126: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

127

Economy

Economy seeks to maximize social benefit compared to its cost.

The public: The programming community. Trade offs are hard to make since values

change unpredictably. Social factors are often more important

than scientific ones. (E.g. major manufacturers, prestigious universities, influential organizations)

Page 127: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

128

Elegance

The feature interaction problem. It is impossible to analyze all the possible feature

interactions. All we need to do is to restrict our attention to

designs in which feature interactions look good.

Designs that look good will also be good. Good designers choose to work in a region of

the design in which good designs look good. A sense of elegance is acquired through

design experience. Design, criticize, revise, discard!

Page 128: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

129

Outline129

Syntactic Structures

Descriptive tools: BNF

Elegance

Evaluation and Epilog

Page 129: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

130

Evaluation

Algol-60 never achieved widespread use.

Algol had no input-output Little uniformity among input-output

conventions. It was decided that input-output would be

accomplished by using library procedures. Eventually several sets of input-output

procedures were designed for Algol: It was too late!

Page 130: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

131

Algol directly competed with FORTRAN

Same application area.

FORTRAN had gained considerable ground.

More focus on reductive aspects of Algol.

IBM decided against supporting Algol.

Algol became an almost exclusively academic

language.

Page 131: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

132

A Major Milestone

Introduced programming language terminology.

Influenced most succeeding languages.

An over general language: Quest for simplicity

that resulted in Pascal.

The basis of several extension, like Simula.

Computer architects began to support Algol

implementation.

Page 132: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

133

Second Generation Programming Languages

Algol-60: The first second generation

programming language.

Data structures are close to first-generation

structures.

Hierarchically nested name structures.

Structured control structures (e.g. recursive

procedures, parameter passing modes).

Shifted from free formats.

Page 133: ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology.

134

The End!