• No results found

Principles of Programming Languages

N/A
N/A
Protected

Academic year: 2022

Share "Principles of Programming Languages"

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

Principles of Programming Languages

Unit-1

COURSE CODE: CSE 2702

COURSE TEACHER: VASUDHA VASHISHT

Vasudha Vashisht

(2)

CONTENTS

Programming Language: Definition, Brief History

Why study programming languages?

Syntax and Semantic Rules of Programming Languages

Characteristics of a good programming language

Programming Language Paradigms

Programming Language Translators

Issues in Language Translation

Syntax and Semantics

Vasudha Vashisht

(3)

Programming Language: Definition, Brief History

Definition :

-A medium to communicate with computers!

-A notation of a algorithm and data structures are called a programming language.

Vasudha Vashisht

(4)

Why study programming languages?

To improve your ability to develop effective algorithms

To improve your use of existing programming languages

To increase your vocabulary of useful programming constructs

To allow a better choice of programming language

To make it easier to learn a new language

To make it easier to design a new language

Vasudha Vashisht

(5)

Syntax and Semantic Rules of Programming Languages

Syntax: what the program looks like.

Semantics: the meaning given to the various syntactic constructs.

Example:

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

int S[10];

Syntactic Concepts

Character set – The alphabet of the language. Several different character sets are used:

ASCII, EBCIDIC, Unicode

Identifiers – strings of letters of digits usually beginning with a letter Operator Symbols – +-*/

Vasudha Vashisht

(6)

Syntactic Concepts continued…

Keywords or Reserved Words – used as a fixed part of the syntax of a statement Noise words – optional words inserted into statements to improve readability

Comments – used to improve readability and for documentation purposes. Comments are usually enclosed by special markers

Blanks – rules vary from language to language. Usually only significant in literal strings Delimiters – used to denote the beginning and the end of syntactic constructs

Expressions – functions that access data objects in a program and return a value

Statements – these are the sentences of the language, they describe a task to be performed

Vasudha Vashisht

(7)

Characteristics of a good programming language

Vasudha Vashisht

Conceptual integrity

Orthogonality

Naturalness for the application

Support for abstraction

Ease of program verification

Programming environment

Portability of programs

Cost of use

Cost of execution.

Cost of program translation.

Cost of program creation, testing, and use.

Cost of program maintenance.

(8)

Programming Language Paradigms

Vasudha Vashisht

Imperative / procedural languages

-C, Pascal, FORTRAN, COBOL

Applicative / functional languages - ML, LISP

Rule-based / declarative languages -Prolog, BNF Parsing

Object-oriented languages -Java, C++, Smalltalk

(9)

Programming Language Translators

Vasudha Vashisht

Any program that is not written in machine language has to be translated in machine language before it is executed by the computer.

There are three types of translator programs i.e. Assembler, Compilers and Interpreters.

-ASSEMBLER: Assembler is a computer program which is used to translate program written in Assembly Language in to machine language. The translated program is called as object program.

- COMPILER: A compiler is a program that translates a program written in HLL to executable machine language.

- INTERPRETER: The basic purpose of interpreter is same as that of complier. In compiler, the program is translated completely and directly executable version is generated. Whereas interpreter translates each instruction, executes it and then the next instruction is translated and this goes on until end of the

program. In this case, object code is not stored and reused. Every time the program is executed, the interpreter translates each instruction freshly

(10)

Programming Language Translators

Vasudha Vashisht

Advantages of using compiler:-

-Since compiler converts the program to native code of the target machine (object code), faster performance can be expected.

-There is a scope for code optimisation.

Advantages of using interpreter:-

- Process of execution can be done in a single stage. There is no need of a compilation stage.

- Alteration of codes possible during runtime.

- Really useful for debugging the codes (because source code execution can be analysed in an IDE)

- Facilitates interactive code development.

(11)

Elementary Data Types

Vasudha Vashisht

Data Objects

