APP Unit I

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Advanced Programming Practise

Unit 1 – INTRODUCTION TO PROGRAMMING PARADIGM


Programming Languages – Elements of Programming languages -
Programming Language Theory - Bohm- Jacopini structured program theorem -
Multiple Programming Paradigm – Programming Paradigm hierarchy –
Imperative Paradigm: Procedural, Object-Oriented and Parallel processing –
Declarative programming paradigm: Logic, Functional and Database processing
- Machine Codes – Procedural and Object-Oriented Programming – Suitability
of Multiple paradigms in the programming language - Subroutine, method call
overhead and Dynamic memory allocation for message and object storage -
Dynamically dispatched message calls and direct procedure call overheads –
Object Serialization – parallel Computing.

Programming Language
Programming Language is a notational system for describing computations in
both machine readable and human readable form. It allows the programmer to
specify a task and the computer to execute the task. It is a notation for writing
computer programs and are text based formal languages but could also be
graphical.

Elements of programming languages


• Syntax
• Semantics
• Pragmatics are the elements of a programming language.

Syntax
It converts the High level input program into a sequence of Tokens. A lexical
token is a sequence of characters that can be treated as a unit in the grammar of
the programming languages.
First, a lexer turns the linear sequence of characters into a linear sequence of
tokens; this is known as "lexical analysis" or "lexing". Second, the parser turns
the linear sequence of tokens into a hierarchical syntax tree; this is known as
"parsing" narrowly speaking. Thirdly, the contextual analysis resolves names
and checks types.
The parsing stage itself can be divided into two parts: the parse tree, or
"concrete syntax tree", which is determined by the grammar, but is generally far
too detailed for practical use, and the abstract syntax tree (AST), which
simplifies this into a usable form.
Words are in regular language, phrases are in CFL (context free language) and
context in context-sensitive language.

Example for Lexical analysis and Parsing:

Keywords; Examples-for, while, if etc


Identifier; Examples-Variable name, function name, etc
Operators; Examples '+', '++', '-' etc
Separators; Examples ',' ';' etc

Non-tokens : Preprocessor directives, macros, blanks, tabs, newline

The syntax of a computer language is the set of rules that define the
combinations of symbols that are considered to be correctly structured
statements or expressions in that language. Computer language syntax is
generally distinguished into three levels
• Words – the lexical level, determining how characters form tokens;
• Phrases – the grammar level, narrowly speaking, determining how tokens
form phrases;
• Context – determining what objects or variables names refer to, if types
are valid, etc.

Semantics
Semantics assigns computational meaning to valid strings in a programming
language syntax. It describes the processes a computer follows when executing
a program in that specific language. Syntax therefore refers to the valid form of
the code, and is contrasted with semantics – the meaning.
Both syntax tree of previous phase and symbol table are used to check the
consistency of the given code.
Type checking is an important part of semantic analysis where compiler makes
sure that each operator has matching operands.
Semantic errors:
• Type mismatch
• Undeclared variables
• Reserved identifier misuse
• No break outside loop

Programming Language Theory


Programming language theory (PLT) is a branch of computer science that deals
with the design, implementation, analysis, characterization, and classification of
formal languages known as programming languages.

Bohm- Jacopini structured program theorem


The structured program theorem, also called the Böhm–Jacopini theorem,[1][2]
is a result in programming language theory. It states that a class of control-flow
graphs (historically called flowcharts in this context) can compute any
computable function if it combines subprograms in only three control
structures. These are
1. Executing one subprogram, and then another subprogram (sequence)
2. Executing one of two subprograms according to the value of a boolean
expression (selection)
3. Repeatedly executing a subprogram as long as a boolean expression is
true (iteration)

Corrado Böhm and Giuseppe Jacopini showed in 1966 that any non-structured
program can be rewritten by combining three techniques: sequence, selection,
and repetition (or loop).

Sequence: perform S1, then perform S2.

Selection: if Q is true, then perform S, else perform S2.


Loop: while Q is true, do S.

Programming Paradigms (PP)

• A programming paradigm is a fundamental style of computer programming,


