CH 01
CH 01
Preliminaries
ISBN 0-321-33025-0
Purpose of This Book
• To examine carefully
• the underlying concepts of the various constructs
and capabilities of programming languages
1-2
Chapter 1 Topics
1-4
Reasons for Studying Concepts of
Programming Languages (2)
1-5
Reasons for Studying Concepts of
Programming Languages (3)
1-7
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
1-8
Programming Domains (3)
• Web Software
– Eclectic collection of languages: markup (e.g.,
XHTML), scripting (e.g., PHP), general-purpose
(e.g., Java)
1-9
Language Evaluation Criteria
1-10
Why Readability Is Important
1-11
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.
1-12
Excessive Simplicity
1-13
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.
1-14
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
timeOut = true
1-15
Characteristics Contributing to the
Readability - Syntax Considerations (1)
1-16
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?
1-17
Characteristics Contributing to the
Readability - Syntax Considerations (3)
1-18
Evaluation Criteria: Writability
1-19
Writability Comparison between Two
Different Languages
1-20
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
1-21
Process Abstraction
1-22
Data Abstraction (1) [Wikipedia]
1-23
Data Abstraction (2)
1-24
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
1-25
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.
1-26
Evaluation Criteria: Reliability
1-27
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
1-28
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
1-29
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.
1-30
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
1-31
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
1-32
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
1-33
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
1-34
Von Neumann Architecture
1-35
The Motherboard of a Computer
1-36
The von Neumann Architecture
1-37
Central Features of Imperative Languages
1-38
Program Execution on a Von Neumann
Computer
1-39
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
1-40
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
1-41
Evolution of Programming Methodologies (1)
1-42
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
1-43
All of the evolutionary steps in software development
methodologies led to new language constructs to
support them.
1-44
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++
1-45
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 [1][2]
– e.g. Perl, JavaScript, and Ruby
1-46
A Typical Session in Microsoft Visual
Basic 6
1-47
Scripting Languages [rockcontent]
1-48
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.
1-49
Markup Programming hybrid languages
1-50
Benefits of Modular Design
1-51
Language Design Trade-offs
1-52
Instances of Language Design Trade-Offs
1-53
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.
1-54
The Machine Language of a Computer
1-55
Machine Code and CPU [Alexandra Williams]
1-56
Operating Systems
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.
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
1-59
Compilation
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
1-61
Optimization
1-62
Optimization vs. Reliability
a:
process 2
process 1 memory
int a;
int foo() int a;
{ foo()
a=1; { a=3;
optimization
if(a>0) return a;
a=3; }
else
If a is not a volatile variable, the optimization
a=-1;
return a; improve performance; otherwise, it introduces
} race condition problem. 1-63
Symbol Table
1-64
The Compilation Process
1-65
User Program Supporting Code
1-66
Linking Operation
1-67
Combine a User Program and All
Supporting Functions Together
address space of a process
0x40ffffff
printf:
linking
compilation loading
1-68
Linking Operation
1-69
Libraries
1-70
Von Neumann Bottleneck
1-72
Advantages of Interpretation
1-73
Disadvantages of Interpretation (1)
1-74
Disadvantages of Interpretation (2)
1-75
Popularity of Interpretation
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
1-78
Example (1)
• Perl programs
– are partially compiled to detect errors before
interpretation to simplify the interpreter.
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.
1-80
Java Bytecode Example [wikipedia]
javac
translated by a Java compiler
1-81
Java Virtual Machine [Wikipedia]
1-82
Java Virtual Machine Specification [Wikipedia]
1-83
Java Virtual Machine Implementation [Wikipedia]
1-84
Java Virtual Machine Instance[Wikipedia]
1-85
Java Runtime Environment [Wikipedia]
1-87
Java Class Library[Wikipedia]
1-89
Just-in-Time (JIT) Implementation
Systems
1-90
Preprocessors
1-91
Preprocessor Instructions
1-92
More Preprocessor instructions
1-93
Programming Environments
• 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++
1-95
Summary
1-96
Supplemental Materials
1-97
Should Languages Support Object-oriented
Programming Form a Separate Language Category?
1-98