Principles of
Programming Language
Amit Dua
BITS Pilani Computer Science and Information Systems Department
BITS, Pilani
Pilani Campus
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Introductory lecture on PPL
Lecture 1
BITS Pilani, Pilani Campus
Why should we study Principles of
Programming Language?
Slides motivated by lectures from Prof C. Hota BITS Pilani, Pilani Campus
Course Motivation
• Reasons for studying concepts of programming
languages
• Increased capacity to express idea.
• Improved background for choosing appropriate languages.
• Increased ability to learn new language.
• Better understanding of the significance of implementation.
• Overall advancement of computing.
BITS Pilani, Pilani Campus
Programming Domains
• Scientific Applications
• Floating point arithmetic, Arrays and Matrices, loops and selection
• FORTRAN, ALGOL 60
• Business Applications
• Reports, Decimal numbers and Character data, Decimal arithmetic
• COBOl
• Artificial Intelligence
• Symbolic Computation mainly with names
• Linked list
• LISP, Prolog
BITS Pilani, Pilani Campus
Programming Domains
Contd..
• System Programming
• Deals with low level features
• UNIX
• Scripting Languages
• Initially began with collection of commands in a file and then followed by control statements ,
functions etc.
• Java Script, PHP
• Special Purpose Languages
• Produce business reports, Instruct programmable Machine tools, System Simulation
• From AI to robotics to computer networks to autonomous vehicles,
programming domains is all pervasive
BITS Pilani, Pilani Campus
What are we going to study in this
course?
Slides motivated by lectures from Prof C. Hota BITS Pilani, Pilani Campus
PPL as a Course
• What is not
– Do not teach you a programming language
– Do not teach you how to program
• What is
– Introduce fundamental concepts of programming languages
– Discuss design issues of various language constructs
– Examine design/implementation choices for these constructs
– Compare design alternatives
BITS Pilani, Pilani Campus
Books
Text Book: Ravi Sethi, "Programming Languages:
Concepts and Constructs" 2nd Edition by Addison
Wesley.
[R1].Robert W. Sebesta, "Concepts of Programming
Languages", 10th Edition by Pearson Publishers.
BITS Pilani, Pilani Campus
What is the architecture of computer on
which we are programming?
Slides motivated by lectures from Prof C. Hota BITS Pilani, Pilani Campus
Von Neumann Architecture
• Stored program concept
• Data and instruction have same format
• Interpret sequences as data and instructions
BITS Pilani, Pilani Campus
The von Neumann
Architecture
BITS Pilani, Pilani Campus
Imperative Lang. & von
Neumann Architecture
• Imperative languages, most dominant, because of von
Neumann computers
• Data and programs stored in memory
• Memory is separate from CPU
• Instructions and data are piped from memory to CPU
• Basis for imperative languages
• Variables model memory cells
• Assignment statements model piping
• Iteration is efficient
BITS Pilani, Pilani Campus
The von Neumann Architecture
• Fetch-execute-cycle (on a von Neumann architecture
computer)
initialize the program counter
repeat forever
fetch the instruction pointed by the counter
increment the counter
decode the instruction
execute the instruction
end repeat
BITS Pilani,
PilaniPilani
CampusCampus
What makes a good Programming
language?
Slides motivated by lectures from Prof C. Hota BITS Pilani, Pilani Campus
Language evaluation criteria
Language evaluation criteria:
Readability: the ease with which programs can be read
and understood
Writability: the ease with which a language can be used to
create programs
Reliability: a program performs to its specifications under
all conditions
Cost
BITS Pilani, Pilani Campus
Features Related to Readability
Overall simplicity: language is more readable if
– Fewer features and basic constructs
Readability problems occur whenever program’s author uses a subset different
from that familiar to reader
– Fewer feature multiplicity (i.e., doing the same operation with different ways)
– Minimal operator overloading
Orthogonality
– A relatively small set of primitive constructs can be combined in a relatively small
number of ways
– The combination is legal.
– Too much orthogonality can also cause problems
BITS Pilani, Pilani Campus
Features Related to Readability
Control statements
– Sufficient control statements for structured prog.
can read program from top to bottom w/o jump
Data types and structures
– Adequate facilities for defining data type & structure
Syntax considerations
– Identifier or keywords
– Special words and methods of forming compound statements
– Form and meaning: self-descriptive constructs, meaningful keywords
BITS Pilani, Pilani Campus
Writability
Simplicity and orthogonality
– But, too orthogonal may cause errors undetected
Support for abstraction
– Ability to define and use complex structures or operations in ways that allow
details to be ignored
– Abstraction in process (e.g. subprogram), data
Expressivity
– A set of relatively convenient ways of specifying operations
– Example: the inclusion of for statement in many modern languages
BITS Pilani,
PilaniPilani
CampusCampus
Reliability
Type checking
– Testing for type errors, e.g. subprogram parameters
Exception handling
– Intercept run-time errors & take corrective measures
Aliasing
– Presence of two or more distinct references for the same memory location
Readability and writability
– A language that does not support “natural” ways of expressing an algorithm will
necessarily use “unnatural” approaches, and hence reduced reliability
BITS Pilani, Pilani Campus
Cost
• Training programmers to use language
• Writing programs (closeness to particular applications)
• Compiling programs
• Executing programs: run-time type checking
• Language implementation system: availability of free
compilers
• Reliability: poor reliability leads to high costs
• Maintaining programs
BITS Pilani, Pilani Campus
Language Evaluation Criteria
CRITERIA
Characteristic READABILITY WRITABILITY RELIABILITY
Simplicity X X X
Orthogonality X X X
Data types X X X
Syntax design X X X
Support for abstraction X X
Expressivity X X
Type checking X
Exception handling X
Restricted aliasing X
BITS Pilani, Pilani Campus
Language Design Trade-Offs
Reliability vs. cost of execution
– e.g., Java demands all references to array elements be checked for proper
indexing but that leads to increased execution costs
Readability vs. writability
– e.g., use of 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 poor readability
Writability (flexibility) vs. reliability
– e.g., C++ pointers are powerful and very flexible but not reliably used
BITS Pilani, Pilani Campus
Questions
BITS Pilani, Pilani Campus
Discussion
• Evaluate and discuss the Assembly language
• Evaluate C language and MATLAB for the following
application
1) Matrix operations
2) System programming
BITS Pilani, Pilani Campus
Language Implementation
Compiler - source code translation into machine code (all at
once)
Interpreter - machine is brought up to the language (one
statement at a time)
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
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.
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Layered View of Computer
The operating system and
Language implementation
are layered over machine
interface of a computer
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
The Compilation
Process
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Compilation
• Translate high-level program (source language) into
machine code (machine language).
• Compilation process has several phases:
• lexical analysis: converts characters in the source program into lexical units e.g.:
identifiers / keywords, operators, punctuation…
• syntax analysis: transforms lexical units into parse trees which represent the
syntactic structure of program.
• Intermediate code generation & Semantics analysis: generate intermediate code
and does type checking.
• Code optimization: (optional)
• code generation: machine code is generated.
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Additional Compilation
Terminologies
• Load module (executable image):
• the user and system code together.
• Linking and loading:
• the process of collecting system program units and
linking them to a user program.
• As well as linking other user programs to it.
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Compiled C
Source Linker
compiler .o
code and
files
in C loader
Machine
code (exe)
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Pure Interpretation
• No translation
Program interpreted by another program (interpreter) without translation
– Interpreter acts a simulator or virtual machine
– Machine is brought to the level of language by building higher level machine
called an interpreter that can run the language directly.
• Easier implementation of programs (run-time errors can easily and immediately be
displayed)
• Slower execution (10 to 100 times slower than compiled programs)
Decoding of higher level language programs is more complex, decoding has to be
done every time a statement is executed.
• Often requires more space
• Used in APL, SNOBOL, LISP.
• Now rare for traditional high-level languages
• Significant comeback with some Web scripting languages (e.g., JavaScript, PHP)
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Pure Interpretation Process
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Comparisons
• Interpretation is slower as decoding of higher level language programs is
more complex, decoding has to be done every time a statement is executed.
• Compiled languages have bias towards static properties since all compiling
decisions are made at translation time. Interpreted languages can deal with
dynamic properties.
• Interpretation is more flexible: due to direct running on the source code,
interpreter can allow to add features or correct errors in the source code.
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Hybrid Implementation Systems
• A compromise between compilers and pure interpreters.
• A high-level language program is translated to an
intermediate language that allows easy interpretation.
• Faster than pure interpretation since the source language
statements are decoded only once.
• Examples
• Perl programs are partially compiled to detect errors before interpretation.
• Initial implementations of Java were hybrid;
• the intermediate form, byte code, provides portability to any machine that has a
byte code interpreter and a run-time system (together, these are called Java Virtual
Machine).
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Hybrid
Implementation
Process
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Just-in-Time Implementation
Systems
• Initially translate programs to an intermediate language.
• Then compile the intermediate language of the subprograms
into machine code when they are called Machine code
version is kept for subsequent calls.
• JIT systems are widely used for Java programs.
• .NET languages are implemented with a JIT system.
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Summary
• The study of programming languages is valuable for a number of reasons:
• Increase our capacity to use different constructs
• Enable us to choose languages more intelligently
• Makes learning new languages easier
• Most important criteria for evaluating programming languages include:
• Readability, writability, reliability, cost
• Major influences on language design have been
• machine architecture and software development methodologies
• The major methods of implementing programming languages are:
• compilation, pure interpretation, and hybrid implementation
BITS BITS
Pilani,Pilani,
Hyderabad
PilaniPilani
CampusCampus
Questions
BITS Pilani, Pilani Campus