Скачать презентацию BBM 301 Programming Languages General Information n Скачать презентацию BBM 301 Programming Languages General Information n

947eb33601e3513a767f668bd397463b.ppt

  • Количество слайдов: 112

BBM 301 –Programming Languages BBM 301 –Programming Languages

General Information n Webpage: http: //web. cs. hacettepe. edu. tr/~bbm 301 n Teaching Assistants: General Information n Webpage: http: //web. cs. hacettepe. edu. tr/~bbm 301 n Teaching Assistants: q Yasin Şahin q Çağdaş Baş n Textbook: q Robert W. Sebesta, “Concepts of Programming Languages”, Pearson, Ninth Edition. n Time and Place: q Tuesdays 9: 30 – 12: 15 Section 1 @ D 8, Section 2 @ D 9 n Communication: q We will use Piazza https: //piazza. com/hacettepe. edu. tr/fall 2012/bbm 301 2

Topics n n n Introduction to Programming Languages Syntax and Semantics Lex Yacc Conflicts Topics n n n Introduction to Programming Languages Syntax and Semantics Lex Yacc Conflicts Names, Bindings, Scope Data Types Expressions, Assignments Control Statements Subprograms Implementing Subprograms Functional Languages 3

Grading Policy n n 10% 20% 30% 40% Assignments and quizzes Course project (may Grading Policy n n 10% 20% 30% 40% Assignments and quizzes Course project (may be done in pairs) Midterm exam Final exam 4

Acknowledgment n Course slides adapted from q q Textbook’s slides Slides of Pinar Duygulu Acknowledgment n Course slides adapted from q q Textbook’s slides Slides of Pinar Duygulu for CS 315 – Programming Languages course 5

Lecture 1: Introduction to Programming Languages Lecture 1: Introduction to Programming Languages

Why Study Programming Languages? n n One or two languages is not enough for Why Study Programming Languages? n n One or two languages is not enough for a computer scientist. You should know the general concepts beneath the requirements and choices in designing programming languages. 7

New Languages will Keep Coming 8 New Languages will Keep Coming 8

Be prepared to program in new languages Languages undergo constant change q q q Be prepared to program in new languages Languages undergo constant change q q q FORTRAN ALGOL 60 C C++ Java 1953 1960 1973 1985 1995 Evolution steps: 12 years per widely adopted language q are we overdue for the next big one? . . . or is the language already here? q q Hint: are we going through a major shift in what computation programs need to express? your answer here: 9

ENIAC (1946, University of Philadelphia) ENIAC program for external ballistic equations: Slide from Ras ENIAC (1946, University of Philadelphia) ENIAC program for external ballistic equations: Slide from Ras Bodik 10

Programming the ENIAC Slide from Ras Bodik 11 Programming the ENIAC Slide from Ras Bodik 11

ENIAC (1946, University of Philadelphia) programming done by – rewiring the interconnections – to ENIAC (1946, University of Philadelphia) programming done by – rewiring the interconnections – to set up desired formulas, etc Problem (what’s the tedious part? ) – programming = rewiring – slow, error-prone solution: – store the program in memory! – birth of von Neuman paradigm Slide from Ras Bodik 12

Reasons for Studying Concepts of Programming Languages n n n n Increased ability to Reasons for Studying Concepts of Programming Languages n n n n Increased ability to express ideas Improved background for choosing appropriate languages Increased ability to learn new languages Better understanding of significance of implementation Better use of languages that are already known Overall advancement of computing Ability to design new languages Copyright © 2009 Addison-Wesley. All rights reserved. 13

Increased Ability to Express Ideas n Natural languages: q q q The depth at Increased Ability to Express Ideas n Natural languages: q q q The depth at which people think is influenced by the expressive power of the language. The more appropriate constructs you have, the easier it is to communicate. It is difficult for people to conceptualize structures that they cannot describe verbally. 14

Increased Ability to Express Ideas n Programming Languages: q q This is similar for Increased Ability to Express Ideas n Programming Languages: q q This is similar for PL’s. The language in which you develop software puts limits on the kinds of data structures, control structures and abstractions that can be used. Awareness of a wider variety of programming language features can reduce such limitations in software development. Programmers increase the range of software development by learning new language constructs. For example, if you learn associate arrays in Perl, you can simulate them in C. 15

Improved background for choosing appropriate languages n n Not every programming language can be Improved background for choosing appropriate languages n n Not every programming language can be suitable for all the software requirements. Many programmers learn one or two languages specific to the projects. Some of those languages may no longer be used. When they start a new project they continue to use those languages which are old and not suited to the current projects. 16

Improved background for choosing appropriate languages n However another language may be more appropriate Improved background for choosing appropriate languages n However another language may be more appropriate to the nature of the project. q q If there is heavy text processing, then using Perl may be a good option. If there is heavy matrix processing, then MATLAB can be used. n If you are familiar with the other languages, particularly the features in those languages, you can choose better languages. n Studying the principles of PLs provides a way to judge languages, and make informed statements: “The advantages of Perl for this problem are…. . ”, “The advantages of Java are…. ” 17

Increased ability to learn new languages n Programming languages are still evolving q n Increased ability to learn new languages n Programming languages are still evolving q n If you know the programming language concepts you will learn other languages much easier compared to programmers knowing only one or two languages. q n Many languages are very new, and new languages can be added in time. For example, if you know concept of object oriented programming, it is easier to learn C++ after learning Java Just like natural languages, q q The better you know the grammar of a language, the easier you find to learn a second language. learning new languages actually causes you to learn things about the languages you already know n Doing something the old way in a new language and getting surprising results may cause you to double check you assumptions about the old language, which will hopefully help you to understand it better 18

Languages in common use 19 Compiled by François Labelle from statistics on open-source projects Languages in common use 19 Compiled by François Labelle from statistics on open-source projects at Source. Forge

Languages in common use (last year) TIOBE programming community index 20 Languages in common use (last year) TIOBE programming community index 20

Languages in common use (today) TIOBE programming community index 21 Languages in common use (today) TIOBE programming community index 21

Languages in common use (today) TIOBE programming community index 22 Languages in common use (today) TIOBE programming community index 22

Better understanding of significance of implementation n The best programmers are the ones having Better understanding of significance of implementation n The best programmers are the ones having at least understanding of how things work under the hood q n n Understand the implementation issues You can simply write a code and let the compiler do everything, but knowing implementation details helps you to use a language more intelligently and write the code that is more efficient Also, it allows you to visualize how a computer executes language constructs q Cost optimization; e. g. recursion is slow 23

Better use of languages that are already known n Many programming languages are large Better use of languages that are already known n Many programming languages are large and complex q n It is uncommon to know all the features By studying the concepts of programming languages, programmers can learn about previously unknown parts of the languages easily. 24

Overall advancement of computing n n New ways of thinking about computing, new technology, Overall advancement of computing n n New ways of thinking about computing, new technology, hence need for new appropriate language concepts Not to repeat history q Although ALGOL 60 was a better language (with better block structure, recursion, etc) than FORTRAN, it did not become popular. If those who choose languages are better informed, better languages would be more popular. 25

Develop your own language Are you kidding? No. Guess who developed: q q PHP Develop your own language Are you kidding? No. Guess who developed: q q PHP Ruby Java. Script perl Done by smart hackers like you q q in a garage not in academic ivory tower Our goal: learn good academic lessons q so that your future languages avoid known mistakes 26 Slide from Ras 26 Bodik

Ability to Design New Languages n You may need to design a special purpose Ability to Design New Languages n You may need to design a special purpose language to enter the commands for a software that you develop. q n A language for an robotics interface Studying PL concepts will give you the ability to design a new language suitable and efficient for your software requirements. 27

Programming Domains n Scientific Applications (first digital computers – 1940 s) Large floating-point arithmetic, Programming Domains n Scientific Applications (first digital computers – 1940 s) Large floating-point arithmetic, execution efficiency, arrays and matrices, counting loops q Examples: FORTRAN, ALGOL 60, C q n Business Applications (1950 s) Producing elaborate reports, decimal numbers and character data q Examples: COBOL(1960 s), Spreadsheets, Wordprocessors, Databases(SQL) q n Artificial Intelligence Symbolic programming (names rather than numbers, linked lists rather than arrays) q Examples: LISP(1959), PROLOG(early 1970 s) q n Systems Programming System software: Operating system and all of the programming support tools of a computer system q Efficient and fast execution, low-level features for peripheral device drivers q Examples: PLS/2(IBM Mainframe), BLISS (Digital), C (Unix) q n Scripting Languages List of commands (Script) to be executed is put in a file. q Examples: sh, csh, tcsh, awk, gawk, tcl, perl, javascript q n Special-Purpose languages q Examples: RPG (Business Reports), SPICE (Simulation of Electronic Circuitry), SPSS (Statistics), Latex (Document preparation). HTML, XML (web prog. ) 28

Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of programming languages n n Readability Writability Reliability Cost Copyright © 2009 Addison-Wesley. All rights reserved. 29 1 -29

Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of programming languages n n Readability Writability Reliability Cost Copyright © 2009 Addison-Wesley. All rights reserved. 30 1 -30

Readability Ease with which programs can be read and understood n in the early Readability Ease with which programs can be read and understood n in the early times, efficiency and machine readability was important n 1970 s-Software life cycle: coding (small) + maintenance (large) Readability is important for maintenance n Characteristics that contribute to readability: q q q Overall simplicity Orthogonality Control statements Data types and structures Syntax Considerations 31

Overall Simplicity n A manageable set of features and constructs q n Large number Overall Simplicity n A manageable set of features and constructs q n Large number of basic components - difficult to learn Minimal feature multiplicity q Feature multiplicity: having more than one way to accomplish an operation n e. g. In Java count = count + 1 count += 1 count ++ ++count 33

Overall Simplicity n Minimal operator overloading q q n Operator overloading: a single operator Overall Simplicity n Minimal operator overloading q q n Operator overloading: a single operator symbol has more than one meaning This can lead to reduced readability if users are allowed to create their own and do not do it sensibly Example: n using + for integer and floating point addition is acceptable and contributes to simplicity n but if a user defines + to mean the sum of all the elements of two single dimensional arrays is not, different from vector addition Simplest does not mean the best q Assembly languages: Lack the complex control statements, so program structure is less obvious 34

Orthogonality n n n It means that a relatively small set of primitive constructs Orthogonality n n n It means that a relatively small set of primitive constructs can be combined in a relatively small number of ways to build the control and data structures of the language Furthermore, every possible combination of primitives is legal and meaningful. Example: q q Four primitive data types : integer, float, double and character Two type operators : array and pointer If the two type operators can be applied to themselves and the four primitive data types, a large number of data structures can be defined However, if pointers were not allowed to point to arrays, many of those useful possibilities would be eliminated 35

Orthogonality n Example : Adding two 32 -bit integers residing in memory or registers, Orthogonality n Example : Adding two 32 -bit integers residing in memory or registers, and replacing one of them with the sum n IBM (Mainframe) Assembly language has two instructions: A Register 1, Memory. Cell 1 Not orthogonal AR Register 1, Register 2 meaning Register 1 ← contents(Register 1) + contents(Memory. Cell 1) Register 1 ← contents(Register 1) + contents(Register 2) n VAX Assembly language has one instruction: ADDL operand 1, operand 2 orthogonal meaning operand 2 ← contents(operand 1) + contents(operand 2) Here, either operand can be a register or a memory cell. 36

Orthogonality n VAX instruction design is orthogonal q n Because a single instruction can Orthogonality n VAX instruction design is orthogonal q n Because a single instruction can use either registers or memory cells as the operands IBM design is not orthogonal q q q Only two operand combinations are legal out of for possibilities and two require different instructions, A and AR More restricted and less writable (you cannot add two values and store the sum in memory location) More difficult to learn because of the restrictions and the additional instructions 37

Orthogonality n n Orthogonality is closely related to simplicity The more orthogonal the design Orthogonality n n Orthogonality is closely related to simplicity The more orthogonal the design of a language, the fewer exceptions the language rules require Pascal is not an orthogonal language, because q A function cannot return a record (only unstructured types allowed), q A file must be passed as a var parameter, q Formal parameter types must be named (cannot be type descriptions) q Compound statements are formed by begin-end pair, except repeat-until C is not an orthogonal language, because q records (structs) can be returned from functions but arrays cannot q a member of a structure can be any type but not void or structure of the same type q a member of an array can be any type but not void or function 38

Orthogonality n n Too much orthogonality can cause problems as well: ALGOL 68 is Orthogonality n n Too much orthogonality can cause problems as well: ALGOL 68 is the most orthogonal language. q q q Every construct has a type Most constructs produce values This may result in extremely complex constructs, Ex: A conditional can appear as the left side of an assignment statement, as long as it produces a location: (if n (A

Orthogonality n Functional languages offer a good combination of simplicity and orthogonality. 40 Orthogonality n Functional languages offer a good combination of simplicity and orthogonality. 40

Control Statements n n Control statement design of a language is a important factor Control Statements n n Control statement design of a language is a important factor in readability. For example, languages such as BASIC and FORTRAN, that allow the use of goto statement, are not readable. 41

Control Statements A example loop in C while (incr < 20) { while (sum Control Statements A example loop in C while (incr < 20) { while (sum < =100) { sum += incr; } incr++; } 42

Control Statements If C didn’t have a loop construct: loop 1: if (incr >=20) Control Statements If C didn’t have a loop construct: loop 1: if (incr >=20) go to out; loop 2: if (sum > 100) go to next; sum += incr; go to loop 2; next: incr++; go to loop 1: out: 43

Data Types and Structures n Facilities for defining data types and data structures are Data Types and Structures n Facilities for defining data types and data structures are helpful for readability q If there is no boolean type available then a flag may be defined as integer: found = 1 (instead of found = true) May mean something is found as boolean or what is found is 1 q An array of record type is more readable than a set of independent arrays 44

Syntax considerations n Identifier Forms: restricting identifier length is bad for readability. q Example: Syntax considerations n Identifier Forms: restricting identifier length is bad for readability. q Example: n n n FORTRAN 77 identifiers can have at most 6 characters. The extreme case is the ANSI BASIC, where an identifier is either a single character or a single character followed by a single digit. Availability of word concatenating characters (e. g. , _ ) is good for readability. 45

Syntax considerations Special Words: Readability is increased by special words (e. g. , begin, Syntax considerations Special Words: Readability is increased by special words (e. g. , begin, end, for). n In PASCAL and C, end or } is used to end a compound statement. It is difficult tell what an end or } terminates. n However, ADA uses end if and end loop to terminate a selection and a loop, respectively. n Another issue is the use of special words as names of variables. For example, in FORTRAN 77, special words, e. g. , DO and END can be used as variable names. 46

Syntax Considerations n n n Forms and Meaning: Forms should relate to their meanings. Syntax Considerations n n n Forms and Meaning: Forms should relate to their meanings. Semantics should directly follow from syntax. For example, sin(x) should be the sine of x, not the sign of x or cosign of x. grep is hard to understand for the people who are not familiar with using regular expressions grep : g/reg_exp/p /reg_exp/ : search for that reg_exp g: scope is whole file , make it global p: print 47

Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of programming languages n n Readability Writability Reliability Cost Copyright © 2009 Addison-Wesley. All rights reserved. 48 1 -48

Writability n n n Ease of creating programs Most of the characteristics that contribute Writability n n n Ease of creating programs Most of the characteristics that contribute to readability also contribute to writability Characteristics that contribute to writability q q q n Simplicity and Orthogonality Support for abstraction Expressivity Writability of a language can also be application dependent 49

Writability n Simplicity and orthogonality q n Support for abstraction q n Few constructs, Writability n Simplicity and orthogonality q n Support for abstraction q n Few constructs, a small number of primitives, a small set of rules for combining them The ability to define and use complex structures or operations in ways that allow details to be ignored Expressivity q A set of relatively convenient ways of specifying operations q Strength and number of operators and predefined functions Copyright © 2012 Addison-Wesley. All rights reserved. 1 -50

Simplicity and orthogonality n n Simplicity and orthogonality are also good for writability. When Simplicity and orthogonality n n Simplicity and orthogonality are also good for writability. When there are large number of constructs, programmers may not be familiar with all of them, and this may lead to either misuse or disuse of those items. A smaller number of primitive constructs (simplicity) and consistent set of rules for combining them (orthogonality) is good for writability However, too much orthogonality may lead to undetected errors, since almost all combinations are legal. 51

Support for abstraction n n Abstraction: ability to define and use complicated structures and Support for abstraction n n Abstraction: ability to define and use complicated structures and operations in ways that allows ignoring the details. Abstraction is the key concept in contemporary programming languages The degree of abstraction allowed by a programming language and the naturalness of its expressions are very important to its writability. PLs can support two types of abstraction: q q process data 52

Process abstraction n n The simplest example of abstraction is subprograms (e. g. , Process abstraction n n The simplest example of abstraction is subprograms (e. g. , methods). You define a subprogram, then use it by ignoring how it actually works. Eliminates replication of the code Ignores the implementation details q e. g. sort algorithm 53

Data abstraction n n As an example of data abstraction, a tree can be Data abstraction n n As an example of data abstraction, a tree can be represented more naturally using pointers in nodes. In FORTRAN 77, where pointer types are not available, a tree can be represented using 3 parallel arrays, two of which contain the indexes of the offspring, and the last one containing the data. 54

Expressivity n n Having more convenient and shorter ways of specifying computations. For example, Expressivity n n Having more convenient and shorter ways of specifying computations. For example, in C, count++; is more convenient and expressive than count = count + 1; for is more easier to write loops than while 55

Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of programming languages n n Readability Writability Reliability Cost Copyright © 2009 Addison-Wesley. All rights reserved. 56 1 -56

Reliability Reliable: it performs to its specifications under all conditions n Type checking q Reliability Reliable: it performs to its specifications under all conditions n Type checking q n Exception handling q n Intercept run-time errors and take corrective measures Aliasing q n Testing for type errors Presence of two or more distinct referencing methods for the same memory location Readability and writability q A language that does not support “natural” ways of expressing an algorithm will require the use of “unnatural” approaches, and hence reduced reliability Copyright © 2012 Addison-Wesley. All rights reserved. 1 -57

Type Checking n n n Testing for type errors in a given program either Type Checking n n n Testing for type errors in a given program either by the compiler or during program execution The compatibility between two variables or a variable and a constant that are somehow related (e. g. , assignment, argument of an operation, formal and actual parameters of a method). Run-time (Execution-time) checking is expensive. Compile-time checking is more desirable. The earlier errors in programs are detected, the less expensive it is to make the required repairs 58

Type Checking n n Ada requires type checking on all variables in compile time. Type Checking n n Ada requires type checking on all variables in compile time. Original C language requires no type checking neither in compilation nor execution time. That can cause many problems. q n Current version required all parameters to be typechecked Java requires checks of the types of nearly all variables and expressions at compile time. 59

Type Checking n For example, the following program in original C compiles and runs! Type Checking n For example, the following program in original C compiles and runs! foo (float a) { printf (“a: %g a, a*a); } main () { char z = ‘b’; foo(z); } n and square(a): %gn”, Output is : a: 98 and square(a): 9604 60

Type checking n Subscript range checking for arrays is a part of type checking, Type checking n Subscript range checking for arrays is a part of type checking, but it must be done in the run-time. Out-of-range subscript often cause errors that do not appear until long after actual violation. 61

Exception Handling n The ability of a program q q n to intercept run-time Exception Handling n The ability of a program q q n to intercept run-time errors, as well as other unusual conditions to take corrective measures and continue Ada, C++, and Java include extensive capabilities for exception handling, but in C and Fortran it is practically non-existent 62

Aliasing n n n Having two distinct referencing methods (or names) for the same Aliasing n n n Having two distinct referencing methods (or names) for the same memory cell. It is a dangerous feature in a programming language. Example: pointers in PASCAL and C q two different variables can refer to the same memory cell 63

Readability and Writability n n The easier a program to write, the more likely Readability and Writability n n The easier a program to write, the more likely it is correct. Programs that are difficult to read are difficult both to write and modify. 64

Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of Language Evaluation Criteria The main criteria needed to evaluate various constructs and capabilities of programming languages n n Readability Writability Reliability Cost Copyright © 2009 Addison-Wesley. All rights reserved. 65 1 -65

Cost n Types of costs: 1) Cost of training the programmers: Function of simplicity Cost n Types of costs: 1) Cost of training the programmers: Function of simplicity and orthogonality, experience of the programmers. q 2) Cost of writing programs: Function of the writability Note: These two costs can be reduced in a good programming environment q 3) Cost of compiling programs: cost of compiler, and time to compile q n First generation Ada compilers were very costly 66

Cost q q q 4) Cost of executing programs: If a language requires many Cost q q q 4) Cost of executing programs: If a language requires many run-time type checking, the programs written in that language will execute slowly. Trade-off between compilation cost and execution cost. Optimization: decreases the size or increases the execution speed. n n n Without optimization, compilation cost can be reduced. Extra compilation effort can result in faster execution. More suitable in a production environment, where compiled programs are executed many times 67

