Ti1220 Lecture 1: Programming Linguistics


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ti1220 Lecture 1: Programming Linguistics

  1. 1. Lecture 1: Programming Linguistics TI1220 2012-2013 Concepts of Programming Languages Eelco Visser / TU Delft
  2. 2. OutlineCourse Organization Programming Linguistics Code of Conduct Why study PLs? Exams & Grades Syntax Course Staff Semantics Literature
  3. 3. Code of Conduct
  4. 4. Inspired by Coursera Honor Code
  5. 5. I will not use electronic devices such aslaptops, phones, or tablets during lectures.Experimental exception: laptop for taking notes at last row
  6. 6. I will not enter the lecture hallonce the lecture has started.
  7. 7. My answers to assignments and exams willbe my own work (except for assignmentsthat explicitly permit collaboration). http://en.wikipedia.org/wiki/File:Plagiarism_vs_Copyright_Infringement.png
  8. 8. I will not make solutions to assignments andexams available to anyone else. This includesboth solutions written by me, as well as anyofficial solutions provided by the course staff. http://ucblibraries.colorado.edu/about/images/Xerox5135.jpg
  9. 9. I will not engage in any other activities thatwill dishonestly improve my results ordishonestly improve/hurt the results ofothers.
  10. 10. Exams & Grades
  11. 11. Course Grade Your final grade G for the course will be computed as follows: G = (E * 0.6) + (GA * 0.4)where E is your grade for the exam an GA is your grade for the graded assignments.
  12. 12. Graded Assignments There will be four Graded Assignments.For each you need to get at least a 4.0 to pass. GA is the average of the four grades.
  13. 13. ExamsYou can pass the exam in two ways: Pass the midterm Q3 exam on April 9 with at least a 6.0 and then pass the Q4 exam on June 26 also with a 6.0. E is the average of two exams. Pass the Q3+Q4 exam on June 26 with at least a 6.0 Pass the August resit with at least a 6.0
  14. 14. All assignments for this course are provided via WebLab and should be submitted via WebLab, including the exams.
  15. 15. Exam/Lab Grades from 2011-2012 Partial result from 2011-2012? Discuss with me offline (but not today)
  16. 16. Warning!This course moves from first year to second year in new curriculum TI1220 will not be taught in 2013-2014 Lab & exams will be repeated, no lectures My advice: pass the course this year!
  17. 17. Course Staff
  18. 18. Course StaffInstructors• Eelco Visser Assistants • Jeff Smits• Vlad Vergu • Victor Spiridon • Tim de Jong • Bastiaan Reijm http://eelcovisser.org/wiki/about/officehours
  19. 19. Literature
  20. 20. Course Notes on WebLab
  21. 21. Programming Linguistics
  22. 22. Why do we have so manyprogramming languages?
  23. 23. Computers Process Datadata computer data
  24. 24. Programmable Computersdata computer data program
  25. 25. Programming Languagedata computer data L program
  26. 26. Turing Machines Turing/Church Thesis: Every effective computation can be carried out by a Turing machine (IN2505) Corollary: All (Turing Complete) programminglanguages can express all effective computationsWhy bother with new programming languages?
  27. 27. History of Programming LanguagesHistory of Programming Languages 1954 1960 1965 1970 1975 1980 1985 1990 1995 2000 2001 2002 2003 2004 1986 1990 1990 1991 1991 1993 1994 1995 1996 1996 1997 1997 2000 2001 2001 2003 2003 2004 For more than half of the fifty years computer programmers have been This timeline includes fifty of the more than 2500 documented writing code, O’Reilly has provided developers with comprehensive, programming languages. It is based on an original diagram createdwww.oreilly.com in-depth technical information. We’ve kept pace with rapidly changing by Éric Lévénez (www.levenez.com), augmented with suggestions technologies as new languages have emerged, developed, and from O’Reilly authors, friends, and conference attendees. matured. Whether you want to learn something new or need For information and discussion on this poster, answers to tough technical questions, you’ll find what you need go to www.oreilly.com/go/languageposter. in O’Reilly books and on the O’Reilly Network. ©2004 O’Reilly Media, Inc. O’Reilly logo is a registered trademark of O’Reilly Media, Inc. All other trademarks are property of their respective owners. part#30417 This timeline includes 50 of the more than 2500 documented programming languages.
  28. 28. Programming Languages in TI @ TUD TI1200: Object-Oriented Programming • Java TI1400: Computer Systems • Assembly TI1500: Web- and Database Technology • HTML, PHP, SQL, JavaScript TI1600: Multi-Agent Systems • Prolog, GOAL
  29. 29. “A programming language is low level when itsprograms require attention to the irrelevant”Alan J. Perlis. Epigrams on Programming. SIGPLAN Notices, 17(9):7-13, 1982.
  30. 30. From Instructions to Expressionsmov &a, &c c = aadd &b, &c c += bmov &a, &t1 t1 = a c = (a + b) & (a - b)sub &b, &t1 t1 -= band &t1,&c c &= t1Source: http://sites.google.com/site/arch1utep/home/course_outline/translating-complex-expressions-into-assembly-language-using-expression-trees
  31. 31. From Calling Conventions to Procedures calc: push eBP ; save old frame pointer mov eBP,eSP ; get new frame pointer sub eSP,localsize ; reserve place for locals . . ; perform calculations, leave result in AX . mov eSP,eBP ; free space for locals pop eBP ; restore old frame pointer ret paramsize ; free parameter space and return push eAX ; pass some register result push byte[eBP+20] ; pass some memory variable (FASM/TASM syntax) push 3 ; pass some constant call calc ; the returned result is now in eAX http://en.wikipedia.org/wiki/Calling_convention def f(x)={ ... } f(e1) function definition and call in Scala
  32. 32. From Malloc to Garbage Collection/* Allocate space for an array with ten elements of type int. */int *ptr = (int*)malloc(10 * sizeof (int));if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */} else { /* Allocation succeeded. Do something. */ free(ptr); /* We are done with the int objects, and free the associated pointer. */ ptr = NULL; /* The pointer must not be used again, unless re-assigned to using malloc again. */} http://en.wikipedia.org/wiki/Malloc int [] = new int[10]; /* use it; gc will clean up (hopefully) */
  33. 33. Linguistic Abstraction design abstraction language A language B use new abstractionidentify pattern
  34. 34. Domains of Computation Application domains Systems programming Embedded software Web programming Enterprise software Database programming ...
  35. 35. Why So Many Programming Languages? Linguistic abstraction: better understanding of the general domain of computation Specialization to domain: different requirements from different application domains
  36. 36. Why Study Programming Languages? Language shapes thought: understand the languages that you are using Choosing the right language for the job Learning to learn new languages Understanding implementation: what’s under the hood?
  37. 37. Topics in this CourseSyntax and SemanticsNames, Bindings, and ScopesStorageData TypesFunctional ProgrammingType Systems 1: PolymorphismType Systems 2: Type Parameterization Parsing and Interpretation Control Abstraction Data Abstraction / Modular Programming Functional Programming Redux Concurrency Concurrent Programming Domain-Specific Languages
  38. 38. How can we describe aprogramming language?
  39. 39. Syntax
  40. 40. Syntaxsyntax |ˈsinˌtaks|nounthe arrangement of words and phrases to create well-formed sentences ina language: the syntax of English.• a set of rules for or an analysis of this: generative syntax.• the branch of linguistics that deals with this.ORIGIN late 16th cent.: from French syntaxe, or via late Latin fromGreek suntaxis, from sun- ‘together’ + tassein ‘arrange.’
  41. 41. Syntax“The syntax of a programming language is the form of its expressions, statements, and program units.” Sebesta Ch3
  42. 42. Lexical vs Context-free Syntax lexical syntax • words made from letters • structure not relevant • regular expressions context-free syntax • sentences made from words • structure relevant • context-free grammars • Backus-Naur Form • Extended Backus-Naur Form
  43. 43. Lexemes & TokensLexemes are the words that make a sentences Tokens are the categories of lexemes
  44. 44. Lexemes & Tokensindex = 2 * count + 17; Lexeme Token index Identifier = EqualSign 2 IntegerLiteral * MultOp count Identifier + PlusOp 17 IntegerLiteral ; Semicolon
  45. 45. Describing Lexemes with Regular Expressions Identifier: [A-Za-z][A-Za-z0-9]* IntegerLiteral: [0-9]+ PlusOp: ‘+’ Semicolon: ‘;’
  46. 46. Regular Expressionsbasics • strings: "nil" • character classes: [a-zA-Z]combinators • concatenation: E1 E2 • optional: E? • zero or more: E* • one or more: E+ • alternative: E1 | E2
  47. 47. Describing Structure with Railroad Diagrams
  48. 48. Chomsky Hierarchy BNF: Backus-Naur Formhttp://www.cs.nott.ac.uk/~txa/g51mal.2001/notes/img4.png
  49. 49. Ambiguous Grammar<assign> -> <id> = <expr><id> -> A | B | C<expr> -> <expr> + <expr> | <expr> * <expr> | ( <expr> ) | <id>
  50. 50. Unambiguous Grammar<assign> -> <id> = <expr><id> -> A | B | C<expr> -> <expr> + <term> | <term><term> -> <term> * <factor> | <factor><factor> -> ( <expr> ) | <id>
  51. 51. Syntax in this Course You should be able to determine whether aprogram is syntactically correct according to acontext-free grammar (BNF, railroad diagram) (There will be exercises on WebLab)
  52. 52. €€€€€€€€€ Millionaires Corner€€€€€€€€€
  53. 53. Demonstration ofDeclarative Syntax Definition in the Spoofax Language Workbench
  54. 54. Semantics
  55. 55. Semanticssemantics |səәˈmantiks|pluralnoun [ usu. treated as sing. ]the branch of linguistics and logic concerned with meaning. There are anumber of branches and subbranches of semantics, including formalsemantics, which studies the logical aspects of meaning, such as sense,reference, implication, and logical form, lexical semantics, which studiesword meanings and word relations, and conceptual semantics, whichstudies the cognitive structure of meaning.• the meaning of a word, phrase, sentence, or text: such quibbling oversemantics may seem petty stuff.DERIVATIVESsemantician |ˌsēmanˈtiSHəәn|noun,semanticist noun
  56. 56. Semantics“The semantics of a programming language is the meaning of its expressions, statements, and program units.” Sebesta Ch3
  57. 57. Static vs Dynamic Semantics Static Semantics: (context-senstive)restriction of the set of valid programs Dynamic Semantics: run-time behaviour of a program
  58. 58. Operational SemanticsOperational semantics: describe the meaning of a statement or program by specifying the effects of running it on a machine In second part of course (Q4): write interpreter for small functional language
  59. 59. Denotational Semanticsa denotational semantics defines a mapping from the syntactic domain to a semantic domain of mathematical objects<bin> -> ‘0’ | ‘1’ | <bin> ‘0’ | <bin> ‘1’ M(‘0’) = 0 M(‘1’) = 1 M(<bin> ‘0’) = 2 * M(<bin>) M(<bin> ‘1’) = 2 * M(<bin>) + 1
  60. 60. Axiomatic Semanticsaxiomatic semantics specifies what can be proven about a program {x > 3} x = x - 3 {x > 0}
  61. 61. Semantics in this Course Informal operational semantics: Reasoning about the operational behavior of programsComparative/translational semantics:Explaining the semantics of construct/concept by translating it to another language
  62. 62. Reading & Programming in Week 1Reading: Sebesta Chapter 1: Preliminaries Chapter 2: Evolution of the major programming languages (read) Chapter 3: Describing Syntax and Semantics WebLab: Scala Tutorial Week 2: Names, Bindings, and Scope (Chapter 5)