COSC 2 P 05 Programming languages COSC 2

  • Slides: 41
Download presentation
COSC 2 P 05 – Programming languages COSC 2 P 05 Programming Languages •

COSC 2 P 05 – Programming languages COSC 2 P 05 Programming Languages • • Instructor: – Michael Winter – Office: MCC J 323 – Email: mwinter@brocku. ca Lecture: Mon & Thu, 14: 00 -15: 30 online in Lifesize Course web page: www. cosc. brocku. ca/~mwinter/Courses/2 P 05/ Textbook: Sebesta, Robert W. Concepts of Programming Languages (12 th edition) Pearson, 2019 ISBN: 9780135102268 © M. Winter 1. 1

COSC 2 P 05 – Programming languages Course Work • Marking Scheme – Midterm

COSC 2 P 05 – Programming languages Course Work • Marking Scheme – Midterm 1 – Midterm 2 – Final Exam Feb 11, 14: 00 -15: 30 Mar 18, 14: 00 -15: 30 Apr 12, 13: 00 -15: 00 20% 60% Both tests and the final exam will be online in Lifesize during the regular lecture time. © M. Winter 1. 2

COSC 2 P 05 – Programming languages Course Outline Week Date Chapter Topic 1

COSC 2 P 05 – Programming languages Course Outline Week Date Chapter Topic 1 Jan 11/14 1, 2 Introduction, Evolution of programming languages 2 Jan 18/21 2 Evolution of programming languages 3 Jan 25/28 5, 6 Names, Bindings, Scope, Data types 4 Feb 01/04 6, 7 Data types, Expressions and assignments 5 Feb 08/11 8 Control structures (Midterm 1) 6* Feb 22/25 9, 10 Subprograms 7 Mar 01/04 11 Abstract data type 8 Mar 08/11 12 Object-oriented languages 9 Mar 15/18 13 Concurrency (Midterm 2) 10 Mar 22/25 15 Functional programming languages 11 Mar 29/Apr 01 15, 16 Functional programming languages, Logic programming languages 12 Apr 05/08 16 Logic programming languages, Review * Feb 15 -19 is Reading Week. © M. Winter 1. 3

COSC 2 P 05 – Programming languages Why Studying Programming Languages • • •

COSC 2 P 05 – Programming languages Why Studying Programming Languages • • • Increased capacity to express ideas – Implementation of concepts learnt in another language that is not available in the given language Improve background for choosing appropriate language – Knowing only one or two languages leads to using these languages even for problems they are poorly suited for Increased ability to learn new languages – Knowing the fundamental concepts of programming languages makes it easy to learn new languages Better understanding of the significance of implementation – Understanding concepts and their implementation allows using the language more intelligently; certain bugs can only be found if certain implementation details are known Better use of languages that are already known – Knowing fundamental concepts may lead to use previously unknown (or unused) feature of a language Overall advancement of computing – Better understanding of programming languages in the community leads to better lanaguages © M. Winter 1. 4

COSC 2 P 05 – Programming languages Programming Domains • • • Scientific applications

COSC 2 P 05 – Programming languages Programming Domains • • • Scientific applications – Main domain in the early days of computing (FORTAN) Business applications – Facilities needed: Reports, Various (precise) decimal numbers and operations, character data (COBOL) Artificial Intelligence – Symbolic computation and lists (LISP) System programming – Low-level features (PL I, C) Web software – Dynamic web content, scripting (Java. Script, PHP) © M. Winter 1. 5

COSC 2 P 05 – Programming languages Language Evaluation Criteria • • • Readability

COSC 2 P 05 – Programming languages Language Evaluation Criteria • • • Readability – How easy is it to understand a given program? – Early languages focused on machine readability rather than programmer readability – Important for maintenance and extension – Has to be considered in the context of the problem domain Writability – How easy is it to write a program for a chosen problem? – Has to be considered in the context of the problem domain Reliability – A program is reliable if it performs to its specification under all conditions – Program correctness © M. Winter 1. 6

COSC 2 P 05 – Programming languages Language Evaluation Criteria © M. Winter 1.

COSC 2 P 05 – Programming languages Language Evaluation Criteria © M. Winter 1. 7

COSC 2 P 05 – Programming languages Readability • • Simplicity – A language

COSC 2 P 05 – Programming languages Readability • • Simplicity – A language with a large number of basic constructs is more difficult to learn – Feature multiplicity – Operator overloading – Simplicity vs. high-level language Orthogonality – Small number of primitive constructs can be combined in a small number of ways, each combination is legal and meaningful – Independent of context Data types – Adequate data types are available, e. g. , Booleans Syntax Design – Identifier forms, special words, form and meaning © M. Winter 1. 8