Cost q q q 5) Cost of the implementation system. If expensive or runs Cost q q q 5) Cost of the implementation system. If expensive or runs only on expensive hardware it will not be widely used. 6) Cost of reliability – important for critical systems such as a power plant or X-ray machine 7) Cost of maintaining programs. For corrections, modifications and additions. n n n Function of readability. Usually, and unfortunately, maintenance is done by people other that the original authors of the program. For large programs, the maintenance costs is about 2 to 4 times the development costs. 68

Other Criteria for Evaluation n Portability: The ease with which programs can be moved Other Criteria for Evaluation n Portability: The ease with which programs can be moved from one implementation to another Generality: The applicability to a wide range of applications Well-definedness: The completeness and precision of the language’s official definition 69

Influence on Language Design n Several other factors influence the design of programming languages Influence on Language Design n Several other factors influence the design of programming languages q q Computer architecture: Languages are developed around the prevalent computer architecture, known as the von Neumann architecture Programming methodologies: New software development methodologies (e. g. , object-oriented software development) led to new programming paradigms and by extension, new programming languages 70

Computer Architecture Influence n n Well-known computer architecture: Von Neumann Imperative languages, most dominant, Computer Architecture Influence n n Well-known computer architecture: Von Neumann Imperative languages, most dominant, because of von Neumann computers q q Data and programs stored in memory Memory is separate from CPU Instructions and data are piped from memory to CPU Basis for imperative languages n n n Variables model memory cells Assignment statements model piping Iteration is efficient Copyright © 2012 Addison-Wesley. All rights reserved. 1 -71

