Programming Languages CSCI-4430 & CSCI-6430, Spring 2016
www.cs.rpi.edu/~milanova/csci4430/
Ana Milanova Lally Hall 314, 518 276-6887
[email protected] Office hours: Wednesdays Noon-2pm
2
Lecture Outline
n
Introduction
n
Art of programming language design
n
Programming language spectrum
n
Why study programming languages?
n
Overview of compilation Read: Scott Chapter 1
Spring 16 CSCI 4430, A Milanova 3
Introduction
n Course webpage
http://www.cs.rpi.edu/~milanova/csci4430
n Announcements – check regularly
n Schedule, Notes, Reading
n Schedule, lecture slides and assigned reading
n Homework
n Homework assignments posted there
n Homework server
n Homework submission and grades
n RPILMS
n Discussion board and grades
Spring 16 CSCI 4430, A Milanova 4
Introduction
n
Required textbook
n Programming Language Pragmatics, 3rd Edition, by Michael Scott, Morgan Kaufmann, 2009
n
Recommended textbook
n Compilers: Principles, Techniques, and Tools, 2nd Edition, by A. Aho, M. Lam, R. Sethi and J.
Ullman, Addison Wesley, 2007 (as known as
“The Dragon Book”)
Spring 16 CSCI 4430, A Milanova 5
Introduction
n
Syllabus
www.cs.rpi.edu/~milanova/csci4430/syllabus.htm Topics, outcomes, policies and grading
n
2 midterm exams and a final exam: 50%
n
9 homework assignments: 42%
n
10 quizzes: 8%
n
2% extra credit for attendance and participation
Introduction
n Homework is due at the beginning of lecture on the due date
n Submit written homework electronically in Homework server AND on paper at the beginning of lecture
n Submit programming homework in Homework server
n
No late days!
n You may submit a late homework at the beginning of next lecture for 50% credit
Spring 16 CSCI 4430, A Milanova 6
Introduction
n
Quizzes
n 10 in-class quizzes
n Will cover material of previous week
n Work in groups is encouraged
n We will distribute quizzes at the beginning of lecture
n Keep and submit quiz at the end of class
Spring 16 CSCI 4430, A Milanova 7
Introduction
n
In-class exercises
n At the end of class, I may give a simple problem on the material we have just covered
n Work in groups and ask questions
n You can discuss or submit answers to receive feedback
n Participation in these exercises carries towards 2% extra credit
Spring 16 CSCI 4430, A Milanova 8
Spring 16 CSCI 4430, A Milanova 9
Introduction
n
Graduate students taking 6430 as part of the PhD qualifying exam
n To earn an A, you must earn 94% or more AND
n Complete (satisfactorily!) a paper critique n
Check syllabus
www.cs.rpi.edu/~milanova/csci4430/syllabus.htm for details
Random Notes…
n
Prolog and Scheme programming assignments must be be completed individually
n
Java programming assignment must be completed in teams of two using pair- programming
n
Ask questions: in class, after class, email, LMS
Spring 16 CSCI 4430, A Milanova 10
Academic Integrity
n
All written homework assignments must be completed individually
n
Programming assignments, except for last one, must be completed individually
n
Excessive similarities in homework and exams! will be considered cheating
n
Incidents of cheating will be taken extremely seriously and will result in penalties
Spring 16 CSCI 4430, A Milanova 11
How to Study
n
Read textbook chapter in advance of lecture
n I will assign reading at the end of each class
n
Read lecture notes and textbook chapter (again) immediately after class
n I will post lecture notes short after class
n
Solve exercises in lecture notes
n
Form study groups
n
ASK QUESTIONS – in class, after class, etc.
Spring 16 CSCI 4430, A Milanova 12
Spring 16 CSCI 4430, A Milanova 13
Course Topics
n Programming language syntax: Scanning and parsing
n Programming language semantics: Attribute grammars
n Naming, binding and scoping
n Data abstraction and types
n Control abstraction and parameter passing
n Concurrency
n A logic-oriented language: Prolog
n A functional language: Scheme
n Imperative languages
n An object-oriented language: Java
n A dynamic language: Python
Spring 16 CSCI 4430, A Milanova 14
Lecture Outline
n
Introduction to the course
n
The art of programming language design
n
The programming language spectrum
n
Why study programming languages
n
Overview of compilation
15
The Art of Language Design
n
Why are there so many languages?
n Different programming domains
n Business applications
n Process data, reports, transactions, increasingly distributed
n Manufacturing/control systems
n Reliability and non-stop operation
n Scientific applications
n Computationally intensive n Personal preference…
Spring 16 CSCI 4430, A Milanova 16
The Art of Language Design
n
Some languages are more successful than others
n Expressive Power
n Abstraction
n Ease of use by novice
n Excellent compilers
n Powerful sponsors
n Circumstances…
Spring 16 CSCI 4430, A Milanova
17
The Programming Language Spectrum
n
Imperative languages
n Von Neumann languages: Fortran, C,…
n Object-oriented languages: Java, C++, Smalltalk,
…
n Dynamic languages: Perl, Python, PHP,…
n
Declarative languages
n Functional languages: Scheme/Lisp, ML, Haskell
n Logic languages: Prolog
n There are other declarative languages: e.g.,
dataflow languages Spring 16 CSCI 4430, A Milanova. Graph: Sebesta, 2005 18
The Programming Language Spectrum
n
Imperative languages
n Evolved from the von Neumann Architecture
n Variables
n Assignment Statements
Spring 16 CSCI 4430, A Milanova 19
The Programming Language Spectrum
n
Imperative languages
n
j := i – j
lw t0,28(sp) lw t1,24(sp) subu t2,t0,t1 sw t2,24(sp)
“von Neumann bottleneck”:
tube connecting CPU and memory
20
The Programming Language Spectrum
n
Imperative languages
n Most widely popular programming style
n FORTRAN, C, C++, C#, Java, Python, Visual BASIC, Perl, JavaScript, Ruby, etc.
n Variable and assignment statement are central concepts
n Program is a sequence of statements:
j := i – j;
k := j * l;
n Execution is a sequence of transitions on memory state
Spring 16 CSCI 4430, A Milanova
21
The Programming Language Spectrum
n
FORTRAN was invented in mid-1950
n
John Backus, the inventor of FORTRAN, wrote the following paper in 1979:
“Can programming be liberated from the von Neumann style? A functional style and its algebra of programs”
n Problems with imperative languages
n Difficult to understand programs
n Difficult to reason about correctness of programs
n Von Neumann Bottleneck
Spring 16 CSCI 4430, A Milanova Spring 16 CSCI 4430, A Milanova 22
The Programming Language Spectrum
n Functional Programming
n Main alternative to imperative programming
n Lisp/Scheme, ML, Haskell
n Program consists of function definitions + evaluation expr (fun3 (fun2 (fun1 data)))
(fun3 (fun2 data2)) (fun3 data3) data4
n Execution is a sequence of function applications (i.e., reductions) n Logic Programming
n Perform queries against knowledge base
n Prolog, Datalog, SQL
Fall 16 CSCI 4430, A Milanova 23
An Example: Inner Product
n
Inner product in FORTRAN:
n
Illustrates state-transition semantics
1. C := 0;2. for I := 1 step 1 until N do 3. C := C + a[I]* b[I];
24
An Example: Inner Product
n
Inner product in FP:
Def IP = (Insert +) º (ApplyToAll *) º Transpose
IP <<1,2,3>,<6,5,4>> is(Insert +) ((ApplyToAll *) (Transpose <<1,2,3>,<6,5,4>>)) (Insert +) ((ApplyToAll *) <<1,6>,<2,5>,<3,4>>)
(Insert +) <6,10,12>
28
n
Illustrates reduction (applicative) semantics
Function composition
25
Why Study Programming Languages
n
Goal of the course: learn to analyze programming languages
n What are the questions we ask when facing a new programming language
n Helps learn new languages, choose the right language for a problem, understand language features, design better languages!
Spring 16 CSCI 4430, A Milanova Spring 16 CSCI 4430, A Milanova 26
Lecture Outline
n
Introduction to the course
n
The art of language design
n
The programming language spectrum
n
Why study programming languages
n
Overview of compilation
Spring 16 CSCI 4430, A Milanova 27
Compilation and Interpretation
n Compilation
n A “high-level” program is translated into executable machine code
n Compiler
n Pure interpretation
n A program is translated and executed one statement at a time
n Interpreter
n Hybrid interpretation
n A program is “compiled” into intermediate code;
intermediate code is “interpreted”
n Both a compiler and an interpreter
Spring 16 CSCI 4430, A Milanova 28
Compilation
Scanner (lexical analysis) Parser (syntax analysis)
Machine-independent code improvement
Code Generation COMPILER character stream
token stream
parse tree modified
intermediate form
target language (assember) Semantic analysis and
intermediate code generation
abstract syntax tree
and/or intermediate form Machine-dependent code improvement
modified target language
Spring 16 CSCI 4430, A Milanova 29
Compilation
Scanner
Parser
position = initial + rate * 60;
<id,1> <=> <id,2> <+> <id,3> <*> <60>
Semantic analysis and intermediate code generation
1. position, … 2. initial, … 3. rate, … Symbol table
30
Compilation
= <id,1> +
<id,2> *
<id,3> inttoreal 60 tmp1 = inttoreal (60) tmp2 = id3 * tmp1 tmp3 = id2 + tmp2 id1 = tmp3 Semantic analysis and intermediate code generation
Abstract Syntax Tree
Intermediate form (three-address code)
Spring 16 CSCI 4430, A Milanova
31
Compilation
tmp1 = id3 * 60.0 id1 = id2 + tmp1
movf id3, R2 mulf #60.0, R2 movf id2, R1 addf R2, R1 movf R1, id1 Machine-independent code improvement
Code generation
Improved intermediate form
Target language
32
Pure Interpretation
e.g. BASIC REM COMMENT LET X = 5 LET Y = 8 PRINT X PRINT Y LET Z = X PRINT Z ...
Also JavaScript....
Spring 16 CSCI 4430, A Milanova
Spring 16 CSCI 4430, A Milanova 33
Hybrid Interpretation
e.g. Java byte code 09 AB 19 29 99 73 09 AB 19 29 99 73 09 AB 19 29 99 73 09 AB 19 29 99 73 09 AB 19 29 99 73 09
e.g. Java Virtual Machine (JVM) Also Perl....
Compilation vs. Interpretation
n
A language can be implemented using a compiler or using an interpreter
n One can build a compiler for Lisp and one can easily build an interpreter for C or Fortran
n
However, language features (determined during language design) have tremendous impact on “compilability” and the decision
“ compiler vs. interpreter”
Spring 16 CSCI 4430, A Milanova 34
Compilation vs. Interpretation
n
What are the features that impact
“compilability”?
n
“Static” languages, i.e., languages that perform most semantic checks
program execution, are suitable for compilation
n
“Dynamic” languages, i.e., languages that perform most semantic checks
execution, are suitable for interpretation
Spring 16 CSCI 4430, A Milanova 35
Compilation vs. Interpretation
n
Advantages of compilation?
n Faster execution
n
Advantages of interpretation?
n Greater flexibility
n Sandboxing, source-level debugging, dynamic semantic (type) checks, other dynamic features are much easier
Spring 16 CSCI 4430, A Milanova 36
Compilation vs. Interpretation
n
New languages are increasingly dynamic
n Hybrid implementations are common: compiler from source to intermediate form, then a smart interpreter (Virtual Machine) which does interpretation and JIT (Just-in-Time) compilation
n
Tons of code written in static languages (C, C++, Fortran)
n Dynamic instrumentation tools (Valgrind, Pin, DynamoRio) serve as “interpreters” for compiled binaries
Spring 16 CSCI 4430, A Milanova 37
Next Class
n
We will review regular expressions and context free grammars
n
Read Chapter 2.1 and 2.2 from Scott’s book
Spring 16 CSCI 4430, A Milanova 38
Spring 16 CSCI 4430, A Milanova 39