programing paradigm & implementation

30
Programming Languages

Upload: bilal-maqbool-

Post on 03-Mar-2017

78 views

Category:

Education


0 download

TRANSCRIPT

Programming Languages

What is a Programming

Language?

1.2

A formal language for describing computation

A “user interface” to a computer

Syntax + semantics

Compiler, or interpreter, or translator

A tool to support a programming paradigm

A programming language is a notational

system for describing computation in a

machine-readable and human-readable form.

— Louden

What is a Programming

Language? (II)

1.3

A programming language is a tool for

developing executable models for a

class of problem domains.

consists of words, symbols, and

rules for writing a program

Language Definition

4

Syntax

set of rules that define form

Grammar or syntax diagram

Semantics

specify meaning of well-formed programs

formal semantics: pre- and post- conditions

Lexical Rules

rules for determining tokens

tokens: syntactic units (e.g., number,

identifier, semicolon, equals)

Syntactic Rules

Grammar: set of productions

productions: terminals (tokens) and non

terminals

Programming Paradigms

1.5

Paradigms

How do different language paradigms

support problem-solving?

Semantics

How can we understand the semantics

of programming languages?

Foundations

What are the foundations of

programming languages?

Paradigms of Programming?

6

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

Some Programming Paradigms

7

Procedural

◦ examples: C, Pascal, Basic, Fortran

Functional

◦ examples: Lisp, ML

Object-oriented

◦ examples: C++, Java, Smalltalk

Rule-based (or Logic)

◦ example: Prolog

Procedural Languages

8

Focus is on writing good functions and procedures use the most appropriate implementation and employ correct efficient algorithms

Describes computation in terms of

Statements that change a program state

Explicit control flow

Synonyms

Imperative programming

Operational

Procedural Languages

9

Program State

Collection of Variables and their values

Contents of variables change

Expression (to be computed) : a + b + c

Recipe for Computation

Account for machine limitations

Intermediate Location

T := a + b; T := T + c;

Functional Languages

10

Program as a collection of (math) functions

functional programming is a programming

paradigm that treats computations as the

evaluation of mathematical functions and avoids

state.

It emphasizes the application of functions, in

contrast to the Procedural style, which

emphasizes changes in state.

Procedural V/s Functional Languages

11

tsum := 0;

i := 0;

while (i < n) do

i := i + 1;

tsum := tsum + I

od

func sumto(n: int): int;

if n = 0

then 0

else n + sumto(n-1)

fi

endfunc;

Procedural Style Functional Style

12

Object-Oriented Concepts

Data Abstraction (specifies behavior)

Encapsulation (controls visibility of names)

Polymorphism (accommodates various

implementations)

Inheritance (facilitates code reuse)

Modularity (relates to unit of compilation)

Object-Oriented Concepts

13

Abstraction –

a concept or idea not associated with any specific

instance.

Data abstraction allows handling data bits in meaningful

ways. For example, it is the basic motivation behind data

types. One can regard the notion of an object as an

attempt to combine abstractions of data and code.

Encapsulation:

Encapsulation of data and operations on that data.

The data and operation to be performed are written

together within same class.

Object-Oriented Concepts

14

Polymorphism:

Overloading of functions and operators

Same function may be found in two or more classes

Function with the same name appears in one class with different

sets of arguments

Inheritance (facilitates code reuse)

Code from one class can be reuse in another class by deriving a

relationship between parent class and child class.

Modularity is a software design technique that increases the

extent to which software is composed of separate,

interchangeable components called modules by breaking down

program functions into modules, each of which accomplishes one

function and contains everything necessary to accomplish this

Why so many?

15

Most important: the choice of paradigm

(and therefore language) depends on

how humans best think about the

problem

Other considerations:

◦ efficiency

◦ compatibility with existing code

◦ availability of translators

Lots of Languages

© Oscar Nierstrasz Safety Patterns 16

There are many programming languages out

there

Lots of other PL-like objects

◦ document languages, e.g. LaTeX,

Postscript

◦ command languages, e.g. bash, MATLAB

◦ markup languages, e.g. HTML and XML

◦ specification languages, e.g. UML

Generations of Programming

Languages 1GL: machine codes

2GL: symbolic assemblers

3GL: (machine-independent) imperative languages (FORTRAN, Pascal, C ...)

4GL: domain specific application generators

5GL: AI languages …

Each generation is at a higher level of abstraction

1.1

7

How do Programming Languages Differ?