The von Neumann Architecture Copyright © 2009 Addison-Wesley. All rights reserved. 72 1 -72 The von Neumann Architecture Copyright © 2009 Addison-Wesley. All rights reserved. 72 1 -72

Computer Architecture n Von Neumann architecture → Imperative languages q q n n n Computer Architecture n Von Neumann architecture → Imperative languages q q n n n CPU executes instructions → operations, statements Operands reside in the memory cells → variables Values are stored back to memory → assignment Branching, jumping → goto, for, repeat, while loops (iteration) Discourages recursion, although it is natural. Functional languages are not efficient on Von Neumann architectures. Several attempts were made on new architectures for LISP (LISP machine, and Symbolics). They did not survive. 73

Fetch-Execute Cycle n n Execution of machine code program on a von Neumann architecture Fetch-Execute Cycle n n Execution of machine code program on a von Neumann architecture computer occurs in a process called the fetch-execute cycle Programs reside in memory but are executed in the CPU Each instruction to be executed must be moved from memory to CPU The address of the next instruction to be executed is maintained in a register called the program counter 74

Fetch-Execute cycle initialize the program counter repeat forever fetch the instruction pointed by the Fetch-Execute cycle initialize the program counter repeat forever fetch the instruction pointed by the program counter increment the program counter decode the instruction execute the instruction end repeat n n Program terminates when stop instruction is encountered Or, control transfers from user program to OS when the user program execution is complete 75

