95
ISBN 0-321-33025-0 Chapter 1 Preliminaries

ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Embed Size (px)

Citation preview

Page 1: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

ISBN 0-321-33025-0

Chapter 1

Preliminaries

Page 2: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-2

Purpose of This Book

• To examine carefully • the underlying concepts of the various constructs

and capabilities of programming languages

Page 3: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-3

Chapter 1 Topics

• Reasons for Studying Concepts of Programming Languages

• Programming Domains• Language Evaluation Criteria• Influences on Language Design• Language Categories• Language Design Trade-Offs• Implementation Methods• Programming Environments

Page 4: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-4

Reasons for Studying Concepts of Programming Languages (1)

• Increased ability to express ideas– The language in which programmers develop software

places limits on the kinds of control structures, data structures, and abstraction they can use

• Awareness of a wider variety of programming language features can reduce such limitations in software development

– Languages constructs can be simulated in other languages that do not support those constructs directly;

• however, the simulation is often – less elegant– more cumbersome– less safe

Page 5: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-5

Reasons for Studying Concepts of Programming Languages (2)

• Improved background for choosing appropriate languages

• Increased ability to learn new languages– According to TIOBE, C, Java, and C++ were

the three most popular languages in use in Feb. 2015.

Page 6: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-6

Reasons for Studying Concepts of Programming Languages (3)

• Better understanding of significance of implementation– Program bugs– Performance

• Better use of languages that are already known

• Overall advancement of computing– Those in positions to choose languages

were not sufficiently familiar with programming language concepts

Page 7: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-7

Programming Domains (1)

• Scientific applications– Large number of floating point computations– Simple data structures– Fortran

• Originally developed by IBM in the 1950s • Business applications

– Facilities for • producing elaborate reports, • Storing decimal numbers and character data• The ability to specify decimal arithmetic operations

– COBOL• The initial version appeared in 1960

• Artificial intelligence– Symbols, consisting of names rather than numbers, are

manipulated– LISP

• Appeared in 1959

Page 8: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-8

Programming Domains (2)

• Systems programming– The operating system and all of the

programming support tools of a computer system are collectively known as its systems software.

• Systems software is used almost continuously and so must be efficient.

– A language for this domain must provide • fast execution• having low-level features that allow the

software interfaces to external devices to be written

– C• The Unix OS is written almost entirely in C

Page 9: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-9

Programming Domains (3)

• Web Software– Eclectic collection of languages: markup

(e.g., XHTML), scripting (e.g., PHP), general-purpose (e.g., Java)

Page 10: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-10

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: conform to specifications (i.e., performs to its specifications)

• Cost: the ultimate total cost

Page 11: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-11

Why Readability Is Important

• Maintenance was recognized as a major part of the software life cycle, particularly in terms of cost.

• Ease of maintenance is determined in large part by the readability of programs.

Page 12: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-12

Characteristics Contributing to the Readability - Simplicity• Overall simplicity

– A manageable set of features and constructs• Readability problems occur whenever the program’s author has

learned a different subset from that subset with which the reader is familiar.

– Few feature multiplicity (methods of doing the same operation)• For example, in Java the following ways could be used to increase

a integer variable count = count + 1 count += 1 count++ ++count

– Minimal operator overloading (a single operator symbol has more than one meaning)• Overloading may simplify a language by reducing the number of

operators; however, it can lead to reduced readability if users are allowed to create their own overloading and do not do it sensibly.

Page 13: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-13

Excessive Simplicity

• Simplicity improves readability; however, excessive simplicity may also reduce readability.– For example:

• The form and meaning of most assembly language statements are models of simplicity.

• This very simplicity, however, makes assembly language programs less readable. Because they lack more complex control statements, program structure is less obvious.

Page 14: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-14

Characteristics Contributing to the Readability – Control Statements• The presence of well-known control structures (e.g.,

while statement)• A program that can be read from top to bottom is much

easier to understand than a program that requires the reader to jump from one statement to some nonadjacent statement in order to follow the execution order.

Page 15: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-15

Characteristics Contributing to the Readability - Data Types and Structures• The presence of adequate facilities for defining data

structures• Example:

– If a language doesn’t have a Boolean type, then it may need to use a numeric type as an indicator flag

timeOut =1

– Comparing with a language providing Boolean type, the following state is much more readable

timeOut = true

Page 16: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-16

Characteristics Contributing to the Readability - Syntax Considerations (1)

• Identifier forms: flexible composition– Restricting identifiers to very short lengths detracts from

readability

Page 17: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-17

Characteristics Contributing to the Readability - Syntax Considerations (2)

• Special words– Program appearance and thus program readability are

