Sie sind auf Seite 1von 64

Principles of Programming Languages

Module 1
Concepts of Programming Languages
Robert W Sebesta
Programming Language
Notational system for describing computation
in machine readable and human readable
form.

Assembly Languages closely resembles the


computer instruction sets than human
readable are called low level languages.
Levels of Programming Languages
High-level program class Triangle {
...
float surface()
return b*h/2;
}

Low-level program LOAD r1,b


LOAD r2,h
MUL r1,r2
DIV r1,#2
RET

Executable Machine code 0001001001000101001001


001110110010101101001.
..

3
High Level
Readable familiar notations
machine independence
availability of program libraries
consistency check (check data types)
Types of Programming Languages
First Generation Languages
Machine
0000 0001 0110 1110
0100 0000 0001 0010

Second Generation Languages


Assembly
LOAD x
ADD R1 R2

Third Generation Languages


High-level imperative/object oriented
public Token scan ( ) {
while (currentchar ==
|| currentchar == \n)
{.} }
Fortran, Pascal, Ada, C, C++, Java, C#
Fourth Generation Languages
Database
select fname, lname
from employee SQL
where department=Sales

Fifth Generation Languages


Functional Logic Lisp, SML, Haskel, Prolog
fact n = if n==0 then 1 uncle(X,Y) :- parent(Z,Y), brother(X,Z).
else n*(fact n-1)
Beyond Fifth Generation Languages

Some talk about


Agent Oriented Programming
Aspect Oriented Programming
Intentional Programming
Natural language programming
Maybe you will invent the next big language

6
1.1: 6 Reasons for Studying Concepts
of Programming Languages

1. Increased ability to express ideas.


COBOL-AI,
Shaping the ideas,
only words can express and concede the idea. And that word is known
as language.
It increases our capability to use different constructs to write
programs.
e.g.; If learned structure in C and use of associative array in perl, then we
can simulate associative array in that lng.
Appreciates the language features and encourages programmers to
use them.
6 Reasons for Studying Concepts of
Programming Languages

2. Improved background for choosing


appropriate languages.

Professional pgmrs: Little formal edn in cse.


Learned their own and through in-house training.
Other Pgmrs: formal training in past.
learned then are no longer used.
features are widely available, but not used.
Continue to use the lng in which he is familiar.
Poorly suited for projects
6 Reasons for Studying Concepts of
Programming Languages Basic
Fortran
PL-1
3. Increased ability to learn new languages Pascal
C
C++
understand the constraints , paradigms and purposes in general.
Java
Expanding the knowledge of lngs.

S/w development is still in a state of continuous evolution:/ Continuous


learning.
Those who know 2 or 3 lngs, can never examine the concepts in general.
Incooperate concepts into the design of lng being learned.

e.g, Pgmrs who study C concepts can study java.


The better u learn the grammer of native lng, then it is easier to learn a
second lng.
6 Reasons for Studying Concepts of
Programming Languages

4. Better understanding of significance of Implmn?


implementation Bugs?

Nail fixhammer
Screwscrewdriver
- Deals with implmn issues that affects those concepts.
- Leads to understanding of y lngs are designed in the way they are.
- Leads to ability to use a lng more intelligently.-> can b better pgmrs.
- Certain pgm bugs can be found and fixed.
- Can visualize how computer execute various lng constructs.
- Hints abt the relative efficiency of alternative constructs. Large caln ->fortran
Buisness
reportscobol,
- E.g; frequently called subpgm is inefficient design choice. seqel.
6 Reasons for Studying Concepts of
Programming Languages

5. Better use of languages that are already


known

Learn about previously unknown


And unused parts of lng
And begin to use their features.
6 Reasons for Studying Concepts of
Programming Languages

6. Overall advancement of computing

Those in position are not familiar with the pgming concepts.


ALGOL60 OR FORTRAN IS BETTER ? Y popular?
Fortran is widely used.
But ALGOL HAS BETTER CONTROL STMTS AND IS MORE ELEGANT.
Conceptual design of ALGOL60 may not b clear to pgmrs.(block structure,
recursion, ctrl stmts)
1.2: 5 Programming Domains
US ->MISSILE TRAJECTORY
-Which directly fires and aims the gun

