Title: Programming Languages Implementation of Control Structures
1Programming Languages Implementation of Control
Structures
- Cao Hoaøng Truï
- Khoa Coâng Ngheä Thoâng Tin
- Ñaïi Hoïc Baùch Khoa TP. HCM
2Contents
- Sequence control
- Data control
-
-
3Sequence Control
- Expressions
- Statements
- Subprograms
-
-
4Expressions
- Control mechanism
- Syntax
- Execution-time representation
- Evaluation
-
-
5Control Mechanism
- Functional composition
- (A B)(C - A)
- ( (A, B), - (C, A))
-
-
6Syntax
- Infix
- A B C
- binary operations only
- computation order ambiguity
-
-
7Syntax
- Prefix
- ordinary
- ( (A, B), - (C, A))
- Cambridge Polish
- ( ( A B) (- C A))
- Polish
- A B - C A
-
-
8Syntax
- Prefix
- different numbers of operands
- ordinary/ Cambridge Polish cumbersome with
parentheses - Polish number of operands known in advance
-
-
9Syntax
- Postfix
- ordinary
- ((A, B) , (C, A) -)
- Polish
- A B C A -
- suitable execution-time representation
-
-
-
10Execution-Time Representation
- Interpretation
- tree structures
-
-
A
B
C
A
11Execution-Time Representation
- Compilation machine code sequences
- PUSH A
- PUSH B
- ADD
- PUSH C
- PUSH A
- SUB
- MUL
A
A
A B
B
A B
A B
A B
(AB)(C-A)
C
C
C - A
A
12Execution-Time Representation
- Compilation machine code sequences
- PUSH A
- PUSH B
- ADD
- PUSH C
- PUSH A
- SUB
- MUL
A
A
A B
B
A B C A -
A B
A B
A B
(AB)(C-A)
C
C
C - A
A
13Evaluation
- No simple uniform evaluation rule is
satisfactory -
-
A
B
C
A
14Evaluation
A
A
FOO(X)
15Evaluation
- Side effects
- A B C 1020 10-20 10-20
- (A B) C 1 10-20 10-20
- A (B C) 1020 0 0
-
16Evaluation
- Short-circuit Boolean expressions
- if (A 0) or (B/A gt C) then
-
17Evaluation
- Short-circuit Boolean expressions
- if (A 0) or else (B/A gt C) then
-
18Sequence Control
- Expressions
- Statements
- Subprograms
-
-
19Statements
- GOTO
- Sequencing
- Selection
- Iteration
-
-
20GOTO
JMP L
L
21Sequencing
S1 codes
S2 codes
Sn codes
22Selection
-
- if A 0 then S1
- else S2
- S3
-
-
-
JEQ0 A L1
S2 codes
JMP L2
S1 codes
L1
S3 codes
L2
23Selection
E ? v
- var E 0..2
- case E of
- 1 S1
- 2 S2
- else S0
- end
- S3
-
-
JMP av
a
JMP L0
a1
JUMP table
JMP L1
a2
JMP L2
L1
S1 codes
JMP L3
L2
S2 codes
JMP L3
L0
S0 codes
L3
S3 codes
24Iteration
- for I E1 to E2 do S
- I E1
- L0 if I gt E2 then GOTO L1
- S
- I I 1
- GOTO L0
- L1
-
-
-
25Iteration
- while C do S
- L0 if not(C) then GOTO L1
- S
- GOTO L0
- L1
-
-
-
26Iteration
- repeat S until C
- L0 S
- if not(C) then GOTO L0
-
-
-
27Sequence Control
- Expressions
- Statements
- Subprograms
-
-
28Subprograms
- Simple call-return
- Recursive calls
-
-
29Simple Call-Return
- No recursive calls
- Explicit calls
- Complete execution
- Immediate control transfer
- Single execution sequence
-
-
30Simple Call-Return
MAIN
A
B
I0
I2
I4
call A
call B
I1
I3
return
return
end
31Simple Call-Return
MAIN
I0
call A
I1
end
local data MAIN
CIP (current instruction pointer)
I0
32Simple Call-Return
MAIN
A
I0
I2
call A
call B
I1
I3
return
end
local data A
local data MAIN
I1
I2
CIP
33Simple Call-Return
MAIN
A
B
I0
I2
I4
call A
call B
I1
I3
return
return
end
local data B
local data A
local data MAIN
I3
I1
I4
CIP
34Recursive Calls
MAIN
A
B
I0
I2
I4
call A
call B
call A
I1
I3
I5
end
return
return
R0
--
--
local data MAIN
CEP (current environment pointer)
I0
CIP
R0
35Recursive 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 MAIN
local data A
I2
CIP
R1
CEP
36Recursive Calls
MAIN
A
B
I0
I2
I4
call A
call B
call A
I1
I3
I5
end
return
return
R0
R1
R2
--
I1
I3
--
R0
R1
local data MAIN
local data A
local data B
I4
CIP
R2
CEP
37Recursive Calls
MAIN
A
B
I0
I2
I4
call A
call B
call A
I1
I3
I5
end
return
return
R0
R1
R2
R3
--
I1
I3
I5
--
R0
R1
R2
local data MAIN
local data A
local data B
local data A
I2
CIP
R3
CEP
38Recursive Calls
MAIN
A
B
I0
I2
I4
call A
call B
call A
I1
I3
I5
end
return
return
R0
R1
R2
R3
--
I1
I3
I5
--
R0
R1
R2
local data MAIN
local data A
local data B
local data A
Dynamic chain
I2
CIP
R3
CEP
39Central Stack
MAIN
R0
I0
--
I0
CIP
--
MAIN
call A
R0
CEP
local data
I1
end
40Central Stack
MAIN
R0
I0
--
I2
CIP
--
MAIN
call A
R1
CEP
local data
I1
R1
I1
end
R0
A
A
local data
I2
call B
I3
return
41Central Stack
A
R0
I2
--
I4
CIP
--
MAIN
call B
R2
CEP
local data
I3
R1
I1
return
R0
A
B
local data
R2
I4
I3
R1
B
call A
local data
I5
return
42Central Stack
B
R0
--
I4
I2
CIP
--
MAIN
call A
local data
R3
CEP
I5
R1
I1
return
R0
A
local data
A
R2
I3
I2
R1
B
local data
call B
R2
I3
I5
return
R2
A
local data
43Exercises
- Illustrate the storage representation of
- A array 0..1, 1..2, 1..3 of integer using the
column-major order. - Give the accessing formula for computing the
location of AI, J, K, supposing that the size
of an integer is 2 bytes. -
-
44Exercises
- Given the following program
- program MAIN
- function FAC(N integer) integer
- begin if N lt 1
- then FAC 1
- else FAC N FAC(N - 1)
- end
- begin
- write(FAC(3))
- end.
-
- Illustrate the code segment and activation
records of MAIN and FAC in the central stack
during execution of the program. -
-
45Contents
- Sequence control
- Data control
-
-
46Data Control
- Basic concepts
- Local data and environments
- Shared data dynamic scope
- Shared data block structure
- Shared data parameter transmission
-
-
47Basic Concepts
- Names
- Referencing environments
- Scope
- Block structure
-
-
48Names
- Variable names
- Formal parameter names
- Subprogram names
- Names for defined types
- Names for defined constants
-
-
49Referencing Environments
- Association
- Name --------gt Data Object
- Referencing environment set of associations
-
-
50Referencing Environments
- program MAIN
- var X integer
-
- X 0
-
-
-
object
Association
51Referencing Environments
- Local
- Non-Local
- Global
- Predefined
-
-
-
52Local Environments
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- X 1
-
-
object1
object2
53Non-Local Environments
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- procedure SUB2
-
- X 2
-
-
object1
object2
54Global Environments
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- procedure SUB3
-
- X 3
-
-
object1
object2
55Predefined Environments
- program MAIN
- var X integer
-
- X MAXINT - 1
-
- write(X)
-
-
-
65535
codes for write
56Referencing Environments
- Visibility of an association
- Referencing operations
- Local, non-local, global references
-
-
-
57Visibility
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- X 1
-
-
Hidden
object1
Visible
object2
58Referencing Operations
- Name ? Environment ? Data Object
-
-
-
59Referencing Operations
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- X 1
-
-
object1
object2
X ? (X ? object2) ? object2
60Referencing Environments
- Visibility of an association
- Referencing operations
- Local, non-local, global references
-
-
-
61Local References
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- X 1
-
-
object1
object2
X ? (X ? object2) ? object2
62Non-Local References
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- procedure SUB3
-
- X 2
-
-
object1
object2
X ? (X ? object2) ? object2
63Global References
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- procedure SUB2
-
- X 2
-
-
object1
object2
X ? (X ? object1) ? object1
64Basic Concepts
- Names
- Referencing environments
- Scope
- Block structure
-
-
65Scope
- The program part (text or execution) within which
the binding is effective. -
-
-
66Dynamic Scope
- The subprogram activations within which the
association is effective. -
-
-
67Dynamic Scope
- program MAIN
- var X integer
-
- procedure SUB1
- var X real
-
- X 1
- procedure SUB2
-
- X 2
-
-
MAIN
SUB2
object1
SUB1
68Static Scope
- The program text within which the declaration is
effective. -
-
-
69Static Scope
- program MAIN
- var X integer X ? integer
-
- procedure SUB1
- var X real
-
- X 1
- procedure SUB2
-
- X 2
-
-
70Static Scope
- program MAIN
- var X integer X ? integer
-
- procedure SUB1
- var X real
-
- X 1
- procedure SUB2
-
- X 2
-
-
Static scopes define dynamic scopes
71Static Referencing Environments
- Local, non-local, global environments
- Local, non-local, global references
-
-
-
72Basic Concepts
- Names
- Referencing environments
- Scope
- Block structure
-
-
73Block Structure
- program MAIN
-
- procedure SUB1
-
- procedure SUB3
-
- procedure SUB4
-
- procedure SUB2
-
-
-
MAIN
SUB1
SUB3
SUB4
SUB2
74Static Scope Rules
- The declarations
- at the head of each
- block defines the
- local referencing
- environment for
- that block
-
-
MAIN
SUB1
SUB3
X real
SUB4
SUB2
75Static Scope Rules
- 2. If no local declarations
- exists, then refer to the
- nearest enclosing block
- having the declaration
- in need
-
-
MAIN
X integer
SUB1
X real
SUB3
X 1
SUB4
SUB2
76Static Scope Rules
- 3. Any local declaration
- of a block is hidden
- from its outer blocks
-
-
MAIN
SUB1
X real
SUB3
SUB4
SUB2
77Static Scope Rules
- 4. The block name is
- part of the local
- referencing environment
- of the containing block
-
-
MAIN
SUB1
SUB3
SUB4
SUB2
78Data Control
- Basic concepts
- Local data and environments
- Shared data dynamic scope
- Shared data block structure
- Shared data parameter transmission
-
-
79Local Data and Environments
- procedure SUB(X integer)
- var Y real
- Z array 1..3 of real
- procedure SUB1
-
- begin
-
- end
- begin
-
- end
-
-
SUB
object2
X
object2
Y
object2
Z
SUB1
SUB1 code segment
80Data Control
- Basic concepts
- Local data and environments
- Shared data dynamic scope
- Shared data block structure
- Shared data parameter transmission
-
-
81Shared Data Dynamic Scope
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
MAIN
SUB1
SUB2
SUB3
82Shared Data Dynamic Scope
MAIN
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
Return point
X
SUB1
Y
83Shared Data Dynamic Scope
MAIN
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
Return point
X
SUB1
Dynamic chain
Y
Return point
B
A
SUB2
U
X
84Shared Data Dynamic Scope
MAIN
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
Return point
X
SUB1
Y
Dynamic chain
Return point
B
A
SUB2
U
X
Return point
Z
Y
SUB3
A
W
V
85Shared Data Dynamic Scope
MAIN
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
Return point
X
SUB1
Y
Dynamic chain
Return point
B
A
SUB2
U
X
Return point
Z
Y
SUB3
A
W
V
86Shared Data Dynamic Scope
MAIN
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
Central table
Return point
X
SUB1
A
0
Y
B
0
U
0
V
0
W
0
X
1
Y
1
Z
0
87Shared Data Dynamic Scope
MAIN
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
Central table
Return point
X
SUB1
A
1
Y
B
1
Return point
U
1
B
V
0
A
SUB2
W
0
U
X
1
X
Y
1
Z
0
88Shared Data Dynamic Scope
MAIN
- program MAIN
- procedure SUB1
- var X, Y real
-
- procedure SUB2
- var B, A, U, X integer
-
- procedure SUB3
- var Z, Y, A, W, V char
-
-
-
-
Central table
Return point
X
SUB1
1
A
Y
1
B
Return point
1
U
B
1
V
A
SUB2
1
W
U
1
X
X
1
Y
Return point
1
Z
Z
Y
SUB3
A
W
V
89Shared Data Dynamic Scope
MAIN
Central table
Hidden stack
Return point
X
SUB1
0
A
Y
0
B
0
U
0
V
0
W
1
X
1
Y
0
Z
90Shared Data Dynamic Scope
MAIN
Central table
Hidden stack
Return point
SUB1
X
1
X
A
Y
1
1
B
Return point
1
1
U
B
0
1
V
A
SUB2
0
1
W
U
0
0
X
X
0
0
Y
0
0
Z
91Shared Data Dynamic Scope
MAIN
Central table
Hidden stack
Return point
SUB1
X
1
X
A
Y
1
1
B
Y
Return point
1
1
U
1
A
B
0
1
V
A
SUB2
0
1
W
U
0
0
X
X
0
0
Y
0
0
Z
Return point
Z
Y
SUB3
A
W
V
92Data Control
- Basic concepts
- Local data and environments
- Shared data dynamic scope
- Shared data block structure
- Shared data parameter transmission
-
-
93Shared Data Block Structure
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
MAIN X, Y, Z char
SUB2 X, Y integer
SUB3 X real
SUB4
SUB1 Y, Z integer
94Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
1
1
0
0
0
0
0
0
? MAIN
95Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
X
1
1
1
Y
0
0
0
0
0
? SUB2
96Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
X
1
1
1
Y
0
1
X
0
0
0
0
? SUB3
97Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
X
1
1
1
Y
0
1
X
0
0
0
0
? SUB4
98Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
X
1
1
1
Y
0
1
X
0
0
0
0
SUB4 ?
99Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
X
1
1
1
Y
0
1
X
0
0
0
0
SUB3 ?
100Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
X
1
0
1
X
0
0
0
0
SUB2 ?
101Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
X
Y
1
Z
0
1
X
0
0
0
0
? SUB1
102Block Structure Compile Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
Symbol table
Scope stack
X
1
Y
1
1
1
Z
1
1
0
1
X
0
0
0
0
SUB1 ?
103Block Structure Run Time
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
MAIN
SUB1
SUB2
SUB3
104Block Structure Run Time
--
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
X
MAIN
Y
Z
105Block Structure Run Time
--
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
X
MAIN
Static chains
Y
Z
RP
SCP
SUB1
Y
Z
106Block Structure Run Time
--
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
X
MAIN
Y
Z
Static chains
RP
SCP
SUB1
Y
Z
RP
SCP
SUB2
X
Y
107Block Structure Run Time
--
- program MAIN
- var X, Y, Z char
-
- procedure SUB2
- var X, Y integer
-
- procedure SUB3
- var X real
-
- procedure SUB4
-
- procedure SUB1
- var Y, Z integer
-
X
MAIN
Y
Z
Static chains
RP
SCP
SUB1
Y
Z
RP
SCP
SUB2
X
Y
RP
SUB3
SCP
X
108Block Structure Run Time
C
C
A
A
A
B
B
B
MAIN
MAIN
MAIN
A
C
C
A
A
B
B
B
109Block Structure Run Time
--
X
MAIN
Display
Y
Z
0
RP
1
1
SCP
2
SUB1
Active static chain
Y
1
Z
1
RP
SCP
SUB2
X
0
Y
RP
loc Y Display1 offset
SUB3
SCP
X
110Data Control
- Basic concepts
- Local data and environments
- Shared data dynamic scope
- Shared data block structure
- Shared data parameter transmission
-
-
111Parameter Transmission
- Parameters
- formal parameters
- actual parameters
- Transmission
- by reference
- by value
-
-
112Parameter Transmission
- procedure SUB2(K integer var L integer)
- begin
- K K 10
- L L 10
- write(K, L)
- end
- procedure SUB1
- begin
- I 1
- J 2
- SUB2(I, J)
- write(I, J)
- end
-
RP
SCP
SUB1
I
1
J
2
RP
SCP
SUB2
K
1
L
113Parameter Transmission
- procedure SUB2(K integer var L integer)
- begin
- K K 10
- L L 10
- write(K, L)
- end
- procedure SUB1
- begin
- I 1
- J 2
- SUB2(I, J)
- write(I, J)
- end
-
-
RP
SCP
SUB1
I
1
J
2
RP
SCP
SUB2
K
11
L
114Parameter Transmission
- procedure SUB2(K integer var L integer)
- begin
- K K 10
- L L 10
- write(K, L)
- end
- procedure SUB1
- begin
- I 1
- J 2
- SUB2(I, J)
- write(I, J)
- end
-
-
RP
SCP
SUB1
I
1
J
12
RP
SCP
SUB2
K
11
L
115Parameter Transmission
- procedure SUB2(Kinteger var Linteger)
- begin
- K K 10
- L L 10
- SUB3(K, L)
- write(K, L)
- end
- procedure SUB1
- begin
- I 1
- J 2
- SUB2(I, J)
- write(I, J)
- end
procedure SUB3(var M, N integer) begin M M
10 N N 10 write(M, N) end
Actual parameters are formal parameters of the
calling program
116Parameter Transmission
- type VECT array 1...3 of integer
- procedurre SUB2 (CVECT var DVECT)
- var I integer
- begin
- C 2 C 2 10
- D 2 D 2 10
- for I 1 to 3 do write (C I)
- for I 1 to 3 do write (D I)
- end
procedurre SUB1 var A, B VECT J
integer begin A 1 7 A 2 8 A 3
9 B 1 7 B 2 8 B 3 9 SUB2
(A, B) for J 1 to 3 do write (A J) for
J 1 to 3 do write (B J) end
Actual parameters are structured data objects
117Parameter Transmission
- type VECT array 1...3 of integer
- procedurre SUB2 (I integer var J integer)
- begin
- I I 10
- J J 10
- write (I, J)
- end
procedurre SUB1 var A VECT K
integer begin A 1 7 A 2 8 A 3
9 SUB2 (A1, A2) for K 1 to 3 do write
(AK) end
Actual parameters are components of structured
data objects
118Parameter Transmission
- type VECT array 1...3 of integer
- procedurre SUB2 (var I, J integer)
- begin
- I I 1
- J J 1
- write (I, J)
- end
procedurre SUB1 var A VECT K
integer begin A 1 7 A 2 8 A 3
9 K 2 SUB2 (K, AK) for K 1 to 3 do
write (AK) end
Actual parameters are array components with
computed subscripts
119Parameter Transmission
- type VECT array 1...3 of integer
- VECTPTR VECT
- procedurre SUB2 (RVECTPTR var SVECTPTR)
- begin
- R1 R1 10
- S1 S1 10
- if . . . then R S else S R
- end
procedurre SUB1 var A, B VECT P, Q
VECTPTR begin A 1 7 A 2 8 A 3
9 B 1 7 B 2 8 B 3 9 P _at_A
Q _at_B SUB2 (P, Q) end
Actual parameters are pointers
120Parameter Transmission
- program MAIN
- var X real
- procedurre SUB2 (X, Y real
- function
F(Ureal) real) - var Z real
- begin
- Z abs (Y - X)
- Z (F(X) F(Y)) Z/2
- write (Z)
- end
- begin
- X 3
- SUB1
- end.
procedurre SUB1 var Y real function
FUNC(Vreal) real begin FUNC XV
Y end begin Y 1 SUB2 (0, 1,
FUNC). end
Actual parameters are subprograms
121Parameter Transmission
- type VECT array 1...3 of integer
- procedurre SUB2 (name I, J integer)
- begin
- I I 1
- J J 1
- write (I, J)
- end
procedurre SUB1 var A VECT K
integer begin A 1 7 A 2 8 A 3
9 K 2 SUB2 (K, AK) for K 1 to 3 do
write (AK) end
Transmissiom by names
122Formal Parameters and Aliases
- A data object may have more than one name, called
aliases. - Side effects
- I 1 J 1
- I J 10
- J J10
123Formal Parameters and Aliases
- An actual parameter is a non-local variable and
is trasmitted by reference. - Actual parameters are of the same data object and
transmitted by reference.
124Exercises
- Given the following program
- program MAIN
- var F real
- procedure PROC(N integer var F real)
- var F1, F2 real
- begin if (N 0) or (N 1) then F 1
- else begin PROC(N-1, F1)
- PROC(N-2, F2)
- F F1 F2
- end
- end
- begin
- PROC(2, F)
- write(F)
- end.
-
-
125Exercises
- Illustrate the code segment and activation
records of MAIN and PROC. - Illustrate the central stack during execution of
this program. -
-
-
-