1. Nature of Programming Languages
Introduction
Nguyễn Hữu Đức
Faculty of Information Technology
Hanoi University of Technology
2. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Contents I
1 Introduction
2 What is a Programming Language?
3 Abstractions in Programming Languages
4 Computational Paradigms
5 Language Definition
6 Language Translation
7 Language Design
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 2 / 54
3. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Goal of this course
There are over 2500 programming languages
We shall not study any special programming language
We are going to study major principles and concepts of programming
languages.
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 3 / 54
4. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computers and Programming Languages
Before 1940s: “hard-wired” computers
“Special purpose” computing
After 1940s: John Von Neumann’s computers
series of codes could be stored as data and would be executed by CPU
“General purpose” computing
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 4 / 54
5. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
John Von Neumann’s Machine
developed in 1940’s so that a series of codes could be stored in memory
as data and would be executed by CPU.
“memory” : where programs and data are stored
“CPU” : sequentially executes instructions
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 5 / 54
6. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
What is a Programming Language?
binary instructions
low-level languages (i.e. assembly)
machine-dependent
difficult to write and to understand programs
low-level of abstraction
high-level languages (i.e. C, Java,...)
higher-level of abstraction
code does not change (or little change) from machine to machine
easier to write and to understand
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 6 / 54
7. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
What is a Programming Language?
Definition
A programming language is a notational system for describing computation in
machine-readable and human-readable form.
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 7 / 54
8. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computation
Turing machine
A mathematical model of computation
High precision
Can carry out any computation that any current computer can carry out
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 8 / 54
9. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computation
Church’s thesis
It is not possible to build a machine more powerful than a Turing machine
Practical aspect of this course
including mathematical computation, text processing, information storage
and retrieval,...
concentrating on “general purpose” programming languages (“special
purpose” languages exist)
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 9 / 54
10. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Machine readability
Machine-readable language can be translated efficially by a computer
into a form that can be executed by the target computer.
There must be an algorithm that translates the language
This step-by-step process is unambiguous and finite
The algorithm mus be able to translate in time proportional to the size of
the program
O(n) complexity
Machine readability is ensured by restricting the structure of a
programming language to that of “context-free languages”
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 10 / 54
11. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Human readability
Require that a programming language provide abstractions of the actions
of computers that are easilly understood
By someone who is not familiar with the details of the underlying
computer
Programming languages should tend to resemble natural languages
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 11 / 54
12. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Human readability
Require suitable mechanisms for reducing the amount of detail required
for understanding the whole program
A small change to one part of a program should not require major changes
to the entire program
Requires collection of local information in one place
Programming language is part of the software development environment
Many people involved in software development
We will not study PLs from software engineering point of view
We will focus on languages themselves
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 12 / 54
13. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Two Categories of Abstraction
Data abstraction
abstract properties of data (strings, numbers, ...) which is the subject of
computation
Control abstraction
abstract properties of the transfer of control
Ex. loops, conditional statements, procedure calls
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 13 / 54
14. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Three Levels of Abstraction
Basic abstractions
collect together the most localized machine information
Structured abstractions
collect more global information about the structure of the program
Unit abstraction
collect information about entire pieces of a program
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 14 / 54
15. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Data Abstractions - Basic Abstraction
Abstract internal representation of the common data values
Ex. integers are often stored in computer using a two’s complement
representation
Ex. floating point values are stored using IEEE 754 standard
Locations in memory are abstracted by a name – a variable
Kind of data values is given a name – data type
Example
var x : integer; // PASCAL
int x; // C
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 15 / 54
16. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Data Abstractions - Structured Abstraction
Data structures abstract collection of data that are related
Array
Record in Pascal or struct in C
datatype in ML
Example
int a[10]; // Array in C
struct Tree { // struct in C
struct Tree *left, *right;
void *data;
}
datatype ’a tree = // datatype in ML
Nil
| Node of {left : ’a tree, right : ’a tree, data : ’a}
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 16 / 54
17. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Data Abstractions - Unit Abstraction
Collect related code into specific locations in the program
Data encapsulation and Information Hiding
Ex. modules in ML, classes in object-oriented languages, packages in Java
class is also considered as a structured abstraction
Reusability
Ability of reuse data abstration in different parts of a program
Save writing code from scratch
Components : Operationally complete pieces of programs
Containers : Data structures containing other user-defined data
Basis for language library
Many interface standards (COM, CORBA)
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 17 / 54
18. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Control Abstractions - Basic Abstraction
Statements in a programming language
combine a few machine instruction into a more understandable abstract
statement
Ex. “x := x + 3” fetches, adds and stores values
Ex. “GOTO 10” abstracts the jump instruction
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 18 / 54
19. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Control Abstractions - Structured Abstraction
Divide a program into groups of instructions that are nested within tests
that govern their execution
Selection statement (“if”, “case”)
They can be nested
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 19 / 54
20. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Control Abstractions - Structured Abstraction
Selection statement in C
if (x > 0) {
numSols = 2;
r1 = sqrt(x);
r2 = - r1;
} else {
numSols = 0;
}
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 20 / 54
21. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Control Abstractions - Structured Abstraction
Selection statement in ML
case numSols(x) of
0 => []
2 => [sqrt(x), - sqrt(x)]
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 21 / 54
22. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Control Abstractions - Structured Abstraction
Looping mechanisms: for, while, repeat ... until,
loop
Subroutines (procedures)
Allows programmer to consider sequence of actions as one action
Subroutines must be declared
Subroutines must be called (they have point of invocation or activation)
Formal parameters are defined in declaration
Arguments or actual parameters are passed when subroutine called
Information about subroutine must be saved at the point of invocation and
restored when subroutine finishes
Runtime environment or operational stack
A subroutine can return a value or does not need to return a value when
finished
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 22 / 54
23. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Control Abstractions - Structured Abstraction
Loop and subroutine in C
int gcd(int u, int v) {
int x = u;
int y = v;
int t;
while ( y != 0) {
t = y;
y = x % y
x = t;
}
return x;
}
Loop and subroutine in ML
fun gcd(x, 0) = x
| gcd(x, y) = gcd(y, x mod y)
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 23 / 54
24. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Control Abstractions - Unit Abstraction
Procedures can be collected into a package or a program unit
Carefully controlled interfaces
Can be understood without knowing the details
Reusability and library buiding are the same as data unit abstraction
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 24 / 54
25. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Parallel Programming Mechanism
Programming languages that are capable of executing parts of a program
simultaneously
They also have mechanism for synchronization and communication
among programs or parts of a program
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 25 / 54
26. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Abstraction Mechanism
All abstractions are provided for human readability
They should also provide all mechanisms to do any computation that a
Turing machine can do
Turing complete
A programming language is Turing complete provided it has integer variables
and arithmetic and sequentially executes statements, which include asignment,
selection (if) and loop (while) statement.
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 26 / 54
27. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computational Paradigms
Most of programming languages are designed based on Von Neumann
model
Sequential execution of instructions
Variables represent memory locations
Asignment allows program to operate on these memory variables
Programming languages characterized by these properties are called
imperative (or procedural) programming languages
It is not neccessary for a programming language to be imperative
Von Neumann model restricts parallel computation, non-deterministic
computation, or computation that does not depend on order (Von
Neumann Bottleneck)
Computational paradigms
Imperative paradigm
Functional paradigm
Logic paradigm
Object-oriented paradigm
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 27 / 54
28. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computational Paradigms - Imperative Paradigm
Properties of imperative programming languages
Sequential execution of instructions
Variables represent memory locations
Asignment allows program to operate on these memory variables
Sequentially execution of statements
Also called “procedural” programming languages
Most PLs today based on this model
Deterministic computation
Can’t use this to describe parallel computation
Typical examples: FORTRAN, PASCAL, C
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 28 / 54
29. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Imperative Paradigm
Example of gcd in C
int gcd(int u, int v) {
int x = u;
int y = v;
int t;
while ( y != 0) {
t = y;
y = x % y
x = t;
}
return x;
}
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 29 / 54
30. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computational Paradigms - Functional Paradigm
Based on description of computation on the evaluation of functions or
the application of functions to known values
Also called “applicative” languages
Functional call
Passing the values as parameters
Actual evaluation of function
Returning values
No notion of variable or assignment
Repetitive operation can be done with recursion instead of loops
Typical examples: LISP, SCHEME, ML, HASKEL
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 30 / 54
31. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Functional Paradigm
Example of gcd in ML
fun gcd(x, 0) = x
| gcd(x, y) = gcd(y, x mod y)
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 31 / 54
32. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computational Paradigms - Logic Paradigm
Based on symbolic logic
Program is a set of statements that describe what is true about a desired
result
No loops or selections
Control is suplied by the underlying program
Also called “descritive” language
Typical example: PROLOG
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 32 / 54
33. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Logic Paradigm
Example of gcd in PROLOG
gcd(U, V, U) :- V = 0
gcd(U, V, X) :- not(V = 0),
Y is U mod V,
gcd(V, Y, X)
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 33 / 54
34. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Computational Paradigms - Object-oriented Paradigm
Based on notion of an object which can be loosely described as a
collection of memory locations together with all the operations that can
change the values of these memory locations
Objects are grouped into classes that represent all the objects with the
same properties
Object can be created as an instance of a class
Members represent memory locations
Methods represent operations on these memory locations
Provide data encapsulation and information hiding
Typical example: C++, JAVA
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 34 / 54
35. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Object-oriented Paradigm
Example of gcd in JAVA
public class IntWithGcd {
public IntWithGcd(int val) {value = val;}
public int intValue() { retuen value; }
public int gcd(int v) {
int z = value;
int y = v;
while ( y != 0) {
int t = y;
y = z % y;
z = t;
}
return z;
}
private int value;
};
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 35 / 54
36. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Definition
A complete, precise description is needed
To know precisely what a computation does
Even today, most PLs are described informally by the so-call “reference
manual”
Reference manual is not precise
With these definitions, it is possible to reason mathematically about
programs
formal verification, proof of behavior
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 36 / 54
37. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Definition
Formal definition of a language leads to standadization
Language becomes independent of hardware platform, OS, ...
ANSI (American National Standards Institute) and ISO (International
Organization for Standardization) standards
definitions of many languages including PASCAL, FORTRAN, C, C++,
Ada, PROLOG
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 37 / 54
38. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Definition
From practical point of view, formal definition needed
because question about program’s behavior may arise
to provide discipline during the design of the language
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 38 / 54
39. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Definition
Two parts of language definition
Syntax : structure of the language
Semantics : Meaning of the language
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 39 / 54
40. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Syntax
Language syntax is like the grammar of a natural language
Language syntax can be given in context free grammar form
<if-statement> ::= if (<expression>) <statement>
[else <statement>]
Language syntax can be given in form using special character and
formatting
if-statement ::= if (expression) statement [else statement]
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 40 / 54
41. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Syntax
Lexical structure
Similar to spelling in a natural language
provide structure of words or token in the language
if, else are token
“+”, “<=” are token
“;”, “.” are token
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 41 / 54
42. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Semantics
Semantics describes the meaning of the language
effect of execution of a piece of code
much more complex and difficult to describe precisely
C if-statement, Kernighan and Richie [1988]
An if-statement is executed by first evaluating its expression, which must have
arithmetic or pointer type, including all side effects, and if it compares
unequal to 0, the statement following the expression is executed. If there is an
else part, and the expression is 0, the statement following the “else” is
executed.
What’s happen if there is no else part?
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 42 / 54
43. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Semantics
Usin formal methods to describe the language semantics
No generally accepted method
There are several methods
operational semantics
denotational semantics
axiomatic semantics
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 43 / 54
44. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Translation
There must be a translator that accepts a program written in the
programming language and
either executes the program directly (interpreter)
or transforms into a form in which it can be executed (compiler)
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 44 / 54
45. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Interpretation
Source code
input interpreter output
one-step process that simulates the underlying machine
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 45 / 54
46. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Compilation
Source target
code code
further Executable
compile transtaltion code
Executable code
input processor output
two-step process that simulates the underlying machine
output of the compiler is called the target code
most common target language is assembler language
assembler program is then generated to object code of the underlying
machine and then linked with other object codes by a linker and loaded
into memory by a loader
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 46 / 54
47. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Compilation
Pseudo-translater
translates a source program into an intermediate code
interpreter will execute the intermediate code
A language is different from a translator for that language
Some translators may or may not adhere closely to the language definition
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 47 / 54
48. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Phases of a Translator
Lexical Analyzer (or scanner) converts source code (in text form) into
sequence of tokens representing keywords, identifiers, constants,...
Syntax Analyzer (or parser) determines structure of the sequence of
token
Semantic Analyzer must determine enough of the meaning of the
program to allow the execution of the target code
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 48 / 54
49. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Operations Performed by a Translator
Translater must maintain a run-time environment which has suitable
memory for program data and record the progress of execution
Interpreter itself maintains a run-time environment
Compiler maintains run-time environment indirectly through target code
Language may require a pre-processor which is run to make the program
suitable for translation
Example
#define PI 3.14
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 49 / 54
50. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Static and Dynamic Properties
Static propeties
Determined prior to execution
Static memory allocation: all variables located in a fixed possitions during
the execution
Dynamic propeties
Determined during the execution
Dynamic memory allocation
Static properties are useful for compilers
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 50 / 54
51. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Static and Dynamic Properties
Language with large amount of static properties more suitable for
compilation
Language with large amount of dynamic properties more suitable for
interpretation
Imperative languages tend to be compiled
Functional and logic languages tend to be interpreted
Languages like C have both aspects
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 51 / 54
52. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Efficiency
Interpreted language generally less efficient than compiled language
Compiler efficiency can be boosted by optimization
Interpreters are better than compilers if interactive input and output
required
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 52 / 54
53. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Error-handling
Translators should try to fix errors or provide meaningful error messages
Translators should be able to find more errors after the first one caught
Errors can be caught by
scanner: use illegal characters
parser: syntax error (Ex. x = 2 +* 6)
semantic analyser: undefined variables, incompatible type
Semantic error can be found during the execution
Index out-of range
Divide by zero
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 53 / 54
54. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Error-handling
Not all logic error can be found by the language translator (Ex. infinite
loops)
Language specification often specifies
What error must be caught at compile time
What error must be generated at run time
What error go undetected
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 54 / 54
55. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Debugging
Translator must provide option for debugging, for interfacing with OS
and with IDE
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 55 / 54
56. Introduction What is a Programming Language? Abstractions in Programming Languages Computational Paradigms Language Definition La
Language Design
Machine readability and Human readability are main requirements
Data abstractions and control abstractions
Complexity control
Nature of programming Languages N.H. Đức FACULTY OF INFORMATION TECHNOLOGY 56 / 54