Controlling nuclear power plants


Video games in mobile phones
5 Programming Domains
1. Scientific applications
Large numbers of floating point computations;
use of arrays, matrices, loops
Fortran (Formula Translator), ALGOL60;
In 1950 and 60s fortran is more efficient. Efficiency is of primary concern.
First HLL., New versions for R&W. Can easily translate.

2. Business applications
Produce reports, use decimal numbers and characters.
COBOL (Common Business Oriented Language) in 1950s.
5 Programming Domains
3. Artificial intelligence:
- Jobs that computers cant do are done.
(Symbol Manipulation, Playing Games, Planning a route based on map,
understanding speech)
Symbols rather than numbers manipulated; use of linked lists
LISP (List Processing)

IPL(Fn oriented lng)

4. Systems programming: o.s+ tools=s/m s/w. Scheme lng

Need efficiency because of continuous use Prolog( for expert s/m


B C Unix o.s C++ java design and string
They all support DS, easy to port, move, processing, and is
its low level, execution efficient. based on predicate
No safety restrictions. calculus)
5 Programming Domains
5. Web Software:

- Web design features, dynamic web content.

- HTML: Annotate the document and display on web page.


- Scripting Language: to write small pgms / scripts, for tasks and include
control structures.
-
Eclectic collection of languages:
Markup, e.g., XHTML (Extensible Hypertext Markup Language)
Scripting, e.g., JavaScript/ PHP. ( general purpose pgming lng)

(XHTML+ PGM CODE= scripting language))


1.3: 4 Language Evaluation Criteria
Lng good/ bad?

Readability: the ease with which programs can be


read and understood- understand easily
Writability: the ease with which a language can
be used to create programs- write easily
Reliability: conformance to specifications (i.e.,
performs to its specifications)
Cost: the ultimate total cost( unrelated to other
criterias).
1 Evaluation Criteria: Readability
Readability is the imp measure of quality of a pgm.

e.g., if a pgm that describes computation is written in a lng that is not


designed to use. Then it is difficult to read.

1. Does computers can read it?


2. Does human can read it?

1. Translate to native algm use context free languages it has


automatic structure>Grammer is simple.

2. Datastuctures and algorithms must be clear.

If both were ok, compromised result is obtained.


1 Evaluation Criteria: Readability(5)
1) Overall simplicity: To be Simple:-
Small set of components, features and constructs that they need
to work on.
Minimal feature multiplicity: multiple ways to do the same set of
operns may complicate the structure.
(e.g., a=a+1, a+=1, a++, ++a)
Minimal operator overloading (e.g., + vector addn, for both
integer and floating point, + b/w array operands)- harder to find.
2) Orthogonality : lng constructs should not behave differently in diff
contexts
We can combine relatively small set of primitive constructs to
build the control and data structure of the language
Every possible combination is legal and meaningful
e.g. a language has 4 primitive data types, and 2 type operators
(array and pointer), a large number of data structures can be
defined.
i.e, lng feature is independent of where we use it in a lng.
Non orthogonalities will create a large
no. of exceptions
IBM Assembler is not Orthogonal
i.e. the use of values in memory and register uses
different instrns.

VAX Assembler is orthogonal


Becoz, the location of operands is irrelavent.
It can use same instrns.
3) Control Statements

Conditional and un conditional Goto statements reduces


readability.
We need to replace goto with while, for, if then else stmts to
ensure readability. this is more readable, writable,
modifiable, easy to check and debug.

E.g refer text.


while(incr<20)
{
while(sum<=100)
{sum+=incr;
}incr++;
}
4) Data types:
Ability of prgmr to create a diverse set of datatype can create more
readability.
int(8,16,32,64 int sizes)->float->char(ASCII, UNICODE),Boolean, Structure
containing arrays can make the pgm more readable.
Presence of adequate predefined data types.
Timeout=1 or
Timeout=True (more clear when Boolean data is included)

Char(len=30):: name(100)
Integer::age(100),employeeno(100)
Real::salary(100)

readable and reliable. Here it uses 4 diff arrays instead of structure of arrays.
5) Syntax considerations