a run-time grouping of one or more pieces of data in a virtual computer.

a location in memory with an assigned name in the actual computer

Programmer defined data objects

System defined data objects

Data value: a bit pattern that is recognized by the computer.

Elementary data object: contains a data value that is manipulated as a unit.

Data structure: a combination of data objects.

Attributes: determine how the location may be used.

Most important attribute - the data type.

(12)

Elementary Data Types…

Vasudha Vashisht

Type: determines the set of data values that the object may take and the applicable operations.

Name: the binding of a name to a data object.

Component: the binding of a data object to one or more data objects.

Location: the storage location in memory assigned by the system (bound at loading)

Value: the assignment of a bit pattern to a name (bound at execution)

(13)

Variables and Constants

Variables : Data objects defined and named by the programmer explicitly.

Constants: Data objects with a name that is permanently bound to a value for its lifetime.

Literals: constants whose name is the written representation of their value.

A programmer-defined constant: the name is chosen by the programmer in a definition of data object.

Persistence: existence of data beyond run time

Vasudha Vashisht

(14)

Data Types

A data type is a class of data objects with a set of operations for creating and manipulating them.

Examples of elementary data types: integer, real, character, Boolean, enumeration, pointer.

Specification of a data type

Attributes

-Distinguish data objects of a given type Data type and name - invariant during the lifetime of the object

Approaches:

-stored in a descriptor and used during the program execution

-used only to determine the storage representation, not used explicitly during execution

Vasudha Vashisht

(15)

Data Types…

Values

-The data type determines the values that a data object of that type may have -Specification: Usually an ordered set, i.e. it has a least and a greatest value

Operations

Operations define the possible manipulations of data objects of that type.

-Primitive - specified as part of the language definition -Programmer-defined (as subprograms, or class methods)

An operation is defined by:

-Domain - set of possible input arguments -Range - set of possible results

-Action - how the result is produced

Vasudha Vashisht

(16)

Elementary Data Types

Scalar Data Types

Numerical Data Types

Other

Composite Data Types

Character Strings

Pointers and Programmer-Constructed Objects

Files

Vasudha Vashisht

(17)

Scalar Data Types

Scalar data types represents a single object, i.e. only one value can be derived. In general, scalar objects follow the hardware architecture of a computer.

Numeric Data Types

Integers

Sub -ranges

Floating-point real numbers

Fixed-point real numbers

Other Data Types -

Enumerations

Booleans

Characters

Vasudha Vashisht

(18)

Numeric Data Types: Integers

Specification

-Maximal and minimal values

Operations:

-Arithmetic -Relational -Assignment -Bit operations

Implementation - hardware defined

Vasudha Vashisht

(19)

Numeric Data Types: Sub-ranges

Specification

-subtype of integer

-a sequence of integer values within some restricted range

Example:

- Pascal declaration A: 1..10 means that the variable A may be assigned integer values from 1 through 10

Implementation:

-smaller storage requirements, better type checking

Vasudha Vashisht

(20)

Numeric Data Types: Floating-point real numbers

Specification: Minimum and maximal value

-Round-off issues - the check for equality may fail due to round -off

Implementation

-Mantissa - exponent model.

Example: 10.5 = 0.105 x 10

2

, Mantissa: 105, Exponent: 2

Vasudha Vashisht

(21)

Numeric Data Types: Fixed-point real numbers

Specification:

real numbers with predefined decimal places

Implementation :

directly supported by hardware or simulated by software

Vasudha Vashisht

(22)

Other Scalar Data Types

Enumerations: Ordered list of different values

Booleans

Characters

Vasudha Vashisht

(23)

Enumerations

Example:

enum StudentClass

{Fresh, Soph, Junior, Senior}

the variable StudentClass may accept only one of the four listed values.

Implementation: represented during run time as integers, corresponding to the listed values.

Vasudha Vashisht

(24)

Questions?

Vasudha Vashisht

References

Related documents