Presentation is loading. Please wait.

Presentation is loading. Please wait.

BBM 301 –Programming Languages

Similar presentations


Presentation on theme: "BBM 301 –Programming Languages"— Presentation transcript:

1 BBM 301 –Programming Languages

2 General Information Webpage: http://web.cs.hacettepe.edu.tr/~bbm301
Teaching Assistants: Yasin Şahin Çağdaş Baş Textbook: Robert W. Sebesta, “Concepts of Programming Languages”, Pearson, Ninth Edition. Time and Place: Tuesdays 9:30 – 12:15 Section D8, Section D9 Communication: We will use Piazza

3 Topics Introduction to Programming Languages Syntax and Semantics Lex
Yacc Conflicts Names, Bindings, Scope Data Types Expressions, Assignments Control Statements Subprograms Implementing Subprograms Functional Languages Lexical analyzer yet another compiler compiler (parser generator)

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

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

6 Lecture 1: Introduction to Programming Languages

7 Why Study Programming Languages?
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.

8 New Languages will Keep Coming
8

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

10 ENIAC (1946, University of Philadelphia)
ENIAC program for external ballistic equations: Atanasoff-Berry Computer (IOWA State College) 1946 ENIAC was unveiled in Philadelphia. ENIAC represented still a stepping stone towards the true computer, for differently than Babbage, Eckert and Mauchly, although they knew that the machine was not the ultimate in the state-of-the-art technology, completed the construction. ENIAC was programmed through the rewiring the interconnections between the various components and included the capability of parallel computation. ENIAC was later to be modified into a stored program machine, but not before other machines had claimed the claim to be the first true computer was the year in which the first computer meeting took place, with the University of Pennsylvania organizing the first of a series of "summer meetings" where scientists from around the world learned about ENIAC and their plans for EDVAC. Among the attendees was Maurice Wilkes from the University of Cambridge who would return to England to build the EDSAC. Later that year Eckert and Mauchly, in a patent dispute with the University of Pennsylvania, left the University to establish the first computer company -- Electronic Control Corp. with a plan to build the Universal Automatic Computer (UNIVAC). After many crises they built the BINAC for Northrup Aviation, and were taken over by Remington-Rand before the UNIVAC was completed. At the same time the Electronic Research Associates (ERA) was incorporated in Minneapolis and took their knowledge of computing devices to create a line of computers; later ERA was also assimilated into Remington-Rand. Slide from Ras Bodik 10 10

11 Programming the ENIAC Slide from Ras Bodik 11 11

12 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 1946 ENIAC was unveiled in Philadelphia. ENIAC represented still a stepping stone towards the true computer, for differently than Babbage, Eckert and Mauchly, although they knew that the machine was not the ultimate in the state-of-the-art technology, completed the construction. ENIAC was programmed through the rewiring the interconnections between the various components and included the capability of parallel computation. ENIAC was later to be modified into a stored program machine, but not before other machines had claimed the claim to be the first true computer was the year in which the first computer meeting took place, with the University of Pennsylvania organizing the first of a series of "summer meetings" where scientists from around the world learned about ENIAC and their plans for EDVAC. Among the attendees was Maurice Wilkes from the University of Cambridge who would return to England to build the EDSAC. Later that year Eckert and Mauchly, in a patent dispute with the University of Pennsylvania, left the University to establish the first computer company -- Electronic Control Corp. with a plan to build the Universal Automatic Computer (UNIVAC). After many crises they built the BINAC for Northrup Aviation, and were taken over by Remington-Rand before the UNIVAC was completed. At the same time the Electronic Research Associates (ERA) was incorporated in Minneapolis and took their knowledge of computing devices to create a line of computers; later ERA was also assimilated into Remington-Rand. Slide from Ras Bodik 12 12

13 Reasons for Studying Concepts of Programming Languages
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

14 Increased Ability to Express Ideas
Natural languages: 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.

15 Increased Ability to Express Ideas
Programming Languages: 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.

16 Improved background for choosing appropriate languages
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.

17 Improved background for choosing appropriate languages
However another language may be more appropriate to the nature of the project. 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. If you are familiar with the other languages, particularly the features in those languages, you can choose better languages. 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….”

18 Increased ability to learn new languages
Programming languages are still evolving Many languages are very new, and new languages can be added in time. If you know the programming language concepts you will learn other languages much easier compared to programmers knowing only one or two languages. For example, if you know concept of object oriented programming, it is easier to learn C++ after learning Java Just like natural languages, 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 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

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

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

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

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

23 Better understanding of significance of implementation
The best programmers are the ones having at least understanding of how things work under the hood 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 Cost optimization; e.g. recursion is slow

24 Better use of languages that are already known
Many programming languages are large and complex 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.

25 Overall advancement of computing
New ways of thinking about computing, new technology, hence need for new appropriate language concepts Not to repeat history 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.

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

27 Ability to Design New Languages
You may need to design a special purpose language to enter the commands for a software that you develop. 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.

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

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

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

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

32 Evaluation Criteria: Readability
Overall simplicity A manageable set of features and constructs Minimal feature multiplicity Minimal operator overloading Orthogonality A relatively small set of primitive constructs can be combined in a relatively small number of ways Every possible combination is legal Data types Adequate predefined data types Syntax considerations Identifier forms: flexible composition Special words and methods of forming compound statements Form and meaning: self-descriptive constructs, meaningful keywords Copyright © 2009 Addison-Wesley. All rights reserved. 1-32 32

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