Programming Methodologies Influences n n 1950 s and early 1960 s: Simple applications; worry Programming Methodologies Influences n n 1950 s and early 1960 s: Simple applications; worry about machine efficiency Late 1960 s: People efficiency became important; readability, better control structures q q n Late 1970 s: Process-oriented to data-oriented q n structured programming top-down design and step-wise refinement data abstraction Middle 1980 s: Object-oriented programming q Data abstraction + inheritance + polymorphism Copyright © 2012 Addison-Wesley. All rights reserved. 1 -76

Language Categories n n Imperative q Central features are variables, assignment statements, and iteration Language Categories n n Imperative q Central features are variables, assignment statements, and iteration q Include languages that support object-oriented programming q Include scripting languages q Include the visual languages q Examples: C, Java, Perl, Visual BASIC. NET, C++ Functional q Main means of making computations is by applying functions to given parameters q Examples: LISP, Scheme, ML, F# Logic q Rule-based (rules are specified in no particular order) q Example: Prolog Markup/programming hybrid q Markup languages extended to support some programming q Examples: JSTL, XSLT Copyright © 2009 Addison-Wesley. All rights reserved. 77

Language Design Trade-Offs n Reliability vs. cost of execution q n Example: Java demands Language Design Trade-Offs n Reliability vs. cost of execution q n Example: Java demands all references to array elements be checked for proper indexing, which leads to increased execution costs Readability vs. writability Example: APL provides many powerful operators for arrays(and a large number of new symbols), allowing complex computations to be written in a compact program but at the cost of poor readability n Writability (flexibility) vs. reliability q Example: C++ pointers are powerful and very flexible but are unreliable Copyright © 2009 Addison-Wesley. All rights reserved. 78 1 -78

