Comp 333: Concepts of Programming Languages Fall 2016 Instructor: Professor Schwartz History Syntax and Semantics Compilers Language Constructs Names, Binding, Scoping, Data Types Expressions, Control Structures, Subprograms Programming Language Types Imperative Functional Logic Concurrent Object Oriented Scripting Languages Chapter 1 FALL 2016 2 1
Imperative Languages C, C++, Fortran, Java, Pascal Functional Languages Lisp, Scheme, ML, Haskell Logic Programming Languages Prolog Object-Oriented Languages Java, C++, Smalltalk, OCAML Scripting Languages Javascript, Perl, Python Tiobe Programming Community Index http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html Chapter 1 FALL 2016 3 Aug 2016 Aug 2015 Change Program ming Language Ratings Change 1 1 Java 19.010% -0.26% 2 2 C 11.303% -3.43% 3 3 C++ 5.800% -1.94% 4 4 C# 4.907% +0.07% 5 5 Python 4.404% +0.34% Exercise: Check out the Tiobe Community Index. Where do the ratings come from? Chapter 1 FALL 2016 4 2
Chapter 1 FALL 2016 5 TIOBE Programming Community Index Definition Basically the calculation comes down to counting hits for the search query <language> programming on various search engines. See www.tiobe.com for further details of choice of search engines and formula for calculation the ratings. Chapter 1 FALL 2016 6 3
Different program domains Scientific applications Business applications Artificial Intelligence Systems programming Web programming Embedded Systems DOD Education Chapter 1 FALL 2016 7 New languages usually evolve from older languages Features are added or modified to make new languages easier to read and write compile and run faster better able to solve a class of problems more modular ( and hence more reusable) more reliable and more maintainable Chapter 1 FALL 2016 8 4
Handout: Genealogy of Selected Programming Languages Scott Text Appendix A (page 860) Chapter 1 FALL 2016 9 Chapter 1 FALL 2016 10 5
Variables: x,y,z Arithmetic Expressions: z = x + 4 * y Data types: int, double, string Control structures: loops, conditionals Block structure: local scope rules Functions and procedures Data structures: arrays, records, pointers Recursion Support for concurrency: threads Object -Oriented Language Features classes, objects, inheritance, polymorphism Chapter 1 SP16 11 Symbolic locations and opcodes Computation of N = I + J ( Pentium 4) FORMULA: MOV EAX,I ADD EAX, J MOV N, EAX I DD 3 ;reserve 4 bytes J DD 4 ;reserve 4 bytes N DD 0 ;reserve 4 bytes Chapter 1 FALL 2016 12 6
Fortran Mid 1950s Developed by John Backus and his group at IBM Used to perform math computations ( formulas) One of the first high level languages Continued development Fortran IV, Fortran77,.. Features Variables, expressions, statements Arrays Iteration and conditional branching Subroutines ( independently compiled) FORMAT for input and output Chapter 1 FALL 2016 13 Fortran IV Example DIMENSION X(10) C C Skipping code that inputs numbers into array X READ(5,*) N is the FORTRAN READ statement R= 0; DO 60 J = 1,10 IF( X(J).LT. 0) GOTO 200 R = R + ( X(J) * X(J) / J ) 60 CONTINUE WRITE(5, 80) R 80 FORMAT( 1X, R =, F20.10) STOP Exercise: Translate this FORTRAN program to a complete Java program 200 WRITE(5, 210) 210 FORMAT(1X, NEGATIVE NUMBER DETECTED ) Chapter 1 FALL 2016 14 7
Class Exercise Translate the following pseudocode into FORTRAN code using the example FORTRAN program. You may also use the goto without an if. sum = 0.0 while ( sum < 10) { write Enter a number read X sum = sum + x } write sum is sum Chapter 1 FALL 2016 15 C ( 1972) Designed by Dennis Ritchie at Bell Labs Ancestor of Java, C++ Features Language for systems programming C compiler was part of the UNIX operating system Used in many application areas Official (ANSI) description of C ( 1989) Chapter 1 FALL 2016 16 8
#include <stdio.h> //Needed for C IO void copystring ( char s[], char t[]) { int k = 0; while( ( s[k] = t[k])!= '\0') { k++; } } int main() { char a[] = banana"; char b[] = orange"; copystring ( b,a); } printf("%s\n", a); printf("%s\n",b); Very insecure code. What happens if b[] is set to kiwi? May depend on compiler. Chapter 1 FALL 2016 17 Lisp ( 1959-1960) Developed by John McCarthy at IBM Ancestor of Scheme and Haskell Features Symbolic processing language ( e.g. list processing) Built on lists, atoms, selectors and constructors Dynamically allocated lists Garbage Collection Recursion Functions are first class objects Chapter 1 FALL 2016 18 9
(define factorial ( lambda (n) (if (= n 0) 1 ( * n (factorial ( - n 1))) ) ) ) This function is written in Scheme Chapter 1 FALL 2016 19 Compare Java with Scheme programs Java: int factorial ( int n) { if ( n == 0) return 1; else return n * f( n-1); } Scheme: (define factorial ( lambda (n) (if (= n 0) 1 ( * n (factorial ( - n 1))) ) ) ) Differences? Similarities? Chapter 1 FALL 2016 20 10
Logic programming language Makes explicit use of logic Very useful for problems that require searching for solutions to logic problems Used for automatic theorem proving Early Prolog interpreter and compiler developed in 1977 in Edinburgh Chapter 1 FALL 2016 21 Jerry is a mouse Mickey is a mouse Tom is a cat Garfield is a cat All mice eat cheese Deduce: Jerry eats cheese Prolog World Chapter 1 FALL 2016 22 11
mouse(jerry). facts mouse(mickey). cat(tom). cat(garfield). eatcheese(x) :- mouse(x). rule?- eatcheese(jerry). query true.?- eatcheese(tom). false. Chapter 1 FALL 2016 23 Designed by Niklaus Wirth in 1960s. Simplified version of Algol 68 Widely used as a teaching language in the 1970s and 1980s. Chapter 1 FALL 2016 24 12
program pascalex( input,output) ; type intlisttype = array [ 1..99] of integer; var intlist: intlisttype; listlen, k, sum, average, result : integer; begin result := 0; sum := 0; readln( listlen); if ( listlen > 0) and ( listlen < 100) then begin for k:= 1 to listlen do begin readln( intlist[k]); sum := sum + intlist[k] end; Class Exercise end. end else average := sum /listlen; for k := 1 to listlen do if ( intlist[k] > average) then result := result + 1; {Print result} writeln( The number of values > average is, result) writeln( Error input list length is not legal ) How does this Pascal program differ from a similar Java program? Chapter 1 FALL 2016 25 13