You are on page 1of 37

Principles of Programming

Languages

An Introduction to Programming Languages

Zebiba N. 1
outline
 Reasons for Studying Concepts of Programming
Languages
 Programming Domains
 Models of Programming Languages
 Language Evaluation Criteria
 Influences on Language Design
 Implementations of PL
 Programming Environments
 Review Question ?

Zebiba N. 2
Why Study PL Concepts?
Increased capacity to express ideas
Improved background for choosing appropriate
languages
Increased ability to learn new languages
Better understanding of the significance of
implementation
Increased ability to design new languages
Background for compiler writing
Overall advancement of computing

Zebiba N. 3
Programming Language
A programming language is an artificial language
designed to express computations or algorithms
that can be performed by a computer.

A program is computer coding of Input


an algorithm that
– Takes input
– Performs some calculations on Program
the input
– Generates output
Output

4
Zebiba N.
What is a language for?
• Why do we have programming languages?
 way of thinking---way of expressing
algorithms
• languages from the user's point of view
 abstraction of virtual machine---way of
specifying what you want the hardware to
do without getting down into the bits
• languages from the implementer's point of view

Zebiba N. 5
Programming Domains
 Scientific applications
– For large numbers of floating point calculations; use of arrays
– FORTRAN
 Business applications
– For produce reports, use decimal numbers and characters
– COBOL
 Artificial intelligence
– For symbols rather than numbers manipulated; use of linked lists
– LISP
 Systems programming
– Need efficiency because of continuous use
– C
 Web Software
– Eclectic collection of languages: markup (e.g., XHTML), scripting (e.g.,
PHP), general-purpose (e.g., Java)

1-6
Zebiba N.
Models of Programming Languages
1st View: Imperative & Procedural
• Computers take commands and do operations
• Thus, programming is like …
issuing procedural commands to the computer
– Example: a factorial function in C
int fact(int n) {
int sofar = 1;
while (n>0) sofar *= n--;
return sofar;
}
• Almost all computers today use the von Neumann
architecture.

7
Zebiba N.
The von Neumann Architecture

Zebiba N. 8
The von Neumann Architecture
• Key features:
– Data and programs stored in memory
– Instructions and data are piped from memory to
CPU
– Fetch-execute-cycle for each machine instruction
initialize the program counter (PC)
repeat forever
fetch the instruction pointed by PC
increment the counter add A,B,C 0100 1001
decode the instruction sub C,D,E 0110 1010
execute the instruction br LOOP 1011 0110
end repeat … …
Assembly Machine
code code
Zebiba N. 9
Imperative Language and Arch.
• Example: a factorial function in C
int fact(int n) {
int sofar = 1; sofar
while (n>0) sofar *= n--; n
return sofar; int fact(int n) {
int sofar = 1;
} while (n>0) sofar *= n--;
return sofar; }
– Indicates that data n,
sofar, and program code
are stored in memory
– Program code instructs
CPU to do operations
Zebiba N.
10
Imperative Languages and Arch.
• Imperative languages, e.g., C, C++, Java, which
dominate programming, mimic von Neumann
architecture
– Variables  memory cells
– Assignment statements  data piping between
memory and CPU
– Operations and expressions  CPU executions
– Explicit control of execution flows  prog. counter
• Allow efficient mapping between language and
hardware for good execution performance, but
limited by von Neumann bottleneck
Zebiba N. 11
Layers of Abstraction/Translation
High Level Language temp = v[k];
Program
v[k] = v[k+1];
Compiler v[k+1] = temp;
Assembly Language lw $15, 0($2)
Program lw $16, 4($2)
Assembler
sw$16, 0($2)
sw$15, 4($2)
Machine Language 0000 1001 1100 0110 1010 1111 0101 1000
Program 1010 1111 0101 1000 0000 1001 1100 0110
1100 0110 1010 1111 0101 1000 0000 1001
0101 1000 0000 1001 1100 0110 1010 1111
Machine
Interpretation All imperative!
Control Signal
Specification ALUOP[0:3] <= InstReg[9:11] & MASK

Zebiba N. 12
2nd View: Functional
• Programming is like …
solving mathematical functions, e.g.,
z = f(y, g(h(x)))
– A program, and its subprograms, are just
implementations of mathematical functions
– Example: a factorial function in ML
Input

fun fact x = Input


if x <= 0
then 1 Function
else x * fact(x-1); Program

Zebiba N. Output Output 13


Another Functional Language: LISP
• Example: a factorial function in Lisp
– Computations by applying functions to parameters
– No concept of variables (storage) or assignment
• Single-valued variables: no assignment, not storage
– Control via recursion and conditional expressions
• Branches  conditional expressions
• Iterations  recursion
– Dynamically allocated linked lists
• 2nd-oldest general-purpose PL still in use (1958)

Zebiba N. 14
3rd View: Logic
• Programming is like …
logic induction
– Program expressed as rules in formal logic
– Execution by rule resolution
– Example: relationship among people

Zebiba N. 15
Logic Programming
• Non-procedural
– Only supply relevant facts (predicate calculus) and
inference rules (resolutions)
– System then infer the truth of given queries/goals
• Highly inefficient, small application areas
(database, AI)
– Example: a factorial function in Prolog
fact(X,1) :- X =:= 1.
fact(X,Fact) :-
X > 1, NewX is X - 1,
fact(NewX,NF),
Fact is X * NF.