Implementation Methods n Compilation q q n Pure Interpretation q q n Programs are Implementation Methods n Compilation q q n Pure Interpretation q q n Programs are translated into machine language; includes JIT systems Use: Large commercial applications Programs are interpreted by another program known as an interpreter Use: Small programs or when efficiency is not an issue Hybrid Implementation Systems q q A compromise between compilers and pure interpreters Use: Small and medium systems when efficiency is not the first concern Copyright © 2012 Addison-Wesley. All rights reserved. 1 -79

Layered View of Computer The operating system and language implementation are layered over machine Layered View of Computer The operating system and language implementation are layered over machine interface of a computer Bare Machine: Internal memory and processor Macroinstructions: Primitive operations, or machine instructions such as those for arithmetic and logic operations Operating system: Higher level primitives than machine code, which provides system resource management, input and output operations, file management system, text and/or program editors… Virtual computers: provides interfaces to user at a higher level Copyright © 2009 Addison-Wesley. All rights reserved. 80

Compilation n Translate high-level program (source language) into machine code (machine language) Slow translation, Compilation n Translate high-level program (source language) into machine code (machine language) Slow translation, fast execution Compilation process has several phases: q q n 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 generation: machine code is generated Example: C, COBOL, Ada Copyright © 2012 Addison-Wesley. All rights reserved. 1 -81