mea
2 Evaluation Criteria: Writability(3)
Simplicity and orthogonality Cobol and Sequel->
Few constructs, a small number of primitives, a
report generation.
small set of rules for combining them But not good for
matrix multiplication.
Support for abstraction
APL/ FORTRAN is in
The ability to define and use complicated reverse.
structures or operations in ways that allow
details to be ignored Data Abstrn is the
e.g., a subprogram to implement a sort program. concept that is used
Without abstraction, the sort code have to be in Binarytrees. But
replicated in all places where it was needed. using Fortran it is
Expressivity
difficult to
implement, since
A language has relatively convenient ways of Fortran does not
specifying computations, e.g., a++ is more know the use with
convenient that a=a+1 in C. pointers and
structures.
3 Evaluation Criteria: Reliability(4)-
Assurance that there wont be any unexpected or unwanted behavior on pgm

Type checking- dataitem behavior must be consistent with their types.


Testing for type errors in a given program, either by the compiler or during
program execution., verify, there is no improper type conversions, and all
values are in range.
Exception handling( x/0; (-1)^2 )
Intercept run-time errors and take corrective measures, and then continue.
Aliasing
Having two or more distinct names that can be used to access the same
memory cell. Change made must reflect in the other.
e.g., two pointers set to point to the same variable.
Readability and writability
A language that does not support natural ways of expressing an algorithm
will require the use of unnatural approaches, and hence reduced reliability
The easier a program is to write, the more likely it is to be correct.
4 Evaluation Criteria: Cost

1)Extra time will be needed to run an inefficient code


2) Cost of running translation depends on whether it uses
compiler or interpreter.
3) Line that is not R &W can increase the cost of testing and
correcting.
4) If harder to R &W, then it will surely b harder to modify.
Evaluation Criteria: Others
Portability
The ease with which programs can be moved from
one implementation to another
Generality
The applicability to a wide range of applications
Well-definedness
The completeness and precision of the languages
official definition
1.4: 2 Influences of language Design
1. Computer Architecture
- Other Factors than previously stated.
- H/w on which pgm runs.
- Von Neumann( pronounced as von Noymann)- basis of architectural design.
- It is somewhat sequential execution of pgm to an extend.
- Same m/y to store instrns and data.
- Imperative lngs( fortran, cobol)

2. Programming Methodologies
Structured Programming and Data Abstraction simplify many tasks in s/w development
Computer Architecture Influence
Since 1940s Well-known computer architecture:
Von Neumann
Imperative languages, In von Neumann computer,
Data and programs stored in memory
Memory is separate from CPU
Instructions and data are piped from memory to CPU
Results moved back to memory
Basis for imperative languages
Central features, Variables model memory cells
Assignment statements model piping, repetition.
Iteration is fast and efficient
Instructions are store in adjacent cells of memory
Repeating the code requires only a simple branch instruction
Discourages the use of recursion.
The von Neumann Architecture
Single m/y

Programs

Variables

A=b+c
A=

Execute
The execution of 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

Decode- examines the action it specifies.


Programming Methodologies Influences

1950s and early 1960s: Simple applications; worry about machine efficiency
Late 1960s: People efficiency became important; readability, better control
structures- shift in cost of computing h/w and s/w.
(petroleum refining, airline reservation systems)
structured programming
top-down design and step-wise refinement.
Incomplete type checking, inadequacy of control stmts, heavy use of gotos
Late 1970s: Shift from Procedure-oriented to data-oriented
(abstraction is the process by which data and programs are defined with a
representation similar to its meaning (semantics), while hiding away the
implementation details)- SIMULA 67
Middle 1980s: Object-oriented programming
Data abstraction + inheritance + polymorphism+ dynamic(run time)
method binding.
(inheritance suggests an object is able to inherit characteristics from another object)
(Polymorphism is the capability of an action or method to do different things based on the
object that it is acting upon)
Language Categories: 4 Bins/
Paradigms
1. Imperative: how to perform tasks.
2. Functional: algm as fn defn.
3. Declarative: specifies fn decln.
4. Object Oriented: create classes and objs.
Implementation Methods
2 ways to translate a pgm
Primary components of a computer
Internal Memory -Compilation
Stores data and programs
Processor
-Pure Interpretation
Primitive operations/ machine -Hybrid implementation.
instructions
Arithmetic and logic operations 1) Bit translation
Macro Instructions
Set of micro instructions
2) Hybrid translation-
Machine Language preliminary processing
Set of instructions and then translate
Cant be seen by software
what u need, when u
Only understood by hardware
Requires system software to create
need.
an interface between the programs in
high level language and machine language
Virtual Computers

