Вы находитесь на странице: 1из 5

Page 1 of 5

COMPUTER SCIENCE
GRADE 12
Handout on Programming Paradigms, The Stages of the translation
process & the role of preprocessors & Linkers
A Programming paradigm is a general approach to programming or the solution of
problems using a programming language. Thus the programming languages that
share similar characteristics are grouped together in the same paradigm. A
programming paradigm is both a method of problem solving and an approach to
programming language design.
The following are referred to as programming paradigms:
1. Procedural or imperative
2. Object-oriented
3. Functional
4. Declarative
5. Aspect-Oriented
6. Scripting
Procedural or Imperative
Procedural paradigm describes a way of writing programs through the use of
procedures or functions which contain a sequence of programming statements. In
other words the commands are executed in the exact order as stated in program.
This paradigm tells a computer how to do something rather than what to do. The
order is in which the commands are executed is determined by control structures.
An example of this approach would be in comparison to a recipe for a cake, as each
instruction in the recipe must be carried out in the precise order that was stated in
order for the cake to come out. Examples of programming languages linked to this
paradigm are: C, Pascal, BASIC, FORTRAN, COBOL, Assembly Machine language, etc.
Advantages:

It is relatively simple
In comparison to other programming paradigm it is easy to execute by
compilers and interpreters.
Procedural programs utilize low memory
Programs are written in a sequence of instructions and procedure, making
them easy to read and understand.
Disadvantages:

It is difficult to use a procedural program to solve a complex problem which


might not be easy to visualize from a procedural view point.
Procedural programming tends to be relatively low-level language compared
to some other paradigms and as a result can be very much less productive.
Difficult to debug at times.

Object-Oriented Paradigm (OOP)


The idea behind object-oriented programming is that a computer program is
composed of a collection of individual objects, as opposed to a traditional view in
which a program is a list of instructions to the computer. Each object is capable of
sending messages, processing data, and receiving messages from other objects.
Objects are treated as real life objects. Just like real life objects these objects have
their own identities or attributes, behaviours and states. OOP is characterized by
the defining of classes of objects and their properties.
OOP provides inheritance of properties which is one way of reducing the amount of
programming requires in developing a program. Examples of programming
languages associated with this paradigm are: Java, C++, Scala, etc. A diagram
depicting a typical OOP structure using a car as the class is shown below. All objects
of the class cars will have the same general attributes. So, all cars will have a make,
Page 2 of 5

model and colour and will have similar behaviours such as to accelerate, steer and
stop.
Example of a Class Object Model

Class: Cars
Make Attributes/stat
Model es
Colour
Behaviours/metho
Accelerate()
ds
Steer()
Stop()

Example of Java program fragment


Class Cars
{
public static void main(String[] args);
{
String make, model, colour;
Int number_of_cars;

Accelerate()
{
}
Steer()
{
}
Stop()
{
}
}
}

Functional Paradigm
In this paradigm we express computations as the evaluation of mathematical
functions combined in expressions. Functional programming avoids changeable
data. All computations in functional programming are carried out by calling
functions. In functional programming, functions are used to state what to do (to an
extent), and less of how to do it. While all languages try to provide a representation
of basic functions like addition, functional languages support a functional way of
expressing computations on large and complex structures. In a pure functional
language a mathematical identity like:
fred(x) + fred(x) = 2*fred(x)

should hold (not change). This is not necessarily the case for a non-functional
language, for example in Pascal or C fred might be a procedure which had a side-
effect (value will likely change), so that calling it twice has a different effect from
calling it once. For example fred might contain the assignment g:= g + 1, where g is
a global variable. In other words a functional language will produce output
dependent on only the input of that function and nothing else; it will also produce
the same result each time it is called with the same values as input,
The primary motivation of writing functionally is to develop programs whose
behaviour can easily be analysed mathematically, and in any case is easy to predict
and understand. Examples: Curl, Scheme, etc.

Declarative Paradigm
This paradigm describes programs that specify the necessary steps to obtain the
desired result of a user. Declarative programming is an approach to computer
programming that involves the creation of a set of conditions to reach a solution,
Page 3 of 5