Compilation n Source language: The language that the compiler translates n Lexical analyzer: gathers Compilation n Source language: The language that the compiler translates n Lexical analyzer: gathers the characters of the source program into lexical units (e. g. identifiers, special words, operators, punctuation symbols) Ignores the comments n Syntax analyzer: takes the lexical units, and use them to construct parse trees, which represent the syntactic structure of the program n Intermediate code generator: Produces a program at an intermediate level. Similar to assembly languages 82

Compilation n Semantic analyzer: checks for errors that are difficult to check during syntax Compilation n Semantic analyzer: checks for errors that are difficult to check during syntax analysis, such as type errors. n Optimization (optional): Used if execution speed is more important than compilation speed. n Code generator: Translates the intermediate code to machine language program. n Symbol table: serves as a database of type and attribute information of each user defined name in a program. Placed by lexical and syntax analyzers, and used by semantic analyzer and code generator. 83

Linker n n Most user programs require programs from the operating system, such as Linker n n Most user programs require programs from the operating system, such as input/output Before the machine language programs produced by a compiler can be executed, the required programs from the operating system must be found and linked to user programs The linking operation connects the user program to the system programs by placing the addresses of the entry points of the system programs in the calls to them in the user programs User programs must often be linked to previously compiled user programs that reside in libraries 84