The operating system and language


implementation are layered over machine
interface of a computer
an operating system and a C compiler provide a virtual C
computer.
With other compilers, a machine can become other kinds
of virtual computers.
Most computer systems provide several different virtual
computers.
User programs form another layer over the top of the
layer of virtual computers.
Hierarchies of Computers
Virtual computer developed by
programmer

The language virtual computer

Operating system virtual computer

Firmware virtual computer

Actual hardware computer


Layered View of Computer

M/c lng i/f or


VC
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
1. Compiler Implementation
Translate high-level program (source language) into machine
code (machine language)
Slow translation, fast execution( saves the low level lng)
C, COBOL, C++, and Ada compilers
Compilation process has several phases:
lexical analysis: converts characters in the source program into lexical
units
syntax analysis: transforms lexical units into parse trees which
represent the syntactic structure of program
Semantics analysis: generate intermediate code
code generation: machine code is generated
Additional Compilation Terminologies

Load module /(executable image): the user and system


code together
Linking and loading/ linker: the process of collecting
system program units and linking them to a user
program.

Compilation is not complete translation of entire s/c code.


It may not include declarations of certain variables,
procedures etc.
Linker resolves the missing references and create 1 large file
with entire executable version of program.
Von Neumann Bottleneck
Connection speed between a computers
memory and its processor determines the speed
of a computer
Program instructions often can be executed
much faster than the speed of the connection;
the connection speed thus results in a bottleneck
Known as the von Neumann bottleneck; it is the
primary limiting factor in the speed of computers
2. Pure Interpretation
Programs are interpreted by another program called an
interpreter, with no translation
The interpreter program acts as a software simulation of a
machine whose fetch-execute cycle deals with high-level
language program statements rather than machine instructions
Easier implementation of programs (run-time errors can easily
and immediately be displayed) e.g; arrayindexoutofbound.
Bottle neck of interpreter.
A) Slower execution (10 to 100 times slower than compiled
programs)- statement decoding.
B) Often requires more space (symbol table and s/c pgm for easy
access is present).
Web scripting languages (e.g., JavaScript, PHP)
Pure Interpretation Process
3. 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
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)
Hybrid
Implementation
Process

1-48
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

i/p

s/c code Intermediate version o/p


Interpr Compil
eter er
Preprocessors
Preprocessor macros (instructions) are commonly used to
specify that code from another file is to be included
A preprocessor processes a program immediately before the
program is compiled to expand embedded preprocessor
macros
# include myLib.h
The Concept of Binding
Binding is an association of a property or
attributes, to particular feature or entity in lng.
It include binding datatype or m/y loc to a variable
or an opern to a lng.

Binding Time: When in lifetime the binding takes


place.
count = count + 5;

Some of the bindings and their binding times for the parts of this
assignment statement are as follows:

The type of count is bound at compile time.

The set of possible values of count is bound at compiler design time.

The meaning of the operator symbol + is bound at compile time, when


the types of its operands have been determined.

The internal representation of the literal 5 is bound at compiler design


time.

The value of count is bound at execution time with this statement.


Types of Bindings(5) (or)
Possible Binding Times
1) Binds subtraction with dash
symbol.

2) This happens when a compiler or


interpreter is been implemented.
Binding float to a var float value.

3) Pgm is translated.

4) When does the pgm loads to


m/y? i.e, binding m/y addr at run
time

5) m/y is allocated on entering the


procedure and deallocated at exit
Type Bindings:
1) Static Type Binding:
It takes place before runtime and cant
change values during execution. And
hence the name.
2) Dynamic Type Binding:
It takes place during runtime and can
change values during execution. And
hence the name
To bind
datatype to a
variable, these 2
questions need to be
answered.