COSC 2 P 05 – Programming languages Writability • • • Simplicity – Misuse

COSC 2 P 05 – Programming languages Writability • • • Simplicity – Misuse of unknown features Orthogonality – Low orthogonality requires to memorize a lot of exceptions – Errors can go undetected if nearly all combinations of primitives are legal Data types and Syntax design – See Readability Support for abstraction – Ability to define and use structures in ways that allow many details to be ignored – Process abstraction: subprograms, polymorphism – Data abstraction: interfaces, references, recursive data types Expressivity – Powerful but convenient constructions © M. Winter 1. 9

COSC 2 P 05 – Programming languages Reliability • • Simplicity, Orthogonality, Data types,

COSC 2 P 05 – Programming languages Reliability • • Simplicity, Orthogonality, Data types, Syntax design, Support for abstraction and Expressivity – See Readability and/or Writability Type checking – Simple test for type errors – Compile-time: more desirable, detect before run – Errors at run-time: costly and errors might not be detected Exception handling – Intercept run-time errors Aliasing – Two or more distinct names accessing the same memory cell – Dangerous feature © M. Winter 1. 10

COSC 2 P 05 – Programming languages Influences on Language Design • Computer Architecture

COSC 2 P 05 – Programming languages Influences on Language Design • Computer Architecture • Program Design Methodologies – Top-down design and stepwise refinement – Procedure-oriented vs. data-oriented Language Categories – Imperative languages – Object-oriented languages – Visual languages – Logic programming languages – Functional programming languages • © M. Winter 1. 11

COSC 2 P 05 – Programming languages Implementation Methods • • • © M.

COSC 2 P 05 – Programming languages Implementation Methods • • • © M. Winter Compilation Interpretation Hybrid System 1. 12

COSC 2 P 05 – Programming languages Compilation © M. Winter 1. 13

COSC 2 P 05 – Programming languages Compilation © M. Winter 1. 13

COSC 2 P 05 – Programming languages Pure Interpretation © M. Winter 1. 14

COSC 2 P 05 – Programming languages Pure Interpretation © M. Winter 1. 14

COSC 2 P 05 – Programming languages Hybrid Systems © M. Winter 1. 15

COSC 2 P 05 – Programming languages Hybrid Systems © M. Winter 1. 15

COSC 2 P 05 – Programming languages Evolution of Programming Languages Zuse’s Plankalkül •

COSC 2 P 05 – Programming languages Evolution of Programming Languages Zuse’s Plankalkül • Developed in 1943 -45 as part of his Ph. D • First published in 1972 • Data types: bit, integer, floating-point type, arrays, records • For and while loops, no goto • Includes assertions, i. e. , mathematical expressions that would be true during execution at the point in the code • Two-dimensional syntax | A + 1 => A V | 4 5 S | 1. n © M. Winter 1. 16

COSC 2 P 05 – Programming languages © M. Winter 1. 17

COSC 2 P 05 – Programming languages © M. Winter 1. 17

COSC 2 P 05 – Programming languages FORTRAN • • FORmula TRANslating system First

COSC 2 P 05 – Programming languages FORTRAN • • FORmula TRANslating system First generally available heigh-level language Goals – Reduce development & debugging costs – Efficient compilation Features – Comments – No data-typing statements (implicit type convention) – Mathematical notation – Looping statement (DO) – Subroutines & functions – I/O formatting – Machine independence • but no standard © M. Winter 1. 18

COSC 2 P 05 – Programming languages • • Reasons for success of FORTRAN

COSC 2 P 05 – Programming languages • • Reasons for success of FORTRAN – Easy to learn compared to assembly language/machine code – Supported by IBM – Most users and applications at the time were scientific – Simplified tedious tasks, e. g. I/O FORTRAN IV, 77, 90, 95, 2003, 2008, 2018 – Type declarations for variables – If construct – Subprograms as parameters – Dynamic arrays, pointers – Modules – Support for object-orientation © M. Winter 1. 19

COSC 2 P 05 – Programming languages FORTRAN Program C FORTRAN PROGRAM TO FIND

