Principles of Programming Languages: Control Structures
Principles of Programming Languages: Control Structures
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)
• 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-*
• Interpretation:
tree structures *
+ -
A B C A
prefix or
postfix
Execution-Time Representation
MUL C C C-A
A
Execution-Time Representation
MUL C C C-A
A
Evaluation
+ -
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
• 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
Ev
• 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: 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
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
MAIN A B
I0 I2 I4
R0 -- R1 I1
-- R0
local data
local data A
MAIN I2 CIP R1 CEP
Recursive Calls
I0 I2 I4
I4 CIP R2 CEP
Recursive Calls
I0 I2 I4
I2 CIP R3 CEP
Recursive Calls
I0 I2 I4
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
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
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
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
Reference y 6
8 b
foo(x,y) proc foo(var a, var b)
ai
Name
b x[i]
foo(i,x[i]) proc foo(name a, name b)
Example
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
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
IK
J 22
2
12
JL
IP -
K := K + 10 EP -
SCP -
L := L + 10
K 1
21
11
SUB1(K,L)
L
KM IP -
EP -
LN 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
A C 7
18
8
BD 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