You are on page 1of 8

Module 3

1. Abstract Data Type

Abstract Data Type is a new data type defined by the programmer. It includes New data
type, Operations and Encapsulation.
2. Encapsulation by Subprograms and Type Definitions
Encapsulation by Subprograms
Subprograms as abstract operations
Subprogram definition and invocation
Type Definitions
A mathematical function that maps each particular set of arguments into a particular set
of results
Specification of a subprogram consists of
the name of the subprogram
the signature of the subprogram: arguments, results
the action performed by the subprogram
Type checking for subprograms
Type checking: similar to type checking for primitive operations.
Difference: types of operands and results are explicitly stated in the program
Problems when describing the function computed by a subprogram
Implicit arguments in the form of non-local variables
Implicit results changes in non-local variables
History sensitiveness results may depend on previous executions
Implementation of a subprogram
Uses the data structures and operation provided by the language
Defined by the subprogram body
Local data declarations
Statements defining the actions over the data
Interface with the user: arguments and returned result
Implementation of subprogram definition and invocation
A simple (but not efficient) approach:

Each time the subprogram is invoked, a copy of its executable statements, constants and local
variables is created.
A better approach:
The executable statements and constants are invariant part of the subprogram - they do not need
to be copied for each execution of the subprogram.
Subprogram Definition and Activation
Subprogram definition: the set of statements constituting the body of the subprogram.
Static property; the only information available during translation.
Subprogram activation: a data structure (record) created upon invoking the subprogram.
It exists while the subprogram is being executed. After that the activation record is destroyed.

The definition serves as a template to create the activation record
Static code and dynamic activation record
A single copy is used for all activations of the subprogram. This copy is called code
segment. This is the static part.
The activation record contains only the parameters, results and local data.
This is the dynamic part. It has same structure, but different values for the variables.
Type Definitions
Type definitions are used for definition of a new type in terms of already defined type.
They do not define a complete abstract data type, because the definitions of the
operations are not included.
Format: typedef definition name
Meaning: definition is already defined type.

nameis substituted with definition.
typedef int key_type;
key_type key1, key2;
struct rational_number
{int numerator, denominator;}
typedef rational_number rational;
rational r1, r2;
Type equivalence and equality of data objects
Two questions to be answered:
When are two types the same?
When do 2 objects have the same value?
Name equivalence
Two data types are considered equivalent only if they have the same name.
I ssues
Every object must have an assigned type, there can be no anonymous types
A singe type definition must serve all or large parts of a program
Structural equivalence
Two data types are considered equivalent if they define data objects that have the same
internal components.
I ssues
Do components need to be exact duplicates?
Can field order be different in records?
Can field sizes vary?
Data object equality
Two objects are equal if each member in one object is identical to the corresponding
member of the other object.
The compiler has no way to know how to compare data values of user-defined type. It is
the task of the programmer that has defined that particular data type to define also the operations
with the objects of that type.
Type definition with parameters
Parameters allow for user to prescribe the size of data types needed array sizes.
Type definition with parameters is used as a template as any other type definition during

Control structures in a programming language provide the framework within which
operations and data are combined into programs and set of programs.
Control of the order of execution of the operations, both primitive and user defined, which we
term sequence control and control of the transmission of data among the subprograms of a
program is called data control.
Sequence control structures may be conveniently categorized into four groups:
1. Expressions form the basic building blocks for statements and express how data are
manipulated and changed by a program. Properties such as precedence rules and parentheses
determine how expressions become evaluated.
2. Statements or groups of statements, such as conditional and iteration statements, determine
how control flows from one segment of a program to another.
3. Declarative programming is an execution model that does not depend on statements, but
nevertheless causes execution to proceed through a program the logic-programming model of
Prolog is an example of this.
4. Subprograms, such as subprogram calls and co routines, form a way to transfer control from
one segment of a program to another.

I mplicit Sequence Control

Implicit sequence control mean, sequence control-structures are those defined by the
language (default) to be in effect unless modified by the programmer through some explicit
structure. Execution sequence determined by the order of the statements in the source program or
by the built-in execution model.
Explicit Sequence Control
Explicit sequence control structures are those that the programmer may optionally use to modify
the implicit sequence of operations defined by the language. The programmer uses statements to
change the order of execution
Levels of sequence control
Expressions: computing expressions using precedence rules and parentheses.
Statements: sequential execution, conditional and iteration statements.
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
The issue: given a set of operations and an expression involving these operations,
what is the sequence of performing the operations?
How is the sequence defined, and how is it represented?

An operation is defined in terms of an operator and operands.
The number of operands determines the arity of the operator.
Basic sequence-control mechanism: functional composition
Given an operation with its operands, the operands may be:
Data objects
Other operations
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)
Functional compositions imposes a tree structure on the expression,
where we have one main operation, decomposable into an operator and operands.
In a parenthesized expression the main operation is clearly indicated.
However we may have expressions without parentheses.
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) ?
In order to answer the questions we need to know:
Operator's precedence
Operator's associativity
Precedence concerns the order of applying operations, associativity deals with the order of
operations of same precedence.
Precedence and associativity are defined when the language is defined - within the semantic
rules for expressions.
Arithmetic operations / expressions
In arithmetic expressions the standard precedence and associativity of operations
are applied to obtain the tree structure of the expression.
Linear representation of the expression tree:
Prefix notation
Postfix notation
Infix notation
Prefix and postfix notations are parentheses-free.There are algorithms to evaluate prefix and

postfix expressions and algorithms to convert an infix expression into prefix/postfix notation,
according to the operators' precedence and associativity.
Other expressions
Languages may have some specific operations, e.g. for processing arrays and vectors,
built-in or user defined. Precedence and associativity still need to be defined - explicitly in the
language definition or implicitly in the language implementation.
Execution-time representation of expressions
Machine code sequence
Tree structures - software simulation
Prefix or postfix form - requires stack, executed by an interpreter.
Evaluation of tree representation
Eager evaluation - evaluate all operands before applying operators.
Lazy evaluation - first evaluate all operands and then apply operations
Side effects - some operations may change operands of other operations.
Error conditions - may depend on the evaluation strategy (eager or lazy evaluation)
Boolean expressions - results may differ depending on the evaluation strategy.
Statement level sequence control
Forms of statement-level control
Composition Statements are executed in the order they appear on the page.
Alternation Two sequences form alternatives so one sequence or the other
sequence is executed but not both. (conditionals)
Iteration A sequence of statements that are executed repeatedly.
Explicit Sequence Control
goto X
if Y goto X transfer control to the statement labeled X if Y is true.
Structured programming design
Hierarchical design of program structures
Representation of hierarchical design directly in the program text using "structured" control
The textual sequence corresponds to the execution sequence
Use of single-purpose groups of statements
"Structured" control statements
Compound statements
Typical syntax:

Execute each statement in sequence.
Sometimes (e.g., C) { ... } used instead of begin ... end
Conditional statements
if expression then statement1 else statement2
if expression then statement1
If we need to make a choice among many alternatives
nested if statements
case statements
Example :
case Tag is
when 0 =>
when 1 =>
when 2 =>
when others =>
end case
Implementation: jump and branch machine instructions, jump table implementation for case
statements (see fig. 8.7)
Iteration statements
Simple repetition (for loop) Specify a count of the number of times to execute a loop:
perform statement K times;

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. Quit if expression is true.
C++ for loop functionally is equivalent to repetition while condition holds
Problems with structured sequence control:
Multiple exit loops
Exceptional conditions
Do-while-do structure
Solutions vary with languages, e.g. in C++ - break statement, assert for exceptions.