but leaves the interpretation of the specific steps needed to arrive at that solution
up to an unspecified interpreter. This programming thus takes a different approach
from the traditional imperative programming found in Fortran, C or Pascal which
requires the programmer to provide a list of instructions to execute in a specified
order. In other words, declarative programming provides the what, but leaves the
how up to interpretation. This is why declarative solutions are said to have to two
independent phases: declaration and interpretation. This paradigm includes both
functional and logic programming. Examples of programming languages associated
with this paradigm are Prolog, SQL, Oz, General Purpose Simulation System (GPSS
(simulation).

Aspect-Oriented Paradigm

Aspect-Oriented languages seek to solve the cross-cutting concerns of object-


oriented programming. These programs implement a program construct called an
aspect, which captures the cross-cutting parts of a software program. Cross-cutting
occurs in, example, object-oriented programs when the responsibilities of one class
is shared with others, causing these responsibilities to cross-cut the entire system
and affect parts of many classes. The removal of these cross-cutting features
causes the classes to be truly independent and totally reusable, as they were
intended to be. Aspects can also log certain method calls in object oriented
programs, that is, if a particular method is called, the aspect will make a note of it.
Aspect-Oriented programming is not used as a stand-alone programming paradigm,
but is used in conjunction with other paradigms so as to provide a new, higher level
of abstraction that could make it easier to design and understand software systems.
Aspects are seen merely as valuable tools. Aspects break the encapsulation of
objects somehow, but do so in a controlled way. Aspects may have access to the
private part of their associated objects, but this doesnt allow for the encapsulation
between objects of different classes to be compromised.
Examples of programming languages integrated with aspect-oriented concepts are
AspectJ (an extension of Java), AspectC++ and Spring.

Scripting Paradigm

Scripting languages seek to provide a connection between applications by accepting


the output from one application and using it as input for another application.
Currently, they are not used to write code for full programs, but only to write
commands to automate simply tasks that a user could do themselves and to make
connections between different applications.

A Script is a sequence of commands embedded in a program which tells the


program to execute a specific procedure when a webpage is opened, a hyperlink
text is clicked, a picture is hovered over etc. Scripts do not have data type
implementations, that is, there is no integer, string, character, float etc. Instead,
each variable created can store any variable type. So a variable called var1 could
store 123, girl or 2.5. Errors are generated only if the variable is used
inappropriately rather than because it is not intended to store a value of a particular
data type, as required for other programs such as C.

For example in C, one would first have to declare a variable called fname to
someones first name:
char fname[] = KIMBERLEY;
In scripting the following would be done fname = KIMBERLEY;

Scripts are interpreted rather than compiled. Because there is no compiler involved,
and consequently no compile time, object code is delivered faster. Scripts are used
in webpages in web browsers, used to arrange collection of user interfaces on the
screen, used to display a text or graphics box when the mouse glides over a picture
or hyperlink, etc. Scripting languages are also becoming popular in enhancing
gaming features and in mobile application development. Examples of Scripting
languages are TCL, Perl, Python, JavaScript, Visual Basic etc.

Stages in the translation/compilation process


Page 4 of 5

The translation /compilation process involves the following stages:

Lexical analysis stage


Syntax analysis stage
Semantic analysis stage
Intermediate code generation stage
Code generation stage

Lexical analysis

At the lexical analysis stage all comments and unnecessary spaces are removed
and some simple error checking is performed. For example: if the language requires
a limit on the length of identifiers or string constants, the lexical analyzer will
determine if the identifier is too long. Additionally, in the lexical analysis stage,
source code is analyzed from left to right and the smallest possible language
components are grouped into tokens. Tokens are sequences of characters with a
collective meaning. Tokens include datatypes such as integer, char, string etc.;
arithmetic, relational and logical operators; punctuation; and reserved words. A
lexical analyzer takes a source program as its input and produces a stream of
tokens as output. The lexical analyzer recognizes such as Name1 for a variable
token, 68 for an integer token or Angie for a string constant token.

Syntax analysis

Syntax analysis is the process of determining whether the syntax of the program is
correct. The codes are checked to see it they follow the rules of a particular
programming language. The syntax rules of a language define how we can put
symbols, reserved words, and identifiers together to make a valid program.

Semantic analysis

Semantic analysis is concerned with the meaning or interpretation of


codes/commands in the context in which they are used. During semantic analysis,
the commands/codes in the program are tested to see if they are correctly used
according to the structure of the programming language used. The semantics of a
program statement define what that statement means (its purpose or role in a
program). A program that is syntactically correct is not necessarily logically
(semantically) correct.

Intermediate code generation (Code optimization and code generation)

At this stage machine codes are generated. It may include an optimization stage
where the codes are modified so that it is executed faster.

Code Optimization

This is the transforming of intermediate code to make it use memory more


efficiently and be executed faster, without changing the output of the code or
producing any other undesirable side-effects. This is done by the compiler.

Code Generation

This stage involves the compilers code generator converting the optimized
intermediate code into an executable file that can be executed by a machine. This
executable is in machine code. Code is also further optimized to suit the particular
machine.

A preprocessor is a text processor which manipulates the text processor which


manipulates the text of a source code by breaking up the source text into tokens so
as to locate macros (macro: an instruction which causes a series of additional
instructions to be started) and to pass the stream of tokens to the parser. This is
what happens in lexical analysis, the first stage of the translation process. The
lexical analyzer is therefore a preprocessor.
Page 5 of 5

Linkers A linker links separately compiled modules by putting the appropriate


addresses in the call and return instructions (the load module for execution). The
object code produced by the compiler may consist of separate modules of machine
code which are related to each other by call and return addresses. In addition, the
program may use library routines held separately form the main program block.
The function of the linker or linkage editor is to link all the modules together by
putting the appropriate machine addresses into all external call and return
addresses.