Von Neumann Bottleneck n n n Connection speed between a computer’s memory and its Von Neumann Bottleneck n n n Connection speed between a computer’s memory and its processor determines the speed of a computer Program instructions often can be executed much faster than the speed of the connection; the connection speed thus results in a bottleneck Known as the von Neumann bottleneck; it is the primary limiting factor in the speed of computers Copyright © 2012 Addison-Wesley. All rights reserved. 1 -85

Pure Interpretation n n n No translation. Each statement is decoded and executed individually. Pure Interpretation n n n No translation. Each statement is decoded and executed individually. The interpreter program acts as a software simulation of a machine whose fetch-execute cycle deals with high level language program statements rather than machine instructions Advantages: q Easier implementation of programs q Debugging is easy (run-time errors can easily and immediately be displayed) Disadvantages: q Slower execution (10 to 100 times slower than compiled programs, and statement decoding is the major bottleneck) q Often requires more space Now rare for traditional high-level languages but available in some Web scripting languages (e. g. , Java. Script, PHP) Copyright © 2009 Addison-Wesley. All rights reserved. 86 1 -86

Pure Interpretation Process Copyright © 2009 Addison-Wesley. All rights reserved. 87 1 -87 Pure Interpretation Process Copyright © 2009 Addison-Wesley. All rights reserved. 87 1 -87

Compiler vs Interpreter How an English speaker can communicate to a French speaker 1. Compiler vs Interpreter How an English speaker can communicate to a French speaker 1. The English speaker employs an interpreter who translates the English sentences into French as they are spoken. The English speaker says a sentence in English, the interpreter hears it, translates it in his/her brain into French, and the speaks the sentence in French. This is repeated for each sentence. Progress is slow: there are pauses between sentences as the translation process takes place. 2. The English speaker writes down (in English) what needs to be said. The whole document is then translated into French, producing another piece of paper with the French version written on it. There are two factors to consider in this scenario: • There is a slight delay at the start as the English document needs to be translated in its entity before it can be read. • The translated document can be read at any time after that, and at the normal speed at which the French-speaker reads. 88

Compiler vs Interpreter A compiler translates a complete source program into machine code. The Compiler vs Interpreter A compiler translates a complete source program into machine code. The whole source code file is compiled in one go, and a complete, compiled version of the file is produced. This can be saved on some secondary storage medium (e. g. floppy disk, hard disk. . . ). This means that: n The program can only be executed once translation is complete n ANY changes to the source code require a complete recompilation. An interpreter, on the other hand, provides a means by which a program written in source language can be understood and executed by the CPU line by line. As the first line is encountered by the interpreter, it is translated and executed. Then it moves to the next line of source code and repeats the process. This means that: n The interpreter is a program which is loaded into memory alongside the source program n Statements from the source program are fetched and executed one by one n No copy of the translation exists, and if the program is to be re-run, it has to be interpreted all over again. 89

Compiler vs Interpreter n n Because the interpreter must be loaded into memory, there Compiler vs Interpreter n n Because the interpreter must be loaded into memory, there is less space available during execution; a compiler is only loaded into memory during compilation stage, and so only the machine code is resident in memory during run-time; Once we have a compiled file, we can re-run it any time we want to without having to use the compiler each time; With any interpreted language, however, we would have to re-interpret the program each time we wanted to run it; Machine code programs run more quickly than interpreted programs; However, it is often quicker and easier to make changes with an interpreted program than a compiled one, and as such development time may be reduced. 90

Hybrid Implementation Systems n n n A compromise between compilers and pure interpreters A Hybrid Implementation Systems n n n 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 Copyright © 2009 Addison-Wesley. All rights reserved. 91

Hybrid Implementation Systems n Examples q q q Perl programs are partially compiled to Hybrid Implementation Systems n Examples q q q 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 Machine) Typically, when used in that generic manner, the term Java compiler refers to a program which translates Java language source code into the Java Virtual Machine (JVM) bytecodes. The term Java interpreter refers to a program which implements the JVM specification and actually executes the bytecodes (and thereby running your program). 92

Q: What happens if there is a compiler and interpreter at the same time? Q: What happens if there is a compiler and interpreter at the same time? Which goes first?

