Sequence Control
Sequence Control
SEQUENCE CONTROL
Control Structure in a PL provides the basic framework within which operations
and data are combined into a program and sets of programs.
+ -
a b c d
Sequence Control Within Expressions
3. Syntax for Expressions
a) Prefix or Polish notation
Named after polish mathematician Jan Lukasiewicz, refers to
notation in which operator symbol is placed before its operands.
*XY, -AB, /*ab-cd
Cambridge Polish - variant of notation used in LISP, parentheses
surround an operator and its arguments.
(/(*ab)(-cd))
b) Postfix or reverse polish
Postfix refers to notation in which the operator symbol is placed
after its two operands.
AB*, XY-
c) Infix notation
It is most suitable for binary (dyadic) operation. The operator
symbol is placed between the two operands.
Sequence Control Within Expressions
4. Semantics for Expressions
Semantics determine the order of expression in which they are evaluated.
a) Evaluation of Prefix Expression
If P is an expression evaluate using stack
i) If the next item in P is an operator, push it on the stack. set the arguments
count to be number of operands needed by operator.
(if number is n, operator is n-ary operator).
ii) If the next item in P is an operand, push it on the stack
iii) If the top n entries in the stack are operand entries needed for the last n-ary
operator on the stack, apply the operator on those operands. Replace the
operator and its n operands by the result of applying that operation on the
n operands.
b) Evaluation of Postfix Expression
If P is an expression evaluate using stack
i) If the next item in P is an operand, push it on the stack.
ii) If the next item in P is an n-ary operator, its n arguments must be top n
items on the stack. Replace these n items by the result of applying this
operation using the n items as arguments.
Sequence Control Within Expressions
c) Evaluation of Infix Expression
Infix notation is common but its use in expression cause the problems:
i) Infix notation is suitable only for binary operations. A language cannot use
only infix notation but must combine infix and postfix (or prefix) notations.
The mixture makes translation complex.
ii) If more than one infix operator is in an expression, the notation is ambiguous
unless parentheses are used.
Sequence Control Within Expressions
5. Execution-Time Representation:
Translators evaluate the expression using a method so as to get efficient result
(optimum value at optimum time with optimum use of memory and processor).
Translation is done in two phases –
In first phase the basic tree control structure for expression is established. In next
stage whole evaluation process takes place.
The following methods are used for translation of expression –
a) Machine code sequences
Expression can be translated into machine code directly performing the two
stages (control structure establishment and evaluation) in one step.
The ordering of m/c code instructions reflect the control sequence of original
expression.
b) Tree Structure
The expressions may be executed directly in tree structure representation using a
software interpreter.
This kind of evaluation used in SW interpreted languages like LISP where programs
are represented in the form of tree during execution
c) Prefix or postfix form
Problems with Evaluation of Expressions
1. Uniform Evaluation Code
Eager Evaluation Rule – For each operation node, first evaluate each of the operands,
then apply the operation to the evaluated operands.
The order of evaluations shouldn’t matter.
In C: A + (B = 0 ? C : C/B) --------- Problem
Lazy Evaluation Rule – Never evaluate operands before applying the operation. Pass
the operands unevaluated and let the operation decide if evaluation is needed.
It is impractical to implement the same in many cases as it requires substantial
software simulation.
LISP, Prolog use lazy rule.
In general, implementations use a mixture of two techniques.
LISP – functions split into two categories
SNOBOL – programmer-defined operations always receive evaluated operands
language-defined operations receive unevaluated operands
2. Side Effects
The use of operations may have side effects in expressions
c / func(y) + c
r-value of c must be fetched and func(y) must be evaluated before division.
If fun(y) has the side effect of modifying the value of c, the order of evaluation is
critical.
Problems with Evaluation of Expressions
3. Short-circuit Boolean Expression
The only difference between a recursive call and an ordinary call is that the
recursive call creates a second activation of the subprogram during the lifetime
of the first activation.
catch – It’s a block that catches the exception thrown by throw statement and
handles it appropriately.
The same exception may be thrown multiple times in the try block.
There may be many different exceptions thrown from the same try block.
There can be multiple catch blocks following the same try block handling
different exceptions thrown.
• The current value of CIP is saved in the resume point location of activation
record for A.
The ip value in the resume point location is fetched from B’s activation record
and assigned to CIP so that subprogram B resume at proper location
SCHEDULED SUBPROGRAMS
Subprogram Scheduling
Normally execution of subprogram is assumed to be initiated immediately
upon its call
Subprogram scheduling relaxes the above condition.
Scheduling Techniques: