Download Presentation
## ALGOL-60 GENERALITY AND HIERARCHY

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**ALGOL-60GENERALITY AND HIERARCHY**Programming Languages Course Computer Engineering Department Sharif University of Technology**Outline**• History and Motivation • Structural Organization • Name Structures • Data Structures • Control Structures**Outline**• History and Motivation • Structural Organization • Name Structures • Data Structures • Control Structures**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**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.**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.**Outline**• History and Motivation • Structural Organization • Name Structures • Data Structures • Control Structures**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 begin end …end end**Category of Construct**• Declaratives: bind name to the objects • Variables • Procedures • Switches • Imperative: do the work of computation • Control-flow • Computational (:=)**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**The Stack, the Central Run-Time Data Structure**• Dynamic allocation and deallocation are achieved by pushing and poping activation records on the stack.**Outline**• History and Motivation • Structural Organization • Name Structures • Data Structures • Control Structures**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**fori:=0 step 1 until N do for i:=0 step 1 until N do • begin • ifData[i]>10 then Data[i]:=10; • sum:=sum+Data[i]; • Print Real(sum); • end sum:=sum+Data[i]**Example**fori:=0 step 1 until N do for i:=0 step 1 until N do • begin • ifData[i]>10 then Data[i]:=10; • sum:=sum+Data[i]; • Print Real(sum); • end sum:=sum+Data[i] The body of a for loop is one statement by default.**Example**fori:=0 step 1 until N do for i:=0 step 1 until N do • begin • ifData[i]>10 then Data[i]:=10; • sum:=sum+Data[i]; • Print Real(sum); • end sum:=sum+Data[i] The compound statement is used where one statement is permitted.**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.**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] end end**Sample Contour Diagram**x y y f z A count**Algol use of nested scopes prevents the programmer from**committing errors that were possible in using FORTRAN COMMON blocks. Impossible Error Principle Making errors impossible to commit is preferable to detecting them after their commission.**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.**Example**a: begin integer m; procedure P m:=1; b: begin integer m; P end; P end**Example**a: begin integer m; procedure P m:=1; b: begin integer m; P end P end With dynamic scoping, the value of this m is 1 when the program finishes block b.**Contour Diagram of Dynamic Scoping**m (a) P (b) m DL Call P (P) m:=1**Example**a: begin integer m; procedure P m:=1; b: begin integer m; P end; P end With static scoping, the value of this m is 1 when the program finishes block b.**Contour Diagram of Static Scoping**m (a) P (b) m DL Call P (P) m:=1**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 begin real procedure f(x); value x; real x; f:=x 2 + 1; sumf:=sum; end 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 begin real procedure f(x); value x; real x; f:=x↑2 + 1; sumf:=sum; end**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 begin real procedure f(x); value x; real x; f:=x 2 + 1; sumf:=sum; end One advantage of dynamic scoping : A general procedure that makes use of variables and procedures supplied by the caller’s environment.**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**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**Dynamic Scoping (Problems)**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 This is called vulnerability. The root procedure is vulnerable to being called from an environment in which its auxiliary procedure is not accessible.**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.**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.**Responsible Design Principle**Do not ask users what they want, find out what they need.**Outline**• History and Motivation • Structural Organization • Name Structures • Data Structures • Control Structures**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.**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.**Algol follows the Zero-One-Infinity principle.**• Arrays are generalized in Algol: • More than 3 dimensions. • Lower bounds can be numbers other than 1. Zero-One-Infinity Principle The only reasonable numbers in a programming language design are zero, one, and infinity.**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.**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.**Outline**47 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 (logical expression) simple statement if expression then statemtent1 else statement2**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 do • inner[i] := outer [N*M-i]; • for i:=i+1 • while 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 do • inner[i] := outer [N*M-i]; • while i < 100 do • begin • A [i] := i; • i=i+1; • end