Interpreter and compiler n If both interpreter and compiler exists for a language (e. Interpreter and compiler n If both interpreter and compiler exists for a language (e. g. , LISP) q q programs are first developed and debugged using the interpreter, then they are compiled to obtain fast executing programs. 94

Just-in-Time Implementation Systems n n n Initially translate programs to an intermediate language Then Just-in-Time Implementation Systems n n n Initially translate programs to an intermediate language Then compile the intermediate language of the subprograms into machine code when they are called Machine code version is kept for subsequent calls JIT systems are widely used for Java programs. NET languages are implemented with a JIT system In essence, JIT systems are delayed compilers Copyright © 2009 Addison-Wesley. All rights reserved. 95 1 -95

Preprocessors n n n Preprocessor macros (instructions) are commonly used to specify that code Preprocessors n n n Preprocessor macros (instructions) are commonly used to specify that code from another file is to be included A preprocessor processes a program immediately before the program is compiled to expand embedded preprocessor macros A well-known example: C preprocessor q expands #include, #define, and similar macros Copyright © 2012 Addison-Wesley. All rights reserved. 1 -96

Programming Environments n n A collection of tools used in software development, such as Programming Environments n n A collection of tools used in software development, such as text editors, linker, compiler, debugger, graphical interface, and interaction between these tools. Examples: q UNIX n n q Microsoft Visual Studio. NET n q An older operating system and tool collection Nowadays often used through a GUI (e. g. , CDE, KDE, or GNOME) that runs on top of UNIX A large, complex visual environment JCreator 97

History Most of the ideas of Modern PLs appear in four or five classic History Most of the ideas of Modern PLs appear in four or five classic languages: n Fortran (1956/8): Jump-based control structures (looping, conditionals) subroutines, arrays, formatted I/O n Cobol: Task-specific types for business applications, e. g. decimal arithmetic and strings n Algol (1960+): Lexical scoping, composite types(records), automated type-checking, high-level control structures, recursion n Lisp(1959): Functions yielding functions as return values, same notation for code and data, dynamic typing, recursion in lieu of iteration n Simula(1967): Information hiding, object oriented programming 98

Genealogy of PL 99 Genealogy of PL 99

“Hello World” in different languages http: //www. all-science-fairprojects. com/science_fair_projects_encyclopedia/Hello_world_program 100 “Hello World” in different languages http: //www. all-science-fairprojects. com/science_fair_projects_encyclopedia/Hello_world_program 100

Java public class Hello { public static void main(String[] args) { System. out. println( Java public class Hello { public static void main(String[] args) { System. out. println("Hello, world!"); } } 101

Assembly Language bdos start: lxi call msg$: end equ 0005 H ; BDOS entry Assembly Language bdos start: lxi call msg$: end equ 0005 H ; BDOS entry point mvi c, 9 ; BDOS function: output string d, msg$ ; address of msg bdos ret ; return to CCP db 'Hello, world!$' start 102

FORTRAN PROGRAM HELLO WRITE(*, 10) 10 FORMAT('Hello, world!') STOP END 103 FORTRAN PROGRAM HELLO WRITE(*, 10) 10 FORMAT('Hello, world!') STOP END 103

COBOL IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. DISPLAY COBOL IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. DISPLAY "Hello, world!". STOP RUN. 104

Ada with Ada. Text_Io; procedure Hello is begin Ada. Text_Io. Put_Line ( Ada with Ada. Text_Io; procedure Hello is begin Ada. Text_Io. Put_Line ("Hello, world!"); end Hello; 105

C #include <stdio. h> int main() { printf( C #include int main() { printf("Hello, world!n"); return 0; } 106

C++ #include <iostream> int main() { std: : cout << C++ #include int main() { std: : cout << "Hello, world!n"; } 107

C# using System; class Hello. World. App { public static void Main() { Console. C# using System; class Hello. World. App { public static void Main() { Console. Write. Line("Hello, world!"); } } 108

LISP (format t LISP (format t "Hello world!~%") 109

PERL print PERL print "Hello, world!n"; 110

Prolog write('Hello world'), nl. 111 Prolog write('Hello world'), nl. 111

HTML <!DOCTYPE HTML PUBLIC HTML Hello, world!

Hello, world!

112

Summary n The study of programming languages is valuable for a number of reasons: Summary n The study of programming languages is valuable for a number of reasons: q q q n Increase our capacity to use different constructs Enable us to choose languages more intelligently Makes learning new languages easier Most important criteria for evaluating programming languages include: q Readability, writability, reliability, cost n Major influences on language design have been machine architecture and software development methodologies n The major methods of implementing programming languages are: compilation, pure interpretation, and hybrid implementation Copyright © 2012 Addison-Wesley. All rights reserved. 1 -113