CS 212 LECTURE 01 PROGRAMMING LANGUAGES CS 212

  • Slides: 19
Download presentation
CS 212 LECTURE 01 PROGRAMMING LANGUAGES

CS 212 LECTURE 01 PROGRAMMING LANGUAGES

CS 212 PROGRAMMING LANGUAGES Instructor : Somchai Thangsathityangkul You can download lecture note at

CS 212 PROGRAMMING LANGUAGES Instructor : Somchai Thangsathityangkul You can download lecture note at http: //kbucomsci. weebly. com/ Class Presence 10% Quiz 20% Homework 10% Midterm 20% Final 40% Total 100% 2

THE TOP PROGRAMMING LANGUAGES 2018 3

THE TOP PROGRAMMING LANGUAGES 2018 3

PROGRAMMING DOMAINS • Scientific applications • Large number of floating point computations • Fortran

PROGRAMMING DOMAINS • Scientific applications • Large number of floating point computations • Fortran • Business applications • Produce reports, use decimal numbers and characters • COBOL • Artificial intelligence • Symbols rather than numbers manipulated • LISP • Systems programming • Need efficiency because of continuous use • C • Web Software • Electic collection of languages: markup (e. g. , XHTML), scripting (e. g. , PHP), general-purpose (e. g. , Java) 4

LANGUAGE EVALUATION CRITERIA • Readability: the ease with which programs can be read and

LANGUAGE EVALUATION CRITERIA • Readability: the ease with which programs can be read and understood • Writability: the ease with which a language can be used to create programs • Reliability: conformance to specifications (i. e. , performs to its specifications) • Cost: the ultimate total cost 5

LANGUAGE CATEGORIES • Imperative • Central features are variables, assignment statements, and iteration •

LANGUAGE CATEGORIES • Imperative • Central features are variables, assignment statements, and iteration • Examples: C, Pascal • Functional • Main means of making computations is by applying functions to given parameters • Examples: LISP, Scheme • Logic • Rule-based (rules are specified in no particular order) • Example: Prolog • Object-oriented • Data abstraction, inheritance, late binding • Examples: Java, C++ • Markup • New; not a programming per se, but used to specify the layout of information in Web documents • Examples: XHTML, XML 6

IMPERATIVE LANGUAGES • Example: a factorial function in C int fact(int n) { int

IMPERATIVE LANGUAGES • Example: a factorial function in C int fact(int n) { int sofar = 1; while (n>0) sofar *= n-; return sofar; } 7

FUNCTIONAL LANGUAGES • Example: a factorial function in ML fun fact x = if

FUNCTIONAL LANGUAGES • Example: a factorial function in ML fun fact x = if x <= 0 then 1 else x * fact(x-1); • Example: a factorial function in Lisp (defun fact (x) (if (<= x 0) 1 (* x (fact (- x 1))))) 8

IMPLEMENTATION METHODS • Compilation • Programs are translated into machine language • Pure Interpretation

IMPLEMENTATION METHODS • Compilation • Programs are translated into machine language • Pure Interpretation • Programs are interpreted by another program known as an interpreter • Hybrid Implementation Systems • A compromise between compilers and pure interpreters 9

COMPILATION • Translate high-level program (source language) into machine code (machine language) • Slow

COMPILATION • Translate high-level program (source language) into machine code (machine language) • Slow translation, fast execution • Compilation process has several phases: • lexical analysis: converts characters in the source program into lexical units • syntax analysis: transforms lexical units into parse trees which represent the syntactic structure of program • Semantics analysis: generate intermediate code • code generation: machine code is generated 10

THE COMPILATION PROCESS 11

THE COMPILATION PROCESS 11

PURE INTERPRETATION • No translation • Easier implementation of programs (run-time errors can easily

PURE INTERPRETATION • No translation • Easier implementation of programs (run-time errors can easily and immediately displayed) • Slower execution (10 to 100 times slower than compiled programs) • Often requires more space • Becoming rare on high-level languages • Significant comeback with some Web scripting languages (e. g. , Java. Script) 12

PURE INTERPRETATION PROCESS 13

PURE INTERPRETATION PROCESS 13

HYBRID IMPLEMENTATION SYSTEMS • A compromise between compilers and pure interpreters • A high-level

HYBRID IMPLEMENTATION SYSTEMS • A compromise between compilers and pure interpreters • A high-level language program is translated to an intermediate language that allows easy interpretation • Faster than pure interpretation • Examples • Perl programs are partially compiled to detect errors before interpretation • Initial implementations of Java were hybrid; the intermediate form, byte code, provides portability to any machine that has a byte code interpreter and a run -time system (together, these are called Java Virtual 14

HYBRID IMPLEMENTATION PROCESS 15

HYBRID IMPLEMENTATION PROCESS 15

JUST-IN-TIME IMPLEMENTATION SYSTEMS • Initially translate programs to an intermediate language • Then compile

JUST-IN-TIME IMPLEMENTATION SYSTEMS • Initially translate programs to an intermediate language • Then compile intermediate language into machine code • Machine code version is kept for subsequent calls • JIT systems are widely used for Java programs • . NET languages are implemented with a JIT system 16

LANGUAGES Symbol a, b. . . , Alphabet A finite, nonempty set of symbols

LANGUAGES Symbol a, b. . . , Alphabet A finite, nonempty set of symbols usually denoted by String - finite sequence of symbols e. g. abba, b, bb Empty string denoted by * =set of all strings over alphabet e. g. {a, b}*} = , a, b, aa, ab{. . . , language - set of strings defined over 17

LANGUAGES Examples = { 0, 1 , 2 , 3, 4, 5, 6, 7,

LANGUAGES Examples = { 0, 1 , 2 , 3, 4, 5, 6, 7, 8, 9 } L = { 0, 1, …, 11, … 99, …} ={a, b, c{ L={ab, ac, cabb{ ={ 1, 2, +, = } L 1 = {1+1=2} L 2 = {2+2+2+2=12} What’s about L 3={1+1= 1} ? And Examples All strings of even length ={a, b{ L= { , aa, ab, ba, bb, aaaa, . . . } All strings of a’s and b’s in equal numbers L= { , ab, ba, aabb, abab, abba, . . . } L 4 = { 1+1=3 } 18

LANGUAGES Let ={a, b, c}. The elemnets of * include Length 0 : Length

LANGUAGES Let ={a, b, c}. The elemnets of * include Length 0 : Length 1 : a, b, c Length 2 : aa, ab, ac, ba, bb, bc, ca, cb, cc Length 3 : aaa, aab, aac, aba, abb, abc, aca, acb, acc, baa, bab, bac, …, cbc, cca, ccb, ccc … In general, if is an alphabet and L is a subset of *, then L is said to be a language over . Example { 0, 11, 001}, { , {10 , and {0, 1}* are subsets of {0, 1}*, and so they are languages over the alphabet {0, 1. { 19