Programming Languages CSCI4430 CSCI6969 Spring 2014 www cs


































- Slides: 34
Programming Languages CSCI-4430 & CSCI-6969, Spring 2014 www. cs. rpi. edu/~milanova/csci 4430/ Ana Milanova Lally Hall 314, 518 276 -6887 milanova@cs. rpi. edu Office hours: Wednesdays Noon-2 pm
Lecture Outline n Introduction to the course n The art of language design The programming language spectrum Why study programming languages n Overview of compilation n n Read: Scott Chapter 1 2
Introduction n Course webpage http: //www. cs. rpi. edu/~milanova/csci 4430 n Announcements – check regularly n Schedule, Notes, Reading n n Homework assignments posted there Projects n n Schedule, lecture slides and assigned reading Programming assignments posted there RPILMS n Copy of course webpage, discussion board, your grades Spring 14 CSCI 4430, A Milanova 3
Introduction n Required textbook n n Programming Language Pragmatics, 3 rd Edition, by Michael Scott, Morgan Kaufmann, 2009 Textbook on reserve n Compilers: Principles, Techniques, and Tools, 2 nd Edition, by A. Aho, M. Lam, R. Sethi and J. Ullman, Addison Wesley, 2007 (as known as “The Dragon Book”) Spring 14 CSCI 4430, A Milanova 4
Introduction n Syllabus www. cs. rpi. edu/~milanova/csci 4430/syllabus. htm Topics, outcomes, policies and grading n n 2 midterm exams and a final exam: 56% 5 homework assignments: 20% 3 programming assignments: 24% 5% extra credit for attendance and participation Spring 14 CSCI 4430, A Milanova 5
Introduction n In-class group exercises n n At the end of class, I will give a simple problem on the material we have just covered Work in groups of 2 -3 and ask questions You can discuss or submit your answers to me and receive feedback Participation in in-class exercises along with regular attendance and participation in class, carry towards the 5% extra credit Spring 14 CSCI 4430, A Milanova 6
Introduction n Graduate students taking 6969 as part of the Ph. D qualifying exam To pass qualifying exam, you must earn 90% or more AND n Satisfactorily complete a paper critique n n Check syllabus www. cs. rpi. edu/~milanova/csci 4430/syllabus. htm for details Spring 14 CSCI 4430, A Milanova 7
Random Notes… n n n 1 st (Prolog) and 2 nd (Scheme) programming assignments must be be completed individually 3 rd programming assignment (Python and Java) must be completed in teams of two using pair-programming Ask questions: in class, after class, email, LMS Spring 14 CSCI 4430, A Milanova 8
Academic Integrity n n Homework must be completed individually Projects, except for the last one, must be completed individually Similarities in homework, projects and exams! are easy to spot Incidents of cheating will be taken extremely seriously and will result in penalties Spring 14 CSCI 4430, A Milanova 9
How to Study n Read textbook chapter in advance of lecture n n I will assign reading at the end of each class Read lecture notes and book chapter (again) soon after class n I will post lecture notes about an hour after class n Form study groups n ASK QUESTIONS – in class, after class, etc. Spring 14 CSCI 4430, A Milanova 10
Course Topics n n n n n Programming language syntax: Scanning and parsing Naming, binding and scoping Programming language semantics: Attribute grammars Data abstraction and types Control abstraction and parameter passing Concurrency A logic-oriented language: Prolog A functional language: Scheme Imperative languages n n An object-oriented language: Java A scripting language: Python Spring 14 CSCI 4430, A Milanova 11
Lecture Outline n Introduction to the course n The art of language design The programming language spectrum Why study programming languages n Overview of compilation n n Spring 14 CSCI 4430, A Milanova 12
The Art of Language Design n Why are there so many languages? n Different programming domains n Business applications n n Manufacturing/control systems n n Reliability and non-stop operation Scientific applications n n Process data, reports, transactions, increasingly distributed Computationally intensive Personal preference… Spring 14 CSCI 4430, A Milanova 13
The Art of Language Design n Some languages are more successful than others n Expressive Power n Abstraction n Ease of use by novice, libraries Excellent compilers Powerful sponsors n Circumstances… n n Spring 14 CSCI 4430, A Milanova 14
The Programming Language Spectrum n Imperative languages n n Von Neumann languages: Fortran, C, … Object-oriented languages: Java, C++, Smalltalk, … Dynamic languages: Perl, Python, PHP, … Declarative languages n n n Functional languages: Scheme/Lisp, ML, Haskell Logic languages: Prolog There are other declarative languages: e. g. , dataflow languages 15
The Programming Language Spectrum n Imperative languages n n n Evolved from the von Neumann Architecture Variables Assignment Statements Spring 14 CSCI 4430, A Milanova 16
The Programming Language Spectrum n Imperative languages n j=i–j lw t 0, 28(sp) lw t 1, 24(sp) subu t 2, t 0, t 1 sw t 2, 24(sp) Spring 14 CSCI 4430, A Milanova 17
The Programming Language Spectrum n Imperative languages n Most widely popular programming style n n n FORTRAN, C, C++, C#, Java, Python, Visual BASIC, Perl, Java. Script, Ruby, etc. Concept of variable and assignment statement are central Program is a sequence of statements: j = i – j; k = j * l; n Execution is a sequence of transitions of the state of the memory 18
The Programming Language Spectrum n n FORTRAN was invented in mid-1950 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 n Bottleneck: tube connecting CPU and memory Difficult to reason about correctness of programs Spring 14 CSCI 4430, A Milanova 19
The Programming Language Spectrum n Functional Programming n Main alternative to imperative programming n Lisp/Scheme, ML, Haskell Program consists of function definitions + evaluation expr (fun 3 (fun 2 (fun 1 data))) (fun 3 (fun 2 data 2)) (fun 3 data 3) data 4 n n n Execution is a sequence of function applications (i. e. , reductions) Logic Programming n n Perform queries against knowledge base Prolog, SQL Spring 14 CSCI 4430, A Milanova 20
Why Study Programming Languages n n Programming languages combines knowledge from other disciplines Goal of the course: learn to analyze programming languages n n What are the questions we ask when facing a new programming language Helps learn new languages, choose the right language for a problem, understand language features, design better languages! Spring 14 CSCI 4430, A Milanova 21
Lecture Outline n Introduction to the course n The art of language design The programming language spectrum Why study programming languages n Overview of compilation n n Spring 14 CSCI 4430, A Milanova 22
Compilation and Interpretation n Compilation n Pure interpretation n A “high-level” program is translated into executable machine code Compiler A program is translated and executed one statement at a time Interpreter Hybrid interpretation n n A program is “compiled” into intermediate code; intermediate code is “interpreted” Both a compiler and an interpreter Spring 14 CSCI 4430, A Milanova 23
Compilation COMPILER character stream Scanner (lexical analysis) token stream Parser (syntax analysis) parse tree Semantic analysis and intermediate code generation abstract syntax tree and/or intermediate form Machine-independent code improvement modified intermediate form Code Generation target language (assember) Machine-dependent code improvement modified target language Spring 14 CSCI 4430, A Milanova 24
Compilation position = initial + rate * 60; Scanner <id, 1> <=> <id, 2> <+> <id, 3> <*> <60> Symbol table 1. position, … 2. initial, … 3. rate, … Parser Semantic analysis and intermediate code generation Spring 14 CSCI 4430, A Milanova 25
Compilation Semantic analysis and intermediate code generation = <id, 1> + <id, 2> Abstract Syntax Tree * <id, 3> inttoreal 60 tmp 1 = inttoreal (60) tmp 2 = id 3 * tmp 1 tmp 3 = id 2 + tmp 2 id 1 = tmp 3 Spring 14 CSCI 4430, A Milanova Intermediate form (three-address code) 26
Compilation Machine-independent code improvement tmp 1 = id 3 * 60. 0 id 1 = id 2 + tmp 1 Improved intermediate form Code generation movf id 3, R 2 mulf #60. 0, R 2 movf id 2, R 1 addf R 2, R 1 movf R 1, id 1 Target language 27
Pure Interpretation e. g. BASIC REM COMMENT LET X = 5 LET Y = 8 PRINT X PRINT Y LET Z = X PRINT Z. . . Also Java. Script. . Spring 14 CSCI 4430, A Milanova 28
Hybrid Interpretation 09 99 19 09 99 AB 73 29 AB 73 19 09 99 19 09 29 AB 73 29 e. g. Java byte code e. g. Java Virtual Machine (JVM) Spring 14 CSCI 4430, A Milanova Also Perl. . 29
Compilation vs. Interpretation n A language can be implemented using a compiler or using an interpreter n n One can build a compiler for Lisp and one can easily build an interpreter for C or Fortran However, language features (determined during language design) have tremendous impact on “compilability” and the decision “compiler vs. interpreter” Spring 14 CSCI 4430, A Milanova 30
Compilation vs. Interpretation n What are the features that impact “compilability”? “Static” languages, i. e. , languages that perform most semantic checks before program execution, are suitable for compilation “Dynamic” languages, i. e. , languages that perform most semantic checks during execution, are suitable for interpretation Spring 14 CSCI 4430, A Milanova 31
Compilation vs. Interpretation n Advantages of compilation? n n Faster execution Advantages of interpretation? n Greater flexibility n Sandboxing, source-level debugging, dynamic semantic (type) checks, other dynamic features are much easier Spring 14 CSCI 4430, A Milanova 32
Compilation vs. Interpretation n New languages are increasingly dynamic n 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 But we have tons of legacy systems written in static languages (C, C++, Fortran) n Dynamic instrumentation tools (Valgrind, Pin, Dynamo. Rio) serve as “interpreters” for compiled binaries Spring 14 CSCI 4430, A Milanova 33
Next Class n n We will review regular expressions and context free grammars Read Chapter 2. 1 and 2. 2 from Scott’s book Spring 14 CSCI 4430, A Milanova 34