• Serve as a way of building the structure and elements of computer programs.
• It is a way to classify programming languages based on their concepts and
constructs.
Languages can be classified into multiple paradigms.
Some programming languages follow only one paradigm [Ex: Smalltalk -
object-oriented and Haskell - functional], while others support multiple
paradigms. [Ex: C++, Java, C#, Scala, Python, Ruby]
• OO PP models a program as a collection of interacting objects;
• Functional PP models a program as a sequence of stateless function
evaluations;
• Event-driven PP models a program as a sequence of events such as user
clicks or key presses. Main loops, waits for events and callbacks are called for
specific events.
• Process PP allows a program to be modelled as a set of concurrent processes
that act upon a shared data structure.
• Pure functional PP forbids side-effects
• Structured PP disallows GoTo.

Hierarchy of Programming Paradigms

Imperitive programming paradigm

• Programmer instructs the machine how to change its state.


• Instructions are statements that can be used to assign values to variables,
control the flow of execution, and perform operations on data.

• A variable name is bound to an address and a value to bit pattern. The


location is called as l-value and the value is r-value. x=x+2, where the x on
the left is the location and x on the right is the value.

• State of a program: Collection of names, associated values, the location of


the control in the program together is the state of a program. Imperative
programs are a sequence of state changes

• Constructs of IPP: Assignment, Sequence, Control

• Assignment construct creates a statement that has a variable and


expression. x=t; x location is filled with the value of the expression t.

• Expression involves variables and constants with arithmetic/logical


operators. Literals are constants.

• Variables have to be declared before usage. Declaration ties the variable


and its location. Variables need not be declared with initialised values.

• Constant variable: The value of an variable can’t be changed after


initialising.

• Mutable variable: A variable that is not a constant is mutable


variable. To declare a const, use final keyword in Java.
• Sequence is a set of statements that are executed in a sequential manner.
Block is a sequence {s1; s2} is a sequence, where s1 gets executed to
change the state of the program and then s2 is executed.

• Control constructs determine the sequence to be executed. Example: If-


Else test constructs and for, while loop constructs.

• Example: C, C++, Java, Ruby, Javascript and Python.

Advantage: Easy to write, Efficient and Widely supported.

Disadvantage: Difficult to debug and maintain.

Procedural programming paradigm

• Groups instructions into procedures,

• A program is built from one or more procedures (also termed subroutines or


functions) and is modular.

• They use procedures and control flow to describe the flow.

Example: Fortan, C, Cobol

Parallel Processing Programming Paradigm:

• Many calculations or processes are carried out simultaneously.

• Large problems are divided into smaller ones, which can then be solved at the
same time.

• For example, a program that needs to calculate the sum of a large number of
numbers can be parallelized by dividing the numbers into smaller groups and
assigning each group to a different processor.

• Different parallel programming approaches:

• Thread-based parallelism (uses threads to execute different parts of a


program simultaneously. Threads are lightweight processes that share the
same memory space, which makes them easy to synchronize.):

• Process-based parallelism (uses processes to execute different parts of a


program simultaneously. Processes are independent of each other, which
makes them more difficult to synchronize, but they also offer more
flexibility):

• Data parallelism (divides a program's data into smaller pieces and assigns
each piece to a different processor. This approach is well-suited for
problems that can be divided into independent tasks.).

Advantage: Scalability, Improved performance

Disadvantage: Complexity, synchronisation

Object-oriented programming paradigm

• Groups instructions with the part of the state they operate on.

• Concept of "objects", which contain data and code.

• A common feature of objects is that procedures (or methods) are attached to


them and can access and modify the object's data fields.

• C++, Java, C#, Python are examples

Advantages: Reusable, modular Disadvantage: Complex inheritance

Declarative programming paradigm:

• Programmer merely declares properties of the desired result, but not how to
compute it.

• It expresses the logic of a computation without describing its control flow.

• It is quite easy to write and understand by humans.

• Common declarative languages include those of database query languages


(e.g., SQL, XQuery, Prolog).

Advantages: Easy to read/write, Flexible, Efficient for searching, querying and


reasoning.

Disadvantages: Inefficient as compiler can’t optimise code


Logic programming paradigm

• Desired result is declared as the answer to a question about a system of facts


and rules.

• Focuses on expressing knowledge and rules in a declarative way.

• Logic programming language families include Prolog, answer set


programming (ASP) and Datalog.

• Concepts in logic programming

• Facts: Facts are statements that are known to be true

• Rules: Rules are statements that describe how facts can be deduced from
other facts

• Inference: The process of deducing new facts from existing facts.

Example: In python

fact: father(john, Michael).

fact: father(john, peter).

rule: grandfather(X, Y) :- father(X, Z), father(Z, Y).


query: grandfather(john, Michael).

Functional programming paradigm

• Computation is used as the evaluation of mathematical functions and avoids


changing-state and mutable data.

• Features

• Uses first-class functions, where either the input parameter is a function


or the return type is a function.

• Data is immutable, where it can’t be changed.

• It uses recursions and pure functions (functions that do not have side
effects. A side effect is a change to the state of the program outside of the
function.)
• Example: Lisp, Standard ML

Advantage: Used in data analysis and machine learning.

Disadvantage: Not widely supported. Ex: Haskell, Scala, Lisp, Erlang

Pure function: It does not produce side effect, It only depends of its input, It is
always deterministic.

Example for pure function

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

Example of not pure function

def even_factorial(n):
result = 0
for i in range(n + 1):
if is_even(i):
result += factorial(i)
return result

Multi-paradigm programming language

A programming language having features from more than one programming


paradigm. Example Java supports structured PP, procedural PP, object oriented
PP. Some features of functional and declarative PP’s are supported.

Machine codes
In 1940, John von Neumann had the idea that a computer should be
permanently hardwired with a small set of general-purpose operations. The
operator could then input into the computer a series of binary codes that would
organize the basic hardware operations to solve more-specific problems.
In this program, each line contains 16 bits or binary digits. A line of 16 bits
represents either a single machine language instruction or a single data value.
Program execution begins with the first line of code, which is fetched from
memory, interpreted, and executed. Control then moves to the next line of code,
and the process is repeated, until a special halt instruction is reached.
The first 4 bits are opcode (type of operation) and remaining 12 bit are
operands. Operands could be registers or memory location. It was error prone to
input 0s and 1s.
1950, Assembly language: mnemonic symbols were used in place of binary
codes. Assembler could help translate the mnemonics back to machine code.
Example:
.ORIG x300 ; LD R1, FIRS ; LD R2, SECON ; ADD R3, R2, R ; ST
R3, SU ; HALT
Each computer architecture has a specific set of machine code and required
varied sets of assembly language codes.
1960, Algol was released which removed machine specific codes. ALGOL
compilers converted standard Algol programs for any type of machine.
Function call overload
• Overload refers to the amount of time and resources that are consumed during
the function call.
• The arguments of the function has to be pushed onto the stack, time taken to
call the function, and the time taken to pop the arguments off the stack.
• Reduce the overload:

• Use inline functions (compiler exapands the code and no need for stack
push/pop)

• declare arguments as constants (compiler optimization is good)


• design functions with few parameters (uses registers instead of stack)
• Keep frequently used parameters on the leftmost position.

Static and Dynamic memory allocation

Static memory allocation is the process of allocating memory at compile-time.


Dynamic memory allocation allocates memory at runtime.
M​​
0​​
T​​
D​
1​

You might also like