Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Survey of programming languages

The document provides an analytical overview of various programming languages, their paradigms, and historical context. It covers procedural, object-oriented, functional, declarative, and scripting languages, along with their design trade-offs and implementation methods. Additionally, it discusses fundamental characteristics, types, and expressions in programming languages, emphasizing the importance of understanding these concepts for effective language selection and software development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Survey of programming languages

The document provides an analytical overview of various programming languages, their paradigms, and historical context. It covers procedural, object-oriented, functional, declarative, and scripting languages, along with their design trade-offs and implementation methods. Additionally, it discusses fundamental characteristics, types, and expressions in programming languages, emphasizing the importance of understanding these concepts for effective language selection and software development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

survey of programming

languages

1
This is the analytical and formal study of different
programming languages in terms of data types
and structures

2
Objectives of the course
•improve background for choosing appropriate languages
•ability to easily learn new languages and technologies
as they arrive (ability to focus on the big picture and not
the details)
•some things cannot be expressed as easily in certain
languages increase capacity to express ideas about
computation (thinking out of the box)
•why these languages?
•increase ability to design and implement new languages
•to better appreciate the historical context
•to become a well-rounded computer scientist
3
•in summary, this course shd make you a better
• History of programming languages

4
programming paradigms

• Procedural languages are those languages that use a programming


paradigm which is dependent on procedure calls. The procedures may
be subroutines, or functions, which contain a series of instructions
• Eg Fortran, ALGOL, COBOL, and BASIC, Pascal and C

• They are modular in nature where inputs are arguments and result
are returned as values.
• They permit program segment reuse
5
In procedural or imperative languages, Programs are
composed of a collection of variables, data structures,
and subroutines.
It specifies a series of well-structured steps and procedures
within its programming context to compose a program and
statements, functions and commands to complete a
computational task are systematic

6
OBJECT-
OBJECT-ORIENTED LANGUAGES
• In OOL, programs are segmented into objects that expose (methods)
and data using interfaces. OOL combines procedures and data
structures, c++, java are examples

7
FUNCTIONAL LANGUAGES
• functional languages are languages that promotes writing programs
purely based on mathematical functions

• They are designed to handle symbolic computation and list


processing. Eg Lisp, Python, Erlang,

8
Declarative languages
• languages. Declarative languages are nonprocedural or very high
level languages that a program specifies what is to be done rather
than how to do it.

• A declarative format is one in which you declare the intention/end-


result of the program, instead of how it should be arrived at
• HTML, XML, CSS, SQL, Prolog

9
SCRIPTING LANGUAGES
• A script or scripting language is a
computer language with a series of commands within
a file that is capable of being executed without being
compiled. Good examples of server-
side scripting languages include Perl, PHP, and
Python, javascript

10
Influence of computer architecture on
languages
• architecture of computers has had a profound effect on language design.

• Most of the popular languages are tailored towards the von Neumann
architecture where both data and programs are stored in the same
memory and are mostly imperative.

• The CPU that executes instructions, is separate from the memory.


Therefore, instructions and data must be pipelined or transmitted, from
memory to the CPU.

• Results of operations in the CPU must be moved back to memory.


11
• the central features are variables, which model the memory cell,
assignment statements, which are based on piping operation and the
iterative form of repetition, which is the most efficient way to
implement repetition on this architecture.

• Operands in expressions are piped from memory to the CPU and the
result of evaluating the expression sent back to the memory cell
represented by the left side of the assignment as in a=5;

• Iteration is fast on von Neumann computers because they are stored


in adjacent cells of memory and repeating the execution of a section
of code requires only a simple branch instruction.

12
Recap- Architecture
• Universal Turing machine (UTM) - used only a memory tape and a finite
state box to move the tape, write a symbol or erase one after consulting
a small table of states and responses

• ENIAC - separate memory for data and program

• Von Neumann architecture

13
14
15
• The execution of a machine code program on a von Neumann
architecture computer occurs in a process called fetch-execute cycle.
See algorithm below

Initialize the program counter


Repeat forever
Fetch the instruction pointed by the program counter,
Increment the program counter to point at the next instruction,
Decode the instruction.
Execute the instruction.
End repeat

In class- demonstrate pseudo coding of the above

