Sunteți pe pagina 1din 62

Principles of Programming Languages

Control Structures
Outline

 Sequence Control
 Activation & Activation Record
 Central Stack
 Dynamic Scope and Referencing
Environment
 Parameter Passing Mechanisms
Sequence Control

• Expressions

• Statements

• Subprograms
Expressions

• Control mechanism

• Syntax

• Execution-time representation

• Evaluation
Control Mechanism

• Functional composition:

(A + B)*(C - A)

* (+ (A, B), - (C, A))


Syntax

• Infix:
A*B+C
 binary operations only
 computation order ambiguity
Syntax

• Prefix:
 ordinary
* (+ (A, B), - (C, A))

 Cambridge Polish
(* (+ A B) (- C A))

 Polish
*+AB-CA
Syntax

• Prefix:
 different numbers of operands
 ordinary/ Cambridge Polish: cumbersome with
parentheses
 Polish: number of operands known in advance
Syntax

• Postfix:
 ordinary
((A, B) +, (C, A) -) *

 Polish
AB+CA-*

 suitable execution-time representation


Execution-Time Representation

• Interpretation:
 tree structures *

+ -

A B C A

 prefix or
postfix
Execution-Time Representation

• Compilation: machine code sequences


A A A+B
PUSH A
PUSH B B
ADD
PUSH C
PUSH A
SUB A+B A+B A+B (A+B)*(C-A)

MUL C C C-A
A
Execution-Time Representation

• Compilation: machine code sequences


A A A+B
PUSH A
PUSH B B AB+CA-
ADD *
PUSH C
PUSH A
SUB A+B A+B A+B (A+B)*(C-A)

MUL C C C-A
A
Evaluation

• No simple uniform evaluation rule is satisfactory:


*

+ -

A B C A
Evaluation

• Side effects:
A * FOO(X) + A
+

*
A

A FOO(X)
Evaluation

• Side effects:
A * B * C = 1020 * 10-20 * 10-20

(A * B) * C = 1 * 10-20 = 10-20

A * (B * C) = 1020 * 0 = 0
Evaluation

• Short-circuit Boolean expressions:

if (A = 0) or (B/A > C) then …


Evaluation

• Short-circuit Boolean expressions:

if (A = 0) or else (B/A > C) then …


Sequence Control

• Expressions

• Statements

• Subprograms
Statements

• GOTO

• Sequencing

• Selection

• Iteration
GOTO

JMP L
GOTO L

L
Sequencing

begin
S1 codes
S1;
S2; S2 codes

Sn;
end Sn codes
Selection
• if:

JEQ0 A L1 JNEQ0 A L1
if A = 0 then S1
else S2; S2 codes S1 codes
S3; JMP L2 JMP L2
L1 S1 codes L1 S2 codes
L2 S3 codes L2 S3 codes
Selection
Ev
• JMP a+v
case: a JMP L0
var E: 0..2; a+1 JMP L1 JUMP table
case E of a+2 JMP L2
1: S1;
L1 S1 codes
2: S2;
else: S0 JMP L3
end; L2 S2 codes
S3; JMP L3
L0 S0 codes
L3 S3 codes
Iteration
• for:
for I := E1 to E2 do S;

I := E1;
L0: if I > E2 then GOTO L1;
S;
I := I + 1;
GOTO L0;
L1: …
Iteration
• while:
while C do S;

L0: if not(C) then GOTO L1;


S;
GOTO L0;
L1: …
Iteration
• repeat:
repeat S until C;

L0: S;
if not(C) then GOTO L0;
Sequence Control

• Expressions

• Statements

• Subprograms
Subprograms

• Simple call-return

• Recursive calls
Simple Call-Return

• No recursive calls
• Explicit calls
• Complete execution
• Immediate control transfer
• Single execution sequence
Simple Call-Return

MAIN A B
I0 I2 I4

call A call B
I1 I3
return
end return
Simple Call-Return
MAIN
I0

call A
I1

en
d
local data
MAIN

I0 CIP (current instruction


pointer)
Simple Call-Return
MAIN A
I0 I2

call A call B
I1 I3
return
end
local data local data A
MAIN
I1

I2 CIP
Simple Call-Return

MAIN A B
I0 I2 I4

call A call B
I1 I3
return
return
end
local data B
local data local data A
MAIN I3
I1

I4 CIP
Recursive Calls

MAIN A B
I0 I2 I4

call A call B call A


I1 I3 I5
end return return
R0 --
--
local data
MAIN I0 CIP R0 CEP (current environment
pointer)
Recursive Calls