strongly influenced by the forms of a language’s special words

– Whether the special words of a language can be used as names for program variables?

• methods of forming compound statements– C and its descendants use braces to specify compound

statements.– All of these languages suffer because statements groups are

always terminated in the same way, which makes it difficult to determine which group is being ended when an end or } appears.

Page 18: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-18

Characteristics Contributing to the Readability - Syntax Considerations (3)

• Form and meaning: self-descriptive constructs, meaningful keywords

Page 19: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-19

Evaluation Criteria: Writability

• Writability is a measure of how easily a language can be used to create programs for a chosen problem domain.

• Most of the language characteristics that affect readibility also affect writability.– This follows directly from the fact that the

process of writing a program requires the programmer frequently to reread the part of the program that is already written

Page 20: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-20

Writability Comparison between Two Different Languages

• It is simply not reasonable to compare the writability of two languages in the realm of a particular application when one was designed for that application and the other was not.

Page 21: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-21

Characteristics Contributing to the Writability - Support for Abstraction• Abstraction - the ability to define and

use complex structures or operations in ways that allow details to be ignored

• Programming languages can support two distinct categories of abstraction:– Process– Data

Page 22: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-22

Process Abstraction

• A simple example of process abstraction is the use of a subprogram to implement a sort of algorithm that is required several times in a program.

Page 23: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-23

Data Abstraction (1) [Wikipedia]

• Data abstraction enforces a clear separation between the abstract properties of a data type and the concrete details of its implementation.

• The abstract properties are those that are visible to client code that makes use of the data type—the interface to the data type—while the concrete implementation is kept entirely private, and indeed can change, for example to incorporate efficiency improvements over time.

• The idea is that such changes are not supposed to have any impact on client code, since they involve no difference in the abstract behavior.

Page 24: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-24

Data Abstraction (2)

• For example, one could define an abstract data type called lookup table which uniquely associates keys with values, and in which values may be retrieved by specifying their corresponding keys.

• Such a lookup table may be implemented in various ways: as – a hash table– a binary search tree or– even a simple linear list of (key:value) pairs.

• As far as client code is concerned, the abstract properties of the type are the same in each case.

Page 25: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-25

Data Abstraction (3)

• A binary tree– Fortran 77 – use integer arrays to implement– C++ and Java – use a class with two pointers

(or references) and an integer

Page 26: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-26

Characteristics Contributing to the Writability - Expressivity• A set of relatively convenient ways of

specifying operations– Example:

• the inclusion of for statement in many modern languages makes writing counting loops easier than with the use of while.

Page 27: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-27

Evaluation Criteria: Reliability

• A program is said to be reliable if it performs to its specifications under all conditions.

Page 28: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-28

Characteristics Contributing to the Reliability – Type Checking• Testing for type errors in a given program, either by the compiler or

during program execution.– Run-time type checking is expensive– Compile-time type checking is more desirable– The earlier errors in programs are detected, the less expensive it is to make the

required repairs– Example

void greater_than(unsigned int a, int b){ if(a<b) printf("a<b\n"); else printf("a>b\n");}bar(){ greater_than(-1,2);}

Page 29: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-29

Characteristics Contributing to the Reliability – Exception Handling

• Exception handling– Intercept run-time errors and– take corrective measures and– then continue the corresponding program’s

execution

Page 30: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-30

Characteristics Damaging the Reliability – Aliasing

• Presence of two or more distinct referencing methods for the same memory location

• It is now widely accepted that aliasing is a dangerous feature in a programming language

• Most programming languages allow some kind of aliasing – – for example, two pointers set to point to the

same variable.

Page 31: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-31

Characteristics Contributing to the Reliability – Readability and Writability

• Readability and writability– A language that does not support “natural”

ways of expressing an algorithm will necessarily use “unnatural” approaches, and hence reduced reliability

Page 32: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-32

Evaluation Criteria: Cost

• Training programmers to use language• Writing programs • Compiling programs• Executing programs• Language implementation system:

availability of free compilers• Reliability: poor reliability leads to high

costs• Maintaining programs

Page 33: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-33

Evaluation Criteria: Others

• 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

Page 34: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-34

Influences on Language Design

• 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

Page 35: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-35

Von Neumann Architecture

• Most of the popular languages of the past 50 years have been designed around the prevalent computer architecture: Von Neumann architecture

• These language are called imperative languages.– Data and programs are stored in the same memory– Memory is separate from CPU– Instructions and data are transmitted from memory to CPU– Results of operations in the CPU must be moved back to

memory

• Nearly all digital computers built since the 1940s have been based on the von Neumann architecture

Page 36: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-36