16
• In a functional language programming can be done without the kind
of variables that are used in imperative languages, without
assignment statements and without iteration.

• The structure of imperative programming languages is modeled on a


machine architecture rather than on the abilities and inclinations of
the users of programming languages

17
Programming Methodologies evolution
• New software development methodologies (e.g., object- oriented software
development) led to new programming paradigms and by extension, new
programming languages

• 950s 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
18
LANGUAGE DESIGN TRADE-
TRADE-OFFS
• Reliability vs. Cost of Execution – Conflicting criteria – a language like Java may
demand all references to array elements be checked for proper indexing, which
leads to increased execution costs

• Readability vs. Writability – a language providing 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 poorly rated readability like in
APL

• Writability (flexibility) vs. Reliability – Another conflicting criteria – Example:


pointers may be very powerful and flexible but are unreliable as in C++

19
IMPLEMENTATION METHODS

• Compilation – Programs are translated into machine language

• Pure Interpretation – Programs are interpreted by another program


known as an interpreter

• Hybrid Implementation Systems – A compromise between compilers and


pure interpreters.

• JIT

20
Fundamental characteristics of a
programming language
• Universality -every problem must have a solution that can be programmed
in the language, if that problem can be solved at all by a computer.

• Naturality- ability to have a natural flow and broad eg not depenedtn on


numbers only or string inputs only

• Implementability -must be possible to execute every well-formed program


in the language.

• Focus, C or C++, Java

21
What makes up a programming language
• syntax is concerned with the form of programs:
• how expressions, commands, declarations, and other constructs must
be arranged to make a well-formed program eg for… next loop

• semantics is concerned with the meaning of programs: how a well-


formed program may be expected to behave when executed on a
computer.

• pragmatics is concerned with the way in which the language is


intended to be used in practice.

22
Language processor
• Any system for processing programs – executing programs, or
preparing them for execution – is called a language processor.

23
24
Values and types
Data are raw quantities, characters, or symbols on which operations
are performed by a computer.

• A value is any entity that can be manipulated by a program. Values


can be evaluated, stored, passed as arguments, returned as function
results

25
Different types of values supported by
languages
• C supports integers, real numbers, structures, arrays, unions, pointers
to variables, and pointers to functions. (Integers, real numbers, and
pointers are primitive values; structures, arrays, and unions are
composite values.)
• C++ supports all the above types of values plus objects. (Objects are
composite values.)
• JAVA supports booleans, integers, real numbers, arrays, and objects.
• (Booleans, integers, and real numbers are primitive values; arrays and
• objects are composite values.)

26
A type is a set of values that supports similar
operations in a programming language
• If v is a value of type T, we mean simply that v ∈ T.
eg to be given in class

• When we say that an expression E is of type T, we are


asserting that the result of evaluating E will be a value
of type T.
eg to be given in class

27
• A primitive value is one that cannot be decomposed into simpler
values.

• For example, JAVA has boolean, char, int, and float,

• Boolean = {false, true} (2.1)


• Character = {. . . , ‘a’, . . . , ‘z’, . . . , ‘0’, . . . , ‘9’, . . . , ‘?’, . . .} (2.2)
• Integer = {. . . ,−2,−1, 0,+1,+2, . . .} (2.3)
• Float = {. . . ,−1.0, . . . , 0.0, . . . ,+1.0, . . .}

28
• The cardinality of a type T, written #T, is the number of distinct
values in T.
• For example:
• #Boolean = 2

29
Not all languages have a distinct type corresponding to Boolean. For
example, C++ has a type named bool, but its values are just small
integers;
there is a convention that zero represents false and any other integer
represents true. This convention originated in C.
• Not all languages have a distinct type corresponding to Character. For
example, C, C++, and JAVA all have a type char,
• Some languages provide not one but several integer types.
For example, JAVA provides byte {−128, . . . ,+127}, short
{−32 768, . . . ,+32 767}, int {−2 147 483 648, . . . ,+2 147 483 647}, and
long {−9 223 372 036 854 775 808, . . . ,+9 223 372 036 854 775 807}.

