Programming Methodology
CST-207
Introduction
Dr. Arka Prokash Mazumdar
Major Course Outline
Introduction
Programming Paradigms
Abstraction in Languages
Sequence Control
Subprogram Control
Data Control
Storage Management
2
Books
Main:
1. Pratt: Programming language design and implementation, PHI.
2. Sebasta: Concept of programming language, Addison Wesley.
Reference:
1. Ghezzi and Jazayeri: Programming Language Concepts, John Wiley
& Sons.
2. Sethi Ravi: Programming Language Concepts & Constructs, Addison
Wesley
3. Louden: Programming Languages Priciples and Practice, Cengage
Learning.
4. Friedman and Wand: Essential of Programming Languages, PHI.
3
Weight Distribution
Continuous evaluation 20%
Assignments
Quiz
Case studies
Attendance
Mid-Terms -40%
Mid I
Mid-II
End Term 40%
4
IMPORTANT !!!
All the slides and other materials will be
uploaded on the following link:
sites.google.com/a/mnit.ac.in/arka/home/cst-207
5
Course Objective
To learn about
Programming Methodologies and Principles
Different programming paradigms
To understand concepts of syntax, translation,
abstraction, and implementation
Problem solving
6
Course Outcomes
Outcomes
Ability to understand structures of languages
Ability to solve simple algorithmic problems
Ability to write efficient and effective codes
7
Language Principles
Syntax
What constitutes a structurally correct
language
Names and Types
Vocabulary of a language variables,
functions etc.
Semantics
Meaning of a program
8
Programming Paradigms
There are several ways to think about
computation:
a set of instructions to be executed
a set of expressions to be evaluated
a set of rules to be applied
a set of objects to be arranged
a set of messages to be sent and received
9
Major Paradigms
Procedural (or Imperative)
examples: C, Pascal, Basic, Fortran
Functional
examples: Lisp, ML
Object-oriented
examples: C++, Java, Smalltalk
Rule-based (or Logic)
example: Prolog
10
Choice of Paradigm
Depends on how people can formulate a
problem at his/her best
Other considerations:
efficiency
compatibility with existing code
availability of translators
11
Von Neumann Architecture
12
A more complex view
13
Memory Hierarchy
14
Layered View of Computer
15
Computer Components
Hardware
Firmware
Software
16
Hardware-level operations
Fetch-execute cycle:
Processor fetches an instruction from
memory.
Processor executes the fetched instruction
Instruction cycle- processing that is
required for a single instruction
17
Instruction Cycle
4.Write-
1.Fetch
back
3.Execute 2.Decode
18
Fetch-execute cycle
1. Program counter contains the address
of the next instruction to be executed
2. The fetched instruction is loaded into
Instruction Register
3. Program counter is incremented after
fetching
4. Processor interprets the bits stored in the
IR and performs the required action
19
Firmware
A set of machine-language instructions
implemented by programs,
called microprograms,
stored in programmable read-only memory in
the computer (PROM).
20
Firmware Advantage
Flexibility
Low cost hardware
21
Software
Translators and virtual architectures
High-level language machine language
Types:
Translation (Compilation)
Interpretation
22
Translation
Input: high-level language program
Output: machine language code
Types of translation (high to low-level):
Preprocessor
Compiler
Assembler
Loader (usually part of the operating system)
Advantages: efficient executable
23
Compiler
24
Interpretation
The program is not translated to machine
language code.
Instead, it is executed by another program.
Example:
Prolog interpreter written in C++
25
Interpretation
Advantages of interpretation:
Easy to be implemented
Easy to debug
Portability
Flexibility: easy to modify the interpreter
Disadvantages: slow execution
26
Virtual Machine
Program:
data + operations on these data
Computer:
implementation of data structures +
implementation of operations
Similarity?
Example: JVM
27
Interpreter / VM
28
Hardware, Firmware, and Software
computer
Hardware computer:
Elementary data items, very simple
operations
Firmware computer:
Elementary data items, machine language
instructions (we may have specialized
processors e.g. vector multiplication)
Software computer :
Each programming environment defines a
specific software computer.
29
TIOBE Index
Jul 2016 Jul 2015 Programming Language Ratings Change
1 1 Java 19.804% +2.08%
2 2 C 12.238% -3.91%
3 3 C++ 6.311% -2.33%
4 5 Python 4.166% -0.09%
5 4 C# 3.920% -1.73%
6 7 PHP 3.272% +0.38%
7 9 JavaScript 2.643% +0.45%
8 8 Visual Basic .NET 2.517% +0.09%
9 11 Perl 2.428% +0.62%
10 12 Assembly language 2.281% +0.75%
11 15 Ruby 2.122% +0.74%
12 13 Delphi/Object Pascal 2.045% +0.57%
13 10 Visual Basic 2.012% +0.07%
14 16 Swift 1.960% +0.73%
15 6 Objective-C 1.881% -1.46%
16 19 MATLAB 1.558% +0.35%
17 17 R 1.514% +0.28%
18 18 PL/SQL 1.456% +0.24%
19 22 COBOL 1.135% +0.10%
20 39 Groovy 1.125% +0.80%
30
Tombstone Diagrams
What are they?
Diagrams consisting out of a set of puzzle pieces we can use to
reason about language processors and programs
Different kinds of pieces
Combination rules (not all diagrams are well formed)
Program P implemented in L Translator implemented in L
P S -> T
L L
Machine implemented in hardware Language interpreter in L
M M
L
31
Tombstone diagrams:
Combination rules
P P P
M S S -> T T
OK!
M M OK!
OK!
M OK!
P
P
L
WRONG! L S -> T
M
M
WRONG!
32
Compilation
Example: Compilation of C programs on an x86 machine
Tetris Tetris Tetris
C C -> x86 x86 x86
x86
x86
x86
33
Cross compilation
Example: A C cross compiler from x86 to PPC
A cross compiler is a compiler which runs on one machine (the host machine) but
emits code for another machine (the target machine).
Tetris Tetris Tetris
C C -> PPC PPC download
PPC
x86
PPC
x86
Host Target
Q: Are cross compilers useful? Why would/could we use them?
34
Two Stage Compilation
A two-stage translator is a composition of two translators. The output of the first
translator is provided as input to the second translator.
Tetris Tetris Tetris
Java Java->JVM JVM JVM->x86 x86
x86 x86
x86 x86
35
Compiling a Compiler
Observation: A compiler is a program!
Therefore it can be provided as input to a language processor.
Example: compiling a compiler.
Java->x86 Java->x86
C C -> x86 x86
x86
x86
36
Interpreters
Tetris Tetris
JVM JVM
JVM JVM
x86 PPC
x86 PPC
37
Bootstrapping
How does the first compiler gets
compiled?
What language should be used?
Implementing in the same language gives
independence from other languages.
But,Egg and Hen problem
38
Bootstrap
Step 1a: build a compiler (v1) for Ada-S in another language.
v1
Ada-S ->M
Step 1b: Compile v1 compiler on M
v1 v1
Ada-S ->M Ada-S->M
C C->M M
M
This compiler can be used for
M bootstrapping on machine M but we do not
want to rely on it permanently!
39
Bootstrap
Step 2a: Implement v2 of Ada-S compiler in Ada-S
v2
Ada-S ->M
Q: Is it hard to rewrite the compiler in Ada-S?
Ada-S
Step 2b: Compile v2 compiler with v1 compiler
v2 v2
Ada-S ->M v1 Ada-S->M
Ada-S Ada-S ->M M
M
We are now no longer dependent on the
M availability of a C compiler!
40
Bootstrap
Step 3a: Build a full Ada compiler in Ada-S
v3
Ada->M
Ada-S
Step 3b: Compile with v2 compiler
v3 v3
Ada->M v2 Ada->M
Ada-S Ada-S ->M M
M
From this point on we can maintain the compiler in Ada.
Subsequent versions v4,v5,... of the compiler can be written in Ada and each
compiled with the previous version.
41
Assignment
Hand-written document on
1. P-code machine
2. Pascal bootstrapping method (as self-
hosting language), along with T-diagram
42