The Motherboard of a Computer

Page 37: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-37

The von Neumann Architecture

Page 38: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-38

Central Features of Imperative Languages

• Variables: model memory cells• Assignment statements: model piping• Iteration is fast on von Neumann

computers because instructions are stored in adjacent cells of memory and repeating the execution of a section of code requires only a simple branch instruction

Page 39: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-39

Program Execution on a Von Neumann Computer

• The execution of a machine code program on a von Neumann architecture computer occurs in a process called the fetch-execute cycle.

• Each instruction to be executed must be moved from memory to the processor.

• The address of the next instruction to be executed is maintained in a register called the program counter.

Page 40: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-40

Fetch-execute-cycle (on a von Neumann Architecture)

initialize the program counter

repeat forever

fetch the instruction pointed by the counter

increment the counter

decode the instruction

execute the instruction

end repeat

P.S.: the ``decode the instruction’’ step in the algorithm means the instruction is examined to determine what action it specifies.

Page 41: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-41

Functional Language Programs Executed on a Von Neumann Machine

• A functional language is one in which the primary means of computation is applying functions to given parameters.

• Programming can be done in a functional language– without the kind of variables that are used in imperative

languages– without assignment statements and – without iteration.

• Although many computer scientists have expounded on the myriad benefits of functional languages, it is unlikely that they will displace the imperative language until a non-von Neumann computer is designed that allows efficient execution of programs in functional languages

Page 42: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-42

Evolution of Programming Methodologies (1)

• 1950s and early 1960s: – simple applications– worry about machine efficiency

• 1970s: – hardware costs decreased– programmer costs increased – larger and more complex problems were being solved by

computers – Emphasis:

• structured programming • top-down design and step-wise refinement

– Deficiency:• Incompleteness of type checking

Page 43: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-43

Evolution of Programming Methodologies (2)

• Late 1970s: – shift from procedure-oriented to data-oriented– emphasize data design, focusing on the use of abstract

data types to solve problems– most languages designed since the late 1970s support

data abstraction• Middle 1980s: Object-oriented programming

– data abstraction • encapsulates processing with data objects• controls access to data

– Inheritance• enhances the potential reuse of existing software, thereby

providing the possibility of significant increases in software development productivity

– dynamic method binding• allow more flexible use of inheritance• overloaded method• overridden method

Page 44: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-44

All of the evolutionary steps in software development methodologies led to new language constructs to

support them.

Page 45: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-45

Programming 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++

Page 46: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-46

Should Languages Support Object-oriented Programming Form a Separate Language Category?

• The author of this book claimed that he does not consider languages that support object-oriented programming to form a separate category of language, because, both imperative languages and function languages support object-oriented programming.

Page 47: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-47

Subcategories of Imperative Languages

• Visual languages:– e.g. Visual BASIC and Visual BASIC .NET– These languages include capabilities for drag-

and-drop generation of code segments.– Once called fourth-generation Languages– Provide a simple way to generate graphical

user interfaces to programs.

• Scripting Languages– e.g. Perl, JavaScript, and Ruby

Page 48: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-48

A Typical Session in Microsoft Visual Basic 6

Page 49: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-49

Execution Order of Programs

• In an imperative language, – an algorithm is specified in great detail and– the specific order of execution of the

instructions or statements must be included.

• In a rule-based language, however, rules are specified in NO particular order– The language implementation system must

choose an execution order that produces the desired result.

Page 50: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-50

Markup Programming hybrid languages

• not a programming language, but used to specify the layout of information in Web documents– examples: XHTML, XML– However, some programming capability has

crept into some extensions to XHTML and XML

Page 51: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-51

Benefits of Modular Design

• Modular design brings with it great productivity improvements. – First of all, small modules can be coded

quickly and easily. – Secondly, general purpose modules can be re-

used, leading to faster development of subsequent programs.

– Thirdly, the modules of a program can be tested independently, helping to reduce the time spent debugging.

Page 52: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-52

Language Design Trade-offs

• The programming language evaluation criteria provide a framework for language design; however, that framework is self-contradictory.

Page 53: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-53

Instances of Language Design Trade-Offs

• Reliability vs. cost of execution– Conflicting criteria– Example: Java demands all references to array

elements be checked to ensure that the index is in it legal ranges but that leads to increased execution costs

• Readability vs. writability– Another conflicting criteria– Example: APL provides many powerful operators (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– Another conflicting criteria– Example: C++ pointers are powerful and very flexible

but not reliably used

Page 54: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-54

Primary Components of a Computer

• Internal Memory– Used to store data and program

• Processor – a collection of circuits that provides a

realization of a set of primitive operations, or machine instructions, such as those for arithmetic and logic operations.

Page 55: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-55

The Machine Language of a Computer

• Is its set of instructions.• Is the ONLY language that the hardware of

the computer can understand directly.• Provide the most commonly needed

primitive operations.• Programs written by high level languages

require system software (language implementation systems) to translate them into corresponding machine language versions.

Page 56: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-56

Operating Systems

• Supply Higher-level primitives than those of the machine language.

• These primitives provide– system resource management– input and output operations– a file management system– text and/or program editors– a variety of other commonly needed functions

Page 57: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-57

Language Implementation Systems and an Operating Systems

• Because language implementation systems need many of the operating system facilities, they utilize the operating system to do their work rather than develop their own code to interact with the hardware directly.

Page 58: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-58

Implementation Methods

• Compilation– Programs are translated into machine

language, which can be executed directly on the computer

• Pure Interpretation– Programs are interpreted by another

program known as an interpreter

• Hybrid Implementation Systems– A compromise between compilers and

pure interpreters

Page 59: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-59

Compilation

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

• Slow translation, fast execution

Page 60: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-60

Phases of Compilation Process

• lexical analysis: gathers the characters of the source program into lexical units.– lexical units: identifiers, special words, operators and

punctuation symbols

• syntax analysis: transforms lexical units into parse trees which represent the syntactic structure of program

• intermediate code generation: translate a source program into an intermediate language one– semantics analysis: check for errors that are difficult if

not impossible to detect during syntax analysis, such as type errors.

• code generation: machine code is generated

Page 61: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-61

Optimization

• Improve programs (usually in their intermediate code version) by making them smaller or faster or both, is often an optional part of compilation.

• Some compilers are incapable of doing any significant optimization.

• Optimization may – omit some code in your program– change the execution order of code in your program

• P.S.: Sometimes, especially when synchronization between processes is required, the above results may create some bugs in your programs which cannot be detected by just checking the source code.

Page 62: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-62

Optimization vs. Reliability

int a;int foo(){

a=1; if(a>0)

a=3; else a=-1; return a;}

int a;

foo()

{ a=3;

return a;

}

If a is not a volatile variable, the optimization improve performance; otherwise, it introduces race condition problem.

optimization

a:

memoryprocess 1

process 2

Page 63: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-63

Symbol Table

• The symbol table serves as a database for the compilation process.

• The primary contents of the symbol table are – the type and attribute information of each

user-defined name in the program. • P.S.: This information is placed in the symbol table

by the lexical and syntax analyzers and is used by the semantic analyzer and the code generator.

Page 64: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-64

The Compilation Process

Page 65: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-65

User Program Supporting Code

• The machine language generated by a compiler can be executed directly on the hardware; however, it must nearly always be run along with some other code.

• Most user programs also require functions from the OS.

• Among the most common of these are functions for input and output.

Page 66: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-66

Linking Operation

• Before the machine language programs produced by a compiler can be executed, the required functions from the OS must be found and linked to the user program.

• The linking operation connects the user program to the system functions by placing the addresses of the entry points of the system functions in the calls to them in the user program.

Page 67: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-67

Combine a User Program and All Supporting Functions Together

main()

{

printf()

}

main:

call add_of_printf

0x40ffffff

printf:

main:

call

compilation loading

0x40ffffff

address space of a process

linking

Page 68: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-68

Linking Operation

• Load module (executable image): the user and system code together

• Linking and loading (linking): the operation of collecting system functions and linking them to user programs– Accomplished by a systems program called a

linker

Page 69: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-69

Libraries

• In addition to system functions, user programs must often be linked to previously compiled user functions that reside in libraries.

• The linker not only links a given program to system functions, it may also link it to other user functions.

Page 70: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-70

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 a lot faster than the above connection speed; the connection speed thus results in a bottleneck

• Known as von Neumann bottleneck; it is the primary limiting factor in the speed of computers

Page 71: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-71

Interpreter

• Programs are interpreted by another program called an interpreter, with no translation whatever.

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

• This software simulation obviously provides a virtual machine for the language.

Page 72: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-72

Advantages of Interpretation

• Allowing easy implementation of many source-level debugging operations, because all run-time error messages can refer to source-level unit.– For example, if an array is found to be out of

rang, the error message can easily indicate the source line and the name of the array.

Page 73: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-73

Disadvantages of Interpretation (1)• Slower execution (10 to 100 times slower

than compiled programs)– The decoding of the high-level language

statements are far more complex than machine language instruction.

– Regardless of how many times a statement is executed, it must be decoded every time.

– Therefore, statement decoding, rather than the connection between the processor and memory, is the bottleneck of a pure interpreter.

Page 74: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-74

Disadvantages of Interpretation (2)• Often requires more space.

– In addition to the source program, the symbol table must be present during interpretation

– The source program may be stored in a form designed for easy access and modification rather than one that provides for minimal size

Page 75: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-75

Popularity of Interpretation

• Some simple early languages of the 1960s (APL, SNOBOL, and LISP) were purely interpreted.

• By the 1980s, the approach was rarely used on high-level languages.

• In recent years, pure interpretation has made a significant comeback with some Web scripting languages, such as JavaScript and PHP, which are now widely used.

Page 76: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-76

Pure Interpretation Process

Page 77: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-77

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

Page 78: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-78

Example (1)

• Perl programs – are partially compiled to detect errors before

interpretation to simplify the interpreter.

Page 79: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-79

Example (2)

• Initial implementations of Java – initial implementations of Java were all hybrid– its intermediate form, byte code, provides portability

to any machine that has a byte code interpreter and the Java class library.

– There are now systems that translate Java byte code into machine code for faster execution.

Page 80: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Java Bytecode Example [wikipedia]

1-80

Java code (*.java)

Java bytecode (*.class)

translated by a Java compiler

javac

Page 81: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Java Virtual Machine [Wikipedia]

• A Java virtual machine (JVM) [Wikipedia][zhebel] is an abstract computing machine.– p.s.: computing machine ≡ computer

• There are three notions of the JVM:– specification, – implementation, – and instance.

1-81

Page 82: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Java Virtual Machine Specification [Wikipedia]

• The specification is a book that formally describes what is required of a JVM implementation.

• Having a single specification ensures all implementations are consistent.

1-82

Page 83: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Java Virtual Machine Implementation [Wikipedia]

• A JVM implementation is a computer program that implements requirements of the JVM specification.

1-83

Page 84: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Java Virtual Machine Instance[Wikipedia]

• An instance of the JVM is a process that executes a computer program compiled into Java bytecode.

1-84

Page 85: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Java Runtime Environment [Wikipedia]

• The Oracle Corporation owns the Java trademark.

• Oracle distributes the Java Virtual Machine implementation HotSpot together with an implementation of the Java Class Library.

• The JVM and the Java class library are named Java Runtime Environment (JRE).

1-85

Page 86: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

The java Command [oracle]

• The java command starts a Java application. – It does this by starting a Java runtime

environment, loading a specified class, and calling that class's main method.

1-86

Page 87: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

Java Class Library[Wikipedia]

• The Java Class Library (JCL) is a set of dynamically loadable libraries that Java applications can call at run time.

• Because the Java Platform is not dependent on a specific operating system, applications cannot rely on any of the platform-native libraries.

• Instead, the Java Platform provides a comprehensive set of standard class libraries, containing the functions common to modern operating systems.

1-87

Page 88: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-88

Hybrid Implementation Process

Page 89: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-89

Just-in-Time (JIT) Implementation Systems

• Initially translate programs to an intermediate language

• Then during execution, it compiles intermediate language methods 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

Page 90: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-90

Preprocessors

• A preprocessor is a program that processes a program immediately before the program is compiled.

Page 91: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-91

Preprocessor Instructions

• Preprocessor instructions are embedded in programs.

• Preprocessor instructions are commonly used to specify that code from another file is to be included.– For example, the following C preprocessor

instruction #include myLib.c, causes the preprocessor to copy the contents of myLib.c into the program at the position of the #include myLib.c.

Page 92: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-92

More Preprocessor instructions

• Other preprocessor instructions are used to define symbols to represent expressions.– For example, one could use

#define max(A, B) ((A) > (B) ? (A): (B))

to determine the largest of two given expressions.

Page 93: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-93

Programming Environments

• The collection of tools used in software development

• This collection may consist of only – a file system– a text editor– a linker– a compiler

• Or a programming environment may include a large collection of integrated tools, each accessed through a uniform user interface.

Page 94: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-94

Programming Environment Examples

• UNIX– Provides a wide array of powerful support tools for

software production and maintenance in a variety of languages.

– Nowadays often used through a GUI (e.g., CDE, KDE, or GNOME) that run on top of UNIX

• Borland JBuilder– An integrated development environment for Java

• Microsoft Visual Studio.NET– A large and elaborate collection of software

development tools, all used through a windowed interface.

– Used to program in C#, Visual BASIC.NET, Jscript, J#, or C++

Page 95: ISBN 0-321-33025-0 Chapter 1 Preliminaries. 1-2 Purpose of This Book To examine carefully the underlying concepts of the various constructs and capabilities

1-95

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, – hybrid implementation