30
• In a Cartesian product, values of several (possibly different) types are
grouped into tuples.
• We use the notation (x, y) to stand for the pair whose first component
is x and whose second component is y. We use the notation S × T to
stand for the set of all pairs (x, y) such that x is chosen from set S and
y is chosen from set T. Formally:
• S × T = {(x, y) | x ∈ S; y ∈ T}

31
Objects
JAVA objects
Consider a JAVA program containing the following class declarations:
class Point {
private float x, y;
. . . // methods
}
class Circle extends Point {
private float r;
. . . // methods
}
class Rectangle extends Point {
private float w, h;
. . . // methods
}

32
Recursive types
• A recursive type is one defined in terms of itself eg list and trees

33
BUILD IN PRIMITIVES
• Consider the following JAVA declarations:
int countryPopulatn;
long worldPopulatn;
The variable countryPopulatn could be used to contain the current
population of any country (since no country yet has a population
exceeding 2 billion). The variable worldPopulatn could be used to
contain the world’s total population.

But note that the program would fail if worldPopulatn’s type were
int rather than long (since the world’s total population now
34
exceeds 7 billion).
C++ type definition:

•enum Month {jan, feb, mar, apr, may, jun,


•jul, aug, sep, oct, nov, dec};
•defines Month to be an integer type, and
binds jan to 0, feb to 1, and so on. Thus:
•Month = {0, 1, 2, . . . , 11}

35
• A discrete primitive type is a primitive type whose values
have a one-to-one relationship with a range of integers.

• A composite value (or data structure) is a value that is


composed from simpler values. composite values can be
understood in terms of a small number of structuring
concepts, which are:
• Cartesian products (tuples, records)
• mappings (arrays)
• disjoint unions (algebraic types, records, objects)
• recursive types (lists, trees). 36
TYPE SYSTEM
• A programming language’s type system groups values
into types. This allows programmers to describe data
effectively. It also helps prevent programs from
performing nonsensical operations, such as
multiplying a string by a boolean.
• Performing such a nonsensical operation is called a
type error.

37
STATIC VS DYNAMIC TYPING
• Before any operation is to be performed, the types of its operands must be
checked in order to prevent a type error.
In a statically typed language, each variable and each expression has a
fixed type (which is either explicitly stated by the programmer or inferred
by the compiler). Using this information, all operands can be type-checked at
compile-time. Give examples
In a dynamically typed language, values have fixed types, but variables and
expressions have no fixed types. Give examples
• LISP, PROLOG, BASIC, PERL, and PYTHON are examples of dynamically typed
languages.
38
EXAMPLE
C++ static typing
Consider the following C++ function definition:
bool even (int n) {
return (n % 2 == 0);
}
def even (n):
return (n % 2 == 0)
• ADD QBASIC TO EXPLAIN TO STUDENTS IN CLASS

39
JUSTIFICATION FOR CHOOSING A TYPE
Static typing is more efficient. Dynamic typing requires (possibly
repeated)
run-time type checks, which slow down the program’s execution.

Static typing requires only compile-time type checks, whose cost


is minimal (and one-off). Moreover, dynamic typing forces all
values to be tagged (in order to make run-time type checks
possible), and these tags take up storage space. Static typing
requires no such tagging.

40
• Static typing is more secure: the compiler can certify that
the program contains no type errors. Dynamic typing provides
no such security. (This point is important because type errors
account for a significant proportion of programming errors.)

• Dynamic typing provides greater flexibility, which is needed


by some applications where the types of the data are not
known in advance.

41
Expressions
An expression is a construct that will be evaluated to yield a
value.
Fundamental forms of expression:
• literals
• constructions
• function calls
• conditional expressions
• iterative expressions
• constant and variable accesses 42
Literals
The simplest kind of expression is a literal, which
denotes a fixed value of some type. EG 365 3.1416
false '%' "What?“

A construction is an expression that constructs a


composite value from its component values. In some
languages the component values must be literals; in
others, the component values are computed by
evaluating subexpressions. The following C or C++
structure construction: {jan, 1}. 43
In the following C++ code:
int size[] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
...
if (is_leap(this_year))
{
size[feb] = 29
};
EG an array construction is used to initialize the variable size. The component
values must be literals.

44
JAVA object constructions
The following JAVA object construction:
new Date(today.m, size(today.m))

