41
Programming Languages Marjan Sirjani

Pratt Chapter 2

Embed Size (px)

Citation preview

Page 1: Pratt Chapter 2

Programming Languages

Marjan Sirjani

Page 2: Pratt Chapter 2

2

2. Language Design Issues

Design toRun efficiently : early languagesEasy to write correctly : new languages

Data typing features in MLClass of C++Package of Ada

Page 3: Pratt Chapter 2

3

Major influences on designing a language

The underlying computer which programs execute upon;

The execution model, or virtual computer that supports the language on the actual hardware;

The computational model that the language implements.

Page 4: Pratt Chapter 2

4

2.1. The Structure And Operation Of A ComputerA computer is an integrated set of algorithms and data structures capable of storing and executing programs.Hardware computer or virtual computer

Page 5: Pratt Chapter 2

5

Major Components of a Computer (From a Programming

Language Designers View)

DataVarious kinds of elementary and structured

data.

Primitive operations

Sequence controlControlling the sequence of primitive

operations execution.

Page 6: Pratt Chapter 2

6

Major Components of a Computer (From a Programming

Language Designers View)

Data access Controlling the data supplied to each execution of

an operation.

Storage management Controlling the allocation of storage for programs

and data.

Operating environment Providing mechanisms for communication with an

external environment containing programs and data.

Page 7: Pratt Chapter 2

7

Data

Main memory

High-speed register

High-speed cache memory

External files

Data and Program

Page 8: Pratt Chapter 2

8

operations

A set of build-in primitive operations Arithmetic operations on each built-in numeric

data (+,-,*,/) Testing various properties of data items (test for

zero, positive, and negative numbers) Accessing and modifying various parts of a data

item Controlling input-output devices Sequence control (jumps)

Page 9: Pratt Chapter 2

9

Sequence Control

There is an interpreter :Fetch the instructionDecode instructionFetch designated operandsBranch to designated operationExecute primitive operations 1 to n

Using an address register

Page 10: Pratt Chapter 2

10

Data Access

Access to operands of the operation

Page 11: Pratt Chapter 2

11

Storage Management

Keeping all resources of the computer operating as much as possibleMemoryCentral processorExternal data devices

MultiprogrammingCache memory

Page 12: Pratt Chapter 2

12

Operating Environment

The outside world of computer;

a set of peripherals and input-output devices

Page 13: Pratt Chapter 2

13

Computer States

We saw the static organization of the computer,

We also must see the dynamic operation of it during program execution.

Page 14: Pratt Chapter 2

14

Language Implementation

Translation (compilation) Accept programs in some source language as input and produce functionally equivalent programs

in another object language as output.

Assembler : assembly to machine language. Compiler : a high level language to assembly or

machine code. Loader,linker editor : machine code in relocatable form

to machine code really executable. Preprocessor : a high level language to a high level

language in a standard form .

Page 15: Pratt Chapter 2

15

Language Implementation

Software simulation (interpretation)

the simulator executes the input program directly.

Page 16: Pratt Chapter 2

16

Differences Between Software ImplementationsTranslation versus Interpretation

Physical input sequence –

Logical flow of control

Process each program statement exactly once –

might process some statements repeatedly

Page 17: Pratt Chapter 2

17

Differences Between Software Implementations

Faster program execution – slow program execution

Loss of information about program – leaving statements in their original form

Object program much larger then the source program –

no space is needed for object program

Page 18: Pratt Chapter 2

18

Firmware Computers

A common alternative to the strict hardware realization of a computer is the firmware computer,

simulated by a micro-program running on a special micro-programmable hardware computer.

(emulation)

Page 19: Pratt Chapter 2

19

2.2. Virtual Computers

Hardware realizationPhysical devices

Firmware realizationmicroprogramming

Software simulationSome other programming language

Combination of these techniques

Page 20: Pratt Chapter 2

20

Syntax and Semantics

Syntax: what the program looks like.

Semantics: the meaning given to the various syntactic constructs.

Example:

V: array [0..9] of integer;

int V[10];

Page 21: Pratt Chapter 2

21

Hierarchies of Computers

Virtual computer developed by programmer

The language virtual computer

Operating system virtual computer

Firmware virtual computer

Actual hardware computer

