Programing Paradigms PPT - 01. 04. 2023
Programing Paradigms PPT - 01. 04. 2023
Abhimanyu Sahu
Assistant Professor
Dept. of Computer Science and Engineering
Motilal Nehru National Institute of Technology Allahabad
Syllabus:
• UNIT 1: Introduction: Role of Programming Languages: Why Programming Languages, Towards Higher Level
Languages, Programming Paradigms, Programming Environments Language Description: Syntactic
Structure, Language Translation Issues: Programming Language Syntax, Stages in Translation, Formal
Translation Models
• UNIT II: Data, Data Types, and Basic Statements: Names , Variables , Binding, Type Checking, Scope, Scope
Rules , Lifetime and Garbage Collection, Primitive Data Types, Strings, Array Types, Associative Arrays
,Record Types, Union Types, Pointers and References, Arithmetic Expressions , Overloaded Operators, Type
Conversions, Relational and Boolean Expressions, Assignment Statements, Mixed Mode Assignments,
Control Structures, Selection ,Iterations, Branching, Guarded Statements
• UNIT III: Subprograms and Implementations: Subprograms, Design Issues, Local Referencing, Parameter
Passing, Overloaded Methods, Generic Methods, Design Issues for Functions, Semantics of Call and Return,
Implementing Simple Subprograms, Stack and Dynamic Local Variables, Nested Subprograms, Dynamic
Scoping.
Syllabus:
• UNIT IV: Object-Orientation, Concurrency, and Event handling: Grouping of Data and Operations —
Constructs for Programming Structures, Abstraction Information Hiding, Program Design with Modules,
Defined Types, Object Oriented Programming — Concept of Object, Inheritance, Derived Classes and
Information Hiding — Templates, Semaphores, Monitors, Message Passing, Threads, Statement Level
Concurrency Exception Handling (Using C++ and Java as Example Language).
• Reference Books:
1. Concepts of Programming Languages, Robert W. Sebesta, 10th Ed.,Pearson
2. Programming Language Design Concept”, David A. Watt, Willey India
3. “Programming languages: Concepts and Constructs”, Ravi Sethi, Second Ed.,Pearson.
4. “Types and programming Languages”, Benjamin C. Pierce. The MIT Press Cambridge, Massachusetts
Why Study Programming Languages:
History:
• Hundreds of different programming languages have been designed and
implemented.
• In 1969, Sammet listed 120 that were fairly widely used, and many others
have been developed.
• The particular use of a language such as Java, C, or Ada.
• Six primary reasons :
• To improve your ability to develop effective algorithms
• Improper use of recursion
• Object-oriented programming, logic programing, concurrent programming
• 2000-??? P2P
Organization of Programming Languages:
• Understand how languages are designed and implemented
• Syntax--What a program looks like
• Semantic--What a program means
• Implementation--How a program executes
• But today
• Compiler programs that are built efficiently
• CPU power and memory very cheap
• Direct connection between language features and design concepts-
encapsulation, records, inheritance, functionality, assertions
Languages for various application domains:
Factorial Functions (Example):
• C, C++, Java:
int fact (int n) { return (n == 0) ? 1 : n * fact (n-1); }
• Scheme:
(define fact
(lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))))
• ML:
fun fact n = if n=0 then 1 else n*fact(n-1);
• Haskell:
• fact :: Integer->Integer
• fact 0 = 1
• fact n = n*fact(n-1)
Role of Programming Language:
• Some of these influences on programming language development include
the following:
• Computer capabilities
• Applications
• Programming methods
• Implementation methods
• Theoretical studies
Role of Programming Language:
Role of Programming Language:
Attributes of a good language:
• Clarity, simplicity, and unity-Provides both a framework for thinking about algorithms and a means of
expressing those algorithms
• Have a minimum number of different concepts, with the rules for their combination, simple and regular
(conceptual integrity)
• Readability
• Orthogonality-Being able to combine various features of a language in all possible combinations
• every combination of features is meaningful
• Logical errors and inefficiency
• Naturalness for the application-program structure reflects the logical structure of algorithm
• Sequential algorithm, concurrent algorithms, logic algorithm, non-deterministic algorithm
• Appropriate data structures, operations, control structures, natural syntax
• Support for abstraction-program data reflects problem being solved Attributes of a good language
• Data abstraction
• Encapsulation
Attributes of a good language:
• Ease of program verification-verifying that program correctly performs its required function
• Verification/validation
• Comments, assert()
• Design specification
• Programming environment-external support for the language
• Debugger, syntax-directed editor
• Supporting function, platforms
• Smalltalk
• Supporting all the software lifecycle phases
• Portability of programs-transportability of the resulting programs from the computer on which they are
developed to other computer systems
• Transportability
• C, C++, Pascal<=> Java
• ML: Single source implementation
• Cost of use
• Cost of program execution
• Cost of program translation
• Cost of program creation
• Cost of program maintenance
Influences on Programming languages:
Choosing language for application :
• Pascal, C-procedural, statement oriented
• C++, Java, Smalltalk-Object oriented
• ML, Lisp-Functional
• Prolog-Rule-based
Language paradigms :
Language paradigms :
Imperative programming paradigm:
• Examples of Imperative programming paradigm:
• C : developed by Dennis Ritchie and Ken Thompson
• Fortran : developed by John Backus for IBM
• Basic : developed by John G Kemeny and Thomas E Kurtz
Procedural programming paradigm – Examples of Procedural programming paradigm:
• C : developed by Dennis Ritchie and Ken Thompson
• C++ : developed by Bjarne Stroustrup
• Java : developed by James Gosling at Sun Microsystems
• ColdFusion : developed by J J Allaire
• Pascal : developed by Niklaus Wirth
Object oriented programming – Examples of Object Oriented programming paradigm:
Simula : first OOP language
Java : developed by James Gosling at Sun Microsystems
C++ : developed by Bjarne Stroustrup
Objective-C : designed by Brad Cox
Visual Basic .NET : developed by Microsoft
Python : developed by Guido van Rossum
Ruby : developed by Yukihiro Matsumoto
Smalltalk : developed by Alan Kay, Dan Ingalls, Adele Goldberg
Parallel processing approach – Examples are NESL (one of the oldest one) and C/C++ also supports
because of some library function.
Language paradigms :
Generality of Computational Model:
Declarative programming paradigm: :
• It is divided as Logic, Functional, Database.
Logic programming paradigms – Prolog
• FORTRAN - implicit naming does not help us catch misspellings (like indx
and index, both are good integer variables, even though the programmer
wanted index to be index)
• Target language is often, but not always, the assembly language for a particular
machine
Checks During Compilation:
• Syntactically invalid constructs
• Invalid type conversions
• A value is used in the “wrong” context, e.g., assigning a float to an int
• Static determination of type information is also used to generate more
efficient code
• Know what kind of values will be stored in a given memory region during program
execution
• Some programmer logic errors
• Can be subtle: if (a = b) … instead of if (a == b) …
Different stages in language translation:
• Language translation work done by compiler
• Logically , language translation divide into major parts:
• The analysis of the input source program
• The synthesis of the executable object code.
• Structure of Compiler: A compiler takes input as source program and
produces as output an equivalence sequence of machine instructions.
• The compilation process is divided into a series of sub process called phases.
A phase is a logically cohesive operation that takes as input one
representation of source program and produce as output i.e.; target
program in other representation.
Different stages in language translation:
• A simple compiler typically uses two passes
• First pass:- The first analysis pass decomposes
the program into its constituent components
and derives information, such as variable name
usage, from the program
• Second pass:- The second pass typically
generates an object code from this collected
information.
• If compilation speed is important, a one-pass
strategy may be employed.
-Pascal was designed so that a one-pass
compiler could be developed for the language
• If execution speed is paramount, a three (or
more) pass compiler may be developed
-first pass analyzes the source program
-second pass rewrites the source program
into a more efficient from using various well-
defined optimization algorithms
Figure: Structure of a compiler -third pass generates the object code
Phases of Compilation:
• First Phase: lexical analyzer or scanner separate character of source language into a group of logically belongs to
each other. This group is called as tokens. The usual tokens are keywords such as do or if, identifiers such as a or b,
operator symbol as = + < > and punctuation symbol as () and comma. The output is a stream of tokens pass to the
rest of phases.
• Second Phase: the syntax analyzer or parser group tokens together into syntactic structure for example 3 tokens as
A+B group together into syntactic structure as expression. Expression might further be combined into statement.
We form a tree who's leaf are tokens, an interior node represent string of token that logically belong together. for
example : the statement – if (5 equ max) goto 100 has 8 tokens.
• Third Phase: intermediate code generator use the structure produced by syntax analyzer to create a stream of
simple instructions. Different style we are using like MACROS.
• Fourth Phase: code optimization is an optional phase design to improve the intermediate code so that object
program run faster and take less space. Its output is also intermediate code but it saves time.
• Fifth Phase: the final phase at code generation produce the object code by deciding on memory location for data,
selecting code and selecting register in which each computation done.
• The table management or book keeping keeps track of the name and record and all the essential information
about each of the data type. The data structure which is used to record the information called the symbol table.
Analysis of the Source Program:
• It consists of three steps:
• Lexical Analysis (Linear or Scanning) : reads from left-to-right and grouped into tokens that are
sequences of characters having a collective meaning
• Syntax Analysis (Hierarchical or Parsing) : characters or tokens are grouped hierarchically into nested
collections with collective meaning
• Semantic Analysis : Certain checks are performed to ensure that the components of a program fit
together meaningfully
• Ambiguity in Grammars:
- A grammar is ambiguous iff it generates a sentential form
that has two or more distinct parse trees
Example: S -> SS | 0 | 1 T -> 0T | 1T | 0 | 1
Ambiguous Grammars:
• Example:
Parse Tree:
Example : Generate Parse tree for an assignment statement W=Y X (U+V) using the BNF grammar
*Draw the corresponding parse tree
*Draw the corresponding leftmost derivation