34 Overall Simplicity Minimal operator overloading
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: using + for integer and floating point addition is acceptable and contributes to simplicity 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 Assembly languages: Lack the complex control statements, so program structure is less obvious

35 Orthogonality 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: 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

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

37 Orthogonality VAX instruction design is orthogonal
Because a single instruction can use either registers or memory cells as the operands IBM design is not orthogonal 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

38 Orthogonality 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 A function cannot return a record (only unstructured types allowed), A file must be passed as a var parameter, Formal parameter types must be named (cannot be type descriptions) Compound statements are formed by begin-end pair, except repeat-until C is not an orthogonal language, because records (structs) can be returned from functions but arrays cannot a member of a structure can be any type but not void or structure of the same type a member of an array can be any type but not void or function

39 Orthogonality Too much orthogonality can cause problems as well:
ALGOL68 is the most orthogonal language. 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 (A<B) then C else D) := 3 This extreme form of orthogonality leads to unnecessary complexity

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

41 Control Statements 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.

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

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

44 Data Types and Structures
Facilities for defining data types and data structures are helpful for readability 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 An array of record type is more readable than a set of independent arrays

45 Syntax considerations
Identifier Forms: restricting identifier length is bad for readability. Example: FORTRAN77 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.

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

47 Syntax Considerations
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

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

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

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

51 Simplicity and orthogonality
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.

52 Support for abstraction
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: process data

53 Process abstraction 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 e.g. sort algorithm

54 Data abstraction As an example of data abstraction, a tree can be represented more naturally using pointers in nodes. In FORTRAN77, 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.

55 Expressivity 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

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

57 Reliability Reliable: it performs to its specifications under all conditions Type checking Testing for type errors Exception handling Intercept run-time errors and take corrective measures Aliasing Presence of two or more distinct referencing methods for the same memory location Readability and writability 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.

58 Type Checking 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

59 Type Checking 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. Current version required all parameters to be type-checked Java requires checks of the types of nearly all variables and expressions at compile time.

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

61 Type checking 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.

62 Exception Handling The ability of a program
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

63 Aliasing 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 two different variables can refer to the same memory cell

64 Readability and Writability
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.

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

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

67 Cost 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. 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

68 Cost 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. 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.

69 Other Criteria for Evaluation
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

70 Influence on Language Design
Several other factors influence the design of programming languages 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

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

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

73 Computer Architecture
Von Neumann architecture → Imperative languages 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.

74 Fetch-Execute Cycle 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

75 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 Program terminates when stop instruction is encountered Or, control transfers from user program to OS when the user program execution is complete

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

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

78 Language Design Trade-Offs
Reliability vs. cost of execution 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 Writability (flexibility) vs. reliability Example: C++ pointers are powerful and very flexible but are unreliable Copyright © 2009 Addison-Wesley. All rights reserved. 1-78 78

79 Implementation Methods
Compilation Programs are translated into machine language; includes JIT systems Use: Large commercial applications Pure Interpretation Programs are interpreted by another program known as an interpreter Use: Small programs or when efficiency is not an issue Hybrid Implementation Systems 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.

80 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

81 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 Example: C, COBOL, Ada Copyright © 2012 Addison-Wesley. All rights reserved.

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

83 Compilation Semantic analyzer: checks for errors that are difficult to check during syntax analysis, such as type errors. Optimization (optional): Used if execution speed is more important than compilation speed. Code generator: Translates the intermediate code to machine language program. 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.

84 Linker 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

85 Von Neumann Bottleneck
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.

86 Pure Interpretation 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: Easier implementation of programs Debugging is easy (run-time errors can easily and immediately be displayed) Disadvantages: Slower execution (10 to 100 times slower than compiled programs, and statement decoding is the major bottleneck) Often requires more space Now rare for traditional high-level languages but available in some Web scripting languages (e.g., JavaScript, PHP) Copyright © 2009 Addison-Wesley. All rights reserved. 1-86 86

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

88 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.

89 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: The program can only be executed once translation is complete 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: The interpreter is a program which is loaded into memory alongside the source program Statements from the source program are fetched and executed one by one No copy of the translation exists, and if the program is to be re-run, it has to be interpreted all over again.

90 Compiler vs Interpreter
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.

91 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 Copyright © 2009 Addison-Wesley. All rights reserved. 91

92 Hybrid Implementation Systems
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 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).

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

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

95 Just-in-Time Implementation Systems
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. 1-95 95

96 Preprocessors 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 expands #include, #define, and similar macros Copyright © 2012 Addison-Wesley. All rights reserved.

97 Programming Environments
A collection of tools used in software development, such as text editors, linker, compiler, debugger, graphical interface, and interaction between these tools. Examples: UNIX 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 Microsoft Visual Studio.NET A large, complex visual environment JCreator

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

99 Genealogy of PL

100 “Hello World” in different languages

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

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

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

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

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

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

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

108 C# using System; class HelloWorldApp { public static void Main()
Console.WriteLine("Hello, world!"); }

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

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

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

112 HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
" <html> <head> <title>Hello, world!</title> <meta http-equiv="Content-Type“ content="text/html; charset=UTF-8"> </head> <body> <p>Hello, world!</p> </body> </html>

113 Summary The study of programming languages is valuable for a number of reasons: 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: Readability, writability, reliability, cost Major influences on language design have been machine architecture and software development methodologies The major methods of implementing programming languages are: compilation, pure interpretation, and hybrid implementation Copyright © 2012 Addison-Wesley. All rights reserved.


Download ppt "BBM 301 –Programming Languages"

Similar presentations


Ads by Google