ALGOL-60 GENERALITY AND HIERARCHY
description
Transcript of ALGOL-60 GENERALITY AND HIERARCHY
ALGOL-60 GENERALITY AND
HIERARCHY
Programming Languages CourseComputer Engineering DepartmentSharif University of Technology
Outline2
History and Motivation Structural Organization Name Structures Data Structures Control Structures
Outline3
History and Motivation Structural Organization Name Structures Data Structures Control Structures
4
The International Algebraic Language
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
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.
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.
8
Outline
History and Motivation Structural Organization Name Structures Data Structures Control Structures
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
10
Category of Construct Declaratives: bind name to the objects
Variables Procedures Switches
Imperative: do the work of computation Control-flow Computational (:=)
11
The Familiar Control Structure
goto If-Then-Else For loop Procedure invocation
Compile-time, Run-time Distinction FORTRAN: static compilation process
Algol: not completely static Dynamic arrays Recursive procedures
12
The Stack, the Central Run-Time Data Structure Dynamic allocation and deallocation are
achieved by pushing and poping activation records on the stack.
13
14
Outline
History and Motivation Structural Organization Name Structures Data Structures Control Structures
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.
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
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
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.
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.
20
Examplebegin real x,y; procedure f(y,z); integer y,z; begin real array A[1:y]; end begin integer array Count [0:99] endend
21
Sample Contour Diagramxyf
yz
A
count
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
23
Static and Dynamic Scoping
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.
25
Example a: begin integer m; procedure P m:=1; b: begin integer m; P end; P end
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.
27
Contour Diagram of Dynamic Scoping
mP
(a)
m(b)Call P
DLm:=1
(P)
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.
29
Contour Diagram of Static Scoping
mP
(a)
m(b)Call P
DLm:=1
(P)
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.
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
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.
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
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
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.
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.
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.
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.
39
Do not ask users what they want, find out what they need.
Responsible Design Principle
40
Outline
History and Motivation Structural Organization Name Structures Data Structures Control Structures
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.
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.
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
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.
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.
Outline47
History and Motivation Structural Organization Name Structures Data Structures Control Structures
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
Control Structures, Generalization of FORTRAN’s
FORTRAN:
Algol:
if expression then statemtent1 else statement2
IF (logical expression) simple statement
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;
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
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)
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.
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
Nested Statements
the Algol designers’ realized:
All control structures should be allowed to govern an arbitrary number of
statements.
Nested Statements Compound statement:
beginstatement 1 statement 2...statement n
end
Nested Statements Begin-end brackets do double duty:
1. Group statements into compound statements.
2. Delimit blocks, which define nested scopes.
Nested Statements Begin-end brackets do double duty:
1. Group statements into compound statements.
2. Delimit blocks, which define nested scopes.
Lack of orthogonality!
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.
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.”
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
Recursive Procedures Example:
integer procedure fac (n);value n; integer n;fac := if n =0 then 1 else n * fac(n-1);
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.
Parameter Passing by Value Example:
procedure switch(n);value n, integer n;n:=3;
Parameter Passing by Value Example:
Procedure switch(n);Value n, integer n;n:=3;
Useful only for input parameters!
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;
Parameter Passing by Name Example:
Pass by name is based on substitution!
Procedure Inc (n);integer n;n := n+1;
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
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!
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 doS := S + ak;
Sum:=S;end;
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 doS := S + v [i];
x := S;end;
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.
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
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.
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”
Expensive Out of Block GOTOs
An identifier declaration is visible if we are nested within the block in
which it occurs .
Algol Scope Rule
Expensive Out of Block GOTOs
Example:a : begin array X [1:100];
…b : begin array Y [1:100];…goto exit;…end;
exit :…
end
Expensive Out of Block GOTOs
Another example:
beginprocedure P (n);
value n; integer n;if n=0 then goto outelse P(n-1);
P(25);out:end
Expensive Out of Block GOTOs
Another example:
beginprocedure P (n);
value n; integer n;if n=0 then goto outelse 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!
Future Interaction, a Difficult Design Problem
Algol visibility rules versus GOTO statement
Problem of Interaction!
Future Interaction, a Difficult Design Problem
Designer must develop a “nose” for spotting possible problem
areas!
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…
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)
The Baroque for-Loop
Users should pay only for what they use; avoid distributed costs.
The Localized Cost Principle
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;
The Baroque switch Example:
beginswitch S=L, if i>0 then M else N, Q;goto S [j];
end
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;
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;
89
The Baroque switch Example:
beginswitch S=L, if i>0 then M else N, Q;goto S [j];
end
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;
91
SYNTAX AND ELEGANCE: ALGOL-60
Programming Languages CourseComputer Engineering DepartmentSharif University of Technology
92
Outline92
Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog
93
Outline93
Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog
94
Software Portability
Avoid features or facilities that are dependent on a particular computer
or small class of computers.
Portability Principle
95
Free Format FORTRAN: fixed format
Algol: free format
96
Free Format FORTRAN: fixed format
Algol: free format
Programmers had to think about:
“How a program should be formatted?”
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;
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;
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!
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;
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.
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
103
Problems of FORTRAN Lexics Example:
IF IF THEN THEN = 0;
ELSE; ELSE ELSE = 0;
104
Problems of FORTRAN Lexics Example:
IF IF THEN THEN = 0;
ELSE; ELSE ELSE = 0;
Confusion!
105
Problems of FORTRAN Lexics How does Algol solve the problem?
if procedure then until := until +1 else do := false;
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.
107
Arbitrary Restrictions, Eliminated!
Number of chars in an identifier Subscript expression
Remember theZero-One-Infinity Principle!
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;
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”
110
Algol’s lexical and syntactic structures became so popular that virtually all languages designed since have been
“Algol-like”.
111
Outline111
Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog
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’
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 … .
114
Various Descriptive tools
3. Backus-Naur Form (BNF)
115
Backus Formal Syntactic Notation
How to combine perceptual clarity and precision?
Example: FORTRAN subscript expressions c
vv + c or v - cc * vc * v + c′ or c * v - c′
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!
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>
118
Describing Alternates in BNF
Example: alternative forms
<integer> ::== +<unsigned integer>| -<unsigned integer>| <unsigned integer>
119
Describing Repetition in BNF
Example: recursive definition
<unsigned integer> ::== <digit> | <unsigned integer><digit>
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>
121
Extended BNF How to directly express the idea
“sequence of …”? Kleene cross Kleene star
Why preferable? Remember the
Structure principle!
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
123
Mathematical Theory of PLs
Languages described by BNF are context-free.
→Mathematical analysis of the syntax and grammar of PLs.
124
Outline124
Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog
125
Design Has Three DimensionsEfficiency
Scientific
Economy
Social
Elegance
Symbolic
126
Efficiency Efficiency seeks to minimize resources
used.
Resources : Memory usage Processing time Compile time Programmer typing time
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)
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!
129
Outline129
Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog
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!
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.
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.
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.
134
The End!