Introduction to
Programming
Languages
Dr. M. A. Rouf
The first computers
• Scales – computed relative weight of two items
• Computed if the first item’s weight was less than, equal to, or greater
than the second item’s weight
• Abacus – performed mathematical computations
• Primarily thought of as Chinese, but also Japanese, Mayan, Russian,
and Roman versions
• Can do square roots and cube roots
2
Computer Size Electronic Numerical
Integrator and Computer (ENIAC)
ENIAC then…
ENIAC today…
• With computers (small) size does matter!
3
Why study programming
languages?
• Become a better software engineer
• Understand how to use language features
• Appreciate implementation issues
• Better background for language selection
• Familiar with range of languages
• Understand issues / advantages / disadvantages
• Better able to learn languages
• You might need to know a lot
4
Why study programming
languages?
• Better understanding of implementation issues
• How is “this feature” implemented?
• Why does “this part” run so slowly?
• Better able to design languages
• Those who ignore history are bound to repeat it…
5
Why are there so many
programming languages?
• There are thousands!
• Evolution
• Structured languages -> OO programming
• Special purposes
• Lisp for symbols; Snobol for strings; C for systems; Prolog for relationships
• Personal preference
• Programmers have their own personal tastes
• Expressive power
• Some features allow you to express your ideas better
6
Why are there so many
programming languages?
• Easy to use
• Especially for teaching / learning tasks
• Ease of implementation
• Easy to write a compiler / interpreter for
• Good compilers
• Fortran in the 50’s and 60’s
• Economics, patronage
• Cobol and Ada, for example
7
Programming domains
• Scientific applications
• Using the computer as a large calculator
• Fortran and friends, some Algol, APL
• Using the computer for symbol manipulation
• Mathematica
• Business applications
• Data processing and business procedures
• Cobol, some PL/I, RPG, spreadsheets
• Systems programming
• Building operating systems and utilities
• C, PL/S, ESPOL, Bliss, some Algol and derivitaves
8
Programming domains
• Parallel programming
• Parallel and distributed systems
• Ada, CSP, Modula, DP, Mentat/Legion
• Artificial intelligence
• Uses symbolic rather than numeric computations
• Lists as main data structure
• Flexibility (code = data)
• Lisp in 1959, Prolog in the 1970s
• Scripting languages
• A list of commands to be executed
• UNIX shell programming, awk, tcl, Perl
9
Programming domains
• Education
• Languages designed to facilitate teaching
• Pascal, BASIC, Logo
• Special purpose
• Other than the above…
• Simulation
• Specialized equipment control
• String processing
• Visual languages
10
Compilation vs. Translation
• Translation: does a ‘mechanical’ translation of the source
code
• No deep analysis of the syntax/semantics of the code
• Compilation: does a thorough understanding and translation
of the code
• A compiler/translator changes a program from one language
into another
• C compiler: from C into assembly
• An assembler then translates it into machine language
• Java compiler: from Java code to Java bytecode
• The Java interpreter then runs the bytecode
11
Compilation Steps of C Program/
High-level Program
1. Scanner
2. Parser
3. Semantic analysis
4. Intermediate code generation
5. Machine-independent code improvement
(optional)
1. Target code generation
2. Machine-specific code improvement (optional)
Static linking
CSE-3821 Dr. M. A. Rouf, Dept. of CSE, DUET,
Gazipur
12
Example of Compilation
Process
Object Code
Library
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 13
, DUET, Gazipur
Peephole Optimizations
• Constant Folding
x := 32 becomes x := 64
x := x + 32
• Unreachable Code
goto L2
x := x + 1 Unneeded Code
L2:
• Flow of Control Optimizations
goto L1 becomes goto L2
…
L1: goto L2
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 14
, DUET, Gazipur
Peephole Optimizations
• Algebraic Simplification
x := x + 0 unneeded
• Dead code
void example1() {
int x = 10;
return; // Program exits here
x = 20; // Dead code (never executed)
}
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 15
, DUET, Gazipur
Compiler Optimization
• Reduction in strength
Ex1: x := x * 2 x := x + x
Ex2: Y=X^2; -> Y=X*X;
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 16
, DUET, Gazipur
Basic Block Level
• Common Subexpression elimination
• Constant Propagation
• Dead code elimination
• Plus many others such as copy propagation, value numbering, partial
redundancy elimination, …
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 17
, DUET, Gazipur
Common Expression can be Eliminated
Simple example:
• x=a+b+y; • t1 = a+b;
z=a+b+w; • x=t1+y;
• z=t1+w
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 18
, DUET, Gazipur
Loop Optimization
Before
optimization:
while(i<100) {
a = Sin(x)/Cos(x) + i;
i++; }
After optimization:
t = Sin(x)/Cos(x);
while(i<100) {
a = t + i;
i++;
} CSE-3821 Dr. M. A. Rouf, Dept. of CSE 19
, DUET, Gazipur
Loop Unrolling
Original Code:
for (i=0; i<=100;i++)
x=x+i;
Loop Unrolling by 2
For (i=0;i<=100;i+=2)
{
x=x+i;
x=x+i+1;
}
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 20
, DUET, Gazipur
Control Flow Graph - CFG
CFG = < V, E, Entry >, where
V = vertices or nodes, representing an instruction or basic
block (group of statements).
E = (V x V) edges, potential flow of control
Entry is an element of V, the unique program entry
Two sets used in algorithms:
• Succ(v) = {x in V| exists e in E, e = v x}
• Pred(v) = {x in V| exists e in E, e = x v}
1 2 3 4 5
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 21
, DUET, Gazipur
Constant Propagation
b=5 b=5 b=5
c = 4*b c = 20 c = 20
c>b c>5 20 > 5
t t t
f f f
d=b+2 d=7 d=7
e=a+b e=a+5 e=a+5
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 22
, DUET, Gazipur
Constant Propagation
b=5 b=5
c = 20 c = 20
20 > 5 d=7
t e=a+5
f
d=7
e=a+5
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 23
, DUET, Gazipur
Copy Propagation
b=a b=a
c = 4*b c = 4*a
c>b c>a
d=b+2 d=a+2
e=a+b e=a+a
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 24
, DUET, Gazipur
Data Dependencies
• Flow Dependencies – Read After Write (RAW)
x := 4;
y := x + 1
• Output Dependencies – Write After Write (WAW)
x := 4;
x := y + 1;
• Antidependencies – Write After Read (WAR)
y := x + 1;
x := 4;
CSE-3821 Dr. M. A. Rouf, Dept. of CSE 25
, DUET, Gazipur
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: conformance to specifications (i.e., performs to its
specifications)
• Cost: the ultimate total cost
1-26
Evaluation Criteria: Readability
• Overall simplicity
• A manageable set of features and constructs
• Minimal feature multiplicity
• Minimal operator overloading
• Orthogonality
• A relatively small set of primitive constructs can be combined in a relatively small
number of ways
• Every possible combination is legal
• Data types
• Adequate predefined data types
• Syntax considerations
• Identifier forms: flexible composition
• Special words and methods of forming compound statements
• Form and meaning: self-descriptive constructs, meaningful keywords
1-27
Evaluation Criteria: Writability
• Simplicity and orthogonality
• Few constructs, a small number of primitives, a small set of rules for
combining them
• Support for abstraction
• The ability to define and use complex structures or operations in ways that
allow details to be ignored
• Expressivity
• A set of relatively convenient ways of specifying operations
• Strength and number of operators and predefined functions
1-28
Evaluation Criteria: Reliability
• Type checking
• Testing for type errors
• Exception handling
• Intercept run-time errors and take corrective measures
• Aliasing
• Presence of two or more distinct referencing methods for the same memory
location
1-29
Evaluation Criteria: Cost
• Training programmers to use the language
• Writing programs (closeness to particular applications)
• Compiling programs
• Executing programs
• Language implementation system: availability of free compilers
• Reliability: poor reliability leads to high costs
• Maintaining programs
1-30
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-31
Influences on Language Design
• Computer Architecture
• Languages are developed around the prevalent computer architecture,
known as the von Neumann architecture
• Program Design Methodologies
• New software development methodologies (e.g., object-oriented software
development) led to new programming paradigms and by extension, new
programming languages
1-32
Computer Architecture Influence
• Well-known computer architecture: Von Neumann
• Imperative languages, most dominant, because of von
Neumann computers
• Data and programs stored in memory
• Memory is separate from CPU
• Instructions and data are piped from memory to CPU
• Basis for imperative languages
• Variables model memory cells
• Assignment statements model piping
• Iteration is efficient
1-33
The von Neumann Architecture
1-34
The von Neumann Architecture
• Fetch-execute-cycle (on a von Neumann architecture computer)
initialize the program counter
repeat forever
1)fetch the instruction pointed by the
counter
2)increment the program counter
3)decode the instruction
4)execute the instruction
end repeat
1-35
Programming Methodologies
Influences
• 1950s and early 1960s: Simple applications; worry about
machine efficiency
• Late 1960s: People efficiency became important; readability,
better control structures
• structured programming
• top-down design and step-wise refinement
• Late 1970s: Process-oriented to data-oriented
• data abstraction
• Middle 1980s: Object-oriented programming
• Data abstraction + inheritance + polymorphism
1-36
Language Categories
• Imperative
• Central features are variables, assignment statements, and iteration
• Include languages that support object-oriented programming
• Include scripting languages
• Include the visual languages
• Examples: C, Java, Perl, JavaScript, Visual BASIC .NET, C++
• Functional
• Main means of making computations is by applying functions to given
parameters
• Examples: LISP, Scheme, ML, F#
• Logic
• Rule-based (rules are specified in no particular order)
• Example: Prolog
• Markup/programming hybrid
• Markup languages extended to support some programming
• Examples: JSTL, XSLT
1-37
Language Design Trade-Offs
• Reliability vs. cost of execution
• Example: Java demands all references to array elements be checked for
proper indexing, which leads to increased execution costs
• Readability vs. writability
Example: APL provides many powerful operators (and a large number of
new symbols), allowing complex computations to be written in a
compact program but at the cost of poor readability
• Writability (flexibility) vs. reliability
• Example: C++ pointers are powerful and very flexible but are unreliable
1-38