Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Sequence control
1
to proceed, though a program prolog is the example for
this.
2- Left-to-right evolution-
Operators of the same priority are evaluated from the left to right.
Arithmetic expressions are evaluated from left to right using the
rules of precedence:
eg- 18-7-5
= (18-7)-5
= 11-5
= 6
#Not equal to
18-(7-5)
= 18-2
=16 wrong ans.
3. Parentheses-
An expression enclosed in parentheses is evaluated before being
applied as an argument. In combination with priorities and left-to-
right evaluation parentheses are only needed where the standard
evaluation order is not desired.
For example- 18-(7-5)
= 18-2
= 16
Tree-structure representation-
3
In arithmetic expression tree, leaves are used to represent operands
and root in used for main operation of expression. The nodes
between roots and leaves are used for intermediate operations.
Example: -1
Evaluate the following expression-
A-(C/5 *2) + (D *5 % 4)
4
Example 2. Tree representation of quadratic formula –
Consider the following formula for computing root of the quadratic
equation.
5
Expression Tree:-
The structure of an expression may be represented by an expression
tree. An expression tree is a recursive data structure.
Which are defined as –
6
SYNTAX FOR EXPRESSION: -
For syntax purpose of expression, we need three notations. These
are:-
Prefix notation (Polish prefix Notation)
Postfix Notation (Suffix or reverse Polish Notation)
Infix Notation.
2. Postfix Evaluation :-
Let P is the given expression then it is executed in following
manner:
If the next item of P is an operand, place it on the stack.
If the next item of P is an n-ary operator, then it’s n arguments
must be the top n items on the stack. Replace these n items by the
result.
3. Infix Evaluation :-
Let P is the given expression then it is executed in following
manner:
As infix notation is suitable for binary operations, A language
can not use only infix notation. Therefore, we can use mixture of
infix with prefix or postfix. But to evaluate mixture it is very
complex task.
When more then one infix operator appears in an expression,
parentheses are used at the right place.
8
Sequence control for statements :-
(Sequence control between statement) :-
1. Basic statement :
The result for any program are determined by
its basic statement that apply operations to data objects. Basic
statements include assignment statements, subprogram calls and
input-output statements.
Normally basic statement are written in a single line.
Few basic statements are –
Assignment statement
Input statement
Go to statement
1. Assignment statement –
9
Assignment is the central operation define for every elementary
data type.
For Example-
int i, *p;
p = &i;
*p = 7
We have:
• i is declared as an integer ;
• p is declared as a pointer to an integer;
• p is set to point to i ,
i.e., L-value is converted to an R-value (& i) and this R- value
stored as the R-value of p;
• p’s R-value is converted to be an L-value (*p);
it is the L-value of i, so R-value set to 7.
2. Input statement-
To give the input at run time, we can use input statement.
In c-programming we have –
3. Goto statement –
10
Goto statement is explicit sequence control statement. It transfer
the control from one location to another location.
4. Break statement –
2.COMPOUND STSTEMENT –
Compound statement is the
sequence of statement that may be treated as a single statement in
the construction of larger statements.
A compound statement is written as :
begin
... Sequence of statements (one or more)
end
3.CONDITIONAL STATEMENT –
11
A conditional statement is
one that expresses alternation of two or more statements, or optional
execution of a single statement;
The choice of alternative is controlled by a test on some
condition, usually written as an expression involving relational
and Boolean operations. The most common forms of conditional
statements are the if and case statements.
- if statement
- for statement
- while statement
- Do loop
Subprogram
In/out Localvariables Read/ Global
parameters Modifies variables
Statements
Out parameters
12
Generic view of subprogram
Implementation-
13
To understand the implementation of the simple call-return
control structure, it is important to build a more complete
model of what it means to say that a program is being
executed.
For expressions and statement sequence, we think of each as
represented by a block of executable code at run time.
Execution of the expression or statement sequence means
simply execution of the code, using a hardware and software
interpreter but for subprograms, we need more:
There is a distinction between subprogram definition and
subprogram activation.
An activation is implemented as two parts: A code segment
containing the executable code and constants, and an
activation record containing local data, parameters and various
other data items.
The code segment is invariant during execution.
The activation record is created each time the subprogram is
call, and it is destroyed when the subprogram returns.
Call
Return
14
• Current-instruction pointer (CIP)
Exceptions Handling :-
Exceptions are exceptional
circumstances (like runtime errors) in our program by transferring
control to special functions called handlers.
Exception handling is a programming language
construct or computer hardware mechanism designed to handle the
occurrence of some condition that changes the normal flow of
execution. The condition is called an exception.
When an error occurs with in a
method, the method creates an object and hands it of to the runtime
system. The object, called an exception object, contains information
about the error; creating an exception object and handing it to the
runtime system is called throwing an exception.
After a method throws an exception, the runtime
system attempts to find some thing to handle it. The set of possible
“some things” to handle the exception is the ordered list of methods
that had been called to get to the method where the error occurred.
The list of methods is known as the call stack.
16
The run time system searches the call stack for a method that
contains a block of code that can handle the exception. This block of
code is called an exception handler. The search begins with the
method in which the error occurred and proceeds through call stack
in the reverse ordered in which the methods were called. When an
appropriate handler is found, the runtime system passes the
exception to the handler.
17
Thus, exceptions provide a way to react to exceptional
circumstances (like runtime errors) in our program by transferring
control to special function called handlers. Because an exception
handler is invoked without an explicit call, it ordinarily does not
require a name or parameters.
The definition of an exception handler typically contains only-
• A set declarations of local variables (if any), and
• A sequence of executable statements.
$$ COROUTINES: -
Co-routines are subprograms that can return control to the caller
before completion of execution. When a coroutine receives control
from another subprogram, it executes partially and then is
suspended when it returns control. At a later point, the calling
program may resume execution of the coroutine from the point at
which execution was previously suspended.
18
For Example-
If A calls subprograms B as a coroutine, B executes awhile and
returns control to A, just as any ordinary subprogram would do.
When A again passes control to B via a resume B, B again executes
awhile and returns control to A, Just as an ordinary subprogram.
However, the situation is similar when viewed from subprogram B.
B, in the middle of the execution, resume execution of A.
A executes awhile and returns control to B. B continues execution
awhile and return control to A. A continues execution awhile and
returns control to B. From subprogram B, A appears much like an
ordinary subprogram.
The name coroutine derives from this
symmetry, the two programs appear more as equals-two
subprograms swapping control back and forth as each executes, with
neither clearly controlling the other.
“Coroutines are execution context that exist concurrently, but that
execute one at a time and that transfer control to each other
explicitly, by name.”
19
$$ SCHEDULED SUBPROGRAMS
The concept of subprogram scheduling results from relaxation of the
assumption that execution of a sub program should always be
initiated immediately upon its call. One may think of an ordinary
subprogram call statement as specifying that the called subprogram
is to be scheduled for execution immediately, without completing
execution of the calling program. Completion of execution of the
calling program is rescheduled to occur immediately on termination
of the subprogram. The exception-handling control structure may be
viewed also as means of subprogram scheduling.
Generalizing further, other subprogram scheduling techniques are
possible:
1. Schedule subprograms to be executed before or after other
subprograms, as, for example:-
call B after A,
Which would schedule execution of subprogram B after
execution of subprogram A is completed.
Concurrent execution: -
The Principal mechanism for installing
parallel execution in a programming language is to create a
construct that allows for parallel execution.
The end statement performs this task:
Satatement1 and statement2 and…and statement n
And has the semantics that each of the various statement i
execute in parallel; the statement following the and statement
does not begin until all the parallel statements terminate.
It provides the full parallel power we need for
parallel execution.
For Example:-
If an operating system consists of a task to read from a
terminal, a task to write to a screen and a process to execute a
user program, we could specify this operating system as:
Call Read Process and
Call Write Process and
Call Execute user program;
Pratt/pg-294/7.2
$$
Data Control Referencing Environments
Attributes of data control:-
The data-control features of a
programming language are those parts concerned with the
accessibility of data at different points during program
execution.
The data-control features of a language determine
how data may be provided to each operation, and how a result
of one operation may be saved and retrieved for later use as an
operand by a subsequent operation.
For Example:
A Pascal program contains:
X := Y + 2 * Z
21
Simple inspection indicates three operations in sequence: a
multiplication, an addition, and an assignment. One operand of
the multiplication is clearly the number 2, but the other
operands are marked only by the identifiers X, Y and Z and
these obviously are not the operands but only designate the
operands in some manner.
1. Variables names
2. Formal parameters names
3. Subprograms names
22
4. Names for defined types
5. Names for defined constants
6. Statement labels (names for statements)
7. Exception names
8. Names for primitive operations, e.g., +, *, SQRT.
9. Names for literal constants, e.g., 17, 3.25.
Associations:
Data control is concerned in large part with the binding
of identifiers to particular data objects and subprogram. Such a
binding is termed an association and may be represented as a pair
consisting of the identifier and its associated data object or
subprogram.
During the execution of a program, we observe the following:
1. At the beginning of execution of the main program, identifier
associations bind each variable name declared in the main
program to a particular data object and bind each subprogram
name invoked in the main program to a particular subprogram
definition.
2. As the main program executes, it invokes referencing
operations to determine the particular data object or
subprogram associated with an identifier.
For Example: A := B+ FN (C)
23
4. As the subprogram executes, it invokes referencing operations
to determine the particular data object or subprogram
associated with each identifier.
5. When the subprogram returns control to the main program, its
associations are destroyed (or become inactive).
6. When control returns to the main program, execution continues
as before using the associations originally setup at the start of
the execution.
Referencing Environment: -
Each program or subprogram has a set
of identifiers associations available for use in referencing during
its execution. This set of identifier associations is termed the
referencing environment of the subprogram (or program). The
referencing environment of a sub program is ordinarily invariant
during its execution. It is set up when the subprogram activation
is created and it remains unchanged during the lifetime of the
activation.
The referencing environment of a subprogram may have several
components:
1. Local referencing environment (or simply local environment).
2. Nonlocal referencing environment.
3. Global referencing environment.
4. Predefined referencing environment
Dynamic Scope:
Bindings cannot always be resolved by
examining the program because they are dependent on calling
sequences. Dynamic Scope is:
Based on calling sequences of program units, not their textual
layout.
References to variables are connected to declarations by
searching back through the chain of subprogram calls that
forced execution to this point.
Dynamic scope rules are usually encountered interpreted
languages.
Such languages do not normally have type checking at
compile time because type determination is not always possible
when dynamic scope rules are in effect.
Bindings between names and objects depend on the flow of
control at runtime.
-------------------------------------------------------------------------
25
Most modern programming languages such as Pascal, C++
and Java use Static Scope.
Some languages such as APL, SNOBOL4 and early versions of
LISP use Dynamic Scope.
Static Scoping
Advantages
• Read ability
• Locality of reasoning
• Less run-time overhead
Disadvantages
• Some loss of flexibility
Dynamic Scoping\
Advantages
• Some extra convenience
Disadvantages
• Loss of readability
• Unpredictable behavior (no locality of reasoning)
• More run-time overhead
27
Specification: A common environment is identical to a local
environment for a subprogram except that it is not part of any
single subprogram. It may contain definitions of variable,
constants, and types, but no subprograms or formal parameters.
29