Specify binding either


a) Statically or : life of pgm exn.
b) Dynamically : can change the value of variables.
Static : 2 possibilities of binding
b1) explicit
b2) implicit
Explicit / Implicit Declarations
Explicit declaration: it is used by c, c++, java.
It is a formal declaration, as variable is first assigned value or in
separate stmt at top of procedure before it is used.

Implicit Declaration: uses default mechanism, datatype is


determined by association b/w variable type and name.
In FORTRAN, variables that begin with i,j,k,l,m,n are integers. And
hence they are explicit decln. Other letters are real.
In Perl, Special character in front of variable name in character
determines whether it is an scalar array, hash, associative array etc.

Implicit declns are writable, and have reliability pblms if not


properly used.
Type Inference
It is the way of determining type of a variable,
or exprn based on other values from which it
is assigned or from other values with which it
will be used.
var- reserved word

Initial value infers the type of the variable.

Statically typed-i.e, once a type is assigned to a variable, it will be the


type of variable throughout the pgm and it will not change.

Used in VB, Pascal etc.


Inferring type
We wont have anything to infer the
datatype. This is Dynamic type
binding

Variable can take


value assigned to it.
It can be real value
or an array.

Slows down exn of


pgm, it uses
interpreter

Difficult for type


related errors.
Variable Attributes (continued)
Storage Bindings & Lifetime
The lifetime of a variable is the time during which
it is bound to a particular memory cell.
- Most variables in lngs have limited lifetime within that pgm.
- Lngs like Fortran, have static allocation of m/y, which means m/y is
allocated at compile time. i.e, at the time of loading to
termination.
- Hence the computer can rarely use data items at this fraction of
time
Allocation - getting a cell from some pool of available
cells
Deallocation - putting a cell back into the pool
Static Variable- history sensitive
- it retains values b/w separate exn of pgm
Fortran set aside storage of variables at compile time. But now it is
rarely used.
Now allocations are mostly dynamic and some are static.
e.g of static is C, C++
External Variables are statically allocated.
Internal variables are declared as static datatype variablename;
Advtg: 1) can use direct addressing which does not use the
register contents as address.
2) faster execution-improved efficiency
3) no runtime allocation and deallocation of static variables.
Disadvtg: 1) prevents recursion; So flexibility is reduced.
2) no shared storage of variables.( large static arrays need to
be stored again in separate, even if it is never or rarely used)
Categories of Variables by Lifetimes
Stack-dynamic--Storage bindings are created for variables when their
declaration statements are elaborated.
(A declaration is elaborated when the executable code associated with it
is executed)

- Done when transferring control to another procedures.


- Allows variable defns (C,JAVA) to appear at block of code, while the
procedures are executed in FILO manner.
- Stack is used to allocate and deallocate m/y.

If scalar, all attributes except address are statically bound


local variables in C subprograms (not declared static) and Java methods

Advantage: allows recursion; conserves storage, m/y use is efficient.


Disadvantages:
Overhead of allocation and deallocation- slow exn
Subprograms cannot be history sensitive
cant retain data values of local variables between calls.
Slower access because of indirect addressing
Categories of Variables by Lifetimes
Explicit heap-dynamic -- Allocated and deallocated by explicit run time
instructions.
Have no name.
Allocated and deallocated to the heap
Heap is a collection of storage cells whose organization is highly disorganized
because of the unpredictability of its use.
m/y location in heap can be deallocated and returned for years and is difficult to
manage.
Referenced using pointers or references, e.g. dynamic objects in C++ (via
new and delete), all objects in Java(new), C(lib fns),PL-1(allocate, free)
Advantage: provides for dynamic storage management- works with large
arrays.
Disadvantage: inefficient and unreliable
Data in Linked list is implemented by EHBV, & can search linear.
but binary search, hashing etc were not possible.
EXPLICIT HEAP DYNAMIC VARIABLES
In C++
int *intnode- creates pointer
..
int node=new int; create heap dynamic variable
.
..
delete int node; deallocate the heap dynamic
variable to which int node points.

Das könnte Ihnen auch gefallen