8/6/2019 Stein Berg
1/35
CS 314, LS,LTM: L1: Introduction 1
Principles ofPrinciples of
Programming LanguagesProgramming Languages
Topic: Introduction
Professor Louis Steinberg
8/6/2019 Stein Berg
2/35
8/6/2019 Stein Berg
3/35
CS 314, LS,LTM: L1: Introduction 3
BooksBooks
Kenneth Louden, Programming Languages
Kenneth Reek, Pointers on C
Kip Irvine, C++ and Object-Oriented
Programming
Clocksin & Mellish, Programming in Prolog
8/6/2019 Stein Berg
4/35
CS 314, LS,LTM: L1: Introduction 4
WorkWork
Midterm (Friday 3/5 2:50-4:10PM )
Final
3 projects
Homework
In-lab programming test (?)
8/6/2019 Stein Berg
5/35
CS 314, LS,LTM: L1: Introduction 5
TopicsTopics
Introduction
Formal Languages - REs, BNF, context-free grammars,parsing
Functional Programming (Scheme)
Names, Bindings, Memory management
Imperative Programming (C)
Parameter Passing
ADTs, Object-oriented Design (C++)
Logic Programming (Prolog) Types
Parallel Programming? Threads and monitors?
8/6/2019 Stein Berg
6/35
CS 314, LS,LTM: L1: Introduction 6
Course GoalsCourse Goals
To gain an understanding of the basic structure of
programming languages:
Data types, control structures, naming conventions,...
To learn the principles underlying all programminglanguages:
So that it is easier to learn new languages
To study different language paradigms:
Functional (Scheme), Imperative (C), Object-Oriented(C++, Java), Logic (Prolog)
So that you can select an appropriate language for a task
8/6/2019 Stein Berg
7/35
CS 314, LS,LTM: L1: Introduction 7
What is a programmingWhat is a programming
language?language?a language intended for use by a person to express a process
by which a computer can solve a problem -Hope and
Jipping
a set of conventions for communicating an algorithm - E.
Horowitz
the art of programming is the art of organizing complexity -Dijkstra, 1972
8/6/2019 Stein Berg
8/35
CS 314, LS,LTM: L1: Introduction 8
AnthropomorphismAnthropomorphism
Whenever a human term (e.g., language) is used
about computers, ask:
How analogous
How differs
8/6/2019 Stein Berg
9/35
CS 314, LS,LTM: L1: Introduction 10
Desiderata for PL DesignDesiderata for PL Design
Readable
comments, names, () syntax
Simple to learn
Orthogonal - small number of concepts combineregularly and systematically (without exceptions)
Portable
language standardization
Abstraction
control and data structures that hide detail
Efficient
8/6/2019 Stein Berg
10/35
CS 314, LS,LTM: L1: Introduction 11
Why learn more than one PL?Why learn more than one PL?
So you can choose the right language for a given
problem
If all you have is a hammer, every problem looks like a
nail
8/6/2019 Stein Berg
11/35
CS 314, LS,LTM: L1: Introduction 12
Why learn more than one PL?Why learn more than one PL?
So you can learn a new language more easily later
As your job changes, you may need to used differentlanguages
As our understanding of programming improves, new
languages are created
To learn new ways of thinking about problems
Different languages encourage you to think aboutproblems in different ways
Paradigms
8/6/2019 Stein Berg
12/35
CS 314, LS,LTM: L1: Introduction 13
What is a ParadigmWhat is a Paradigm
A way of looking at a problem and seeing a
program
What kind of parts do you look for?
Problem: Print a large X of size n. E.g., size 5 is
X X
X X
X
X X
X X
8/6/2019 Stein Berg
13/35
CS 314, LS,LTM: L1: Introduction 17
Imperative ParadigmImperative Paradigm
A program is: A sequence of state-changing actions
Manipulate an abstract machine with:
Variables that name memory locations
Arithmetic and logical operations Reference, evaluate, assign operations
Explicit control flow statements
Fits the Von Neumann architecture closely
Key operations: Assignmentand GoTo
8/6/2019 Stein Berg
14/35
CS 314, LS,LTM: L1: Introduction 18
Imperative ParadigmImperative Paradigm
Fortran
C
SUM = 0 DO 11 K=1,N
SUM = SUM + 2*K11 CONTINUE
sum = 0;for (k = 1; k
8/6/2019 Stein Berg
15/35
CS 314, LS,LTM: L1: Introduction 19
Functional ParadigmFunctional Paradigm
A program is: Composition of operations on data
Characteristics (in pure form):
Name values, not memory locations
Value binding through parameter passing Recursion rather than iteration
Key operations:Function Application andFunction
Abstraction
Based on the Lambda Calculus
8/6/2019 Stein Berg
16/35
CS 314, LS,LTM: L1: Introduction 20
Functional ParadigmFunctional Paradigm
(define (sum n)(if (= n 0)
0
(+ (* n 2) (sum (- n 1)))
)
)
(sum 4) evaluates to 20
Scheme
8/6/2019 Stein Berg
17/35
CS 314, LS,LTM: L1: Introduction 21
Logic ParadigmLogic Paradigm
A program is: Formal logical specification of
problem
Characteristics (in pure form):
Programs say whatproperties the solution must have, nothow to find it
Solutions are obtained through a specialized form of
theorem-proving
Key operations: Unification andNonDeterministicSearch
Based on First Order Predicate Logic
8/6/2019 Stein Berg
18/35
CS 314, LS,LTM: L1: Introduction 22
Logic ParadigmLogic Paradigm
sum(0,0).sum(N,S) :- N>0,
NN is N - 1,sum(NN, SS),
S is N * 2 + SS.?- sum(1,2).
yes?- sum (2,4).no?-sum(20,S).S = 420
?-sum (X,Y).X = 0 = Y
Prolog
8/6/2019 Stein Berg
19/35
CS 314, LS,LTM: L1: Introduction 23
Object-Oriented ParadigmObject-Oriented Paradigm
A program is: Communication between abstract
objects
Characteristics:
Objects collect both the data and the operations Objects provide data abstraction
Can be either imperative or functional (or logical)
Key operation:Message Passing orMethod
Invocation
8/6/2019 Stein Berg
20/35
CS 314, LS,LTM: L1: Introduction 24
Object-oriented ParadigmObject-oriented Paradigmclass intSet : public Set{ public: intSet() { }//inherits Set add_element(), Set del_element()//from Set class, defined as a set of Objects
public int sum( ){int s = 0;SetEnumeration e = new SetEnumeration(this);while (e.hasMoreElements()) do{s = s + ((Integer)e.nextElement()).intValue();}return s;}
}
Java
8/6/2019 Stein Berg
21/35
CS 314, LS,LTM: L1: Introduction 25
TranslationTranslation
Compilation: Program is translated from a high-
level language into a form that is executable on an
actual machine
Interpretation: Program is translated and executedone statement at a time by a virtual machine
Some PL systems are a mixture of these two
E.g., Java
Translator
Virtual Machine
Source Intermediate code
Input
Intermediate code Output
foo.java foo.classbytecodejavac
java (JVM)
8/6/2019 Stein Berg
22/35
CS 314, LS,LTM: L1: Introduction 26
CompilationCompilation
scanner
parser
intermediate
code
generator
optimizer
code generator
assembler
linker
8/6/2019 Stein Berg
23/35
CS 314, LS,LTM: L1: Introduction 27
CompilationCompilation
scanner
parser
intermediate
code
generator
position = initial + rate * 60;
id1 := id2 + id3 * 60
optimizer
code generator
assembler
linker
8/6/2019 Stein Berg
24/35
CS 314, LS,LTM: L1: Introduction 28
CompilationCompilation
scanner
parser
intermediate
code
generator
symbol table
(position, ...)(initial, )
(rate, )
:= parse tree
id1 +
id2 *
id3 int-to-real
60tmp1 = inttoreal (60)tmp2 = id3 * tmp1
tmp3 = id2 + tmp2
id1 = tmp3
8/6/2019 Stein Berg
25/35
CS 314, LS,LTM: L1: Introduction 29
CompilationCompilation
optimizer
code generator
assembler
linker
tmp2 = id3 * 60.0
id1 = id2 + tmp2
movf id3, R2mulf #60.0, R2
movf id2, R1
addf R2, R1
movf R1, id1move R1, R-base, R-offset
movf R1, 45733
8/6/2019 Stein Berg
26/35
CS 314, LS,LTM: L1: Introduction 30
History ofHistory ofPLsPLs
Prehistory
300 B.C. Greece, Euclid invented the greatest common
divisor algorithm - oldest known algorithm
~1820-1850 England, Charles Babbage invented twomechanical computational devices
difference engine
analytical engine
Countess Ada Augusta of Lovelace, first computer programmer
Precursors to modern machines 1940s United States, ENIAC developed to calculate trajectories
8/6/2019 Stein Berg
27/35
CS 314, LS,LTM: L1: Introduction 31
History ofHistory ofPLsPLs
1950s United States, first high-level PLs invented
Fortran 1954-57, John Backus (IBM on 704) designed for
numerical scientific computation
fixed format for punched cards
implicit typing
only counting loops, if test versus zero
only numerical data
1957 optimizing Fortran compiler translates into code as efficient
as hand-coded
8/6/2019 Stein Berg
28/35
CS 314, LS,LTM: L1: Introduction 32
History ofHistory ofPLsPLs
Algol60 1958-60, designed by international committee for
numerical scientific computation [Fortran]
block structure with lexical scope
free format, reserved words
while loops, recursion
explicit types
BNF developed for formal syntax definition
Cobol1959-60, designed by committee in US,
manufacturers and DoD for business data processing
records
focus on file handling
English-like syntax
8/6/2019 Stein Berg
29/35
CS 314, LS,LTM: L1: Introduction 33
History ofHistory ofPLsPLs
APL 1956-60 Ken Iverson, (IBM on 360, Harvard)
designed for array processing
functional programming style
LISP 1956-62, John McCarthy (MIT on IBM704,
Stanford) designed for non-numerical computation uniform notation for program and data
recursion as main control structure
garbage collection
Snobol1962-66, Farber, Griswold, Polansky (Bell Labs)
designed for string processing
powerful pattern matching
8/6/2019 Stein Berg
30/35
CS 314, LS,LTM: L1: Introduction 34
History ofHistory ofPLsPLs
PL/I1963-66, IBM designed for general purpose
computing [Fortran, Algol60, Cobol]
user controlled exceptions
multi-tasking
Simula671967, Dahl and Nygaard (Norway) designed asa simulation language [Algol60]
data abstraction
inheritance of properties
Algol68 1963-68, designed for general purpose computing
[Algol60]
orthogonal language design
interesting user defined types
8/6/2019 Stein Berg
31/35
CS 314, LS,LTM: L1: Introduction 35
History ofHistory ofPLsPLs
Pascal1969, N. Wirth(ETH) designed for teachingprogramming [Algol60]
1 pass compiler
call-by-value semantics
Prolog 1972, Colmerauer and Kowalski designed forArtificial Intelligence applications
theorem proving with unification as basic operation
logic programming
Recent
C 1974, D. Ritchie (Bell Labs) designed for systemsprogramming
allows access to machine level within high-level PL
efficient code generated
8/6/2019 Stein Berg
32/35
CS 314, LS,LTM: L1: Introduction 36
History ofHistory ofPLsPLs
Clu 1974-77, B. Liskov (MIT) designed for simulation
[Simula]
supports data abstraction and exceptions
precise algebraic language semantics
attempt to enable verification of programs
Smalltalkmid-1970s, Alan Kay (Xerox Parc), considered
first real object-oriented PL, [Simula]
encapsulation, inheritance
easy to prototype applications
hides details of underlying machine
Scheme mid-1970s, Guy Steele, Gerald Sussman (MIT)
Static scoping and first-class functions
8/6/2019 Stein Berg
33/35
CS 314, LS,LTM: L1: Introduction 37
History ofHistory ofPLsPLs
Concurrent Pascal1976 Per Brinch Hansen (U Syracuse)
designed for asynchronous concurrent processing
[Pascal]
monitors for safe data sharing
Modula 1977 N. Wirth (ETH), designed language forlarge software development [Pascal]
to control interfaces between sets of procedures or modules
real-time programming
Ada 1979, US DoD committee designed as general
purpose PL
explicit parallelism - rendezvous
exception handling and generics (packages)
8/6/2019 Stein Berg
34/35
CS 314, LS,LTM: L1: Introduction 38
History ofHistory ofPLsPLs
C++ 1985, Bjorne Stroustrup (Bell Labs) general purpose
goal of type-safe object-oriented PL
compile-time type checking
templates
Java~1995, J. Gosling (SUN) aimed at portability across platform through use of JVM -
abstract machine to implement the PL
aimed tofix some problems with previous OOPLs
multiple inheritance
static and dynamic objects
ubiquitous exceptions
thread objects
8/6/2019 Stein Berg
35/35
CS 314, LS,LTM: L1: Introduction 39
http://www.http://www.oreillyoreilly.com/pub/a/.com/pub/a/oreillyoreilly/news//news/languageposterlanguageposter_0504.html_0504.html