COSC 2 P 05 – Programming languages FORTRAN Program C FORTRAN PROGRAM TO FIND MEAN OF N NUMBERS AND C NUMBER OF VALUES GREATER THAN THE MEAN DIMENSION A(99) REAL MEAN READ(1, 5) N 5 FORMAT(I 2) READ(1, 10)(A(I), I=1, N) 10 FORMAT(6 F 10. 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(8 H MEAN = , F 10. 5, 5 X, 20 H NUMBER OVER MEAN = , I 5) STOP END © M. Winter 1. 20

COSC 2 P 05 – Programming languages Fortran 95 Program ! Fortran 95 Example

COSC 2 P 05 – Programming languages Fortran 95 Program ! Fortran 95 Example program ! Input: An integer, List_Len, where List_Len is less ! than 100, followed by List_Len-Integer values ! Output: The number of input values that are greater ! than the average of all input values Implicit none Integer Dimension(99) : : Int_List Integer : : List_Len, Counter, Sum, Average, Result= 0 Sum = 0 Read *, List_Len If ((List_Len > 0). AND. (List_Len < 100)) Then ! Read input data into an array and compute its sum Do Counter = 1, List_Len Read *, Int_List(Counter) Sum = Sum + Int_List(Counter) End Do © M. Winter 1. 21

COSC 2 P 05 – Programming languages ! Compute the average Average = Sum

COSC 2 P 05 – Programming languages ! Compute the average Average = Sum / List_Len ! Count the values that are greater than the average Do Counter = 1, List_Len If (Int_List(Counter) > Average) Then Result = Result + 1 End If End Do ! Print the result Print *, 'Number of values > Average is: ', Result Else Print *, 'Error - list length value is not legal' End If End Program Example © M. Winter 1. 22

COSC 2 P 05 – Programming languages BASIC • • • BASIC (Beginner’s All

COSC 2 P 05 – Programming languages BASIC • • • BASIC (Beginner’s All Purpose Symbolic Instruction Code) Designed as a language for liberal arts students in 1963, based on FORTRAN Goals – Easy to learn for nonscience students – “Pleasant and friendly” – Fast turnaround for homework (Timesharing systems) – Allow free and private access – Consider user time more important than computer time Features – Very small language – No input during run-time possible (batch-oriented) Later versions: Visual BASIC © M. Winter 1. 23

COSC 2 P 05 – Programming languages A BASIC Program REM Basic Example Program

COSC 2 P 05 – Programming languages A BASIC Program REM Basic Example Program REM Input: An integer, listlen, where listlen is less REM than 100, followed by listlen-integer values REM Output: The number of input values that are greater REM than the average of all input values DIM intlist(99) result = 0 sum = 0 INPUT listlen IF listlen > 0 AND listlen < 100 THEN REM Read input into an array and compute the sum FOR counter = 1 TO listlen INPUT intlist(counter) sum = sum + intlist(counter) NEXT counter REM Compute the average = sum / listlen REM Count the number of input values that are > average © M. Winter 1. 24

COSC 2 P 05 – Programming languages FOR counter = 1 TO listlen IF

COSC 2 P 05 – Programming languages FOR counter = 1 TO listlen IF intlist(counter) > average THEN result = result + 1 NEXT counter REM Print the result PRINT "The number of values that are > average is: "; result ELSE PRINT "Error-input list length is not legal" END IF END © M. Winter 1. 25

COSC 2 P 05 – Programming languages Lisp • • • Developed for AI

COSC 2 P 05 – Programming languages Lisp • • • Developed for AI applications in 1958 Two kinds of data structures: atoms and lists Atoms have the form of an identifier or a numeric literal Functional programming language List example: (A B C D) © M. Winter 1. 26

COSC 2 P 05 – Programming languages Lisp Program ; Lisp Example function ;

COSC 2 P 05 – Programming languages Lisp Program ; Lisp Example function ; The following code defines a Lisp predicate function ; that takes two lists as arguments and returns True ; if the two lists are equal, and NIL (false) otherwise (DEFUN equal_lists (lis 1 lis 2) (COND ((ATOM lis 1) (EQ lis 1 lis 2)) ((ATOM lis 2) NIL) ((equal_lists (CAR lis 1) (CAR lis 2)) (equal_lists (CDR lis 1) (CDR lis 2))) (T NIL) ) ) © M. Winter 1. 27

COSC 2 P 05 – Programming languages Related Languages • • Scheme – Developed

COSC 2 P 05 – Programming languages Related Languages • • Scheme – Developed in the mid-1970 s – Static scope – Functions are fully treated as first-class entities COMMON LISP – Created to incorporate several Lisp dialects into one common language – Allows static and dynamic scope – Large number of data types ML – Functional programming language that also allows imperative programming Haskell – Lazy evaluation – Use of monads © M. Winter 1. 28

COSC 2 P 05 – Programming languages ALGOL 60 • • • ALGOrithmic Language

COSC 2 P 05 – Programming languages ALGOL 60 • • • ALGOrithmic Language Joint European-US Committee (GAMM and ACM) Goals – Standard mathematical notation – Use to describe computing processes – Machine translatable ALGOL 58 report – BNF Features – Formal language definition – Block structure – Two different means of passing parameters (pass by value/name) – Arrays with variable bounds – Structured control statements – Recursion © M. Winter 1. 29

COSC 2 P 05 – Programming languages • • • Very successful in Europe

COSC 2 P 05 – Programming languages • • • Very successful in Europe Became the only acceptable formal means of communicating algorithms Reasons not widely used in North America – 3 years after FORTRAN – More features, harder to learn – Compilation too complex for the time, less efficient – No standard I/O © M. Winter 1. 30

COSC 2 P 05 – Programming languages An ALGOL 60 Program comment ALGOL 60

COSC 2 P 05 – Programming languages An ALGOL 60 Program comment ALGOL 60 Example Program Input: An integer, listlen, where listlen is less than 100, followed by listlen-integer values Output: The number of input values that are greater than the average of all the input values ; begin integer array intlist [1: 99]; integer listlen, counter, sum, average, result; sum : = 0; result : = 0; readint (listlen); if (listlen > 0) ^ (listlen < 100) then begin comment Read input into an array and compute the average; © M. Winter 1. 31

COSC 2 P 05 – Programming languages for counter : = 1 step 1

COSC 2 P 05 – Programming languages for counter : = 1 step 1 until listlen do begin readint (intlist[counter]); sum : = sum + intlist[counter] end; comment Compute the average; average : = sum / listlen; comment Count the input values that are > average; for counter : = 1 step 1 until listlen do if intlist[counter] > average then result : = result + 1; comment Print result; printstring("The number of values > average is: "); printint (result) end else printstring ("Error-input list length is not legal"; end © M. Winter 1. 32

COSC 2 P 05 – Programming languages The Algol Family • • ALGOL W

COSC 2 P 05 – Programming languages The Algol Family • • ALGOL W – Tidied up ALGOL 60 – Introduced • Records and references (linked structures) • Case statement • Multiple looping structures • Parameter passing: call-by-value, call-by-result, call-byvalue/result, call-by-name • String support • Assert statement Algol 68 – IFIP working group – Highly orthogonal – Highly-formal specification © M. Winter 1. 33

COSC 2 P 05 – Programming languages Pascal • • • Built on ALGOL

COSC 2 P 05 – Programming languages Pascal • • • Built on ALGOL W Goals – Efficient implementation – For teaching good programming style Widely used in universities and became common in industry Features – Restrictions on goto’s – User-defined data types – Strings Lacking features – No modules – Array parameters cannot be of variable length © M. Winter 1. 34

COSC 2 P 05 – Programming languages A Pascal Program {Pascal Example Program Input:

COSC 2 P 05 – Programming languages A Pascal Program {Pascal Example Program Input: An integer, listlen, where listlen is less than 100, followed by listlen-integer values Output: The number of input values that are greater than the average of all input values } program pasex (input, output); type intlisttype = array [1. . 99] of integer; var intlist : intlisttype; listlen, counter, sum, average, result : integer; begin result : = 0; sum : = 0; readln (listlen); if ((listlen > 0) and (listlen < 100)) then begin { Read input into an array and compute the sum } © M. Winter 1. 35

COSC 2 P 05 – Programming languages for counter : = 1 to listlen

COSC 2 P 05 – Programming languages for counter : = 1 to listlen do begin readln (intlist[counter]); sum : = sum + intlist[counter] end; { Compute the average } average : = sum / listlen; { Count the number of input values that are > average } for counter : = 1 to listlen do if (intlist[counter] > average) then result : = result + 1; { Print the result } writeln ('The number of values > average is: ', result) end { of then clause of if (( listlen > 0. . . } else writeln ('Error-input list length is not legal’) end. © M. Winter 1. 36

COSC 2 P 05 – Programming languages COBOL • • COmmon Business Oriented Language

COSC 2 P 05 – Programming languages COBOL • • COmmon Business Oriented Language Probably the most used programming language Based on FLOW-MATIC by UNIVAC Business data processing Usage of English as a programming language Features – English-like syntax – Emphasis on file processing; records – 4 divisions: identification, environment, data, procedure Mandated by the Do. D Tight language control – Relatively unchanged in 25 yearrs © M. Winter 1. 37

COSC 2 P 05 – Programming languages A COBOL Program IDENTIFICATION DIVISION. PROGRAM-ID. PRODUCE-REORDER-LISTING.

COSC 2 P 05 – Programming languages A COBOL Program IDENTIFICATION DIVISION. PROGRAM-ID. PRODUCE-REORDER-LISTING. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. DEC-VAX. OBJECT-COMPUTER. DEC-VAX. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT BAL-FWD-FILE ASSIGN TO READER. SELECT REORDER-LISTING ASSIGN TO LOCAL-PRINTER. DATA DIVISION. FILE SECTION. FD BAL-FWD-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 80 CHARACTERS. © M. Winter 1. 38

COSC 2 P 05 – Programming languages 01 BAL-FWD-CARD. 02 BAL-ITEM-NO PICTURE IS 9(5).

COSC 2 P 05 – Programming languages 01 BAL-FWD-CARD. 02 BAL-ITEM-NO PICTURE IS 9(5). 02 BAL-ITEM-DESC PICTURE IS X(20). 02 FILLER PICTURE IS X(5). 02 BAL-UNIT-PRICE PICTURE IS 999 V 99. 02 BAL-REORDER-POINT PICTURE IS 9(5). 02 BAL-ON-HAND PICTURE IS 9(5). 02 BAL-ON-ORDER PICTURE IS 9(5). 02 FILLER PICTURE IS X(30). FD REORDER-LISTING LABEL RECORDS ARE STANDARD RECORD CONTAINS 132 CHARACTERS. 01 REORDER-LINE. 02 RL-ITEM-NO PICTURE IS Z(5). 02 FILLER PICTURE IS X(5). 02 RL-ITEM-DESC PICTURE IS X(20). 02 FILLER PICTURE IS X(5). 02 RL-UNIT-PRICE PICTURE IS ZZZ. 99. 02 FILLER PICTURE IS X(5). 02 RL-AVAILABLE-STOCK PICTURE IS Z(5). 02 FILLER PICTURE IS X(5). 02 RL-REORDER-POINT PICTURE IS Z(5). 02 FILLER PICTURE IS X(71). © M. Winter 1. 39

COSC 2 P 05 – Programming languages WORKING-STORAGE SECTION. 01 SWITCHES. 02 CARD-EOF-SWITCH PICTURE

COSC 2 P 05 – Programming languages WORKING-STORAGE SECTION. 01 SWITCHES. 02 CARD-EOF-SWITCH PICTURE IS X. 01 WORK-FIELDS. 02 AVAILABLE-STOCK PICTURE IS 9(5). PROCEDURE DIVISION. 000 -PRODUCE-REORDER-LISTING. OPEN INPUT BAL-FWD-FILE. OPEN OUTPUT REORDER-LISTING. MOVE "N" TO CARD-EOF-SWITCH. PERFORM 100 -PRODUCE-REORDER-LINE UNTIL CARD-EOF-SWITCH IS EQUAL TO "Y". CLOSE BAL-FWD-File. CLOSE REORDER-LISTING. STOP RUN. 100 -PRODUCE-REORDER-LINE. PERFORM 110 -READ-INVENTORY-RECORD. IF CARD-EOF-SWITCH IS NOT EQUAL TO "Y" PERFORM 120 -CALCULATE-AVAILABLE-STOCK IF AVAILABLE-STOCK IS LESS THAN BAL-REORDER-POINT PERFORM 130 -PRINT-REORDER-LINE. © M. Winter 1. 40

COSC 2 P 05 – Programming languages 110 -READ-INVENTORY-RECORD. READ BAL-FWD-FILE RECORD AT END

COSC 2 P 05 – Programming languages 110 -READ-INVENTORY-RECORD. READ BAL-FWD-FILE RECORD AT END MOVE "Y" TO CARD-EOF-SWITCH. 120 -CALCULATE-AVAILABLE-STOCK. ADD BAL-ON-HAND BAL-ON-ORDER GIVING AVAILABLE-STOCK. 130 -PRINT-REORDER-LINE. MOVE SPACE TO REORDER-LINE. MOVE BAL-ITEM-NO TO RL-ITEM-NO. MOVE BAL-ITEM-DESC TO RL-ITEM-DESC. MOVE BAL-UNIT-PRICE TO RL-UNIT-PRICE. MOVE AVAILABLE-STOCK TO RL-AVAILABLE-STOCK. MOVE BAL-REORDER-POINT TO RL-REORDER-POINT. WRITE REORDER-LINE. © M. Winter 1. 41