Вы находитесь на странице: 1из 17

Assignment-2

Principle of programming language

Roll no.: 0822it151047

Q.1 Explain Data types with Example

Ans.: In computer science and computer programming, a data type or simply type is a

classification of data which tells the compiler or interpreter how the programmer intends to
use the data. Most programming languages support various types of data, for
example: real, integer or Boolean. A data type provides a set of values from which
an expression (i.e. variable, function ...) may take its values. The type defines the
operations that can be done on the data, the meaning of the data, and the way values of
that type can be stored.[

Data types are used within type systems, which offer various ways of defining,
implementing and using them. Different type systems ensure varying degrees of type
safety.
Almost all programming languages explicitly include the notion of data type, though different
languages may use different terminology. Common data types include:

 integers
 booleans
 characters
 floating-point numbers
 alphanumeric strings
For example, in the Java programming language, the "int" type represents the set of 32-
bit integers ranging in value from -2,147,483,648 to 2,147,483,647, as well as the
operations that can be performed on integers, such as addition, subtraction, and
multiplication. Colors, on the other hand, are represented by three bytes denoting the
amounts each of red, green, and blue, and one string representing that color's name;
allowable operations include addition and subtraction, but not multiplication.
The type system uses data type information to check correctness of computer
programs that access or manipulate the data.

Classes of data types


Primitive data types

Machine data types


All data in computers based on digital electronics is represented as bits (alternatives 0 and
1) on the lowest level. The smallest addressable unit of data is usually a group of bits called
a byte (usually an octet, which is 8 bits). The unit processed by machine code instructions is
called a word (as of 2011, typically 32 or 64 bits). Most instructions interpret the word as
a binary number, such that a 32-bit word can represent unsigned integer values from 0 to or
signed integer values from to . Because of two's complement, the machine language and
machine doesn't need to distinguish between these unsigned and signed data types for the
most part.
There is a specific set of arithmetic instructions that use a different interpretation of the bits
in word as a floating-point number.

Boolean type
The Boolean type represents the values true and false. Although only two values are
possible, they are rarely implemented as a single binary digit for efficiency reasons. Many
programming languages do not have an explicit Boolean type, instead interpreting (for
instance) 0 as false and other values as true. Boolean data simply refers to the logical
structure of how the language is interpreted to the machine language. In this case a
Boolean 0 refers to the logic False. True is always a non zero, especially a one which is
known as Boolean 1.

Numeric types
Such as:

 The integer data types, or "whole numbers". May be sub-typed according to their ability
to contain negative values (e.g. unsigned in C and C++). May also have a small
number of predefined subtypes (such as short and long in C/C++); or allow users to
freely define sub ranges such as 1.12 .
 Floating point data types, usually represent values as high-precision fractional values
(rational numbers, mathematically), but are sometimes misleadingly called real
(evocative of mathematical real numbers). They usually have predefined limits on both
their maximum values and their precision. Output of these values is often represented in
a decimal number format.
 Fixed point data types are convenient for representing monetary values. They are often
implemented internally as integers, leading to predefined limits.
Composite types
Composite types are derived from more than one primitive type. This can be done in a
number of ways. The ways they are combined are called data structures. Composing a
primitive type into a compound type generally results in a new type, e.g. array-of-integer is a
different type to integer.

 Array stores a number of elements of the same type in a specific order. They are
accessed randomly using an integer to specify which element is required (although
the elements may be of almost any type). Arrays may be fixed-length or expandable.
 A list is similar to an array, but its contents are strung together by a series of
references to the next element.

 Record (also called tuple or struct) Records are among the simplest data structures. A
record is a value that contains other values, typically in fixed number and sequence and
typically indexed by names. The elements of records are usually
called fields or members.
 Union. A union type definition will specify which of a number of permitted primitive types
may be stored in its instances, e.g. "float or long integer". Contrast with a record, which
could be defined to contain a float and an integer; whereas, in a union, there is only one
type allowed at a time.

Enumeration
The enumerated type has distinct values, which can be compared and assigned, but which
do not necessarily have any particular concrete representation in the computer's memory;
compilers and interpreters can represent them arbitrarily. For example, the four suits in a
deck of playing cards may be four enumerators named CLUB, DIAMOND, HEART, SPADE,
belonging to an enumerated type named suit. If a variable V is declared having suit as its
data type, one can assign any of those four values to it. Some implementations allow
programmers to assign integer values to the enumeration values, or even treat them as
type-equivalent to integers.
String and text types
Such as:

 Alphanumeric character. A letter of the alphabet, digit, blank space, punctuation mark,
etc.
 Alphanumeric strings, a sequence of characters. They are typically used to represent
words and text.
Other types
Types can be based on, or derived from, the basic types explained above. In some
languages, such as C, functions have a type derived from the type of their return value.
Pointers and references
The main non-composite, derived type is the pointer, a data type whose value refers directly
to (or "points to") another value stored elsewhere in the computer memory using
its address. It is a primitive kind of reference. (In everyday terms, a page number in a book
could be considered a piece of data that refers to another one). Pointers are often stored in
a format similar to an integer; however, attempting to dereference or "look up" a pointer
whose value was never a valid memory address would cause a program to crash.

Abstract data types


Any type that does not specify an implementation is an abstract data type. For instance,
a stack (which is an abstract type) can be implemented as an array (a contiguous block of
memory containing multiple values), or as a linked list (a set of non-contiguous memory
blocks linked by pointers).
Abstract types can be handled by code that does not know or "care" what underlying types
are contained in them. Programming that is agnostic about concrete data types is
called generic programming. Arrays and records can also contain underlying types, but are
considered concrete because they specify how their contents or elements are laid out in
memory.
Examples include:

 A queue is a first-in first-out list. Variations are Deque and Priority queue.
 A set can store certain values, without any particular order, and with no repeated
values.
 A stack is a last-in, first out data structure.
 A tree is a hierarchical structure.
 A graph.
 A hash, dictionary, map or associative array is a more flexible variation on a record, in
which name-value pairs can be added and deleted freely.
 A smart pointer is the abstract counterpart to a pointer. Both are kinds of references.
Utility types
For convenience, high-level languages may supply ready-made "real world" data types, for
instance times, dates and monetary values and memory, even where the language allows
them to be built from primitive types.
Q.2 Explain design and implementation issues in record, union and
pointer.

Ans. : Record Types

• A record is a possibly heterogeneous aggregate of data elements in which


the individual elements are identified by names

• COBOL uses level numbers to show nested records (others use recursive
definition)

01 EMP-REC.

02 EMP-NAMES.

03 FIRST PIC X(20).

04 MID PIC X(10).

05 LAST PIC X(20).

06 HOURLY-RATE PIC 99V99.

1. COBOL

– field_name OF record_name_1 OF ... OF record_name_n

2. Others (dot notation)

– record_name_1.record_name_2. ... record_name_n.field_name

• Operations:

– Assignment is common if types are identical

– Ada allows record comparison


– COBOL provides MOVE CORRESPONDING

• Copies a field of the source record to the corresponding field in


the target record

Unions Types

• A union is a type whose variables are allowed to store different types of


values at different times

union time {
long simpleDate;
double perciseDate;} mytime;

printTime (mytime.perciseDate);

• Design issues

– Should type checking be required?

– Should unions be embedded in records?

– In Fortran, C, and C++, no language-supported type checking for


union, called free union

– Most common way of remembering what is in a union is to embed it


in a structure

– struct var_type {

– int type_in_union;

– union {

– float un_float;
– int un_int; } vt_un;

– } var_type;

• Discriminated union: in order to type-checking unions, each union includes


a type indicator called a discriminate

– Supported by Ada

– Can be changed only by assigning entire record, including


discriminate  no inconsistent records

Evaluation of Unions

• Free unions are unsafe

– Do not allow type checking

• Java and C# do not support unions

– Reflective of growing concerns for safety in programming language

• Ada’s descriminated unions are safe

Pointer and Reference Types

• A pointer type variable has a range of values that consists of memory addresses
and a special value, nil

• Provide the power of indirect addressing

• Provide a way to manage dynamic memory


• A pointer can be used to access a location in the area where storage is
dynamically created (heap)

Pointer Assignment

• The assignment operation j = *ptr

Design Issues of Pointers

• Dangling pointers (dangerous)

– A pointer points to a heap-dynamic variable that has been deallocated


 has pointer but no storage

– What happen when deferencing a dangling pointer?

• Lost heap-dynamic variable


– An allocated heap-dynamic variable that is no longer accessible to the
user program (often called garbage)
 has storage but no pointer

• The process of losing heap-dynamic variables is called memory


leakage

Dangling Pointer Problem

• Tombstone: extra heap cell that is a pointer to the heap-dynamic variable

– The actual pointer variable points only at tombstones

– When heap-dynamic variable de-allocated, tombstone remains but is


set to nil

– Any pointer variables pointing to that heap-dynamic variable will


know it is gone by noticing tombstone becomes nil

– Costly in time and space


Q.3 Explain following terms with example:

a. Type Checking.
b. Binding
c. Type Compatibility

Ans.: (a) Type Checking.

Type Checking (Type Casting):

• The activity of ensuring that the operands of an operator are of compatible


types

– A compatible type is one that is either legal for the operator, or is


allowed to be implicitly converted, by compiler-generated code, to a
legal type, e.g.,
(int) A =(int) B + (real) C

– This automatic conversion is called a coercion

• All type bindings static  nearly all type checking can be static
Type binding dynamic  type checking dynamic

Strong Typing:

• A programming language is strongly typed if type errors are always detected

– Advantage: allows the detection of the misuses of variables that result


in type errors

– FORTRAN 77 is not: EQUIVALENCE

– C and C++ are not: unions are not type checked


• Coercion rules can weaken strong typing

– Example: a and b are int ; d is float;


no way to check a + b mistakenly typed as a + d

Type Equivalence

Type checking checks compatibility of operand types for operators 


compatibility rules

• Simple and rigid for predefined scalar types


• Complex for structured types, e.g., arrays, structures, user-defined types
– They seldom coerced  no need to check compatibility
– Important to check equivalence, i.e., compatibility without coercion 
how to define type equivalence?

Name Type Equivalence


• Two variables have equivalent types if they are in either the same
declaration or in declarations that use the same type name

• Easy to implement but highly restrictive:

– Sub ranges of integer types are not equivalent with integer types, e.g.,
Ada

type Index type is 1100;

count: Integer; index: Index type;

– Formal parameters must be the same type as their corresponding


actual parameters

Structure Type Equivalence


• Two variables have equivalent types if their types have identical structures
• More flexible, but harder to implement  need to compare entire structures

– Are two record types compatible if they are structurally the same but
use different field names?

– Are two array types compatible if they are the same except the
subscripts? e.g. [1..10] and [0..9]

– Are two enumeration types compatible if their components are spelled


differently?

– How about type celsius & fahrenheit of float?

(b)Binding

Type binding is the process of 'associating' a declared variable to a particular type.


(Done by the compiler) .

Type binding can be classified as –

• Static type binding.

• Dynamic type binding

Early Binding

• In early binding, the compiler matches the function call with the correct
function definition at compile time. It is also known as Static
Binding or Compile-time Binding. By default,

The compiler goes to the function definition which has


been called during compile time. So, all the function calls you have studied till
now are due to early binding.

• About function overriding in which the base and derived classes have
functions with the same name, parameters and return type. In that case also,
early binding takes place.
Late Binding

• In the case of late binding, the compiler matches the function call with the
correct function definition at runtime. It is also known as Dynamic
Binding or Runtime Binding.

• In late binding, the compiler identifies the type of object at runtime and then
matches the function call with the correct function definition.

• By default, early binding takes place. So if by any means we tell the


compiler to perform late binding, then the problem in the previous example
can be solved.

• This can be achieved by declaring a virtual function.

d. Type Compatibility

Every type is compatible with itself. Two distinct types are compatible if they
satisfy at least one of the following conditions.

 They are both real types.


 They are both integer types.
 One type is a sub range of the other.
 Both types are sub ranges of the same type.
 Both are set types with compatible base types.
 Both are packed-string types with the same number of characters.
 One is a string type and the other is a string, packed-string, or Char type.
 One type is Variant and the other is an integer, real, string, character, or
Boolean type.
 Both are class, class-reference, or interface types, and one type is derived
from the other.
 One type is Pointer (an untyped pointer) and the other is any pointer type.
 Both types are (typed) pointers to the same type and the {$T+} compiler
directive is in effect.
 Both are procedural types with the same result type, the same number of
parameters, and type-identity between parameters in corresponding
positions.
Q.4 Explain the sequence control with expression.

Ans.: Sequence Control

There is an interpreter:

• Fetch the instruction

• Decode instruction

• Fetch designated operands

• Branch to designated operation

• Execute primitive operations 1 to n

• Control of the order of execution of the operations both primitive and user
defined.

• Implicit : determined by the order of the statements in the source program or


by the built-in execution model

• Explicit : the programmer uses statements to change the order of execution


(e.g. uses If statement)

Levels of sequence control

Expressions: How data are manipulated using precedence rules and parentheses.

Statements: conditional and iteration statements change the sequential execution.

Declarative programming: an execution model that does not depend


on the order of the statements in the source program.

Subprograms: transfer control from one program to another.


Sequencing with expressions

Functional composition: Basic sequence-control mechanism:

Given an operation with its operands, the operands may be:

 · Constants

 · Data objects

· Other operations

Example

Example 1: 3 * (var1 + 5)

operation - multiplication, operator: *, arity - 2

operand 1: constant (3)

operand 2: operation addition

operand1: data object (var1)

operand 2: constant (5)

More examples and questions

Example 2: 3* var1 +5

Question: is the example equivalent to the above one?

Example 3: 3 + var1 +5

Question: is this equivalent to (3 + var1) + 5,

or to 3 + (var1 + 5) ?

Precedence and associativity

Precedence concerns the order of applying operations

Associatively deals with the order of operations of same precedence.


Precedence and associative are defined when the language is defined - within the
semantic rules for expressions.

Conditional statements

if expression then statement1

else statement2

if expression then statement1

a choice among many alternatives

nested if statements

case statements

Implementation: jump and branch machine instructions, jump table implementation


for case statements

Iteration statements

simple repetition (for loop)

specifies a count of the number

of times to execute a loop:

perform statement K times;

for loop –

Examples: for I=1 to 10 do statement;

for (I=0;I<10; I++) statement;

Repetition while condition holds.

While expression do statement;

Evaluate expression and if true execute statement, then repeat process.


Repeat statement until expression;

Execute statement and then evaluate expression.

Repeat if expression is not true.

C++ for loop functionally is equivalent to repetition while condition holds

Вам также может понравиться