Document Sample

COP 4020 Programming Languages I Euripides Montagne University of Central Florida (Summer 2010) COP 4020 Programmin Language 1 Outline 1. Course Organization 2. Why studying programming languages? COP 4020 Programmin Language 1 COP 4020 Programming Language I Who am I and where to find me? Instructor: Euripides Montagne Tele.: 823-2684 email: eurip@eecs.ucf.edu Office hours: M from 4:00 p.m. to 5:30 p.m. (HEC 217) T from 12:00 p.m. to 2:30 p.m. (HEC 217) COP 4020 Programmin Language 1 COP 4020 Programming Language I Course information Lecture meetings: MW 12:00 p.m. – 1:50 p.m. (HEC 103 ) Web page: http://www.cs.ucf.edu/courses/cop3402/sum2010/ COP 4020 Programmin Language 1 COP 4020 Programming Language I Course outline Course Outline: This course is designed to provide a fundamental understanding of the design and implementation issues surrounding programming languages and their running environment. Students will be exposed different models of computer programming during the course, including: concurrent programming model, declarative computation model and relational model. Course Topics: History of programming languages. Fundamental concepts of programming languages, such as scope, binding, abstraction, encapsulation, typing etc. Run time environment. Lambda calculus. Declarative computation model. Concurrent programming model and message passing. Operational semantics, axiomatic semantics, and denotational semantics. Relational computation model. Prerequisites: COP 3530C COP 4020 Programmin Language 1 COP 4020 Programming Language I Course outline (continued) Grading Policy: (15%) Exam #1 (15%) Exam #2 (15%) Presentation (25%) Final Exam (30%) Programming project. “Late assignments will not be accepted” COP 4020 Programmin Language 1 COP 4020 Programming Language I Course outline (continued) Material: Required textbook: Concepts, Techniques, and Models of Computer Programming, P. van Roy and S. Haridi, MIT press, 2004, ISBN: 0-262-22069-5. Other recommended source: Concepts of Programming Languages, 9th Edition, Robert W. Sebesta, Addison Wesley, 2010, ISBN: 0-13-607347-6 The text will be supplemented with additional notes that will be provided for you via the course web site: http://www.cs.ucf.edu/courses/cop4020/sum2010/ COP 4020 Programmin Language 1 COP 4020 Programming Language I Who are your TAs? COP 4020 Programmin Language 1 UNIVERSITY OF CENTRAL FLORIDA Lecture 1 History of Programming Languages Class 1: History of Programming Languages • ASSEMBLY LANGUAGE • FORTRAN • ALGOL SIMULA • COBOL • PL/1 • BASIC • PASCAL • C • LISP • The so called High Level Programming Languages started their development in the mid 50s. ASSEMBLERS The earliest programming languages were oriented to control the behavior of computers. They reflected the structure of the underlying machine. This view point change mainly for two reasons: 1.- What was easy for a machine to reason about was not necessarily easy for a human being to reason about. 2.- The number of different kinds of machines( architectures) increased and the need arose for a common programming language with which to program all of them. COP 4020 Programmin Language 1 FORTRAN (FORmulaTRANslation) • Fortran was designed to develop scientific applications on the IBM 704(with punched card inputs) computer and the major role in its design was run-time efficiency. • Developed by John Backus and his group in 1954. PUNCH CARD COP 4020 Programmin Language 1 Main features: • Comments • Assignment statements that allowed mathematical expressions of some complexity on the “right-hand” side • The simplicity of writing loops with the DO statement • Subroutines and functions: Not a new idea but it was implemented employing a symbolic notation • Formats for input and output: Difficult feature to implement on early computers • Machine independence: A fortran program could run on different machines Main reasons for its popularity: • It made efficient use of programming time • Easy to learn (good for non-specialist programmers) • It was supported by IBM • It simplifies INPUT/OUTPUT Example of a Fortran Program C FORTRAN PROGRAM TO FIND MEAN OF N NUMBERS C AND NUMBER OF VALUES GREATER THAN MEAN DIMENSION A(99) REAL MEAN READ (1,5)N 5 FORMAT (I2) READ (1,10) (A(I), I=1, N) 10 FORMAT (6F10.5) SUM = 0.0 DO 15 I=1, N 15 SUM = SUM + A(I) MEAN = SUM/FLOAT(N) NUMBER = 0 DO 20 I=1, N IF (A(I).LE.MEAN) GOTO 20 NUMBER = NUMBER + 1 20 CONTINUE WRITE (2,25) MEAN, NUMBER 25 FORMAT (8H MEAN = , F10.5, 5X, 20H NUMBER OVER MEAN = , I5) STOP END. Development: • The first design of Fortran was made in 1954 (Fortran 0) • The first implementation was developed in 1957(Fortran I) • A better compiler was developed in 1958 (Fortran II) • After many revisions in 1962 a stable compiler (Fortran IV) • First ANSI standard 1966 (Fortran 66) • After a major revision new features are added (Fortran 77) • A more modern Fortran is created in 1990 (Fortran 90) FORTRAN 77: • IF-THEN-ELSE FORTRAN 90: • Offers features that are common to modern programming languages: • Records • Modules • Pointers • Subprograms could be recursively called FORTRAN 95: • FOR ALL construct to aid vectorization FORTRAN 2003: • Object-oriented programming • IEEE floating-point arithmetic COP 4020 Programmin Language 1 FORTRAN 0 FORTRAN I(1957) COBOL(1960) ALGOL 58 FORTRAN II ALGOL 60 FORTRAN IV PL/1(1964) BASIC FORTRAN 66 FORTRAN 77 FORTRAN 90 Referencing in FORTRAN • All variables are local to each routine: The local environment of a subroutine consists of the variables, arrays, etc. declared at the start of the subprogram. The local environment is retained between calls because the activation record is allocated statically. • Global variables are created using the COMMON declaration: COMMON /<name>/<var or array>, <var or array>, <var or array>, … Example: COMMON/ Globalvar/X/Y/A(20) program main real X, Y common /BLK/ X, Y statements stop End subroutine sub1 ( arguments) declarations of arguments real X, Y common /BLK/ X, Y statements return End subroutine sub2 ( arguments) declarations of arguments real X, Y common /BLK/ X, Y statements return end FORTRAN Memory layout System data and I/O buffers Code and local data for main program BLK (common block) Code and local data for sub1 Code and local data for sub2 System I/O routine Implementing FORTRAN 77 Subprograms Activation Record COP 4020 Programmin Language 1 FORTRAN 77 Subprograms • FORTRAN 77 subprograms cannot be recursive. • Variables declared in subprograms are statically allocated. Therefore, both parts of a FORTRAN 77 Subprogram (the code and the local variables) have fixed sizes. • So the activation record of a FORTRAN 77 subprogram has a fixed size, and therefore can be statically allocated. COP 4020 Programmin Language 1 FORTRAN 77 Activation Record Format Return address Local variables Parameters COP 4020 Programmin Language 1 FORTRAN 77 Code and Activation Records for Subprograms A, B, and C COMMON is a mechanism used by FORTRAN 77 for referencing nonlocal variables COMMON Storage MAIN Local variables Return address A Local variables Data Parameters Return address B Local variables Parameters Return address C Local variables Parameters MAIN A Code B COP 4020 Programmin Language C 1 ALGOL (ALGOrithmic Language) ALGOL was develop in 1958 as a committee effort to design a language for the description of computing processes in publications. The objectives of the language were stated as follows: • It should be close as possible to standard mathematical notation and be readable without too much additional explanation. • It should be mechanically translate into machine code. ALGOL 58 ALGOL 60 ALGOL 68 ALGOL W SIMULA 67 PASCAL COP 4020 Programmin Language 1 Major Concepts introduced in ALGOL(1): • Language definition: a formal definition in Backus Naur Form (BNF) was used to define the syntax for the first time; this led to syntax-directed compilers. • ALGOL 60 was structured: It was the originally block structured language and variables were not visible outside the block in which they were declared. • Arrays could have variables bounds. • Contained several structured control statements: Major Concepts introduced in ALGOL(2): • Sequence: S1,S2,…,Sn • Selection (IF-THEN-ELSE) • Iteration (For I := 1 step 1 until n do) • First language to introduce recursive procedures. COP 4020 Programmin Language 1 Why ALGOL 60 did not supersede FORTRAN? • Compilers came out approximately three years after Fortran. • As it had more features than Fortran it was harder to learn. • IBM customers were happy with Fortran. • Fortran compilers were simpler to produce and more efficient. • Algol 60 had not official Input/output therefore they left this feature to the individual manufacturers. Example of an Algol program: begin comment this program finds the mean of n numbers and the number of values greater than the mean; integer n; read(n); begin real array a[1:n]; integer i, number; real sum, mean; for i := 1 step 1 until n do read(a[i]); sum := 0.0; for i := 1 step 1 until n do sum := sum + a[i]; mean := sum/n; number := 0; for i := 1 step 1 until n do if a[i] > mean then number := number + 1; write (“MEAN = “, mean, “NUMBER OVER MEAN = “, number); end; end; ALGOL 60 ALGOL W(1966) ALGOL68 SIMULA 67 BCPL PASCAL C MODULA C++ ADA(83) OBERON Eiffel(90) ADA(95) JAVA The successor of ALGOL 60 was ALGOL W which was proposed by Wirth and Hoare in 1966 . The more important changes and additions were(1): Records and references allowed linked list, trees and graphs. The case statement. Changes which separate “for” and “While”. Procedures and function parameters could be passed by values and by name. Long real and complex data type were introduced (complex arithmetic) The successor of ALGOL 60 was ALGOL W which was proposed by Wirth and Hoare in 1966 . The more important changes and additions were(2): • The bits data type gave low-level processing ability. • Some string facilities were included. • Assert statement were allowed and the assertion tested during a program run. • Concurrent execution was implemented (B6700) COP 4020 Programmin Language 1 The next step was ALGOL 68 which amid its interesting features introduced: An economy of constructs: produce a core language with a small number of powerful constructs. Orthogonality: This was its major design goal. There ware no interactions between constructs when they were combined. Having determined how a feature worked in one situation, it could be expected to behave in a similar way in any other situation. Run-time stack • The run-time stack and the activation record allows us to have more than one instance (incomplete execution) of a subprogram at a given time. • Recursion adds the possibility of multiple simultaneous activations (incomplete execution) of a subprogram at a given time. COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real; procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; P1(R2, T2, A2); /* calls P1 */ end;(* P2 *) begin P2; /* calls P2 */ end; (* P1 *) begin P1(R1, T1, A1); /* calls P1 */ end;(* program *) COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real; procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; P1(R2, T2, A2); /* calls P1 */ end;(* P2 *) begin P2; /* calls P2 */ end; (* P1 *) begin P1(R1, T1, A1); /* calls P1 */ end;(* program *) Activation Working space for Procedure P0, the record for main program P0 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real; procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; P1(R2, T2, A2); /* calls P1 */ end;(* P2 *) begin P2; /* calls P2 */ end; (* P1 *) begin P1(R1, T1, A1); /* calls P1 */ end;(* program *) Working space for Procedure P0, the main program Activation record for Memory for variable T1 P0 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real; procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; P1(R2, T2, A2); /* calls P1 */ end;(* P2 *) begin P2; /* calls P2 */ end; (* P1 *) begin Working space for Procedure P0, the P1(R1, T1, A1); /* calls P1 */ main program end;(* program *) Activation Pointer to elements of A1 record for Memory for variable T1 P0 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real;) procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; P1(R2, T2, A2); /* calls P1 */ end;(* P2 *) begin P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real;) procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; P1(R2, T2, A2); /* calls P1 */ end;(* P2 *) Activation begin Working space for Procedure P1 record for P1 P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real;) procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; P1(R2, T2, A2); /* calls P1 */ end;(* P2 *) Working space for Procedure P1 Activation record for begin P1 Memory for variable X P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real;) procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; T2 := 25; Working space for Procedure P1 P1(R2, T2, A2); /* calls P1 */ Activation end;(* P2 *) record for Pointer to variable T1 P1 begin Memory for variable X P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real;) procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; begin R2 := 5; Working space for Procedure P1 T2 := 25; Activation P1(R2, T2, A2); /* calls P1 */ Indirect pointer to elements of A1 record for end;(* P2 *) Pointer to variable T1 P1 begin Memory for variable X P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real;) procedure P2; var R2, T2: real; A2: array[0:5,0:20] of real; Working space for Procedure P1 begin R2 := 5; Reference to procedure P2 Activation T2 := 25; record for P1(R2, T2, A2); /* calls P1 */ Indirect pointer to elements of A1 P1 end;(* P2 *) Pointer to variable T1 begin Memory for variable X P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Y: array[0:5,0:20] of real;) Activation procedure P2; Working space for Procedure P2 record for var P2 R2, T2: real; A2: array[0:5,0:20] of real; Working space for Procedure P1 begin R2 := 5; Reference to procedure P2 Activation T2 := 25; record for P1(R2, T2, A2); /* calls P1 */ Indirect pointer to elements of A1 P1 end;(* P2 *) Pointer to variable T1 begin Memory for variable X P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; var T: real; Working space for Procedure P2 Y: array[0:5,0:20] of real;) Activation record for Memory for the variable T2 P2 procedure P2; var Memory for the variable R2 R2, T2: real; A2: array[0:5,0:20] of real; Working space for Procedure P1 begin R2 := 5; Reference to procedure P2 Activation T2 := 25; record for P1(R2, T2, A2); /* calls P1 */ Indirect pointer to elements of A1 P1 end;(* P2 *) Pointer to variable T1 begin Memory for variable X P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Program Stack program P0; var R1, T1: real; A1: array[0:5,0:20] of real; procedure P1( X: real; Working space for Procedure P2 var T: real; Activation Y: array[0:5,0:20] of real;) Pointer to the elements of A2 record for Memory for the variable T2 P2 procedure P2; var Memory for the variable R2 R2, T2: real; A2: array[0:5,0:20] of real; Working space for Procedure P1 begin R2 := 5; Reference to procedure P2 Activation T2 := 25; record for P1(R2, T2, A2); /* calls P1 */ Indirect pointer to elements of A1 P1 end;(* P2 *) Pointer to variable T1 begin Memory for variable X P2; /* calls P2 */ end; (* P1 *) Working space for Procedure P0, the begin main program P1(R1, T1, A1); /* calls P1 */ Reference to procedure P1 Activation end;(* program *) record for Pointer to elements of A1 P0 Memory for variable T1 Memory for variable R1 COP 4020 Programmin Language 1 Stack state while P2 is executing Working space for Procedure P2 Activation Elements of record for Pointer to the elements of A2 array A2 P2 Memory for the variable T2 Memory for the variable R2 Working space for Procedure P1 Activation record for Reference to procedure P2 P2 code P1 Indirect pointer to elements of A1 Pointer to variable T1 Memory for variable X Working space for Procedure P0, the Activation main program record for P0 Reference to procedure P1 P1 code Pointer to elements of A1 Memory for variable T1 Elements of Memory for variable R1 array A1 COP 4020 Programmin Language 1 FORTRAN 0 Flow-Matic (Grace Hopper-57) FORTRAN I(1957) COBOL(1960) ALGOL 58 FORTRAN II ALGOL 60 FORTRAN IV PL/1(1964) BASIC FORTRAN 66 FORTRAN 77 FORTRAN 90 COP 4020 Programmin Language 1 COBOL (Common Business Oriented Language) COBOL was based on the Flow-Matic language(1957), developed by Grace Hopper, and IBM's specification of its planned Commercial Translator. COBOL is essentially a data processing language and that is the reason why it differs significantly from FORTRAN and ALGOL. Several design principles guided the design of COBOL – separate data & procedures – machine dependent statements in one place – Naturalness (English-like form) – ease of transcription to require media (cards at the time) – effectiveness of problem structure – ease of implementation (for compiler writers) – physically available character set (printable) – long data names COBOL-60 (First Compiler – Only Three DIVISIONS) COBOL-68 (standardized version - (ANSI ) American National Standards Institute) COBOL-74 (revisions to improve COBOL-68 and interaction with remote devices ) COBOL-85 (facilities for structured programming - DB2 and SQL support) COBOL 2002 (Object Oriented Programming) COP 4020 Programmin Language 1 Main features: • The language is simple • No pointers • No user defined types • Record data type • Self documented A COBOL program is divided in four parts or divisions: Division Name Contains • IDENTIFICATION Program identification. • ENVIRONMENT Type of computer used. • DATA Buffers, constants, work areas. • PROCEDURE The processing (program logic). A COBOL program is divided in four parts: • The identification division: Contains commentary and program documentation. 000100 INDENTIFICATION DIVISION. 000110 PROGRAM-ID. EXAMPLE-1-PROG. 000120 AUTHOR. TIM R P BROWN. 000130 INSTALLATION. XYZ GROUP. 000140 DATE-WRITTEN. 5/15/06. 000150 DATE-COMPILED. 000160 SECURITY. LOCAL GROUP • The environment division: Contain machine-dependent program specifications. Thus, it specifies the connections between the COBOL program and the external data file. 000260 ENVIRONMENT DIVISION. 000270 CONFIGURATION SECTION. 000280 SOURCE-COMPUTER. IBM PC. 000290 OBJECT-COMPUTER. IBM PC. 000300 INPUT-OUTPUT SECTION. 000310 FILE-CONTROL. 000320 SELECT INPUT-FILE ASSIGN TO 'input.dat‘ 000330 ORGANIZATION IS LINE SEQUENTIAL. 000340 SELECT PRINT-FILE ASSIGN TO PRINTER. The data division: Gives a logical description of the data. 000800 DATA DIVISION. 000900 FILE SECTION. 001000 FD SALESPERSON-FILE. 001100 01 SALESPERSON-RECORD. 001200 05 FILLER PIC XX. 001300 05 SP-NUMBER PIC X(4). 001400 05 SP-NAME PIC X(18). 001500 05 FILLER PIC X(21). 001600 05 SP-CURRENT-SALES PIC 9(5)V99. 001700 05 SP-CURRENT-RETURNS PIC 9(4)V99. 001800 FD REPORT-FILE. 001900 01 REPORT-RECORD. 002000 05 FILLER PIC X(10). 002100 05 RT-NUMBER PIC X(4). 002200 05 FILLER PIC X(6). 002300 05 RT-NAME PIC X(18). 002400 05 FILLER PIC X(6). 002500 05 RT-CURRENT-SALES PIC ZZ,ZZZ.99. 002600 05 FILLER PIC X(6). 002700 05 RT-CURRENT-RETURNS PIC Z,ZZZ.99. 002800 05 FILLER PIC X(65). 002900 WORKING-STORAGE SECTION. 003000 01 WS-EOF-FLAG PIC X. • The procedure division: Contains the algorithms necessary to solve the problem. 000900 PROCEDURE DIVISION. 000910 CONTROL-PARAGRAPH. 000920 PERFORM READ-DATA-FILE 000930 PERFORM CALULATE-PRICES 000940 PERFORM PRINT-PRICE-REPORT 000950 STOP RUN. Example of a COBOL program(Hello world): 000010 IDENTIFICATION DIVISION. 000020 PROGRAM-ID. HELLO-WORLD-PROG. 000030 AUTHOR. TIMOTHY R P BROWN. 000040*The standard Hello world program 000050 000060 ENVIRONMENT DIVISION. 000070 000080 DATA DIVISION. 000090 WORKING-STORAGE SECTION. 000100 01 TEXT-OUT PIC X(12) VALUE 'Hello World!'. 000110 000120 PROCEDURE DIVISION. 000130 MAIN-PARAGRAPH. 000140 DISPLAY TEXT-OUT 000150 STOP RUN. Example program 2: 000100 ID DIVISION. 000200 PROGRAM-ID. ACCEPT1. 000300 DATA DIVISION. 000400 WORKING-STORAGE SECTION. 000500 01 WS-FIRST-NUMBER PIC 9(3). 000600 01 WS-SECOND-NUMBER PIC 9(3). 000700 01 WS-TOTAL PIC ZZZ9. 000800* 000900 PROCEDURE DIVISION. 001000 0000-MAINLINE. 001100 DISPLAY 'ENTER A NUMBER: '. 001200 ACCEPT WS-FIRST-NUMBER. 001300* 001400 DISPLAY 'ANOTHER NUMBER: '. 001500 ACCEPT WS-SECOND-NUMBER. 001600* 001700 COMPUTE WS-TOTAL = WS-FIRST-NUMBER + WS-SECOND- NUMBER. 001800 DISPLAY 'THE TOTAL IS: ', WS-TOTAL. 001900 STOP RUN. Some basic commands to be used in the Procedure Division: MOVE zero to I, J, K ADD a TO b. ADD c TO a GIVING c. COMPUTE x = a + b * c. SORT input-file ON ASCENDING KEY k USING inventory-file GIVING sorted-inventory-file DISPLAY totalcost. ACCEPT identifier. 000020 IDENTIFICATION DIVISION. 000040 PROGRAM-ID. MEAN. 000060 AUTHOR. JOHN DOE. 000080 * Program to find the mean and number of values greater than the mean 000100 000120 DATA DIVISION. 000140 000160 WORKING-STORAGE SECTION. 000180 01 N PIC 999 VALUE ZEROS. 000200 01 I PIC 999 VALUE ZEROS. 000220 01 NUMBER PIC 999 VALUE ZEROS. 000240 01 MEAN PIC 999V999 VALUE ZEROS. 000260 01 SUM PIC 999V999 VALUE ZEROS. 000280 01 A PIC 999V999 OCCURS 100 TIMES VALUE ZEROS. 000300 000320 000340 PROCEDURE DIVISION. 000360 000380 DISPLAY "ENTER NUMBER OF ARRAY ELEMENTS: ". 000400 ACCEPT N. 000420 000440 ENTER-NUMBER. 000460 ADD 1 TO I. 000480 DISPLAY "ENTER NUMBER: ". 000500 ACCEPT A(I). 000520 IF I < N GO ENTER-NUMBER. 000540 MOVE 0 TO I. 000560 GO SUM-NUMBER. 000580 000600 SUM-NUMBERS. 000620 ADD 1 TO I. 000640 COMPUTE SUM = SUM + A(I). 000680 IF I < N GO SUM-NUMBERS. 000700 COMPUTE MEAN = SUM / N. 000720 MOVE 0 TO I. 000740 GO GREATER-NUMBER. 000760 000780 GREATER-NUMBER. 000800 ADD 1 TO I. 000820 IF A(I) > MEAN ADD 1 TO NUMBER. 000840 IF I < N GO GREATER-NUMBER. 000860 GO OEJ. 000880 000900 EOJ. 000920 DISPLAY "MEAN = ", MEAN. 000940 DISPLAY "NUMBER OVER MEAN = ", NUMBER. 000960 STOP RUN. 000980 001000 COP 4020 Programmin Language 1 COBOL continues to progress with work being done in the Object-Oriented COBOL standard. PROGRAMMING LANGUAGE 1 (PL/1) In the early 1960s two categories of programmers could be distinguished: Scientific programmers (Fortran) Commercial programmers (COBOL) A committee at IBM developed PL/1 based in the following principles: – Programmers’ time is an important asset and should not be wasted. – There is a unity in programming which current division between scientific and commercial languages did not reflect. FORTRAN 0 FORTRAN I(1957) COBOL(1960) ALGOL 58 FORTRAN II ALGOL 60 FORTRAN IV PL/1(1964) BASIC FORTRAN 66 FORTRAN 77 FORTRAN 90 PROGRAMMING LANGUAGE 1 (PL/1) (Cont.) Pl/1 combined ideas from Fortran, Algol, and COBOL: FORTRAN: parameter passing mechanism, independently compiled subprograms, formatted input/output and COMMON blocks. ALGOL: Block structure and structured statements. COBOL: record input/output,, PICTURE type declaration, and heterogeneous data structures. From elsewhere: list processing concepts, control structures and methods for storage management, exception handling by means of “ON-conditions” and concurrent execution of tasks (multi-tasking). Example of a PL/1 program: PROCEDURE OPTIONS (MAIN) /* Program to find the mean of n numbers and the number of values greater than the mean */ GET LIST (N) IF N > 0 THEN BEGIN; DECLARE MEAN, A(n) DECIMAL FLOAT, SUM DEC FLOAT INITIAL(0), NUMBER FIXED INITIAL (0); GET LIST (A); DO I = 1 TO N; SUM = SUM + A(1); END; MEAN = SUM/N; DO I = 1 TO N; IF A(I) > MEAN THEN NUMBER = NUMBER +1; END; PUT LIST (‘MEAN=’, MEAN, ‘NUMBER GRATER THAN MEAN=’, NUMBER); END; BASIC (Beginner’s All-purpose Symbolic Instruction Code) It was originally designed at Dartmouth College by John Kemeny and Thomas Kurtz in the mid-1960s. FORTRAN 0 FORTRAN I(1957) COBOL(1960) ALGOL 58 FORTRAN II ALGOL 60 FORTRAN IV PL/1(1964) FORTRAN 66 BASIC(mid 60s) FORTRAN 77 FORTRAN 90 Main Features: • Variables cannot be declared and are single letters. • Variables are initialized to zero • Easy to learn and use. • It was interactive with commands as NEW to create a program or LIST, RUN, and SAVE. Example of BASIC program: 10 REM THIS IS A BASIC PROGRAM FOR FINDING THE MEAN 20 DIM A(99) 30 INPUT N 40 FOR I = 1 TO N 50 INPUT A(I) 60 LET S = S + A(I) 70 NEXT I 80 LET M = S/N 90 LET K = 0 100 FOR I = 1 TO N 110 IF A(I) < M THEN 130 120 LET K = K + 1 130 NEXT I 140 PRINT “MEAN IS”, M 150 PRINT “NUMBER GREATER THAN MEAN IS”, K 160 STOP 170 END ALGOL COBOL ALGOL 68 ALGOL W PL/1 PASCAL ADA Pascal • It was designed by Niklaus Wirth. • A teaching language • The language most used in the 1970s • Include run time environment (code, static data, stack … heap) • It uses the concept of static array like in Fortran. • New data types(define by the user) can be built up from primitive data types. Run time environment for: Object code Pascal Static Data C Ada Stack Java Heap Example of a Pascal Program: • (*Pascal program for finding the mean*) • Program main (input, output); • type intlist = array [1 . . 99] of integer; • var • a : intlist; • i, n, number : integer; • sum, mean : real; • (*main program starts here *) • begin • number := 0; • sum := 0; • readln (n); • for I := 1 to n do • begin • readln(a[i]); • sum := sum + a[i] • end; • mean := sum/n; • for I := 1 to n do • if (a[i] > mean) then number := number + 1; • writeln (‘the number over mean is: ‘, number) • end. Example for “temporary expression” x[ i ] = (I + J) * (I/J + f( J )) (Stack Pointer) Outgoing results SP Result of I / K Result of I + J Address of x [ i ] Activation Record Local Variables Formal Parameter RA (Stack Frame) SF Static Link Dynamic Link Some comments of N. Wirth about language design “Power of a language lies in its regularity and not in its abundant of Features” “Character of a language is defined by what it prevents more than by what it allows to be expressed” C Implemented by Dennis Ritchie in 1972 system programming c – 1972 c – 1989 (ANSI) c – 1992 (ISO) ALGOL 60 ALGOL W(1966) ALGOL68 SIMULA 67 BCPL PASCAL C(1972) MODULA C++ ADA(83) OBERON Eiffel(90) ADA(95) JAVA An Example of a C Program: /*C program for finding the mean*/ main(){ float a[100], mean, sum; /* the array a has 100 elements – a[0], .. a[99] */ int n, i, number; scanf(“%d”, &n); for( i = 0; i < n; ++i) scanf(“%f”, &a[ i ]); sum = 0.0; for( i = 0; i < n; ++i) sum += a[ i ]; mean = sum / n; number = 0; for( i = 0; i < n; ++i){ if( a[ i ] > mean) number++; } printf(“ MEAN = %f \n”, mean); printf(“NUMBER OVER MEAN = %d\n”, number); } * No nested procedures are allowed COP 4020 Programming Languages I THE END COP 4020 Programmin Language 1

DOCUMENT INFO

Shared By:

Categories:

Tags:

Stats:

views: | 9 |

posted: | 6/6/2013 |

language: | Unknown |

pages: | 84 |

OTHER DOCS BY cuiliqing

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.