Page 22: Pratt Chapter 2

22

Binding and Binding Times

Binding of a program element to a particular characteristic or property : the choice of the property from a set of possible properties.

Binding time of the property for that element: the time during program formulation or processing when this choice is made.

Page 23: Pratt Chapter 2

23

Examples Of Binding

Setting the value of an attributeA variable: name , type, storage areaA routine: name formal parameters of a

certain type, certain parameter-passing conventions,

A statement: associated actions.

Page 24: Pratt Chapter 2

24

Classes of Binding Times

1. Execution time (run time)

(dynamic binding)

2. Translation time (compile time)

3. Language implementation time

4. Language definition time

Page 25: Pratt Chapter 2

25

1. Execution time :On entry to subprogram or block.

Binding of formal to actual parameters in C.At arbitrary points during execution .

Binding of variables to values by assignments.

(can be modified repeatedly during execution)

Page 26: Pratt Chapter 2

26

2. Translation time (compile time)Bindings chosen by the programmer

Variable names and typesBindings chosen by the translator

Relative locations of a data object in the storage allocated for a procedure

Bindings chosen by the loaderActual addresses

Page 27: Pratt Chapter 2

27

3. Language implementation timeThe details associated with the

representation of numbers and of arithmetic operations.

(integer type memory representation)

4. Language definition timePossible statement forms, data structure

types, program structures. (integer type definition)

Page 28: Pratt Chapter 2

28

Programming languages differ inThe number of entities with which they can

deal,The number of attributes to be bound to

attributes,The time at which such bindings occur

(binding time),The stability of the binding (fixed or

modifiable)

Page 29: Pratt Chapter 2

29

Importance of Binding Times

Many of the most important differences among languages involve differences in binding times.

Early binding : efficiency.

(FORTRAN types, arithmetic operations)

Late binding : flexibility.

(ML types, string manipulations)

Page 30: Pratt Chapter 2

30

Binding Time: An Example

X := X + 10

Set of possible types for variable X.

Type of variable X.

Set of possible values for variable X.

Value of the variable.

Representation of the constant 10.

Properties of the operator +.

Page 31: Pratt Chapter 2

31

Attributes of a variable:NameScopeTypeL-valueR-value

Page 32: Pratt Chapter 2

32

Name and Scope

Static scope binding: defines the scope in terms of the lexical structure of a program . (C, Pascal)

Dynamic scope binding: defines the scope of a variable’s name in term of program execution. (same dcl until new dcl)

(APL, LISP, SNOBOL)

Page 33: Pratt Chapter 2

33

Name and Scope (dynamic binding)

{ /* block A*/ int x; … } { /* block B*/ int x; … } { /* block C*/ … x:= …; … }

Page 34: Pratt Chapter 2

34

Type

Static typing: bind variables to their type at compile time, and the binding can not be changed during execution. Static type checking

Dynamic typing: run-time binding between variables and their types. (according to the value assigned)

Page 35: Pratt Chapter 2

35

2.3. Language Paradigms

How does the program execute?

What sort of constructs does the language provide?

Page 36: Pratt Chapter 2

36

Four basic computational models:

Imperative languages

Applicative language

Rule-based language

Object-oriented programming

Page 37: Pratt Chapter 2

37

Imperative Languages

Imperative or procedural languages are command-driven or statement-oriented languages.Basic concept : machine state.The syntax has the form like: statement1; statement2; …

Page 38: Pratt Chapter 2

38

Applicative Languages

Applicative or functional languages look at the function that program represents rather than just to the state changes as the program executes, statement by statement.

The syntax has the form like: functionn(…function2(function1 (data))…)

Page 39: Pratt Chapter 2

39

Rule-based Languages

Rule-based or logical languages execute by checking for the presence of a certain condition and when it is satisfied, they execute an appropriate action.The syntax has the form like: enabling condition1 action1

enabling condition2 action2

enabling conditionn actionn

Page 40: Pratt Chapter 2

40

Object-Oriented Languages

Object-oriented languages can be viewed as combining imperative and functional paradigms. Abstract data types inheritance

efficiency of imperative languages flexibility and reliability of functional languages.

Page 41: Pratt Chapter 2

41

Generality of Computational Model

How one uses a programming language depends on the programmer.