Common Constructs:

basic data types (numbers, etc.); variables;

expressions; statements; keywords; control

constructs; procedures; comments; errors ...

Uncommon Constructs:

type declarations; special types (strings,

arrays, matrices, ...); sequential execution;

concurrency constructs; packages/modules;

objects; general functions; generics; modifiable

state; ...

1.1

8

Programming Paradigms

A programming language is a problem-solving tool.

Imperative style: program = algorithms + data

good for decomposition

Functional style: program = functions o functions

good for reasoning

Logic programming style: program = facts + rules

good for searching

Object-oriented style: program = objects + messages

good for modeling(!)

Other styles and paradigms: blackboard, pipes and filters,

constraints, lists, ... 1.1

9

A Brief Chronology Early 1950s ―order codes‖ (primitive assemblers)

1957 FORTRAN the first high-level programming language

1958 ALGOL the first modern, imperative language

1960 LISP, COBOL Interactive programming; business programming

1962 APL, SIMULA the birth of OOP (SIMULA)

1964 BASIC, PL/I

1966 ISWIM first modern functional language (a proposal)

1970 Prolog logic programming is born

1972 C the systems programming language

1975 Pascal, Scheme two teaching languages

1978 CSP Concurrency matures

1978 FP Backus’ proposal

1983 Smalltalk-80, Ada OOP is reinvented

1984 Standard ML FP becomes mainstream (?)

1986 C++, Eiffel OOP is reinvented (again)

1988 CLOS, Oberon, Mathematica

1990 Haskell FP is reinvented

1990s Perl, Python, Ruby, JavaScript Scripting languages become mainstream

1995 Java OOP is reinvented for the internet

2000 C# 1.2

0

Fortran

History

John Backus (1953) sought to write programs in conventional

mathematical notation, and generate code comparable to

good assembly programs.

No language design effort (made it up as they went along)

Most effort spent on code generation and optimization

FORTRAN I released April 1957; working by April 1958

The current standard is FORTRAN 2003

(FORTRAN 2008 is work in progress)

1.2

1

Fortran …

Innovations

Symbolic notation for subroutines and functions

Assignments to variables of complex expressions

DO loops

Comments

Input/output formats

Machine-independence

Successes

Easy to learn; high level

Promoted by IBM; addressed large user base

(scientific computing)

1.2

2

Object-Oriented Languages

History

Simula was developed by Nygaard and Dahl (early 1960s) in Oslo as a language for simulation programming, by adding classes and inheritance to ALGOL 60

Smalltalk was developed by Xerox PARC (early 1970s) to drive graphic workstations

1.2

3

Begin

while 1 = 1 do begin

outtext ("Hello World!");

outimage;

end;

End;

Transcript show:'Hello World';cr

Object-Oriented Languages

Innovations

Encapsulation of data and operations (contrast ADTs)

Inheritance to share behaviour and interfaces

Successes

Smalltalk project pioneered OO user interfaces

Large commercial impact since mid 1980s

Countless new languages: C++, Objective C, Eiffel, Beta, Oberon, Self, Perl 5, Python, Java, Ada 95 ...

1.2

4

Issues for all Languages

25

Can it be understood by people and

processed by machines?

◦ although translation may be required

Sufficient expressive power?

◦ can we say what needs to be said, at an

appropriate level of abstraction?

Translation

26

Compilation ◦ Translate into instructions suitable for some other (lower

level) machine

◦ During execution, that machine maintains program state information.

◦ Translate the program into machine code at once. That Machine code then executes and perform similar function as interpreter.

Interpretation ◦ May involve some translation

◦ Interpreter maintains program state

◦ Translate, Analyze & Execute program instruction by instruction.

Trade-offs

27

Compilation ◦ lower level machine may be faster, so

programs run faster

◦ compilation can be expensive

◦ examples: C (and Java?)

Interpretation ◦ more ability to perform diagnostics (or

changes) at run-time

◦ examples: Basic, UNIX shells, Lisp

Variable

28

A named location in memory that can hold a value Formally, a 5-tuple:

1. name 2. scope 3. type 4. l-value 5. r-value

Variable

Name and Scope

Declaration

Identifier rules and significant characters

Scope

range of instructions over which variable name is known.

Type:

Consists of

Set of values

Operations

Built-in/Primitive vs User-defined types

29

Variable

l-value: address/location

◦ lifetime

◦ memory allocation

r-value: contents/encoded value

◦ initialization

◦ constants

30