MAIN A B
I0 I2 I4

call A call B call A


I1 I3 I5
end return return

R0 -- R1 I1
-- R0
local data
local data A
MAIN I2 CIP R1 CEP
Recursive Calls
I0 I2 I4

call A call B call A


I1 I3 I5
end return return
R0 -- R1 I1 R2 I3
-- R0 R1
local data
local data A local data B
MAIN

I4 CIP R2 CEP
Recursive Calls

I0 I2 I4

call A call B call A


I1 I3 I5
end return return
R0 -- R1 I1 R2 I3 R3 I5
-- R0 R1 R2
local data
local data A local data B local data A
MAIN

I2 CIP R3 CEP
Recursive Calls

I0 I2 I4

call A call B call A


I1 I3 I5
end return return
R -- R I1 R I3 R I5
0 1 2 3
-- R0 R1 R2
local data
local data A local data B local data A
MAIN

Dynamic chain
I2 CIP R3 CEP
Central Stack
MAIN R0 --
I0 CIP I0 MAIN --
local data
call A CEP R0
I1
end
Central Stack
MAIN
I0 CIP I2 R0 --
MAIN --
call A CEP R1 local data
I1 R1
end I1
A R0
A local data
I2

call B
I3
return
Central Stack
A
I2 R0
CIP I4 --
call B MAIN --
I3 CEP R2 local data
return R1
I1
A R0
B
local data
I4 R2
I3

call A B R1

I5 local data
return
B
R0 --
I4 CIP I2
MAIN --
call A CEP local data
R3
I5 R1
I1
return
A R0
local data
A R2
I3
I2
B R1
call B local data
I3 R2
I5
return A R2
local data
Dynamic Scope

• The subprogram activations within which the


association is effective.
• Association : A binding between a name and a data
object.
 Data object: a block of storage, containing data value
Dynamic Scope

program MAIN; MAIN SUB2


var X: integer;

procedure SUB1; object1
var X: real;

X := 1;

procedure SUB2; SUB1



X := 2;
Static Scope

program MAIN;
var X: integer; X  integer

procedure SUB1;
var X: real;
… Static scopes
X := 1; define dynamic
scopes
procedure SUB2;

X := 2;
program main;
var A, B, C: real;
procedure Sub1 (A: real);

var D: real;
procedure Sub2 (C: real);
Local Non-local Global
var D: real;
begin Sub2 C, D A,Sub2 B,Sub1
B,Sub1
… C:= C+B; …
end; Sub1 A,D,Sub2 B,C,Sub1 B,C,Sub1
begin main A,B,C,Sub1 A,B,C,Sub1
… Sub2(B); …
end;
begin Static referencing environment
… Sub1(A); …
end.
main  sub1  sub2  sub2
a obj1
a obj4 c obj6 c obj8
b obj2
d obj5 d obj7 d obj9
c obj3
sub2
sub1

main sub1 sub2 sub2

local non-local global

Sub2 obj8,obj9 obj4, obj2,sub1,sub2 obj2,sub1

sub2 obj6,obj7 obj4, obj2,sub1,sub2 obj2,sub1

sub1 obj4, obj5,sub2 obj2,obj3,sub1 obj2, obj3,sub1

main obj1,obj2,obj3,sub1 obj1,obj2,obj3,sub1

Dynamic referencing environment


a var integer 0
Implementation b var integer 2
Compilation time
c var char 4
var a,b:integer; Symbol Table

c:char;
begin
a := b -1;
… Execution a obj1
Time b obj2
end
c obj3
Activation
CEP + 0 Record
CEP
CEP + 2
program MAIN; Symbol Table Scope Stack
var X, Y, Z: char;
procedure SUB2; X | char|0
var X, Y: integer;
1Y | char|1 1
procedure SUB3;
1Z | char |1
var X: real;
Sub2 1| void -> void 1
procedure SUB4;
X|
1 integer|0 1
begin …end
begin … end Y|
0 integer|4
1 0
begin … end Sub3 1
0| void -> void 0
procedure SUB1; X|
0 integer|0
1 0
var Y, Z: integer; Sub4 1
0| void -> void
begin …end
begin … end
Formal Parameters and Actual
Parameters

 Formal parameters: data objects specified at


function declartion
Ex: function foo(x:integer; y:real);
 Actual parameters: data objects passed from
caller to callee
Ex: foo(a,b);
Passing parameter mechanisms

 In – out:
– Value - result
– Reference
– Name
 In only
– Value
– Constant value
– Constant parameter
 Out only