Zebiba N. 16
Summary: Language Categories

Imperative Functional
Logic Language
Language Language

Memory

ALU Control

von Neumann Architecture


17
Zebiba N.
Summary: Language Categories
 Imperative
– Variables, assignment statements, and iteration
– Include languages that support object-oriented programming,
scripting languages, visual languages
– Ex.: C, Java, Perl, JavaScript, Visual BASIC .NET
 Functional
– Computing by applying functions to given parameters
– Ex.: LISP, Scheme
 Logic
– Rule-based (rules are specified in no particular order)
– Ex.: Prolog
 Markup/programming hybrid
– Markup languages extended to support some programming
– Examples: JSTL, XSLT

Zebiba N. 18
What Make a Good PL?
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: a program performs to its
specifications under all conditions
 Cost

Zebiba N. 19
Features Related to Readability
• Overall simplicity: lang. is more readable if
– Fewer features and basic constructs
• Readability problems occur whenever program’s author
uses a subset different from that familiar to reader
– Fewer feature multiplicity (i.e., doing the same
operation with different ways)
– Minimal operator overloading
• Orthogonality
– 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 combination is legal, independent of context
 Few exceptions, irregularities
Zebiba N. 20
Features Related to Readability
• Control statements
– Sufficient control statements for structured prog.
 can read program from top to bottom w/o jump
• Data types and structures
– Adequate facilities for defining data type & structure
• Syntax considerations
– Identifier forms: flexible composition
– Special words and methods of forming compound
statements
– Form and meaning: self-descriptive constructs,
meaningful keywords

Zebiba N. 21
Writability
• Simplicity and orthogonality
– But, too orthogonal may cause errors undetected
• Support for abstraction
– Ability to define and use complex structures or
operations in ways that allow details to be ignored
– Abstraction in process (e.g. subprogram), data
• Expressivity
– A set of relatively convenient ways of specifying
operations
– Example: the inclusion of for statement in many
modern languages
Zebiba N. 22
Reliability
• Type checking
– Testing for type errors, e.g. subprogram parameters
• Exception handling
– Intercept run-time errors & take corrective measures
• Aliasing
– Presence of two or more distinct referencing methods
for the same memory location
• Readability and writability
– A language that does not support “natural” ways of
expressing an algorithm will necessarily use
“unnatural” approaches, and hence reduced reliability
Zebiba N. 23
Cost
• Training programmers to use language
• Writing programs (closeness to particular
applications)
• Compiling programs
• Executing programs: run-time type checking
• Language implementation system: availability
of free compilers
• Reliability: poor reliability leads to high costs
• Maintaining programs

Zebiba N. 24
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
 Power efficiency?
Zebiba N. 25
Language Design Trade-Offs
• Reliability vs. cost of execution
– e.g., Java demands all references to array elements be
checked for proper indexing but that leads to increased
execution costs
• Readability vs. writability
– e.g., 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
– e.g., C++ pointers are powerful and very flexible but not
reliably used
Zebiba N. 26
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

Zebiba N. 1-27
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-28
Zebiba N.
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

Zebiba N. 1-29
Implementations of PL
• It is important to understand how features and
constructs of a programming language, e.g.,
subroutine calls, are implemented
– Implementation of a PL construct means its
realization in a lower-level language, e.g. assembly
 mapping/translation from a high-level language to
a low-level language
– Why the need to know implementations?
Understand whether a construct may be
implemented efficiently, know different
implementation methods and their tradeoffs, etc.
Zebiba N. 30
Implementation by Compilation
• Translate a high-level program into equivalent
machine code automatically by another program
(compiler)
• Compilation process has several phases:
– Lexical analysis: converts characters in the source
program into lexical units
– Syntax analysis: transforms lexical units into parse
trees which represent syntactic structure of program
– Semantics analysis: generate intermediate code
– Code generation: machine code is generated
– Link and load

Zebiba N. 31
Compilation
Process

Zebiba N. 32
Implementation by Interpretation
• Program interpreted by another program
(interpreter) without translation
– Interpreter acts a simulator or virtual machine
• Easier implementation of programs (run-time
errors can easily and immediately displayed)
• Slower execution (10 to 100 times slower than
compiled programs)
• Often requires more space
• Popular with some Web scripting languages (e.g.,
JavaScript)

Zebiba N. 33
Hybrid Implementation Systems
• A high-level language program is translated to
an intermediate language that allows easy
interpretation
– Faster than pure interpretation

Zebiba N. 34
Programming Environments
• A collection of tools used in software development
 UNIX
– An older operating system and tool collection
– Nowadays often used through a GUI that runs on top of UNIX
 Microsoft Visual Studio.NET
– A large, complex visual environment
– Used to build Web applications and non-Web applications in any .NET
language
 NetBeans
– Related to Visual Studio .NET, also for Web applications in Java

Zebiba N. 1-35
Review Question

 Why study the concept of programming languages?

 what are the major Programming Domains?

 What are the Models of Programming Languages?

 What are the Most important criteria for evaluating


programming languages?

 What are the Major influences on language design?

 Explain Implementations of PL and Compilation Process

Zebiba N. 1-36
Thank you

Zebiba N. 37

You might also like