• Function calls
A function call computes a result by applying a function procedure
(or method) to one or more arguments. The function call typically
has the form ‘‘F(E)’’ An operator may be thought of as denoting a
function.
USE EXAMPLES TO ILLUSTRATE

45
• A conditional expression computes a value that
depends on a condition.

• to use 3 languages to illustrate above

• An iterative expression is one that performs a


computation over a series of values (typically the
components of an array or list),

46
VARIABLES
• Variables and storage ‘‘Once a programmer has
understood the use of variables, he has
understood the essence of programming

47
• In imperative (an object-oriented and concurrent)
programming languages, a variable is a container for a
value, and may be inspected and updated as often as
desired.

• Variables are used to model real-world objects whose


state changes over time, such as today’s date, the
current weather, the population of the world, or a
country’s economic performance.

48
• A variable is either a simple or composite in
nature

• A simple variable occupies a single allocated


storage cell.
• A composite variable occupies a group of
contiguous allocated storage cells.

49
• A storable value is one that can be stored in a single storage cell.

• C++’s storable values are primitive values and pointers. (Structures, arrays,
unions, and objects are not storable, since none of these can be stored in
a single storage cell. Functions also are not storable, since they cannot be
stored at all. However, pointers to all of these things are storable.)
• JAVA’s storable values are primitive values and pointers to objects. (Objects
themselves are not storable, but every object is implicitly accessed through
a pointer.)

50
Explaining simple variable in C++
• C simple variable
• Consider the simple variable declared in the following C block:
• { int n;
• n = 0;
• n = n+1;
•}

51
Explaining composite values in c++
• C++ record and array variables
• Consider the following C++ (or C) declarations:
• struct Date {
• int y, m, d;
• };
• Date today;

• Use arrays also to explain

52
Total vs selective update of composite variable…
• Static vs dynamic vs flexible arrays…..
• A static array is an array variable whose index range is fixed at compile-
time. A dynamic array is an array variable whose index range is fixed at
the time when the array variable is created.

53
Copy semantics vs reference semantics
• When a program assigns a composite value to a variable of the same
type, what happens depends on the language. There are in fact two
distinct possibilities:
• Copy semantics. The assignment copies all components of the
composite value into the corresponding components of the composite
variable.
• Reference semantics. The assignment makes the composite variable
contain a pointer (or reference) to the composite value.

54
Lifetime
• Every variable is created (or allocated) at some definite time, and destroyed
(or deallocated) at some later time when it is no longer needed. The interval
between creation and destruction of a variable is called its lifetime.
We can classify variables according to their lifetimes:
• A global variable’s lifetime is the program’s run-time.
• A local variable’s lifetime is an activation of a block.
• A heap variable’s lifetime is arbitrary, but is bounded by the program’s
run-time.
• A persistent variable’s lifetime is arbitrary, and may transcend the run-time
of any particular program.

55
• Aglobal variable is one that is declared for use throughout the
program.
• A local variable is one that is declared within a block, for use only
within that block

56
Use example to illustrate variable lifetime
• A heap variable is one that can be created, and destroyed, at any
time during the program’s run-time.
• A heap variable remains reachable as long as it can be accessed by
following pointers from a global or local variable.

• A pointer is a reference to a particular variable. In fact, pointers are


sometimes called references. The variable to which a pointer refers is
called the pointer’s referent.
• A null pointer is a special pointer value that has no referent.
• In terms of our abstract storage model a pointer is essentially
• the address of its referent in the store. However, each pointer also
has a type, and the type of a pointer allows us to infer the type of its
referent.
57
• In C++, each value of type ‘‘T*’’ is either a pointer to a variable of type
T or a null pointer.
• Consider the following declarations:
• struct IntNode {
• int elem;
• IntNode* succ;
•}
• IntNode* p;

58
Dangling pointers
A dangling pointer is a pointer to a variable that has been destroyed.
Dangling
pointers arise from the following situations:
• A pointer to a heap variable still exists after the heap variable is
destroyed.
• A pointer to a local variable still exists (e.g., it is stored in a global
variable)
at exit from the block in which the local variable was declared.

use other examples, algorithm, and other code snippets to explain

59

You might also like