– Function
In – out
x 7
5 a 7
5
 Value - result
foo(x,y)  proc foo(inout a, inout b) y 8
6 b 8
6

foo(x,y)  proc foo(inout a, inout b) x 5


7 a

 Reference y 6
8 b
foo(x,y)  proc foo(var a, var b)
ai
 Name
b  x[i]
foo(i,x[i])  proc foo(name a, name b)
Example

var x: array[1..5] of integer; i: integer;


procedure swap(a,b:integer);
var t:integer;
begin
t := a; a := b; b := t;
end
begin
for i := 1 to 5 do x[i] := 6 – i;
i := 2;
swap(i, x[i]);
end.
In-only
x 5 a 7
5

 By value y 6 b 8
6
foo(x,y)  proc foo(in a,in b)
foo(x,y)  proc foo(in a,in b) x 5 a 5
 By constant value y 6 b 6
foo(x,y)  proc foo(const a,const b)
foo(x,y)  proc foo(const a,const b)
x 5 a
 By constant reference
foo(x,y)  proc foo(constvar a, constvar b) y 6 b
Out-only
x 7
5 a 7
 Result
foo(x,y)  proc foo(out a,out b) y 8
6 b 8

foo(x,y)  proc foo(out a,out b)

 Function form
a := foo() function foo():integer
Example


.
IP -

EP -

SCP -
procedure SUB2(K:int; var L:int)
begin I 1
K = K + 10; J 2
12
L = L + 10; IP -
end EP -
procedure SUB1; SCP -
var I,J: int
K 1
11 I
begin
I = 1; L
J = 2;
SUB2(I,J);
end
Example (cont’d)
procedure SUB1;
var I, J: integer;
procedure SUB3(var M, N: integer); begin
begin I := 1;
M := M + 10 J := 2;
N := N + 10 SUB2(I, J);
write(M, N) write(I, J)
end; end;
procedure SUB2(K: integer; var L: integer);
begin
K := K + 10
L := L + 10
SUB3(K, L); SUB1  SUB2  SUB3
write(K, L)
end;

I := 1 .
IP -
J := 2 EP -

SCP -
SUB2(I,J)
I 1
IK
J 22
2
12
JL
IP -

K := K + 10 EP -

SCP -
L := L + 10
K 1
21
11
SUB1(K,L)
L
KM IP -

EP -
LN SCP -

M := M + 10 M
N := N + 10 N
Example
type VECT = array [1..3] of integer; procedure SUB2(C: VECT; var D: VECT);
procedure SUB1; var I: integer;
var A, B: VECT; begin
J: integer; C[2] := C[2] + 10;
begin D[2] := D[2] + 10;
A[1] := 7; A[2] := 8; A[3] := 9; for I := 1 to 3 do write(C[I]);
B[1] := 7; B[2] := 8; B[3] := 9; for I := 1 to 3 do write(D[I])
SUB2(A, B); end;
for J := 1 to 3 do write(A[J]);
for J := 1 to 3 do write(B[J]);
end;

IP -
EP -
SCP -

A Descriptor
A[1] := 7; A[2] := 8; A[3] :=9; 7

B[1] := 7; B[2[ := 8; B[3] := 9; 8 SUB1


9 Activation
SUB2(A,B); record
B Descriptor

A C 7
18
8
BD 9

C[2] := C[2] + 10 J
IP -
EP
D[2] := D[2] + 10 SCP
-
-

C Descriptor
7 SUB2
Activation
18
8 record
9
D
I
Example

type VECT = array [1..3] of integer; procedure SUB2(R: VECTPTR;


VECTPTR = ^VECT; var S: VECTPTR);
procedure SUB1;
begin
R^[1] := R^[1] + 10;
var A, B: VECT;
S^[1] := S^[1] + 10;
P, Q: VECTPTR;
if . . . then R := S
begin
else S := R
A[1] := 7; A[2] := 8; A[3] := 9; end;
B[1] := 7; B[2] := 8; B[3] := 9;
P := @A; Q := @B;
SUB2(P, Q);
end;
Passing by Name
type VECT = array [1..3] of integer; K := K + 1;
procedure SUB2 (name I, J: integer); A[K] := A[K] + 1;
begin
write(K,A[K])
I := I + 1;
J := J + 1;
write(I, J)
end;
procedure SUB1;
var A: VECT;
K: integer;
begin
A[1] := 7; A[2] := 8; A[3] := 9;
K := 2;
SUB2(K, A[K]);
for K := 1 to 3 do write(A[K])
end;