Upload
sayed-hasan
View
214
Download
0
Embed Size (px)
Citation preview
7/31/2019 332 Ch01
1/48
ISBN 0-321-33025-0
Chapter 1:Preliminaries
7/31/2019 332 Ch01
2/48
.ITCS332 by Dr. Abdel Fattah Salman 1-2
Chapter 1 Topics
Reasons for Studying Concepts of ProgrammingLanguages
Programming Domains Language Evaluation Criteria
Influences on Language Design Language Categories
Language Design Trade-Offs Implementation Methods Programming Environments
7/31/2019 332 Ch01
3/48
.ITCS332 by Dr. Abdel Fattah Salman 1-3
Reasons for Studying Concepts of Programming Languages
Benefits of studying programming concepts course: Increased ability to express ideas
Programming languages place limits on the kind of control
structures, data structures, thus the forms of algorithmsconstructed are limited. Language feature can be integrated or simulated . The fact that many features can be simulated in other language does
not lessen the importance of designing languages with best features.Better to use integrated features than simulated.
Improved background for choosing appropriate languages Professional programmers have learned 1 or 2 languages on their
own or through in-house training. When given a choice of languages for a new project, they continue
to use the one they are most familiar even it is not suited to the newproject.
If they were familiar with the features of other languages, theywould be in a better position to make correct choice.
7/31/2019 332 Ch01
4/48
.ITCS332 by Dr. Abdel Fattah Salman 1-4
Reasons for Studying Concepts of Programming Languages
Increased ability to learn new languages The process of learning a new language can be lengthy and
difficult for one who is comfortable with 1 or 2 languages. When a thorough understanding of the fundamental conceptsof languages is acquired, it becomes easier to see how theseconcepts are incorporated in the language design in general.
For example , one who understand the concepts of OOP willhave it easier to learn Java than those who do not have anyknowledge of OOP.
Better understanding of significance of implementation An understanding of implementation issues leads to an
understanding of why are designed the way they are. This knowledge leads to the ability to use the language more
efficiently.
7/31/2019 332 Ch01
5/48
.ITCS332 by Dr. Abdel Fattah Salman 1-5
Reasons for Studying Concepts of Programming Languages
We become better programmers by understanding the choicesamong language constructs and consequences of thosechoices.
Certain kinds of bugs can be detected and fixed only byprogrammers who know implementation details. It allows us to visualize how a computer executes various
language constructs. This leads to understanding theefficiency of various constructs. For example , programmerswho know little about how recursion is implemented do notknow that a recursive algorithm is slower than an equivalentiterative algorithm .
Better use of known languages Contemporary languages are large and complex. It is uncommon for programmers to use ALL features of those
languages. Programmers can learn unknown and unused features of
languages
7/31/2019 332 Ch01
6/48.ITCS332 by Dr. Abdel Fattah Salman 1-6
Reasons for Studying Concepts of Programming Languages
Overall advancement of computing It is usually possible to determine why a particular language
became familiar.
It might be concluded that a language became widely used notbecause it is the best available, but because those in positionto choose languages were not sufficiently familiar with
programming language concepts. For example , many people believe it would have been better
if ALGOL 60 had replaced FORTRAN, because it was moreelegant and had much better control statements thanFORTRAN. That did not is due software managers of thattime did not clearly understand the conceptual design of ALGOL 60. They found its description difficult to read and to
understand. They did not appreciate the benefits of block structure, recursion, and well-structured control statements.
7/31/2019 332 Ch01
7/48.ITCS332 by Dr. Abdel Fattah Salman 1-7
Programming DomainsComputers are applied for different areas: from controlling nuclear plants tovideo games and mobile phones . Computer applications are classified into 5categories:
Scientific applications
Scientific applications have simple data structures but require large numberof floating point computations. The most common data structures are counting loops and selections Fortran
Business applications Business applications require producing reports, use decimal numbers andcharacters
COBOL
With the advent of PCs: Spreadsheets and Databases Artificial intelligence AI applications are characterized by use of symbols rather than numbers Symbolic computation is better done with linked lists than arrays.
The first widely used language for AI is LISP. An alternative language for AI applications was used in 1970s: Logicprogramming using Prolog and Scheme .
7/31/2019 332 Ch01
8/48.ITCS332 by Dr. Abdel Fattah Salman 1-8
Programming Domains Systems programming
System software involves OS and all programming support tools. System software is used continuous and needs to be efficient so it must
have low-level features. Some computer manufacturers had developed special machine-oriented
high-level languages for system software: PL/S from IBM, BLISS fromDigital
UNIX OS was written almost entirely in C
Web Software The Web is supported by a collection of languages: markup (e.g.,
XHTML), scripting (e.g., PHP), general-purpose (e.g., Java). XHTML provides a way of embedding presentation instructions in the
pages of information: text, images, sound, or animation. The dynamic web content requires embedding programming code in an
XHTML document. Such code is in the form of a scripting language suchas JavaScript or PHP.
Alternatively, the XTHML document can request the execution of aseparate program on the Web server to provide dynamic content.
7/31/2019 332 Ch01
9/48.ITCS332 by Dr. Abdel Fattah Salman 1-9
Language Evaluation Criteria
The purpose of this course: To examine carefully the concepts of the various constructs and
capabilities of PLs. To evaluate these features, focusing on their impact on the softwaredevelopment process including maintenance.
To do this , we need a set of criteria: Readability : means the ease with which programs can be read
and understood
Writability : means the ease with which a language can be usedto create programs Reliability : conformance to specifications (i.e., performs to its
specifications under all conditions) Cost : the ultimate total cost of a programming language
7/31/2019 332 Ch01
10/48.ITCS332 by Dr. Abdel Fattah Salman 1-10
Evaluation Criteria: ReadabilityThe readability of a PL is affected by the following features: Overall simplicity
A manageable set of features and constructs
Few feature multiplicity (means of doing the same operation) 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
Control statements The presence of well-known control structures (e.g., while statement)
Data types and structures The presence of adequate facilities for defining data structures Syntax considerations
Identifier forms: flexible composition
Special words and methods of forming compound statements Form and meaning: self-descriptive constructs, meaningful keywords
7/31/2019 332 Ch01
11/48.ITCS332 by Dr. Abdel Fattah Salman 1-11
Evaluation Criteria: Readability-Overall simplicity
The following describe features that contribute to the readability of aprogramming language:
Overall simplicity The first complicating feature is a large # of basic constructs is moredifficult to learn than one with a small # of them. A manageable set of
features and constructs. Programmers often learn and use a subset of the large language and
ignore other features. Readability problems occur whenever the programs author used a
different subset from that known to the reader. The second complicating feature is feature multiplicity : means having
more than one way of doing the same operation. a=a+1; a+=1; a++; ++a.
The third complicating feature is operator overloading : a single operatorhas more than one meaning. Some overloadings are useful and simplify the language by reducing
the # of operators: Using + for both integer and real additions.
Some overloadings are harmful and make the construct confusing:using + between 2 vector operands to mean their respective elements.
7/31/2019 332 Ch01
12/48.ITCS332 by Dr. Abdel Fattah Salman 1-12
Evaluation Criteria: Readability-Orthogonality
Orthogonality means: (Orthogonal vectors are independent of each other) A relatively small set of primitive constructs can be combined in a relatively
small number of ways Every possible combination of primitives is legal and meaningful.
Suppose a L has 4 primitive data types: int, float, double, char and 2 typeoperators: array and pointer . If the type op can be applied to themselvesand the 4 data types, then a large # of data structures can be defined.
The lack of orthogonality leads to the exceptions to the rules of thelanguage. The use of orthogonality can be illustrated by comparing addition
instruction of assembly on IBM mainframes and VAX superminicomputers:Adding 2 32-bit integers that reside in register/memory and replacing one of them with the sum:
A reg1, mem ; on IBMAR reg1, reg2
ADDL op1, op2 ; on VAX The VAX instruction is orthogonal in that a single instruction can use eitherregisters or memory as operands.
7/31/2019 332 Ch01
13/48.ITCS332 by Dr. Abdel Fattah Salman 1-13
Evaluation Criteria: Readability-Orthogonality
The IBM is not orthogonal because only 2 operand combinations are legalout of 4 possibilities, and the two require 2 different instructions: A and AR .
The IBM is more difficult to learn because of the restrictions and theadditional instructions.
The more orthogonal the design of the language, the fewer exceptions thelanguage rules require. Fewer exceptions mean a higher degree of regularityin the design which makes the language easier to learn, read, andunderstand.
Too much orthogonality can cause problems: every construct in ALGOL 68has a type, there are no restrictions on those types.; most constructs producevalues. This combinational freedom allows complex constructs.
Simplicity in a language is the result of a combination of a relatively small #of primitive constructs and a limited use of orthogonality.
A functional language is one in which computations are made by applyingfunctions to given parameters. They offer the greatest simplicity becausethey can accomplish everything with single construct-function call. Lowefficiency prevent functional languages from becoming more widely used.
An imperative language is one in which computations are usually specifiedwith variables and assignment statements.
7/31/2019 332 Ch01
14/48.ITCS332 by Dr. Abdel Fattah Salman 1-14
Evaluation Criteria: Readability-Control Statements
Control statements A program that can be read from top to bottom is much easier
to understand than a program with a lots of goto (jumps)statements.
Most recent languages have included sufficient control
statements-so the need for goto statement has been reduced(eliminated).
The presence of well-known control structures allows writing
well-structured and readable programs.
7/31/2019 332 Ch01
15/48.ITCS332 by Dr. Abdel Fattah Salman 1-15
Evaluation Criteria: Readability-Data types and structures
Data types and structures The presence of adequate facilities for defining data types and
data structures is significant for readability. For example , we use numeric values to indicate flags because
there is no Boolean type in the language:timeout=1 is unclear meaning, whereas
timeout=true is perfectly clear. Languages without record type usually use a collection of parallel arrays : one for each item in the record. Records makeprograms more readable than using parallel arrays .
Character (Len=30) name(100)Integer Age (100)Integer Employee_number(100)Real salary(100)
7/31/2019 332 Ch01
16/48
7/31/2019 332 Ch01
17/48.ITCS332 by Dr. Abdel Fattah Salman 1-17
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 features that affect readability also affect
writability. Writability must be considered in the context of the target
problem domain.
For example , the writabilities of COBOL and FORTRAN aredramatically different in dealing with multi-dimensional arrays-FORTRAN is ideal. The Writability of COBOL is ideal forproducing financial reports.
Writability is influenced by the following factors: Simplicity and orthogonality
Support for abstraction Expressivity
7/31/2019 332 Ch01
18/48.ITCS332 by Dr. Abdel Fattah Salman 1-18
Evaluation Criteria: Writability
Simplicity and orthogonality Some programmers may not be familiar with all constructs of a large
language. This leads to a misuse of some features and disuse of
others. Few constructs , a small number of primitives, a small set of rules forcombining them is much better than having a large number of primitives.
Too much orthogonality can be detriment to Writability: errors can goundetected when all combinations of primitives are legal.
Expressivity Expressivity means that a language has a set of relatively convenient
ways of specifying operations. a++ is more convenient and shorterthan a=a+1.
Example : the inclusion of for statement in many modern languages
makes writing counting loops easier than with the use of while
7/31/2019 332 Ch01
19/48.ITCS332 by Dr. Abdel Fattah Salman 1-19
Evaluation Criteria: Writability
Support for abstraction Abstraction means the ability to define and use complex structures
or operations in ways that allow details to be ignored. The degree of abstraction allowed by a L and the naturalness of itsexpression are important to Writability .
PL can support 2 categories of abstractions: process and data . Example of process abstraction is the use of subprogram that
represents a code to be repeated multiple times in a program. Example of data abstraction is a binary tree that stores integers in its
nodes. In languages w/o pointers and dynamic memory managementsuch as Fortran, this can be implemented by 3 parallel arrays. InC++ and Java , trees are implemented by an abstraction of a tree
node as a simple class with 2 pointers (references) and an integer.
7/31/2019 332 Ch01
20/48.ITCS332 by Dr. Abdel Fattah Salman 1-20
Evaluation Criteria: ReliabilityReliability : a program is said to be reliable if it conforms to itsspecifications under all circumstances.
Reliability is affected by the following factors:
Type checking Testing for type errors during compile or execute times.
The earlier errors are detected, the less expensive making repairs
Compile-time type checking is cheaper and desirable than run-timechecking.
Java requires type checking of all variables and expressions atcompile time.
Exception handling The ability of a program to intercept run-time errors, take corrective
measures and continue is obvious to reliability.
Ada, C++, and Java include extensive capabilities for exceptionhandling, but Fortran not.
7/31/2019 332 Ch01
21/48.ITCS332 by Dr. Abdel Fattah Salman 1-21
Evaluation Criteria: Reliability
Aliasing Presence of two or more distinct referencing methods or names for
the same memory location. Aliasing is widely accepted as a dangerous feature in PLs .
Readability and writability A language that does not support natural ways of expressing an
algorithm will necessarily use unnatural approaches, and hencereduced reliability.
Unnatural approaches are less likely to be correct for all possiblesituations.
The easier a program to write, the most likely it is to be correct. Readability affects reliability in both writing and maintenance phases
of the life cycle.
Programs that are difficult to read are difficult both to write and tomodify.
7/31/2019 332 Ch01
22/48.ITCS332 by Dr. Abdel Fattah Salman 1-22
Evaluation Criteria: CostThe total cost of a PL is a function of its features: Training programmers to use language: it is a function of
simplicity and orthogonality of a language and the experience of
programmers. Writing programs (closeness to particular applications): is a
function of writeability
Compiling programs : Quality and speed of used compiler. Executing programs : A language that requires many run-time type
checks will prohibit fast code execution. Optimization is a set of techniques used by compilers to
decrease size and/or increase execution speed of the code theyproduce.
If little or no optimization is done, compilation will be faster . The higher the degree of optimization , the faster the code in
execution.
7/31/2019 332 Ch01
23/48
.ITCS332 by Dr. Abdel Fattah Salman 1-23
Evaluation Criteria: Cost
Language implementation system : availability of free compilers Reliability : poor reliability leads to high costs Maintaining programs :
Because maintenance is often done by individuals other thanoriginal software authors, poor readability makes the task challenging.
Maintenance costs can be as high as 2 to 4 times of developingcosts.
Program development , maintenance , and reliability are themost important contributors to the language cost.
7/31/2019 332 Ch01
24/48
.ITCS332 by Dr. Abdel Fattah Salman 1-24
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 languages officialdefinition Language implementers are concerned with the difficulty of
implementing the constructs and features of the language. Language user are worried about writability first and readability
later. Language designer emphasize elegance and the ability to attract
widespread use.
7/31/2019 332 Ch01
25/48
.ITCS332 by Dr. Abdel Fattah Salman 1-25
Influences on Language Design Computer Architecture
Languages are developed around the prevalent computer architecture,known as the von Neumann architecture.
In a von Neumann computer, both data and instructions are stored in thesame memory. Because of the von Neumann architecture, the central features of imperative
languages are variables- which model memory cells; statements which are
based on piping operation; and the iterative form of repetition. Operands in expressions are piped from memory to CPU, the result is piped
back to memory cell ( L-value ). Iteration is fast on von Neumann computers because instructions are stored
in adjacent memory cells. This efficiency discourages the use of recursionfor repetition although recursion is natural. A functional language is one in which the primary means of computation is
applying functions to given parameters. Programming in FL can be done
without: variable, assignment statements, and iterations. Parallel architectures increases the speed of executing functional programs.
7/31/2019 332 Ch01
26/48
.ITCS332 by Dr. Abdel Fattah Salman 1-26
Influences on Language Design Programming Methodologies
In 1960s-early 70s: Structured programming : software development process and PL
design are motivated by the shift in the major cost of computingfrom hardware to software. Programmers productivity was low. Progressively larger and
more complex problems were being solved by computers.
New software development methodology: top-down design andstepwise refinement . PL deficiencies were discovered: incompleteness of type
checking and inadequacy of control statement.
In late 1970s: a shift from procedure-oriented to data-orientedprogram design methodologies: emphasis on data design and theuse of abstract data types to solve problems.
For data abstraction to be used effectively, it must be supportedby the languages used for implementation. The first language thatsupported data abstraction was SIMULA 67.
7/31/2019 332 Ch01
27/48
.ITCS332 by Dr. Abdel Fattah Salman 1-27
Influences on Language Design The next step in the evolution of data-oriented to object-oriented design.
OO methodology begins with data abstraction, encapsulating processingwith data objects and control access to data, and adding inheritance anddynamic method binding. Inheritance is a powerful concept that greatly enhances the reuse of
existing software which increases software development
productivity. Dynamic (run-time) method binding allows more flexible use of
inheritance.
Languages that support OO methodology: Smalltalk , Ada 95 , Java ,and C++.
In summary : New software development methodologies (e.g., object-oriented software development) led to new language constructs,
programming paradigms and by extension, new programminglanguages.
7/31/2019 332 Ch01
28/48
7/31/2019 332 Ch01
29/48
.ITCS332 by Dr. Abdel Fattah Salman 1-29
The von Neumann Architecture
7/31/2019 332 Ch01
30/48
.ITCS332 by Dr. Abdel Fattah Salman 1-30
Programming Methodologies Influences
1950s and early 1960s : Simple applications; worry about machineefficiency
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
7/31/2019 332 Ch01
31/48
7/31/2019 332 Ch01
32/48
.ITCS332 by Dr. Abdel Fattah Salman 1-32
Language Categories
Markup Not a programming language.
It is used to specify the layout of information in Web documents Some programming capabilities has crept into some extensions to
XHTML and XML such as Java Server Pages Standard Taglibrary (JSTL) and eXtensible Stylesheet LanguageTransformations (XSLT).
New: used to specify the layout of information in Web documents Examples: XHTML, XML
7/31/2019 332 Ch01
33/48
.ITCS332 by Dr. Abdel Fattah Salman 1-33
Language Design Trade-Offs Reliability vs. cost of execution - Conflicting criteria
Example : Java demands all references to array elements be checkedfor proper indexing but that leads to increased execution costs
C does not require index range checking - C programs execute fasterthan semantically equivalent Java program although Java are morereliable.
Designers of Java traded execution efficiency for reliability .
Readability vs. writability - Another conflicting criteria APL provides a powerful set of operators for array operands (and a
large number of new symbols), allowing complex computations tobe written in a compact program but at the cost of poor readability .
A huge amount of computation can be specified in a very compactcode. APL programs have very poor readability.
Designers of APL traded readability for writability .
7/31/2019 332 Ch01
34/48
.ITCS332 by Dr. Abdel Fattah Salman 1-34
Language Design Trade-Offs
Writability (flexibility) vs. reliability Another conflicting criteria
C++ pointers are powerful and very flexible but not reliablyused. Pointer are not included in Java
In summary: The task of choosing constructs andfeatures when designing a PL requires manycompromises and trade-offs.
7/31/2019 332 Ch01
35/48
.ITCS332 by Dr. Abdel Fattah Salman 1-35
Implementation Methods Machine language is the only language that most computers understand. A computer can be designed and built to use HLL as its machine
language, but this will be very complex and expensive and highlyinflexible because it would be difficult to use it with other HLLs.
The more practical machine design choice implements in hardware avery low-level language that provides the most commonly neededprimitive operations and requires system software to create an interfaceto programs in HLLs.
In additional to the hardware and the language implementation system,computers need OS.PLs can be implemented by any of 3 methods:
Compilation - Programs are translated into machine language Pure Interpretation Programs are interpreted by another program known as an interpreter
Hybrid Implementation Systems A compromise between compilers and pure interpreters
7/31/2019 332 Ch01
36/48
.ITCS332 by Dr. Abdel Fattah Salman 1-36
Layered View of ComputerThe operating system andlanguage implementationare layered over Machine
interface of a computer.Layers can be thought asvirtual computers providinginterfaces to the user at
higher levels:OS and C compiler provide avirtual C computer.
7/31/2019 332 Ch01
37/48
.ITCS332 by Dr. Abdel Fattah Salman 1-37
Compilation
Translate high-level program ( source language ) into machinecode ( machine language ), which can be executed directly on thecomputer.
Slow translation, fast execution Compilation process has several phases:
Lexical analysis (Scanning) : converts characters in the sourceprogram into lexical units Syntax analysis (Parsing) : transforms lexical units into parse
trees which represent the syntactic structure of program Semantics analysis: generate intermediate code Code generation : machine code is generated
7/31/2019 332 Ch01
38/48
7/31/2019 332 Ch01
39/48
.ITCS332 by Dr. Abdel Fattah Salman 1-39
Additional Compilation Terminologies
Load module (executable image): the user and system code together. Linking and loading : the process of collecting system program and
linking them to user program. Execution of Machine Code
Fetch-execute-cycle (on a von Neumann architecture) Programs reside in memory but are executed in the CPU.
initialize the program counterrepeat forever
fetch the instruction pointed by the counterincrement the counterdecode the instructionexecute the instruction
end repeat
7/31/2019 332 Ch01
40/48
.ITCS332 by Dr. Abdel Fattah Salman 1-40
Von Neumann Bottleneck
Connection speed between a computers 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.
7/31/2019 332 Ch01
41/48
.ITCS332 by Dr. Abdel Fattah Salman 1-41
Pure Interpretation
Programs are interpreted by another program called interpreter with notranslation.
An interpreter acts as a software simulation of a machine whose fetch-
execute cycle deals with HLL program statements rather than machineinstructions. Easier implementation of programs (run-time errors can easily and
immediately displayed)
Slower execution (10 to 100 times slower than compiled programs). The primary source of slowness is the decoding of HLL statements ,which are more complex that machine language instructions. Loopstatements must be decoded in every iteration.
Bottleneck : Statement decoding rather than CPU-MEM connection. Often requires more space Becoming rare on high-level languages Significant comeback with some Web scripting languages (e.g.,
JavaScript and PHP)
7/31/2019 332 Ch01
42/48
.ITCS332 by Dr. Abdel Fattah Salman 1-42
Pure Interpretation Process
7/31/2019 332 Ch01
43/48
.ITCS332 by Dr. Abdel Fattah Salman 1-43
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.
An intermediate language code is interpreted rather thantranslated.
Faster than pure interpretation : a statement is decoded ONCE .
Perl programs are compiled to detect errors before interpretationand to simplify the interpreter.
Initial implementations of Java were hybrid; Javas intermediate
form- byte code - provides portability to any machine with bytecode interpreter and associated run-time system (Both are calledJava virtual machine). There are new systems that translate bytecode into machine language for faster execution.
7/31/2019 332 Ch01
44/48
7/31/2019 332 Ch01
45/48
7/31/2019 332 Ch01
46/48
7/31/2019 332 Ch01
47/48
.ITCS332 by Dr. Abdel Fattah Salman 1-47
Programming Environments
A programming environment is the collection of tools used insoftware development such as: file system , text editor , compiler ,linker. Several programming environment are:
UNIX An older operating system and tool collection Nowadays often used through a GUI (e.g., CDE, KDE, or
GNOME) that run on top of UNIX Borland JBuilder
Provides an integrated compiler, editor, debugger, and file
system for Java development. Microsoft Visual Studio .NET
A large, complex visual environment
Used to program in 5 .NET languages : J#, Jscript , C#,managed C++, Visual BASIC.NET .
7/31/2019 332 Ch01
48/48
Summary
The study of programming languages is valuable for a numberof 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 languagesinclude: Readability , writability, reliability , cost
Major influences on language design have been machinearchitecture and software development methodologies The major methods of implementing programming languages
are: compilation , pure interpretation, and hybridimplementation