1 / 133

ALGOL-60 GENERALITY AND HIERARCHY

ALGOL-60 GENERALITY 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

avani
Download Presentation

ALGOL-60 GENERALITY AND HIERARCHY

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline • History and Motivation • Structural Organization • Name Structures • Data Structures • Control Structures

  3. Outline • 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 begin end …end end

  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

  12. Compile-time, Run-time Distinction • FORTRAN: static compilation process • Algol: not completely static • Dynamic arrays • Recursive procedures

  13. The Stack, the Central Run-Time Data Structure • Dynamic allocation and deallocation are achieved by pushing and poping activation records on the stack.

  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.

  16. 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]

  17. 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.

  18. 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.

  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. 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

  21. Sample Contour Diagram x y y f z A count

  22. 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.

  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. 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.

  27. Contour Diagram of Dynamic Scoping m (a) P (b) m DL Call P (P) m:=1

  28. 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.

  29. Contour Diagram of Static Scoping m (a) P (b) m DL Call P (P) m:=1

  30. 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.

  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 begin real procedure f(x); value x; real x; f:=x↑2 + 1; sumf:=sum; end

  32. 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.

  33. 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

  34. 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

  35. 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.

  36. 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.

  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. Responsible Design Principle Do not ask users what they want, find out what they need.

  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. Zero-One-Infinity Principle The only reasonable numbers in a programming language design are zero, one, and infinity.

  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.

  46. Outline 47 History and Motivation Structural Organization Name Structures Data Structures Control Structures

  47. 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

  48. Control Structures, Generalization of FORTRAN’s • FORTRAN: • Algol: IF (logical expression) simple statement if expression then statemtent1 else statement2

  49. 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;

  50. 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

More Related