CENG 223 ClassNotesChap01
CENG 223 ClassNotesChap01
Preliminaries
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
Chapter 1
Preliminaries
• Scientific applications
– In the early 40s computers were invented for scientific applications.
– The applications require large number of floating point computations.
– Fortran was the first language developed scientific applications.
– ALGOL 60 was intended for the same use.
• Business applications
– The first successful language for business was COBOL.
– Business languages are characterized by facilities for producing elaborate
reports, precise ways of describing and storing decimal numbers and character
data, and the ability to specify decimal arithmetic operations.
– The arrival of PCs started new ways for businesses to use computers.
– Spreadsheets and database systems were developed for business.
• Artificial intelligence
– Symbolic rather than numeric computations are manipulated.
– Symbolic computation is more suitably done with linked lists than arrays.
– LISP was the first widely used AI programming language.
– An alternative approach to AI applications: Prolog
– Scheme, a dialect of LISP
• Systems programming
– The OS and all of the programming supports tools are collectively known as its
system software.
– Need efficiency because of continuous use.
– A language for this domain must provide fast execution. Furthermore, it must
have low-level features that allow the software interfaces to external devices to
be written.
– The UNIX operating system is written almost entirely in C.
• Scripting languages
– Put a list of commands, called a script, in a file to be executed.
– The language, named sh (for shell), began as a small collection of commands that
were interpreted as calls to system subprograms that performed utility functions,
such as file management and simple file filtering.
– awk, another scripting language, began as a report-generation language but later
became a more general-purpose language.
– The Perl language, developed by Larry Wall, was originally a combination of sh
and awk.
– The use of Perl rose dramatically, primarily because it is a nearly ideal language
for Common Gateway Interface (CGI) programming.
– JavaScript (Flanagan, 1998) is a scripting language developed by Netscape.
– JavaScript is used mostly as a client-side scripting language.
– JavaScript is embedded in HTML documents and is interpreted by a browser
that finds the code in a document that is being displayed.
– PHP is a scripting language used on Web server systems. Its code is embedded
in HTML documents. The code is interpreted on the server before the document
is sent to a requesting browser.
• Special-purpose languages
– A host of special-purpose languages have appeared over the past 40 years.
– They range from RPG, which is used to produce business reports, to APT, which
is used for instructing programmable machine tools, to GPSS, which is used for
system simulation.
– This book does not discuss special-purpose language.
Language Evaluation Criteria
Readability
• The most important criteria for judging a programming language is the ease with which
programs can be read and understood.
• Language constructs were designed more from the point of view of the computer than the
users.
• Because ease of maintenance is determined in large part by the readability of programs,
readability became an important measure of the quality of programs and programming
languages. The result is a crossover from focus on machine orientation to focus on human
orientation.
• The most important criterion “ease of use”
• Overall simplicity “Strongly affects readability”
– Too many features make the language difficult to learn. Programmers tend to learn a
subset of the language and ignore its other features.
– Multiplicity of features is also a complicating characteristic “having more than one
way to accomplish a particular operation.” Ex “Java”:
count = count + 1
count += 1
count ++
++count
– Although the last two statements have slightly different meaning from each other and
from the others, all four have the same meaning when used as stand-alone
expressions.
– Operator overloading where a single operator symbol has more than one meaning.
– Although this is a useful feature, it can lead to reduced readability if users are allowed
to create their own overloading and do not do it sensibly.
– 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.
• Orthogonality
– Makes the language easy to learn and read.
– 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.
– Every possible combination is legal and meaningful.
– The more orthogonal the design of a language, the fewer exceptions the language
rules require.
– Example: In C language, parameters are passed by value, unless they are arrays, in
which they are, in effect, passed by reference (because the appearance of an array
name without a subscript in a C program is interpreted to be the address of the
array’s first element).
– Example: Adding two 32-bit integer values that reside in either memory or registers
and replacing on of two values with the sum.
• The IBM mainframes have two instructions:
A Reg1, memory_cell
//Reg1 <- contents (Reg1) + contents(memory_cell)
AR Reg1, Reg2
//Reg1 <- contents (Reg1) + contents(Reg2)
where Reg1 and Reg2 represent registers.
• Control Statements
– It became widely recognized that indiscriminate use of goto statements severely
reduced program readability.
– Example: Consider the following nested loops written in C
while (incr < 20)
{
while (sum <= 100
{
sum += incr;
}
incr++;
}
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:
– Basic and Fortran in the early 1970s lacked the control statements that allow strong
restrictions on the use of gotos, so writing highly readable programs in those
languages was difficult.
– Since then, languages have included sufficient control structures. The control
statement design of a language is now a less important factor in readability than it
was in the past.
timeout = 1
timeout = true
• Syntax Considerations
– The syntax of the elements of a language has a significant effect on readability.
– The following are examples of syntactic design choices that affect readability:
• Identifier forms: Restricting identifiers to very short lengths detracts from
readability.
– Example: In Fortan 77, identifiers can have six characters at most.
– Example: ANSI BASIC (1978) an identifier could consist only of a
single letter or a single letter followed by a single digit.
• Special Words: Program appearance and thus program readability are strongly
influenced by the forms of a language’s special words (while, class, for).
– Example: C uses braces for pairing control structures. It is difficult to
determine which group is being ended.
– Example: Fortran 95 and Ada allows programmers to use special
names as legal variable names. Ada uses end if to terminate a selection
construct, and end loop to terminate a loop construct.
• Form and Meaning: Designing statements so that their appearance at least
partially indicates their purpose is an obvious aid to readability.
• Semantic should follow directly from syntax, or form.
– Example: In C the use of static depends on the context of its
appearance.
– If used as a variable inside a function, it means the variable is created
at compile time.
– If used on the definition of a variable that is outside all functions, it
means the variable is visible only in the file in which its definition
appears. It is not exported from that file.
Writability
• It 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 readability also affect writability.
• Simplicity and orthogonality
– A smaller number of primitive constructs and a consistent set of rules for
combining them (that is, orthogonality) is much better than simply having a large
number of primitives.
– A programmer can design a solution to a complex problem after learning only a
simple set of primitive constructs.
• Support for abstraction
– Abstraction means the ability to define and then use complicated structures or
operations in ways that allow many of the details to be ignored.
– Programming languages can support two distinct categories of abstraction,
process and data.
– A simple example of process abstraction is the use of subprogram to implement
a sort algorithm that is required several times in a program. Without the
subprogram, the sort code would have to be replicated in all places where it was
needed.
– As an example of data abstraction, consider a binary tree that stores integer
data in its nodes. In Fortran 77, three parallel integer arrays, where two of these
integers are used as subscripts to specify offspring nodes. In C++ and Java, theses
trees can be implemented by using an abstraction of a tree node in the form of a
simple class with two pointers (or references) and an integer.
• Expressivity
– It means that a language has relatively convenient, rather than cumbersome, ways
of specifying computations.
– Ex: ++count ⇔ count = count + 1 // more convenient and shorter
Reliability
• A program is said to be reliable if it performs to its specifications under all conditions.
• Type checking: is simply testing for type errors in a given program, either by the
compiler or during program execution.
– The earlier errors are detected, the less expensive it is to make the required
repairs. Java requires type checking of nearly all variables and expressions at
compile time.
• Exception handling: the ability to intercept run-time errors, take corrective measures,
and then continue is a great aid to reliability.
• Aliasing: it is having two or more distinct referencing methods, or names, for the same
memory cell. In C, union members and pointers set to point to the same variable.
– It is now widely accepted that aliasing is a dangerous feature in a language.
• Readability and writability: Both readability and writability influence reliability.
Cost
– Categories
– Training programmers to use language
– Writing programs
– Compiling programs
– Executing programs
– Language implementation system “Free compilers is the key, success of Java”
– Reliability, does the software fail?
– Maintaining programs: Maintenance costs can be as high as two to four times as
much as development costs.
– Portability “standardization of the language”
– Generality (the applicability to a wide range of applications)
Influences on Language Design
• Computer architecture: Von Neumann
• We use imperative languages, at least in part, because we use von Neumann machines
– Data and programs stored in same memory
– Memory is separate from CPU
– Instructions and data are piped from memory to CPU
– Results of operations in the CPU must be moved back to memory
– Basis for imperative languages
• Variables model memory cells
• Assignment statements model piping
• Iteration is efficient
Programming methodologies
• Late 1960s: Procedure-oriented
– People efficiency became important; readability, better control structures
– Structured programming
– Top-down design and step-wise refinement
• Late 1970s: Procedure-oriented to data-oriented
– data abstraction
• early 1980s: Object-oriented programming
Language Categories
• Imperative
– Central features are variables, assignment statements, and iteration
– C, Pascal
• Functional
– Main means of making computations is by applying functions to given parameters
– LISP, Scheme
• Logic
– Rule-based
– Rules are specified in no special order
– Prolog
• Object-oriented
– Encapsulate data objects with processing
– Inheritance and dynamic type binding
– Grew out of imperative languages
– C++, Java