Sei sulla pagina 1di 175

Lecture Notes in

Computer Science
Edited by G. Goos and J. Hartmanis

18

Kathleen Jensen
Niklaus Wirth

PASCAL
User Manual and Report
Editorial Board: P. Brinch Hansen - D. Gries
C. Moler • G. SeegmQIler • N. Wirth

Ms.,Kathleen Jensen
Prof. Dr. Niklaus Wirth
Institut fLir Informatik
ETH Zerich
Clausiusstra6e 55
C H - 8 0 0 6 Z0rich

Library of Congress Cataloging in Publication Data

Jensen, Kathleen, 1949-


PASCAL: user manual and report.

(Lecture notes in computer science, v. 18)


Bibliography: p.
1. PASCAL (Computer program language).
I. Wirth, Niklaus, joint author. II. Title.
IIIo Series.
QA76.73 .P35J46 001.6'424 74-16327

AMS Subject Classifications (1970): 68-02, 68 A 0 5


CR Subject Classifications (1974): 4.2, 4.22

ISBN 3-540-06950-X Springer-Verlag Berlin • Heidelberg • New York


ISBN 0-387-06950-X Springer-Verlag New York • Heidelberg • Berlin

This work is subject to copyright. All rights are reserved, whether the whole
or part of the material is concerned, specifically those of translation,
reprinting, re-use of illustrations, broadcasting, reproduction by photo-
copying machine or similar means, and storage in data banks.
Under § 54 of the German Copyright Law where copies are made for other
than private use, a fee is payable to the publisher, the amount of the fee to
be determined by agreement with the publisher.
© by Springer-Verlag Berlin • Heidelberg 1974. Printed in Germany.
PREFACE

This manual is directed to those who h a v e p r e v i o u s l y acquired


some programming s k i l l . The i n t e n t i o n is to p r o v i d e a m e a n s of
learning Pascal without outside guidance. It is based on The
~rparammino Lanauaae Eascal (~evised ~eaort) Ill--the basic
definition of Pascal and concise reference manual for t h e
experienced Pascal programmer.

The linear structure of a book is by no means ideal for


introducing a language, whether it be a formal or natural one.
Nevertheless, it is recommended to follow the given
organization, paying particular attention to the example
programs, and then t o reread those sections causing dlfficultes.
O n e m a y w i s h , h o w e v e r , t o r e f e r e n c e c h a p t e r 12 if t r o u b l e s a r i s e
concerning the input and output conventions of the programs.

The manual was prepared as e file on a c o m p u t e r , that is . as a


sequence of characters of a single type font. This is very
convenient for the p u r p o s e s of u p d a t i n g : u n f o r t u n a t e l Y ~ it is
sometimes a bit awkward to read. T h e r e a d e r is a s k e d to be
indulgent with the absence of s u b - and s u p e r s c r i p t s (e.g. m
r a i s e d t o t h e p o w e r n is d e n o t e d by m * * n ) .

Chapters 0--12 define the language Pascal and serve as a


standard for both the implementor and the programmer. The
implementer must regard the task of recognizing Standard Pascal
as the minmum requirement of his system, while the programmer
who intends his programs to be transferable from one
installation to another s h o u l d u s e o n l y f e a t u r e s d e s c r i b e d as
Standard P a s c a l . On the o t h e r h a n d , any i m p l e m e n t a t i o n may (and
u s u a l l y d o e s ) go b e y o n d t h e m i n i m u m . C h a p t e r s 13 a n d 14 d o c u m e n t
t h e i m p l e m e n t a t i o n of P a s c a l on t h e CDC 6 0 0 0 m a c h i n e . C h a p t e r 13
describes the a d d i t i o n a l f e a t u r e s of t h e l a n g u a g e P A S C A L 6 0 0 0 ,
w h e r e a s c h a p t e r 14 is d e v o t e d to t h e u s e of t h e c o m p i l e r and t h e
system under the operating system SCOPE.

The efforts o f m a n y go i n t o this manual, e n d we e s p e c i a l l y thank


the members of the Institut fuer Informatik, ETH Z u r i c h . end
John Larmouth, Rudy Schild, Olivier Lecerme, and Pierre
Desjardins for their criticism, suggestions, and encouragement.
Our implementation of Pascsl--which made this manual both
possible and necessary--is the work of Urs A m m a n n , a i d e d by
Helmut Sandmayr.

Kathleen densen
Niklaus Wirth
ETH Zurich
June 1974 Switzerland
Table of Contents

U S E R M A N U A L by K. Jensen and N. Wirth

O. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3
I. N o t a t i o n and Vocabulary . . . . . . . . . . . . . . . . . . . 9
2. T h e Concept of D a t a . . . . . . . . . . . . . . . . . . . . . 12
A. The type Boolean . . . . . . . . . . . . . . . . . . . . . 12
B. The type integer . . . . . . . . . . . . . . . . ..... 13
C. The type real . . . . . . . . . . . . . . . . . . . . . . 14
D. T h e type char . . . . . . . . . . . . . . . . . . . . . . 14
3. T h e Program Heading and the Declaration Part . . . . . . . . 16
A. The program heading . . . . . . . . . . . . . . . . . . . 16
B. The label declaration part . . . . . . . . . . . . . . . . 16
C. The constant definition part . . . . . . . . . . . . . . . 16
D. The type definition part . . . . . . . . . . . . . . . . . 18
E. The variable declaration part . . . . . . . . . . . . . . 18
F. The procedure and function declaration part . . . . . . . 19
4. T h e C o n c e p t of A c t i o n . . . . . . . . . . . . . . . . . . . . . 2O
A. T h e a s s i g n m e n t s t a t e m e n t . . . . . . . . . . . . . . . . . 20
B. T h e c o m p o u n d s t a t e m e n t . . . . . . . . . . . . . . . . . . . 21
C. R e p e t i t i v e statements . . . . . . . . . . . . . . . . . . 22
C.I T h e w h i l e s t a t e m e n t . . . . . . . . . . . . . . . . . 22
C.2 The repeat statement . . . . . . . . . . . . . . . . . 23
C.3 The for statement . . . . . . . . . . . . . . . . . . 23
D. C o n d i t i o n a l statements . . . . . . . . . . . . . . . . . . 26
D.I T h e if s t a t e m e n t . . . . . . . . . . . . . . . . . . . 26
D.2 The case statement . . . . . . . . . . . . . . . . . . 31
E. T h e g o t o s t a t e m e n t . . . . . . . . . . . . . . . . . . . . 31
5. S c a l a r a n d S u b r a n g e T y p e s . . . . . . . . . . . . . . . . . . 34
A. S c a l a r t y p e s . . . . . . . . . . . . . . . . . . . . . . . 34
B. S u b r a n g e t y p e s . . . . . . . . . . . . . . . . . . . . . . 35
6. Structured Types in G e n e r a l -- T h e Array in Particular . 36
7. R e c o r d Types . . . . . . . . . . . . . . . . . . . . . . . . 42
A. T h e with statement . . . . . . . . . . . . . . . . . . . . 47
8. Set Types . . . . . . . . . . . . . . . . . . . . . . . . . . 5O
9. ~ i l e T y p e s . . . . . . . . . . . . . . . . . . . . . . . . . 55
A. T e x t f i l e s . . . . . . . . . . . . . . . . . . . . . . . . 57
B. T h e s t a n d a r d f i l e s " i n p u t " a n d " o u t p u t " . . . . . . . . . 59
10. Pointer Types . . . . . . . . . . . . . . . . . . . . . . . . 62
11. Procedures and Functions . . . . . . . . . . . . . . . . . . 67
A. P r o c e d u r e s . . . . . . . . . . . . . . . . . . . . . . . . 67
B. F u n c t i o n s . . . . . . . . . . . . . . . . . . . . . . . . 78
C. R e m a r k s . . . . . . . . . . . . . . . . . . . . . . . . . 82
12. Input and Output . . . . . . . . . . . . . . . . . . . . . . 84
A. T h e p r o c e d u r e read . . . . . . . . . . . . . . . . . . . . 84
B. T h e p r o c e d u r e write . . . . . . . . . . . . . . . . . . . 86
13. PASCAL 6000-3.4 . . . . . . . . . . . . . . . . . . . . . . . 88
A. E x t e n s i o n s to t h e l a n g u a g e P a s c a l . . . . . . . . . . . . 88
A.I S e g m e n t e d f i l e s . . . . . . . . . . . . . . . . . . . 88
VI

A.2 External procedures . . . . . . . . . . . . . . . . . 9O


B. Specifications left undefined in the preceding chapters . 91
B.I T h e p r o g r a m h e a d i n g a n d e x t e r n a l f i l e s . . . . . . . . 91
B.2 Representation of files . . . . . . . . . . . . . . . 92
B.3 The standard types . . . . . . . . . . . . . . . . . . 93
B.4 The standard procedure write . . . . . . . . . . . . . 96
C. R e s t r i c t i o n s . . . . . . . . . . . . . . . . . . . . . . . 97
D. A d d i t i o n a l p r e d e f i n e d t y p e s , p r o c e d u r e s , a n d f u n c t i o n s . 97
D.I A d d i t i o n a l p r e d e f i n e d t y p e s . . . . . . . . . . . . . 97
D.2 Additional predefined procedures and functions.. 98
14. How to Use the PASCAL 6000-3.4 System . . . . . . . . . . . . I00
A. C o n t r o l s t a t e m e n t s . . . . . . . . . . . . . . . . . . . . 100
B. C o m p i l e r o p t i o n s . . . . . . . . . . . . . . . . . . . . . 100
C. E r r o r m e s s a g e s . . . . . . . . . . . . . . . . . . . . . . I O2
C.I C o m p i l e r . . . . . . . . . . . . . . . . . . . . . . . 102
C.2 Run-time . . . . . . . . . . . . . . . . . . . . . . . I02

References . . . . . . . . . . . . . . . . . . . . . . . . . I04

Appendix A Standard Procedures and Functions . . . . . . . I05


Appendix B S u m m a r y of O p e r a t o r s . . . . . . . . . . . . . . I O8
Appendix C Tables . . . . . . . . . . . . . . . . . . . . . I09
Appendix D Syntax . . . . . . . . . . . . . . . . . . . . . 110
Appendix E Error Number Summary . . . . . . . . . . . . . . 119
Appendix F Programming Examples . . . . . . . . . . . . . . 122

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

by N. Wirth

Preface to the Revised Report . . . . . . . . . . . . . . . . . 133


1. I n t r o d u c t i o n . . . . . . . . . . . . . . . . . . . . . . . . 136
2. Summary of the language . . . . . . . . . . . . . . . . . . . 137
3. N o t a t i o n , terminology, and vocabulary . . . . . . . . . . . . 140
4. Identifiers, Numbers and Strings . . . . . . . . . . . . . . 140
5. Constant definitions . . . . . . . . . . . . . . . . . . . . 141
6. D a t a type definitions . . . . . . . . . . . . . . . . . . . . 142
6.1. Simple types . . . . . . . . . . . . . . . . . . . . . . 142
6.2. Structured types . . . . . . . . . . . . . . . . . . . . 143
6.3. Pointer types . . . . . . . . . . . . . . . . . . . . . 145
7. D e c l a r a t i o n s a n d d e n o t a t i o n s of v a r i a b l e s . . . . . . . . . . 146
7.1. E n t i r e v a r i a b l e s . . . . . . . . . . . . . . . . . . . . 147
7.2. C o m p o n e n t v a r i a b l e s . . . . . . . . . . . . . . . . . 147
7.3. R e f e r e n c e d v a r i a b l e s . . . . . . . . . . . . . . . . . . 148
8. Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.1. O p e r a t o r s . . . . . . . . . . . . . . . . . . . . . . . 149
8.2. F u n c t i o n d e s i g n a t o r s . . . . . . . . . . . . . . . . . . 151
9. Statements . . . . . . . . . . . . . . . . . . . . . . . . . 151
9.1. S i m p l e s t a t e m e n t s . . . . . . . . . . . . . . . . . . . 152
9.2. S t r u c t u r e d s t a t e m e n t s . . . . . . . . . . . . . . . . . 153
10. Procedure declarations . . . . . . . . . . . . . . . . . . . . 158
10.1.Standard procedures . . . . . . . . . . . . . . . . . . 160
11. Function declarations . . . . . . . . . . . . . . . . . . . . 162
11.1.Standard functions . . . . . . . . . . . . . . . . . . . 163
VII

12. Input and Output . . . . . . . . . . . . . . . . . . . . . 164


13. P r o g r a m s . . . . . . . . . . . . . . . . . . . . . . . . . 167
14. A s t a n d a r d for i m p l e m e n t a t i o n and p r o g r a m interchange. . 168
15. I n d e x . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
0
~NTRODUCTION

Much of the following text assumes the reader has a minimal


grasp of computer terminology and a "feeling" for the structure
of e program. The purpose of this section is to spark that
intuition.

{ program 0.1
assuming annual inflation rates o f 9 , 8 , a n d 10 p e r c e n t .
find the factor by which the frank, dollar, pound
sterling, m a r k , or g u i l d e r will have been devalued in
1. 2 . . . . n years.}

~rocram inflation(output);

#onst n = 10 ;
var i : integer; wl.w2~oW3 : real;
becin i := O; w l := 1.0; w 2 := 1.0; w3 := 1.0;
r_@~.~at i := i + I ;
w I := w I * 1.07;
w2 := w2 * 1.08;
w3 := W3 * 1 . 1 0 ;
w r i t e l n (i .w l , w 2 . w 3 )
i=n
end.

1 1.090000000000e+00 1.080000000000e+00 1.100000000000e+00


2 1.144900000000e+00 1.166400000000e+00 1.210000000000e+00
3 1.225043000000e+00 1.2597t2000000e+00 1.331000000000e+00
4 1.310796010000e+00 1.360488960000e+00 1.464100000000e+00
5 1.402551730700e+00 1.469328076UOOe+00 1.610510000000e+00
6 1.500730351849e+00 1.586874322944e+00 1.771561000000e+00
7 1.bObTU1476478e+O0 1.713824268779e+00 1.948717100000e+00
1.718186179832e+00 1.850930210282e+00 2.14358~810000e+00
9 1.838459212420e+00 1.999004627104e+00 2.357947691000e+00
10 1.96915t359290e+00 2.158924997293e+00 2.593742460t00e+00

An &3ooritbj~ or computer program consists of t w o e s s e n t i a l


parts, a description of ~ctions which ere to be performed, and a
description o f t h e ~@_~&o w h i c h is m a n i p u l a t e d by t h e s e a c t i o n s .
Actions are described by so-called ~tatements, end data is
described by so-called ~cl~ra~L~zns end ~L~finitions.

The program is divided into a ~eadin~ and a body, called a


b3ock. The heading gives the program s name a n d l i s t s its
parameters. (These ere (file) variables and represent the
arguments and results of the computation. See chapter 13.) The
file "output" is e compulsory parameter. The b l o c k consists of
six sections, where any except the lest may be e m p t y . I n the
required order they ere:
<label declaration part>
<constant definition part>
<type d e f i n i t i o n p a r t >
<variable declaration part>
<procedure and f u n c t i o n d e c l a r a t i o n p a r t >
<statement p a r t >

The first section l i s t s a l l l a b e l s d e f i n e d in t h i s b l o c k . The


second section defines synonyms for constants: i.e. it
introduces i d e n t i f i e r s t h a t m a y l a t e r be u s e d in p l a c e of t h o s e
constants. The third contains type definitions: end the fourth.
variable definitions. The fifth section defines subordinate
program parts (i.e. procedures and functions). The s t a t e m e n t
part specifies t h e a c t i o n s to be t a k e n .

The above program outline is more precisely e x p r e s s e d in a


syntax ~ a p r a m . Starting a t t h e diagram named p r o g r a m , a p a t h
through the diagram defines a syntactically correct proqram.
Each box r e f e r e n c e s a d i a g r a m by t h a t n a m e . w h i c h is t h e n u s e d
to define its meaning. Terminal symbols (those actually written
in a P a s c a l p r o g r a m ) a r e in r o u n d e d e n c l o s u r e s . (See a p p e n d i x D
for t h e f u l l s y n t a x d i a g r a m of P a s c a l . )
program

block

~1 .... i~.di,l,~o~ I-

j ~ I ~~G-o~

~-~PROCEDUII~~--I p..... ter list I "i

Figure O.a Syntax diagrams defining the


general structure of a program
An alternative formulation of a syntax is the traditional
Backus-Naur ~orm. where syntactic constructs a r e d e n o t e d by
English words enclosed between the angular brackets < a n d >.
These words are suggestive of the n a t u r e or m e a n i n g of t h e
construct. A sequence of constructs (I or more elements)
enclosed by the mete-brackets { and } imply their repetition
zero or m o r e t i m e s . (For t h e B N F of P a s c a l . see appendix D . ) As
an example, the construct <program> of f i g u r e O.a is d e f i n e d by
the following formulas, called "productions":

<program> ::= < p r o g r a m heading> <block> .


<program heading> ::~ ~ r o m r a ~ <identifier> ( <file identifier>
{ . <file identifier>} ) ;
<file identifier> ::= < i d e n t i f i e r >

Each procedure (function) has a structure similar to a program;


i.e. each consists of a heading and a block. Hence. procedures
may be declared (nested) within other procedures. Labels,
constant synonyms, type, variable, and procedure declarations
are local to the procedure in w h i c h t h e y a r e d e c l a r e d , That i s ,
their identifiers have significance only within the program text
which constitutes the procedure declaration a n d w h i c h is c a l l e d
the ~eooe of these identifiers. Since procedures m a y be n e s t e d ,
so may scopes. Objects which are declared in t h e m a i n p r o g r a m ,
i.e. not local to some procedrue, are called ~Iobal and have
significance throughout the entire program.

Since blocks m a y be n e s t e d w i t h i n o t h e r b l o c k s by p r o c e d u r e and


function declarations, o n e is a b l e to a s s i g n a l e v e l of n e s t i n g
to each. If the outermost program-defined block (e.g. the main
program) is called level O, t h e n a b l o c k d e f i n e d within this
block w o u l d be of l e v e l I; in g e n e r a l , a block defined in l e v e l
i would be of level (i+I). Figure O.b i l l u s t r a t e s a block
structure,

where level 0 : M
level I = P, Q
level 2 : A, R, S
level 3 : B

QF--q

I
Figure O.b Block structure
In t e r m s of t h i s f o r m u l a t i o n , t h e s c o p e or r a n g e of v a l i d i t y of
an identifier x is the entire b l o c k in w h i c h x is d e f i n e d ,
i n c l u d i n g t h o s e b l o c k s d e f i n e d in t h e s a m e b l o c k as x. (For t h i s
example, note that all identifiers m u s t be d i s t i n c t . S e c t i o n 3.e
discusses the case where identifiers are not necessarily
distinct,)

objects defined in b l o c k are accessible in blocks

M M ,P ,A .B ,Q .R ,S
P P ,A ,B
A A.B
B B
Q Q ,R ,S
R R
S S

For programmers acquainted with ALGOL, PL/I, or FORTRAN, it may


prove helpful to glance at Pascal in t e r m s of t h e s e o t h e r
languages. For this purpose, we list the following
characteristics of P a s c a l :

1. Declaration of variables is mandatory,


2. Certain key words (e.g. ~eain, ~nd, ~eoeat) ere
"reserved" and cannot be used as identifiers. In this
manual they are underlined.
3. The semicolon (;) is c o n s i d e r e d as a s t a t e m e n t s e p a r a t o r ,
not a s t a t e m e n t t e r m i n a t o r (as e . g . in PL /I).
4. The standard data types are those of whole and real
numbers, the logical values, and the (printable)
characters. The b a s i c d a t a s t r u c t u r i n g facilities include
the array, the record (corresponding to COBOL's and
PL / I ' s "structure"). the set, and the (sequential) file.
These structures can be combined end nested to form
arrays of sets, files of records, etc. D a t a may b e
allocated dynamically and accessed via pointers. These
pointers allow the full generality of l l s t p r o c e s s i n g .
T h e m e is a f a c i l i t y to d e c l a r e n e w , b a s i c d a t a t y p e s w i t h
symbolic constants.
5. The let data s t r u c t u r e offers facilities s i m i l a r to t h e
P L / I "bit s t r i n g " .
6. Arrays may be of arbitrary dimension with arbitrary
bounds; the array bounds are constant. (i.e. There are no
dynamic arrays.)
7. As in FORTRAN. ALGOL, and PL/I, there is a go to
statement. Labels are unsigned integers and m u s t be
declared.
8. The compound statement is that of ALGOL, and corresponds
t o t h e DO g r o u p i n P L / I .
9. The facilities o f t h e ALGOL s w i t c h and the computed go to
o f FORTRAN a r e r e p r e s e n t e d by the case statement.
10. The for statement, corresponding to the DO loop of
F O R T R A N . may o n l y have steps of 1 (~g) or -1 (~ownto) and
is executed only as long as t h e v a l u e o f t h e c o n t r o l
variable lles within the limits. Consequently. the
controlled s t a t e m e n t m a y not be e x e c u t e d at a l l .
11. There are no conditional expressions a n d no m u l t i p l e
assignments.
12. Procedures e n d f u n c t i o n s m a y be c a l l e d r e c u r s i v e l y .
13. T h e r e is no " o w n " a t t r i b u t e for v a r i a b l e s (as in A L G O L ) .
t4. Parameters are called either by value or by reference:
there i s no c a l l by name.
15. The "block structure" differs from that o f ALGOL a n d P L / I
insofar as there are no anonymous blocks, i.e. each block
is given a name, and thereby i s made i n t o a procedure.
16. All objects--constants, variables, etc.--must be declared
~e£o~_~ they are referenced. The following two exceptions
ere however allowed:
I) the type identifier in a pointer type definition
(chaptem 10)
2) procedure and function calls when there is a forward
reference (chapter 11).

Upon f i r s t c o n t a c t w i t h P a s c a l , m a n y t e n d to b e m o a n the a b s e n c e
of certain "favorite features", Examples include an
exponentiation operator, concatenation of strings, dynamic
arrays, arithmetic operations on B o o l e a n v a l u e s , a u t o m a t i c t y p e
conversions, and default declarations. These were not
oversights, but deliberate omissions. In some cases their
presence would be primarily an invitation to inefficient
programming solutions; in o t h e r s , it w a s felt t h a t t h e y w o u l d be
Contrary to the aim of clarity and reliability a n d "good
programming style". Finally, a rigorous selection among the
immense variety of programming facilities available had to be
made in order to keep the compiler relatively compact and
efficient--efficient and economical for both the user who writes
o n l y s m a l l p r o g r a m s u s i n g f e w c o n s t r u c t s of t h e l a n g u a g e a n d t h e
u s e r w h o w r i t e s l a r g e p r o g r a m s a n d t e n d s to m a k e u s e of the full
language.
I
__NTAT~.~.~.!~T.J~A N D VOCABULARY

The basic wocabularv consists of b a s i c symbols classified into


letters, digits, and special symbols. The ~ . ~ ~ymbols are
operators and delimiters:

+ : ( and and ail


- " ) Drrav _f..il~ aot ~hen

/ <> ] case function ~

<: } ~iv iZ £rocedure ~ar


• >: % ~a in ~r_Qar.am ahila
, > .. downto /~l ~ ~ith
91se mod Keoea~

~9~d-delim~ (or reserved words) ere normally underlined in


the hand-wrltten program to e m p h a s i z e t h e i r i n t e r p r e t a t i o n as
single symbols with fixed meaning. The orogrammer m a y not u s e
these words in a context other than that e x p l i c i t in t h e
d e f i n i t i o n of P a s c a l : in p a r t i c u l a r , t h e s e w o r d s m a y not be u s e d
as identifiers. They are written as a s e q u e n c e of l e t t e r s
(without surrounding escape characters).

The construct:

{<any sequence of s y m b o l s not containing "} ">}

m a y be i n s e r t e d b e t w e e n a n y t w o i d e n t i f i e r s , n u m b e r s , or s p e c i a l
symbols. It is called a ~ o m m e n £ a n d m a y be r e m o v e d from t h e
program text without altering its m e a n i n g . T h e s y m b o l s { a n d }
do not occur o t h e r w i s e in t h e l a n g u a g e , a n d w h e n a p p e a r i n g in
syntactic descriptions, they denote meta-symbols l i k e I and ::=.
(On systems where the curly brackets are unavailable, the
c h a r a c t e r p a i r s (* and *) a r e u s e d in t h e i r p l a c e . )

~de~if~ers are names denoting constants, types, variables.


Procedures. and functions. They must begin with a letter, which
may be followed by any combination and number of letters and
d i g i t s . A l t h o u g h an i d e n t i f i e r m a y be v e r y l o n g . i m p l e m e n t a t i o n s
may impose a limit as to how m a n y of t h e s e c h a r a c t e r s a r e
significant. Implementations of Standard Pascal will always
recognise the first ~ characters of an identifier as
significant. That is. identifiers denoting distinct objects
s h o u l d d i f f e r in t h e i r f i r s t U c h a r a c t e r s .
10

~ l e t t e r ~

Figure 1.a Identifier

examples of legal identifiers:


sum root3 pi h4g x
thisisaverylongbutneverthelesslegalidentifier
thisissverylongbutprobablythesameidentifierasabove

illegal identifiers:
3rd array level.4 root-3

Certain i d e n t i f i e r s , called ~ e n d a r d i d e n t i f i e r s , ere predefined


(e.g. sin, cos). In contrast to the w o r d - d e l i m l t e r s (e.g.
QrraM), one is not r e s t r i c t e d to this d e f i n i t i o n and may elect
to redefine any standard identifier, as they are assumed to be
declared in a hypothetical block s u r r o u n d i n g the entire program
block.

Decimal n o t a t i o n is used for ~ u m b e r s . The letter E preceding the


scale factor is pronounced as "times 10 to the power of". The
syntax of u n s i g n e d numbers is s u m m a r i z e d in figure 1.b.

Figure 1.b Unsigned number

Note that if the number c o n t a i n s a decimal point, at least one


digit must precede and succeed the point. Also. no comma may
occur in a number.

unsigned numbers :
3 03 6272844 0.6 5E-8 49.22E +08 IE 10
incorrectly written numbers:
3,487.159 XII .6 El0 5.E-16

Blanks. ends of lines, and comments ere considered as


semara~rs. An a r b i t r a r y n u m b e r of s e p a r a t o r s may occur between
any two consecutive Pascal symbols with the following exception:
no separators may o c c u r w i t h i n i d e n t i f i e r s , n u m b e r s , or s p e c i a l
symbols. H o w e v e r , at l e a s t o n e s e p a r a t o r m u s t o c c u r b e t w e e n a n y
p a i r of c o n s e c u t i v e identifiers, n u m b e r s , or w o r d s y m b o l s .

Sequences of characters enclosed by single quote marks are


celled ~nos. To i n c l u d e a q u o t e mark in s s t r i n g , o n e w r i t e s
the quote mark twice.

examples of strings:
"a' ";" "3 ° 'begln" "don'°t "
* t h i s s t r i n g has 33 c h a r a c t e r s "
2
--THE _C~.~,CF,,.P_-TBE_ D A T A

Data is the general expression d e s c r i b i n g a l l t h a t is o p e r a t e d


on by t h e c o m p u t e r . At t h e h a r d w a r e a n d m a c h i n e c o d e l e v e l s , a l l
data are represented as sequences of binary digits (bits).
Higher level languages allow one to u s e a b s t r a c t i o n s a n d to
ignore t h e d e t a i l s of r e p r e s e n t a t i o n - - l a r g e l y by d e v e l o p i n g t h e
C o n c e p t of d a t a ~ v o e .

A data type defines t h e s e t of v a l u e s a v a r i a b l e m a y a s s u m e .


Every variable occurring in a p r o g r a m m u s t be a s s o c i a t e d w i t h
One and only one type. A l t h o u g h data t y p e s in P a s c a l can be
quite sophisticated, each must be ultimately built from
unstructured types. An unstructured t y p e is e i t h e r d e f i n e d by
the programmer, a n d t h e n c a l l e d a d e c l a r e d s c a l a r t y p e . or o n e
of the four standard scalar types--integer, r e a l , B o o l e a n . or
char.

A scalar type is characterized by the s e t of its d i s t i n c t


values, u p o n w h i c h a l i n e a r o r d e r i n g is d e f i n e d . The v a l u e s a r e
denoted by identifiers in the definition of t h e t y p e (see
chapter 5).

A. T h e type Boolean

A Boolean value is one of t h e logical truth values denoted by


the predefined identifiers false and true.

The following logical operators yield e Boolean value when


applied to Boolean operands: (Appendix B summarizes all
Operators.)

and logical conjunction


or logical disjunction
no__t logical negetion

Each of the relational operators (=, <>. <=, <, >, >=, ~)
yields a Boolean value. Furthermore. the type Boolean is defined
such that false < true. Hence, it is possible to define each of
the 16 B o o l e a n operations using the above logical and relational
operators. For example, if p and q are Boolean values, one can
express

implication as p <= q
equivalence as p = q
e x c l u s i v e OR as p <> q
13

Standard Boolean functions--i.e, standard functions which yield


a Boolean result--ere: (Appendix A summarizes all s t a n d a r d
functions.)

odd(x) t r u e if t h e i n t e g e r x is odd, f a l s e o t h e r w i s e
e o l n (f) e n d of a l i n e , e x p l a i n e d in c h a p t e r 9
eof(f) e n d of f i l e . e x p l a i n e d in c h a p t e r 9

B. The type i n t e g e r

A value of type integer is an element of the


implementation-defined s u b s e t of w h o l e n u m b e r s .

The following arithmetic operators yield an integer value when


a p p l i e d to i n t e g e r o p e r a n d s :

* multiply
divide and truncate (i.e. v a l u e is not rounded)
rag_d_ a mw3t b : a - ((a ~ b)*b)
+ add
- subtract

The relational operators =, <>. <, <=. >=. > y i e l d a B o o l e a n


r e s u l t w h e n a p p l i e d to i n t e g e r opersnds. <> d e n o t e s i n e q u a l i t y .

Four important standard functions yielding integer results are:

abs (x) t h e r e s u l t is t h e a b s o l u t e v a l u e of x .
sqr(x) t h e r e s u l t is x s q u a r e d .
trunc(x) x is a r e a l v a l u e : t h e r e s u l t is its w h o l e p a r t .
(The fractional part is discarded. Hence
trunc(J.7)=3 and trunc(-J.7)=-3)
round(x) x is a real value; the result is t h e r o u n d e d
integer, round(x) m e a n s for x > = 0 t r u n c ( x + 0 . 5 ) , and
for x < 0 t r u n c ( x - 0 . 5 )

Notes: abs and sqr yield an integer result only when their
argument is also of type i n t e g e r . If i is a v a r i a b l e of t y p e
integer, then
succ(1) yields the " n e x t " i n t e g e r , a n d
pred(i) yields the preceding integer
This is, h o w e v e r , m o r e c l e a r l y e x p r e s s e d by the expressions
i+1 and i-I

There exists an implementation-dependent standard identifier


maxint, if a a n d b are i n t e g e r e x p r e s s i o n s , the operation:

a go b

is g u a r a n t e e d to be c o r r e c t l y implemented when:
14

a b s (a o@. b ) <: maxint ,


abs (a) <: maxint , and
abs (b) <: maxint

C . The type real

A v a l u e of t y p e r e a l is an e l e m e n t of the
implementation-defined subset of r e a l numbers.

As l o n g as at l e a s t o n e of t h e operands is of t y p e
real (the other possibly being of type integer) the following
operators yield a real value:

* multiply
/ divide (both operands may be integers, but
the result is always real)
+ add
- subtrcct

Standard functions when accepting a reel argument yield a real


result:

abs (x) absolute value


sqr (x) x squared

Standard functions with real or integer argument and meal


result:

sin (x) trigonometric functions


cos (x)
amctan (x)
ln(x) natural logarithm
e x p (x) exponential function
sqrt (x) square root

Warninq: although r e a l is i n c l u d e d as a s c a l a r t y p e , it c a n n o t
a l w a y s be u s e d in t h e s a m e c o n t e x t as t h e o t h e r s c a l a r t y p e s . I n
Particular. the functions pred and succ cannot take real
arguments, a n d v a l u e s of t y p e m e a l c a n n o t be u s e d w h e n i n d e x i n g
arrays, n o r in c o n t r o l l i n g for statements, nor for defining the
base type of a set.

D. The type char

A value of t y p e c h a r is an e l e m e n t of a f i n i t e a n d o r d e r e d s e t
of characters. Every computer s y s t e m d e f i n e s s u c h a s e t for t h e
purpose of c o m m u n i c a t i o n . These characters are then available on
the input and output equipment. Unfortunately. there does not
15

exist one standard character set; therefore, the definition of


the elements and their ordering is strictly implementation
dependent.

The following minimal assumptions hold for the type char,


independent of t h e u n d e r l y i n g impementation:

The c h a r a c t e r set includes


1. t h e alphabetically ordered set of capital Latin letters
A...Z
2. the numerically ordered and contiguous set of the decimal
digits 0...9
3. t h e b l a n k c h a r a c t e r .

A character enclosed in a p o s t r o p h e s (single quotes) denotes a


c o n s t a n t of t h i s t y p e .

examples:
'~" 'G" '3 ° "°°' °X °

(To represent an apostrophe, one writes it twice.)

The two standard f u n c t i o n s ~r.~[ a n d ~ h r a l l o w t h e m a p p i n g o f t h e


g i v e n c h a r a c t e r s e t o n t o a s u b s e t of n a t u r a l n u m b e r s - - c a l l e d the
ordinal numbers of the character set--and vice verse; ord end
chr e r e c a l l e d ~ r a n s f e r ~ u n c t i o n s .

ord(c) is the ordinal number of the character c in t h e


underlying ordered c h a r a c t e r s e t . (also s e e s e c t i o n
5.A)
chr(i) is the character value with the ordinal number i.

One sees immediately that ord and chr are inverse functions.
i.e.
chr(ord(c)) = c -and- ord(chr(i)) = i

Furthermore, the ordering of a given character set is defined by


ci < e 2 iff ord(cl) < ord(c2)

This definition can be extended to each of t h e r e l a t i o n a l


operators: =. <>. <, <=, >=. >. If R d e n o t e s o n e of t h e s e
operators, then

ci R e2 iff ord(cl) R ord(c2)

When t h e a r g u m e n t of t h e s t a n d a r d functions pred and succ is of


type c h a r , t h e f u n c t i o n s c a n be d e f i n e d as:

pred(c) = c h r (ord (c)-I)


succ(c) = chr (ord (c)+I)

Note: The predecessor ( s u c c e s s o r ) of a c h a r a c t e r is d e p e n d e n t


upon the underlying c h a r a c t e r s e t a n d is u n d e f i n e d if one d o e s
not e x i s t .
3
TH~ PR0~RAM H E A ~ N G AN_E THE DECLARAT~]~N EAR_T_

Every program consists of a heading and a block. The block


contains a declaration part, in which all objects local to the
program are defined, and a statement part. which specifies the
actions to be executed upon these objects.

<program> ::= < p r o g r a m heading> <block>


<block> ::: < l a b e l d e c l a r a t i o n part>
<constant definition part>
<type definition part>
<variable declaration part>
<procedure and function declaration part>
<statement part>

A. Program heading

The heading gives the program a n a m e (not o t h e r w i s e significant


inside the program) a n d l i s t s its p a r a m e t e r s , through which the
program communicates with the environment (see c h a p t e r 13.B.I).

<program heading> ::= & ~ l i g ~ i m < i d e n t i f i e r > ( <file identifier>


{ , <file identifier> } ) ;

B. Label declaration part

Any statement in a program may be m a r k e d by p r e f i x i n g the


statement with a label followed by a c o l o n (making possible a
reference by a goto statement). However, t h e l a b e l m u s t be
defined in t h e j a b e l ~ c l a r ~ & i ~ A &~i~ before its u s e . T h e s y m b o l
label heads this part, which has the general form:

label <label> {, <label>};

A l a b e l is d e f i n e d to be an unsigned integer, end consists of at


most 4 digits.

example:
i~el ~,1~;

C. Constant definition part

A ~&tao~ ~efinition introduces an i d e n t i f i e r as a s y n o n y m for


a constant. The symbol ~ introduces the constant definition
part, which has t h e g e n e r a l form:

f,Ji~&~ <identifier> = <constant>; {<identifier> = <constant>;}


17

where a constant is either" a number, a constant identifier


(possibly signed), or a s t r i n g .

The use of c o n s t a n t i d e n t i f i e r s g e n e r a l l y m a k e s a p r o g r a m m o r e
readable and acts as a c o n v e n i e n t d o c u m e n t a t i o n a i d . It a l s o
allows the programmer to group machine or example dependent
quantities at the beginning of the p r o g r a m w h e r e t h e y can be
easily noted and/or changed. (Thereby aiding the portability and
m o d u l a r i t y of t h e p r o g r a m . )

As an example, consider the following program:

{ program 3.1
example of constant definition pert }

~roaram convert (output);

#onst addin = 32; mulby = 1.8; low = O; high = 39;


sesarator = * °;
var degree : low..high;
J~eain
writeln (separator):
f o r d e g r e e := low J&o h i g h d o
beQi~ write(degree°'c',round(degree*mulby + addin),'f')"
if odd(degree) then writeln
end :
writeln ;
writeln (separator)
and.

0c 32f Ic 34f
2c 36f 3c 37f
4c 39f 5c 41f
6c 43f 7c 45f
8c 46f 9c 48f
I0c 50f 11c 52f
12c 54f 13c 55f
14c 57f 15c 59f
16c 61f 17e 63f
IBC 64f 19c 66f
20C 68f 21c 70f
22c 72f 23c 73f
24c 75f 25c 77f
26c 79f 27c 81f
28c 82f 29c 84f
30c 86f 31c 88f
32c 90f 33c 91f
34c 93f 35c 95f
36c 97f 37c 99f
38c 100f 39c I02f
18

D. Type definition part

A data type in P a s c a l m a y be e i t h e r d i r e c t l y described in t h e


variable declaration or referenced by a ~ypQ ~dentifier.
Provided are not only several standard type identifiers, but
also e mechanism, t h e ~Z#,~ ~ e f i n i t i o o , for creating new types.
The symbol ~.@ introduces a program part containing type
definitions, The definition itself determines a set of values
and associates an i d e n t i f i e r with the set. The g e n e r a l form is:

t~oe <identifier> = <type>: [<identifier> = <type>:}

Examples of type definitions ere found in the subsequent


chapters.

E. Variable declaration part

Every variable occurring in a s t a t e m e n t m u s t be d e c l a r e d in a


~ar!ab!e ~ ~ . This declaration must textually Precede
a n y u s e of t h e variable.

A variable declaration associates en identifier and a data type


with a new variable by s i m p l y listing the identifier followed by
its type. The symbol ~&~ heads the variable declaration part.
The general f o r m is:

<identifier> {, < i d e n t i f i e r > } : <type>:


{<identifier> { , <identifier>} : <type>i}

example :
v~ rootl,root2.root3: reali
count,i: integer ;
found : Boolean;
filler : chari

This identifier/type association is v a l i d t h r o u g h o u t the entire


block containing the declaration, unless the identifier is
redefined in a subordinate block. Suppose a b l o c k B is n e s t e d
within block A. (i.e. declared w i t h i n t h e s c o p e of a n d h e n c e
subordinate t o A . as in f i g u r e 0 . b ) It is p o s s i b l e t o d e c l a r e an
identifier in B that is already declared in A . T h i s has t h e
effect of associating that identifier with a variable local to
B--not available to A--which may be o f a n y t y p e . T h e l a t t e r
definition is then valid throughout the s c o p e o f Bo u n l e s s
redeclared in a block subordinate to B . It is n e t a l l o w e d to
declare a single identifier more than once within the same level
and scope. Hence the following is a l w a y s i n c o r r e c t .
~J~ a : integer;
a : real;
19

F. Procedure and function declaration part

Every procedure or function must be defined (or a n n o u n c e d )


before its use. Procedure end function declarations are treated
in chapter 11. P r o c e d u r e s are subroutines end are activated by
procedure statements. Functions are subroutines that yield a
result value, and therefore can be u s e d as c o n s t i t u e n t s of
expressions.
4
THE ~QNCEPT OE A C T I O N

Essential to a computer program is action. That is, a program


must do something with its data--even if that action is the
choice of doing nothing! ~ ~ describe these actions.
Statements are either &imole (e.g. the assignement statement) or
structured.

A. The assignment statement

The most fundamental of s t a t e m e n t s is t h e ~ s s i ~ d 3 ~ wtatement.


It specifies that a newly computed value be assigned to a
Variable. T h e f o r m o f an a s s i g n m e n t is:

<variable> := < e x p r e s s i o n >

w h e r e := is t h e & s s i c n m e n t ~ p e r a t o r . not t o be c o n f u s e d w i t h t h e
relational operator =. The statement "a := 5" is pronounced "the
current value of a is replaced with the value 5", or simply, "a
~ecomes 5".

The n e w v a l u e is o b t a i n e d by e v a l u a t i n g an ~ x m r e s s i o n consisting
of constant or variable operands, operators, and function
designators, (A f u n c t i o n designator specifies the activation of
a function. Standard functions are listed in Appendix A; user
defined functions are explained in chapter 1 1 . ) An e x p r e s s i o n is
e rule for calculating e value where the conventional rules of
left to right evaluation and ~Eerator erecedence are observed.
The operator ~ot (applied to a Boolean operand) has the highest
precedence, followed by the multiplying operators (*. /, ~iv,
~_#J~, ~U~), then the adding operators (+, -, ~), and of lowest
precedence, the relational operators (=, <>, <, <=. >=, >. &~).
Any expression enclosed within parentheses is evaluated
independent of preceding or succeeding operators.

examp les :
2 * 3-4 * 5 = (2*3) - (4*5) = -14
15 ~ 4 * 4 = (15 ~iv 4)*4 = 12
B0/5/3 = (80/5)/3 = 5.333
4/2 *3 = (4/2)*3 = 6.000
sqrt (sqr(3)+11*5) = 8.000

The syntax of Appendix D reflects the exact rules of precedence.


The reader is recommended to reference it whenever in doubt.

Boolean expressions have the property thst their value may b e


known before the entire expression has been evaluated. Assume
for example, that x=O. Then

(x>O) ~ (x<lO)

is already known to be false after computation of the first


21

factor, and the second need not be e v a l u a t e d . T h e r u l e s of


Pascal neither require nor forbid the evaluation of t h e s e c o n d
part in such cases. This means that the programmer must assure
that the second f a c t o r is w e l l - d e f i n e d , independent of t h e v a l u e
of the first factor. Hence. if o n e a s s u m e s that the array a has
an index ranging f r o m I t o 10. t h e n t h e f o l l o w i n g example is in
error~

x := O:
~eoeat x := x+1 ~ntil (x>lO) ~ (a[x]=O)

(Note that if no a[i] = O, the program will refer to an element


a[11] .)

Direct assignment is p o s s i b l e to variables of any type, except


files. However. the variable (or the function) and the
expression m u s t be of i d e n t i c a l type. with the exception t h a t if
the type of the variable is r e a l , t h e t y p e o f t h e e x p r e s s i o n may
be integer. (If a subrange t y p e is i n v o l v e d , its associated
scalar type determines the validity of the assignment; see
section 5 . B .)

examples of assignments :
root I := p i * x / y
root 1 := - r o o t I
root3 := ( r o o t l + r o o t 2 ) * ( 1 . 0 + y)
found := y>z
count := c o u n t + I
degree := d e g r e e + 10
sqrpr := s q r ( p r )
y := s i n ( x ) + cos(y )

B. The compound statement

The ~pmpound ~tatement specifies t h a t its c o m p o n e n t statements


be executed in the same sequence as they are written. The
symbols ~egin a n d ~ n d a c t as s t a t e m e n t brackets. Note that the
"body" of a Program has t h e f o r m o f a c o m p o u n d statement.

{ program 4.1
the compound statement }

~ro~ram beginend(output);

~j~ sum : integer;


~e~in
s u m := 3 + 5 ;
w r i t e l n (sum , - s u m )
e_o~.

B -8
22

Pascal uses the semicolon to ~eoarete statements, not to


terminate statements; i.e. the semicolon is N O T p a r t of t h e
statement. The explicit rules regarding semicolons are reflected
in the syntax of Appendix D . If one h a d w r i t t e n a s e m i c o l o n
a f t e r t h e s e c o n d s t a t e m e n t , t h e n an ~ m o t y ~ t a t e m e n t ( i m p l y i n g no
action) would have been assumed between the semicolon and the
symbol ~nd. This does no harm, for an e m p t y s t a t e m e n t is
allowable at this point. Misplaced semicolons can. h o w e v e r ,
cause troubles--note t h e e x a m p l e in s e c t i o n 4.D .

C. R e p e t i t i v e statements

Reoetltive ~tatmments specify that certain statements be


repeatedly executed. If the number of repetitions is k n o w n
beforehand (before the repetitions are b e g u n ) , t h e for s t a t e m e n t
is usually the appropriate c o n s t r u c t to e x p r e s s t h e s i t u a t i o n ;
o t h e r w i s e , t h e r e p e a t or w h i l e s t a t e m e n t s h o u l d be u s e d .

C .1 T h e while statement

The while statement has the form:

while <expression> ~g <statement>

The expression controlling the repetition must be of t y p e


Boolean. It is e v a l u a t e d b e f o r e e a c h i t e r a t i o n , so care must be
t a k e n t o k e e p t h e e x p r e s s i o n as s i m p l e es p o s s i b l e .

{ program 4.2
compute h(n) = I + 1/2 + I/3 + o.. + I/n }

Qroeram egwhile(input, output):

varn : integer; h : real;


~ealn read(n); write(n);
h :: O;
while n>O ~
~p.~ h := h + I/n; n := n-1
nnd;
writeln(h)
end.

10 2.928968253968e+00

The statement executed by the while statement (a c o m p o u n d


statement in the above c a s e ) is r e p e a t e d u n t i l t h e e x p r e s s i o n
becomes false. If its value is f a l s e at t h e b e g i n n i n g ° t h e
23

statement is not executed at all.

C.2 The repeat statement

The repeat statement has the form:

reoeat <statement> { ; <statement>} until <expression>

The sequence of statements between the symbols ~eat and until


is executed at least once. Repeated execution is c o n t r o l l e d by
the Boolean expression, which is evaluated after every
iteration.

{ program 4.3
compute h(n) = I + I/2 + I/3 + ... + I/n }

orooram egrepeat(input, output);

vat n : integer ; h : real;


beoin read(n); write(n);
h := O;
repeat h := h + I/n; n := n - 1
n=O;
writeln (h)
am~.

10 2.928968253968e+00

The above program performs correctly for n>O, Consider what


happens if n<=O. The while-verslon of the same program is
correct for a l l n. i n c l u d i n g n=O.

Note that it is a sequence of statements that the repeat


statement executes; a bracketing pair ~e~!~...~nd w o u l d be
redundant (but n o t i n c o r r e c t ) .

C.3 The for statement

The for statement indicates that a statement be r e p e a t e d l y


executed while a progression of values is assigned to t h e
&pn~ zariable of t h e f o r s t a t e m e n t . It h a s t h e g e n e r a l form:

for <control variable> := < i n i t i a l value> to <final value>


do < s t a t e m e n t >
(or)
<control variable> := < i n i t i a l value> downto <final value>
do < s t a t e m e n t >
24

{ program 4.4
compute h(n) = 1 + 1/2 + 1/3 + ... + 1/n }

o r o o r ~ eGfor(input, output);
yam i,n : integer; h : real;
beain r e a d (n) ; w r i t e (n) ;
h := O;
i := n d Q ~ n t o I d ~ h := h + 1/i;
writeln(h)
end.

10 2.928968253968e+00

{ program 4.5
compute the cosine using the expansion:
cos(x) = 1 -x*'2/(2"I) + x*'4/(4"3"2"I) - ... }

~rQoram cosine (input, output);

const eps = Ie-14;


vat x , s x , s , t :real;
i .k ,n :integer ;
b#ain read(n);
for i := I t o n ~L~
beoin read(x); t := I; k :-- O; s := I; s x :~ s q r ( x ) ;
~DJ~ abs(t) > eps*abs(s) do
~e~D k :~ k + 2; t := -t*sx/(k*(k-1));
S := s+t
end ;
writeln (x ,s ,k d i v 2)
end
end.

1.534622222233e-01 9.882477647614e-01 5
3.333333333333e-01 9.449569463147e-01 6
5.000000000000e-01 8.V75825618904e-01 ?
1.000000000000e+O0 5.403023058681e-01 9
3.141592653590e+00 -1.000000000000e+O0 14

The control variable, the initial value, and the final value
must be of t h e s a m e s c a l a r t y p e (excluding type real), and must
not be altered by the for statement. The initial and final
values are evaluated o n l y o n c e . If in t h e c a s e of ~ (~nw~to)
the initial value is g r e a t e r (less) than the final value, the
for statement is not e x e c u t e d . The final value of the control
variable is left undefined upon normal exit from the for
statement.
25

A for statement of t h e form:

for v := el & g e2 ~g S

is equivalent to the sequence of statements:

j~ e1<=e2 &hen
~eain v := el; S; v := succ(v); S: ...; v := e2;

{at this point, v is undefined}

and a for statement of t h e form:

~or v := el #ownto e2 ~ g S

is equivalent to the statement:

if el>=e2 then
beain v := el; S; v := pred(v); S; ...; v := e2; S
ea~
{at this point, v is undefined}

As a final example consider the following program.

{ program 4.6
compute I - I/2 + 1 / 3 - . . . + I / 9 9 9 9 - 1/10000 4 ways.
1) l e f t to r i g h t , in s u c c e s s i o n
2) l e f t to r i g h t , a l l p o s a n d n e g t e r m s , then subtract
3) r i g h t t o l e f t in s u c c e s s i o n
4) r i g h t t o l e f t , a l l p o s a n d n a g t e r m s , then subtract}

~roqram summing (output);

v_ar s 1.s 2p ,s 2n ,s 3,s4p ,s 4n .lrp .lrn ,rlp ,rln : real;


i : integer ;
_be.qin s 1 := 0; s2p := O; s 2 n := O; s 3 := O; s 4 p := O; s 4 n := O;
fgj& i := I J2o 5 0 0 0 ~L~
begin
l r p := I / ( 2 - i - I ) ; { pos terms, left to right }
l r n := I / ( 2 - i ) ; { n e g t e r m s , left to right}
r l p := I / ( I 0 0 0 1 - 2 - i ) ; { p o s t e r m s , r i g h t to l e f t }
r l n := I / ( I 0 0 0 2 - 2 - i ) ; { n e g t e r m s , r i g h t to l e f t }
sl := s l + lrp - l r n :
s 2 p := s 2 p + I r p ; s 2 n := s 2 n + I r n :
s 3 := s 3 + r l p - r l n ;
s 4 p := s 4 p + r l p ; s 4 n := s 4 n + r l n
gnU;
w r i t e l n (s I ,s 2p ~s 2n ) ;
writeln(s3,s4p-s4n)
end.

~.930991830595e-01 6.930971830612e-01
6.930971830599e-01 6.930991830601e-01
26

Why do t h e four "identical" sums differ?

D. Conditional statements

A ~qgo~L~t±onal ~tatement an if or case statement, selects a


single statement of its component statements for execution. The
if ~Lt~tement specifies that a statement be executed only if a
certain condition (Boolean expression) is true. If it is false,
then either no statement or the statement following the symbol
e l s ~ is e x e c u t e d .

D .I The if s t a t e m e n t

The t w o forms for an if s t a t e m e n t are:

if <expression> ~hen <statement>


(or)
if <expression> ~hen <statement> else <statement>

The expression b e t w e e n t h e s y m b o l s if a n d ~ h e n must be of t y p e


Boolean. Note that the first form may be regarded as an
abbreviation of the second when the alternative statement is the
empty statement. Caution: there is never a semicolon before an
else1 Hence, the text:

if p J&heI3 . ~ e a i n $1; S2; $3 end; else $4

is i n c o r r e c t . Perhaps even more deceptive is t h e text:

if p then; ~#ain $1; S2; $3 ~nd

Here. the statement c o n t r o l l e d by the if is t h e e m p t y s t a t e m e n t .


between the then and the semicolon; hence, the compound
statement following the if s t a t e m e n t will a l w a y s be e x e c u t e d .

The s y n t a c t i c a m b i g u i t y a r i s i n g f r o m the c o n s t r u c t :
if < e x p r e s s i o n - l > J~Q2.J3 i f < e x p r e s s i o n - 2 > ~ h e n < s t a t e m e n t - l >
rise <statement-2>

is r e s o l v e d by i n t e r p r e t i n g t h e c o n s t r u c t as e q u i v a l e n t to
iL <expression-l> ~hen
~eain if <expression-2> ~hen <statement-l>
else <statement-2>

The r e a d e r is f u r t h e r c a u t i o n e d t h a t e c a r e l e s s l y f o r m u l a t e d if
statement can be very c o s t l y . Take t h e e x a m p l e w h e r e one has
n-mutually exclusive conditions, c1.,.Cno each instigating a
27

distinct action, si. Let P ( c i ) be t h e probability of ci b e i n g


true, and say that P(ci)>=P(cj) for i < j . Then the most efficient
s e q u e n c e o f if c l a u s e s is :

if ci t h e n sl
e l s e i f c2 S b e n s 2
else ...

else if c(n-1) ~hen s(n-1) e!se sn

The fulfillment of a condition and the execution of its


statement completes the if statement, thereby bypassing the
remaining tests.

If " f o u n d " is a v a r i a b l e of t y p e B o o l e a n , a n o t h e r frequent abuse


of t h e if s t a t e m e n t can be i l l u s t r a t e d by:

if e = b J~b~.J3 f o u n d := t r u e :lse found := f a l s e

A much simpler statement is:

found := a = b
28

{ program 4.7
write roman numerals }

~ro~ram roman (output):

~ar x oy : integer ;
bgqio y := I;
~j~p~&t_ x := y: write(x," "):
w_hile x > = 1 0 0 0 do
bw_qin write(°m°): x := x - l O 0 0 ~n~:
if x>=500 then
bea~j3 w r i t e ( ' d ") ; x := x - 5 0 0 end :
w_h_Ale x>=lO0 d o
b~gin write('c°): x := x - l O 0 end:
if x > = 5 0 t, hen
beEi_.on w r i t e ( ' l ' ) : x := x - 5 0 ~nd:
w_hi!e x>=10 d~
be_~i~n write('x'): x := x - t 0 eZld;
if x > : 5 ~hsn
beoln write('v'); x := x - 5 end;
~h_~le x >= 1 ~l
be~i[z write('i'): x := x - 1 end;
writeln; y : : 2*y
until y>5000
and.

I i
2 ii
4 iiii
U viii
16 xvi
32 xxxii
64 lxiiii
128 cxxviii
256 cclvi
512 dxii
1024 mxxiiii
2048 mmxxxxviii
4096 mmmmlxxxxvi

N o t i c e a g a i n t h a t it is o n l y o n e s t a t e m e n t t h a t is c o n t r o l l e d by
an if c l a u s e . Therefore, when more than one action is i n t e n d e d ,
a compound statement is n e c e s s a r y .

The next program r a i s e s a r e e l v a l u e x t o t h e p o w e r y. w h e r e y


is a non-negative integer. A simpler, and evidently correct
version is obtained by o m i t t i n g the inner while statement: the
result z is t h e n o b t a i n e d through y multiplications by x. N o t e
the loop invariant: z*(u**e)=x**y. The inner while statement
leaves z and u**e invariant, and obviously improves the
efficiency of t h e a l g o r i t h m .
29

{ program 4.B
exponentiation with natural exponent }

#j~oera~ exponentiatiom(input, output);

v~/~ e , y : i n t e g e r ; u , x , z : r e a l ;
#eQin read(x,y); write(x oy)'
Z := 1; u :: X ; e := y;
Wb~ e>O do
b ~ {z*u**e = x**y, e>O}
while ~_~ odd(e) do
b~_~in e :-- e d i v 2: u := s q r ( u )
en~. ;
e := e - l ; z := u * z
a~;
writeln(z) {z = x*~y}

2.000000000000e+O0 7 I .2UOOOOOOOOOOe+02

The following program plots a reel-valued function f ( x ) by


letting the X~xis run vertically and then printing an a s t e r i s k
in positions corresponding to t h e c o o r d i n a t e s . The position of
the asterisk is o b t a i n e d by c o m p u t i n g y=f(x), multiplying by a
scale factor s, rounding the product to t h e n e x t i n t e g e r , a n d
then adding a constant h end letting the asterisk be p r e c e d e d by
that many blank spaces.
30

{ orogram 4.9
graphic representation of e function
f(x) = exp(-x) * sin(2*pi*x) }

&re,ram graph 1(cutout) ;

const d = 0.0625: {3/16o 16 l i n e s for i n t e r v a l [x,x+1]}


s = 31: {31 c h a r a c t e r widths for i n t e r v a l [y,y+1]}
h = 34: { c h a r a c t e r position of x-axis}
c = 6.28318: {2*pi} lim = 32:
var XoY : reel: i,n : integer:
beain x := O;
f_.gor_ i := I J2~ l i m d o
b~eqin y := e x p ( - x ) * s i n ( c * x ) ; n := r o u n d ( s ' y ) + h:
r_epe_a~ w r i t e ( ° "); n := n - 1
until n=O:
writeln('**) :
X := x+d
end_
en_dd.

95
31

O.2 T he case statement

The case statement c o n s i s t s of an e x p r e s s i o n (the s e l e c t o r ) a n d


a llst of s t a t e m e n t s , e a c h b e i n g l a b e l l e d by a c o n s t a n t of t h e
type of t h e s e l e c t o r . T h e s e l e c t o r t y p e m u s t b9 a s c a l a r t y p e ,
excluding the type real. The case statement selects for
execution that statement whose l a b e l is e q u a l t o t h e c u r r e n t
v a l u e of t h e s e l e c t o r ; if no s u c h l a b e l is l i s t e d , t h e e f f e c t is
undefined. Upon completion of t h e s e l e c t e d s t a t e m e n t , control
goes t o t h e e n d of t h e c a s e s t a t e m e n t . T h e f o r m is:

cas~ <expression> ~Z
<case label list> : <statement>;

<case label'list> : <statement>


end

examples: (assume var i: i n t e g e r ; ch: c h a r ; )


gase i of c a s e ch of
O: x := O; ' a ' , ' g ' . ' d ' : ch := s u c c ( e h ) ;
I: x := s i n ( x ) ; "z°,*e°: ch := p r e d ( c h ) ;
2: x := c o s ( x ) ; "f" °S°: {null case}
3: x := e x p ( x ) ; end
4: x := I n ( x )
end

Notes: "Case labels" are ~ o r d i n a r y l a b e l (see s e c t i o n 4 . E )


and c a n n o t be r e f e r e n c e d by a g o t o s t a t e m e n t . T h e i r o r d e r i n g is
arbitrary; however, labels m u s t be u n i q u e w i t h i n a g i v e n c c s e
statement.

Although the efficiency of the case statement d e p e n d s on t h e


implementation, the general rule is to u s e it w h e n o n e has
several mutually exclusive statements with similar probability
of s e l e c t i o n .

E. The goto statement

A qoto ~tatement is a s i m p l e s t a t e m e n t indicating that further


processing s h o u l d c o n t i n u e at a n o t h e r p a r t of t h e p r o g r a m t e x t ,
n a m e l y at t h e p l a c e of t h e l a b e l .

~ot~ <label>

Each label (an u n s i g n e d i n t e g e r t h a t is at m o s t 4 d i g i t s ) m u s t


appear in a label declaration p r i o r to its o c c u r r e n c e in t h e
program body. The s c o p e of a l a b e l L d e c l a r e d in a b l o c k A is
the entire text of block A. That is, ~ i ~ s t a t e m e n t in t h e
statement oart of A may be p r e f i x e d w i t h L : . T h e n any o t h e r
statement w i t h i n t h e ~ b o l e of b l o c k A m a y r e f e r e n c e L in a g o t o
statement.
32

example (program fragment):

label I: {block A}
* o ,

g r o c e d u r e B; { b l o c k B}
l a b e l 3;
:eoin
3: w r i t e l n ( ' e r r o r ' ) ;
. , °

got o 3

end ; { b l o c k B}

~in {block A}

I: w r l t e l n ( ° t e s t f a i l s ' )
{a ~'~oto 3" is not a l l o w e d in b l o c k A}
sod

Warning: The effect of jumps from outside of a s t r u c t u r e d


statement into that statement is not defined . Hence. the
following examples are i n c o r r e c t . (Note t h a t c o m p i l e r s do not
n e c e s s a r i l y i n d i c a t e an e r r o r . )

examples :

a) for i:= I ~ 10 do
_beoin S I;
3:S2
and ;

b) if P then woto 3;
o

if a'~bed~ 3: S

c) oroc@dure P
beoin ...
3: S
eo~ ;
b=_q~i~z ...
~oto 3
end.

A goto statement should be r e s e r v e d for u n u s u a l or u n c o m m o n


s i t u a t i o n s w h e r e the n a t u r a l s t r u c t u r e of an a l g o r i t h m has to be
broken. A good rule is to a v o i d the u s e of j u m p s to e x p r e s s
regular i t e r a t i o n s and c o n d i t i o n a l e x e c u t i o n of s t a t e m e n t s , for
such jumps destroy the reflection of the structure of
computation in the t e x t u a l ( s t a t i c ) s t r u c t u r e of t h e p r o g r a m .
Moreover, the lack of correspondence between textual and
computational (static and dynamic) structure is extremely
detrimental to the c l a r i t y of t h e p r o g r a m and m a k e s the task of
33

verification much more difficult. The presence o £ g o t o ° s in a


Pascal program is o f t e n an i n d i c e t i o n that the programmer has
not yet learned "to think" in P a s c a l (as t h i s is a n e c e s s a r y
construct in other programming lenguages).
5
SCALAR AND SUBRANG~ TYPE~

A. Scalar types

The basic data types in Pascal are the ~calar ~YPeS. Their
definition indicates an o r d e r e d set of values by enumerating the
identifiers which denote the values.

<type identifier> = ( <identifier> {. <identifier>} ) ;

example:
type color = (white,red,blue,yellow.purple.green,
orange,black);
sex = (male,female);
day = (mon,tues.wed,thur,fri,sat,sun);
operators = (plus.minus,times,divide):

illegal example:
~voe workday = (mon,tues,wed,thur,fri.sat):
free = (sat,sun):
(for the type of sat is ambiguous)

The reader is already acquainted with the standard type Boolean


defined as:

type Boolean = (false, true);

This automatically implies the standard identifiers false and


true and specifies that false<true.

The relational operators =, <>, <, <=. >=, and >. are applicable
on all scalar types provided both comparands are of the same
types. The order is determined by t h e s e q u e n c e in which the
constants are listed.

Standard functions with arguments of scalar types are:

succ(×) e.g. succ(blue) = yellow the successor of x


pred (x) pred(blue) = red the predecessor of x
ord ( x ) ord(blue) = 2 the ordinal n u m b e r of x

The ordinal number of t h e first constant listed is O, ord(x) =


ord(pred(x)) + I.

Assuming that c and cl are of type color (above), b is of type


Boolean, and sl...sn are arbitrary statements, then the
following are meaningful statements:
35

for c := b l a c k #ownto red ~ sl

while (cl<>c) and b do sl

if c>white then c := p r e d ( c )

case c of
red .blue ,yellow: s I;
p u r p l e : s2:
g r e e n , o r a n g e : s3;
white.black: s4
and

B. Subrange types

A t y p e may b e d e f i n e d as a a~bran~e of any other already defined


scalar type--called its ~ss#_~iated ~calar ~voa. The definition
o£ a subrange simply indicates the least and the largest
constant value in the subrange, w h e r e t h e l o w e r b o u n d m u s t be
less then the upper bound. A subrange of t h e t y p e r e a l in ~#_~
allowed.

i&t#_~ < t y p e identifier> = <constant> ., < c o n s t a n t > ;

Semantically, a subrange type is an appropriate substitution for


the associated scclar type in all definitions. Furthermore. it
is the associated scalar type which determines the validity of
all operations involving values of subrange types. For example,
given the declaration;

vat a: 1..10; b: 0..30; c:20..30;

The associated scalar type for a. b, and e is integer. Hence the


assignments

a := b; c := b; b:= c;

are all valid statements, although their execution may sometimes


be infeasible. The phrase "or s u b r a n g e thereof" is t h e r e f o r e
assumed to be implied throughout t h i s t e x t a n d is not a l w a y s
mentioned (as it is in t h e R e v i s e d Report.)

example :
type days = (mon,tues ,wed,thur,fri,sat.sun): {scalar type}
workd = mon..fri: {subrange of days}
index = 0..63; {subrange of inteqer}
letter = a .. z ; { s u b r a n g e of c h a r }

Subrange types provide the means for a more explanatory


statement of the problem. To the implementor they also suggest
an opportunity to conserve memory space and to introduce
validity checks upon assignment at run-time. (For an example
with subrange types, see program 6.3.)
6
STRUCTURED TYPES IN GN~NJ~_~AL--THE ARRAY --IN PA_~T~CDLA,B,

Scaler and subrange types are unstructured types, The other


types in Pascal are &~Wcture~ ~vees. As s t r u c t u r e d statements
were compositions of other statements, structured types are
compositions of o t h e r t y p e s . It is t h e t y p e ( s ) of the ~ommo~nt&
and--most importantly--the structuring method that characterize
a structured type,

An option available to e a c h of t h e s t r u c t u r i n g methods is an


indication of t h e p r e f e r r e d internal data representation. A type
definition prefixed with the symbol ~_~ed signals the compiler
to economize storage requirements, even at the e x p e n s e of
additional execution time and a possible expansion of t h e c o d e .
due to the necessary packing and unpacking operations. It is t h e
user's responsibility to realize if he w a n t s t h i s t r a d e of
efficiency for space, (The a c t u a l e f f e c t s upon efficiency and
savings in s t o r a g e space are implementation dependent, and may.
in f a c t , be n i l . )

The array type

An array type consists of e f i x e d n u m b e r of c o m p o n e n t s (defined


when the array is i n t r o d u c e d ) w h e r e a l l a r e of t h e s a m e t y p e ,
called the ~mooneOJ2 or ~se ~voe. Each component can be
explicitly denoted and directly accessed by t h e n a m e of t h e
array variable followed by the so-called io_~ex in square
brackets. Indices are computable; t h e i r t y p e is c a l l e d t h e & B ~ .
~ , Furthermore, the time required to select (access) a
component does not depend upon the value of the selector
(index); hence the array is termed a £_Qndom-~cces& &~uctur~.

The definition of an a r r a y s p e c i f i e s both the component type and


the i n d e x t y p e . The g e n e r a l f o r m is:

~&e A = array[T1] o_f T2;

where A is a n e w t y p e i d e n t i f i e r ; TI is t h e i n d e x t y p e a n d is a
scalar type (where typos integer and real are not allowable
index types); a n d T 2 is a n y t y p e .

examples of variable declarations -and- sample assignments

memory : ~.~L~[O..max] ~L integer memory[i+j] := x


sick : ~r_~[days] £~ Boolean sick[men] := t r u e

(Of c o u r s e these examples assume the definition of the auxiliary


identifiers.)
37

{ program part 6.1


find the largest and smallest number in a g i v e n list }

mrooram minmax(input, output);

const n = 20;
vat i.u .v.min.max : integer;
a : arrav[1..n] of integer;
beoin
[ a s s u m e t h a t at t h i s p o i n t in t h e p r o g r a m , array a
contains the values: 35 6 8 94 7 88 -5 -3 12 3 5 9
- 6 3 0 -2 74 88 52 4 3 5 4}

rain := a[ I] ; m a x := rain; i := 2;
while i < n do
beoiQ u := a]~i] ; v := a [ i + 1 ] ;
_if u > v then
beqin ~ u > m a x J.hen max := u ;
i f v < m i n t h e n rain := v
en_ d e l s e
be_~i__nn ~ v>max then m a x : = v:
if u < m i n t h e n rain := u
en~ :
i := i + 2
e_O~:
if i = n t h e n
i f a [ n ] >max ~Lh~o m a x :: a [ n ]
_ei~_~ i ~ e [ n ] <~nin t h e n rain := a [ n ] ;
writeIn(max,min)
erld .

94 -6
38

[ program 0.2
extend program 4.9 to print x-axls }

~rocr~ graph2(output);

const d = 0.0625: { 1 / 1 6 , 16 l i n e s for interval [x.x+1]~


s = 31; |31 character widths for interval [y.y+1]}
maxl = 67; {max length of line}
hl = 34; {character position of x-axls}
c = 6.2U318; |2*pi} lim = 32;
vat x.y : real, i.n : integer:
a : ~rr~l~[O..maxl] of char"
b@ain x := 0 ;
f o r i := 0 t o m a x l d o a [ i ] := " ",
j~ i := I t o l l m ~-G
beef in a[h] := ° : ° :
y := e x p ( - x ) * s i n ( c * x ) : n := r o u n d ( s ' y ) + hl;
a[n] := " * ' ;
writeln(a);
a[n] := " "; x := x + d
en~
end.

!
39

(Consider how one would extend program 6.2 to print more than
one function--both with and without t h e u s e of an a r r a y . )

Since T2 may be of a n y t y p e , the components of a r r a y s m a y b e


structured. In particular, if T 2 is a g a i n an a r r a y , t h e n t h e
original array A is said to be m u l t i d i m e n s i o n ~ . Hence. the
declamation of a multidimentional a r r a y M c a n be s o f o r m u l a t e d :

vat M : grrav[a..b] of a r r e v [ c . . d ] of T;
and
M [ i ] [j]

then denotes the jth component (of t y p e T) of the ith component


of M.

For multidimensional arrays, it is customary to make the


convenient abbreviations:

va____rr M : ~r_~av[a..b,c.,d] ~ T;
and
M[i.j]

We may regard M as a matrix and say that M[i.j] is t h e j t h


component (in t h e jth column) of the ith component of M (of t h e
i t h r o w o f NI).

T h i s is not l i m i t e d to t w o d i m e n s i o n s , f o r T c a n a g a i n be a
structured t y p e . In g e n e r a l the (abbreviated) f o r m is:

~vo~ <type identifier> =


&r_~2~&~[<index t y p e > [ , <index type>}] gL <component type> ;

If O index types are specified, the array is said t o be


o~dimensional, and a component is denoted by n index
expressions .
40

{ program 6.3
matrix multiplication 1

EroRr~j~ matrixmul(input, output):

const m = 4; p : 3; n : 2;
vat i : 1.,m; j : 1..n; k : 1..p;
s : integer ;
a : arr_@~t[1..m/1..p] of integer;
b : ~r_~[1..p °1..n] o f i n t e g e r ;
c : ~rrav[1..m,1.,n] of integer;
beain {assign ~nitial values t o a a n d b}
fan i "= I t o m d o
~ fan k := I t o p d o
beoio read(s); write(s); eli,k] := s
end :
writeln
end :
writeln ;
~ k := 1 ~j& p ~
~eoin f~L~ J := I • o n d o
bf,~ read(s): write(s); b[k°j] := s
end :
writeln
end :
writ eln ;
{multiply e * b}
? o r i := 1 k s m d a
O~t~l f a n J := I t a n do
b~_qUia s := O:
f o r k := I t o p d o s := s + a [ i , k ] * b [ k ° j ] ;
c[i~,O] := s ; w r i t e ( s )
and :
writeln
n___dd
e ;
writeln
#_O_~ •

I 2 3
-2 0 2
1 0 I
-I 2 -3

-I 3
-2 2
2 I

I 10
6 -4
I 4
-9 -2

~trinc& were defined earlier as sequences of characters enclosed


41

in single quote marks (chapter I). S t r i n g s c o n s i s t i n g of e


single character are the c o n s t a n t s of t h e s t a n d a r d t y p e c h a r
(chapter 2); those of n characters (n>l), are d e f i n e d as
c o n s t a n t s of t h e t y p e d e f i n e d by:

oacked ~r~[ 1..n] ~ char

Assignment (:=) is p o s s i b l e between operands of ~ d e n t i c a l array


types. The relational operators =, <>, <, <=, and >= a r e
applicable on operends of identical packed character arrays,
where the underlying character set determines the ordering,

Access to individual components of packed arrays is o f t e n


c o s t l y , a n d t h e p r o g r a m m e r is a d v i s e d to p a c k or u n p a c k e p e e k e d
array in e single operation. This is possible through the
standard procedures pack end unpack. Letting A be an a r r a y
variable of t y p e

~j~rey[m..n] o~ T

and Z be a v a r i a b l e of t y p e

Eacked ~rrav[u..v] ~L T
where (n-m) >= (v-u), t h e n

p a e k (A ,i .Z ) means ~ j := u ~ v do
Z[J] := A[j--u+i]
and
u n p a c k ( Z ,A ,i ) means ~or j := u ~ £ v ~
A[j--u+i] := Z[j]

where J denotes an a u x i l i a r y variable not occurring elsewhere in


the p r o g r a m .
7
RE&gAE TYPES

The record types are perhaps the most flexible of data


constructs. Conceptually, a record type is a template for a
structure whose parts may h a v e q u i t e distinct characteristics.
For example, assume one wishes to record information about a
person. Known are the name, the social security number, sex.
date of birth, number of dependents, and marital status.
Furthermore, if the person is married or widowed, the date of
the (last) marriaqe is given; if divorced, one knows the date of
the (most recent) divorce and whether this is the first divorce
or not; and if single, given is whether an independent residency
is established. All of this information can be expressed in a
single "record".

More formally, a record is a s t r u c t u r e consisting of a f i x e d


number of components, celled field&. Unlike the array.
components are not constrained to be of identical type and
c a n n o t be d i r e c t l y indexed. A type definition specifies for e a c h
component its t y p e a n d an i d e n t i f i e r , t h e 2ijild i # @ n t i f i e r , to
denote it. The scope of a field identifier is t h e s m a l l e s t
record in which it is defined. In o r d e r t h a t t h e t y p e o f a
selected component be evident from the program text (without
executing the program), the record selector consists of constant
field identifiers rather than a computable value.

To take a simple example, assume one wishes to compute with


complex numbers of the form a+bi. where a and b are real numbers
and i is the square root of - 1 . T h e m e i s no s t a n d a r d type
"complex", However, the programmer can easily define a record
type to represent complex numbers. This record would need two
fields, both of type real. for the real and imaginary parts. The
syntax necessary to express this is:

<record type> ::= ~ e c o r ~ <field list> ~nd


<field list> ::= < f i x e d p a r t > I <fixed part> ; <variant part> l
<variant part>
<fixed part> ::= < r e c o r d s e c t i o n > {; < r e c o r d s e c t i o n > }
<record section> ::= < f i e l d i d e n t i f i e r > { , <field identifier>} :
<type> I <empty>

Applying these rules, one can state the following definition and
declaration:

~Yoe complex = ~ecord re.im : real


~nd ;
vat x : complex;

where complex is e t y p e i d e n t i f i e r ° r e a n d im are identifiers of


fields, a n d x is a v a r i a b l e of t y p e c o m p l e x . Consequently. x is
a r e c o r d m o d e up o f t w o c o m p o n e n t s or f i e l d s .
43

Likewise, a variable representing a date can be defined as:

date : ~ecord mo:(jan.feb,mar,apr,may,june.


july ,aug ,sept ,oct ,nov ,dec ) ;
day: 1..31;
year : integer

a toy as :

toy = ~ecord kind : (ball ,too ,boat ,doll ,blocks


game , m o d e l , b o o k ) ;
cost : real;
received: date;
enjoyed: (alot,some,alittle.none);
b r o k e n ,lost : B o o l e a n

or a homework assignment as:

assignment = ~eeord subject :(history.language,lit,


math,psych.science);
assigned: date;
grade: 0..4:
weight: 1..10
end

To reference a record component, the name o f t h e r e c o r d is


followed by a point, and the respective field identifier. For
example, the following assigns 5+3i to x:
x,re :: 5;
x.im :: 3

If the record is itself nested within another structure, the


naming of the record variable reflects this structure. For
example, assume one wishes to record the most recent small pox
vaccination for each member in t h e f a m i l y . A p o s s i b i l i t y is t o
define the m e m b e r s as a s c a l a r , a n d t h e n t h e d a t e s in an a r r a y
of r e c o r d s :

family= (father,mother.childl,child2,child3);
~L~d~ v a c c i n e : g~[family] £L date:

An u p d a t e might then be recorded as:

vaccine[child3] .mo := a p r ;
vaccine[child3] .day := 23;
vaccine[child3] .year := 1 9 7 3

Note: the type "date" also i~icludes, for instance, e 31st April.
44

{ program 7.1
operations on complex numbers }

&re,ram complex (output);

const fac = 4;
tree complex = record re.im : integer end;
vat x,y : complex;
n : integer ;
be~in
x.re := 2; x .Ira := 7;
y.re := 6 ; y.im : = 3;
for n := 1 &o 4 d~

writeln(" x = ",x.re:3,x.im:3, ' y = ".y.re:3,y.im:3);


{x + y}
writeln( ° sum = ",x.re + y.re:3,
x .im + y.im:3);
{x * y}
writeln (" p r o d u c t = ",x . r e * y . r e - x . i m * y .ira:3,
x.re*y.im + x.im*y.re:3);
writ eln :
x .re := x . r e + fac; x .im := x . i m - fac;
end
end.

x = 2 7 y = 6 3
sum = b 10
product = -9 48

x = 6 3 y = 6 3
sum = 12 6
product = 27 36

x = 10 - 1 y = 6 3
sum = 16 2
product = 63 24

x = 14 - 5 y = 6 3
sum = 20 -2
product = 9 9 12

The syntax for a record type also makes provisions for a ~arisnt
~art. implying that a record type may be specified as consisting
of several ~#j~_~. This means that different variables.
although said to be of the same type. may assume structures
which differ in a certain manner. The differences may consist of
a different number and different types of components.

Each variant is charaeterised by a list. in parentheses, of


declarations of its pertinent comoonents. E a c h l i s t is l a b e l l e d
by one or more labels~, and the set of lists is p r e c e d e d by a
case clause specifying the data type of these labels (i.e. the
45

type according to w h i c h t h e variants are discriminated). As an


exsmple, assume the existence of a

type maritalstatus = (married. widowed, divorced, sinRle)

Then one can describe persons by data of t h e

t~De person =
~ecord <attributes or fields common to all persons> ;
#ase maritalstatus ~
married: ( < f i e l d s of m a r r i e d p e r s o n s o n l y > ) ;
single: ( < f i e l d s of s i n g l e p e r s o n s o n l y > ) ;

end

Usually, e component ( f i e l d ) of t h e r e c o r d i t s e l f i n d i c a t e s its


currently valid variant. For example, the above defined person
r e c o r d is l i k e l y to c o n t a i n a c o m m o n f i e l d

ms : maritelstetus

This frequent situation can be abbreviated by including the


declaration of the discriminating component--the so-called ~a~
f i e l d - - i n t h e c a s e c l a u s e i t s e l f , i.e. by w r i t i n g

case ms: maritalstatus aL

The s y n t a x defining the variant part is:

<variant Dart> : := c a s e < t a g f i e l d > < t y p e i d e n t i f i e r > DJE


<variant> { ; <variant>}
<variant> ::= <case l a b e l l l s t > : ( < f i e l d l i s t > ) I
<emp t y >
<case l a b e l l i s t > : : = <case l a b e l > { , <case l a b e l > }
<ease l a b e l > : : = <unsigned c o n s t a n t >
<tag f i e l d > ::= <identifier> : I <empty>

It is helpful to "outline" the information about a person.


before defining it as a v a r i a n t record structure.

I , Person
A. n a m e (last, f i r s t )
B, s o c i a l s e c u r i t y n u m b e r ( i n t e g e r )
C . s e x (male, f e m a l e )
D. d a t e of b i r t h ( m o n t h , day. y e a r )
E . n u m b e r of d e p e n d e n t s (integer)
F. marital status
if m a r r i e d , w i d o w e d
a. d a t e of m a r r i a g e (month. day, year)
if d i v o r c e d
a. d a t e of d i v o r c e (month. day, year)
b. f i r s t d i v o r c e (false, true)
if s i n g I e
a. i n d e p e n d e n t r e s i d e n c y (false,true)
46

Figure 7.a is a corresponding picture of two "sample" people


with different attributes.

woodyard
A)
edward
i i lliH

845680539 B)
iiiiii i

male c)
~ug i 30 i1941 D)
t
1 H,,,,, ,,
E)
single
F)
true

Figure 7.a Two sample people

A record defining "person" can now be formulated as:

~ype alfa = ~acked ~r_~,~[1..lO] ~L char;


status = (married,widowed,divorced,single);
date = ~eeord mo : ( J a n . f e b . m a r , a p r , m a y , j u n ,
july.aug.sept.oet,nov,dec);
day : I..31;
year : integer
and;
person = ~eeord
name : E e c o ~ first.last: alfa
end;
ss : i n t e g e r ;
sex : (male ,female):
birth : date;
depdts : integer;
c a s e ms : s t a t u s ~
married~widowed : (mdete: date);
divorced : (ddate: date;
firstd: Boolean);
single : (indepdt : Boolean)
end; {person}

I. A l l field names must be distlnct--even if t h e y o c c u r in


different variants.
2. If t h e f i e l d l i s t f o r a l a b e l L is e m p t y , t h e f o r m is:
L : ()
3. A field list can have only one variant p e r t a n d it m u s t
succeed the fixed pert(s). (However, a variant part may
itself contain variants. Hence, it is p o s s i b l e to h a v e n e s t e d
variants.)
47

Referencing a record component is e s s e n t i a l l y a simple linear


reconstruction of the outline. As e x a m p l e , assume a variable p
of t y p e p e r s o n a n d " c r e a t e " t h e f i r s t of t h e m o d e l p e o p l e .

p.name.last := " w o o d y a r d
p.name.first := " e d w a r d
p.ss := 8 4 5 6 8 0 5 3 9 ;
p .sex := m a l e :
p.birth.mo := a u g :
p.birth.day := 30;
p .birth.year := 1941;
p.depdts : = 1;
p .ms := s i n g l e ;
p.indepdt := t r u e

A. The with statement

The above notation can be a b i t t e d i o u s , and the user may wish


to abbreviate it using the ~ith ~tatement. The with clause
effectively opens the scope containing the field identifiers of
the specified record variable, so that the field identifiers may
o c c u r as v a r i a b l e identifiers. (Thereby providing an o p p o r t u n i t y
for the compiler to optimize the qualified statement.) The
general f o r m is:

~tik__h < r e c o r d variable> {. <record variable>} do <statement>

Within the component statement of t h e w i t h s t a t e m e n t one denotes


a field of a record variable by d e s i g n a t i n g o n l y its f i e l d
identifier (without preceding it w i t h t h e n o t a t i o n of t h e e n t i r e
record variable).

The with statement below is equivalent to the preceding series


of a s s i g n m e n t s :

~ith p.name.birth ~L~


~J~l l a s t := " w o o d y a r d ";
first := " e d w a r d ";
ss := 8 4 5 6 8 0 5 3 9 ;
s e x := m a l e ;
mo := a u g ;
d a y := 30;
year := 1941;
depdts := I;
ms := s i n g l e ;
indepdt := t r u e
end {with}
48

Likewise.

vat currentdate : date:


. . °

_with c u r r e n t d a t e do
i ~ too=dec t h e n
b e u i n mo := j a n : year := y e a r + 1
en~
~ I s e mo := s u c c ( m o )

is equivalent to

var currentdate : date;

if currentdate.mo=dec then
b ~ eurrentdate.mo := j a n :
currentdate.year := e u r r e n t d a t e . y e a r + 1
grid
else currentdate.mo := s u c c ( c u r r e n t d a t e . m o )

And the following accomplishes the vaccine update exampled


earlier :

with vaccine[child3] d~
b e Q i ~ mo := a p t : day := 23: year := 1973
grid

No assignments may be m a d e by t h e qualified statement to any


elements of the record variable list. That is, given:

w__ith r do S

r must not contain any variables subject to change by S; for


example :

with a[i] dn
b_eoin ...
i := i+1
end

is _nw~ a l l o w e d .

The form :

w_ith r I, r 2 . . . . . . r n do S

is equivalent to

k i t h r I do
~ith r 2

w~h rn do S
49

Whereas :
yam a : array[2..8] of integer:
a : 2..8;

is NOT allowed, for the definition of a is ambiguous,

y_~ a : integer ;
b : ~eoo~_~[ a: r e a l ; b : Boolean
end ;

IS allowed, for the notation for the integer a is easily


distinguishable from the real "'b.a '°. Likewise. the record
variable b is distinguishable from the Boolean "b.b*
Furthermore, within the qualified statement S of

wi~h b ~ g S

both "b" and "b.b" reference the Boolean "b.b",


8
--THE ~ET T~PE~

A s e t t y p e d e f i n e s t h e s e t of v a l u e s t h a t is t h e p o w e r s e t of its
base type, i.e. the s e t of a l l s u b s e t s of values of t h e b a s e
type. including t h e e m p t y s e t . T h e b a s e t y p e m u s t be s s c a l a r or
subrange type.

~y_#_~ < i d e n t i f i e r > = &~,JL ~L~ < b a s e type>;

The base type of a set must be e scalar type; however,


implementations of Pascal may define limits for the size of
sets, which can be quite small (e.g. the number bits in a word).

Sets are built up from their elements by s e t c o n s t r u c t o r s


(denoted by <set> in the syntax). They consist of the
enumeration of the set elements, i . e . of e x p r e s s i o n s of t h e b a s e
type. which are separated by c o m m a s a n d e n c l o s e d by s e t b r a c k e t s
[ and ] . Accordingly. [] d e n o t e s the empty set,

<set> ::= [ < e l e m e n t llst> ]


<element l i s t > ::= < e l e m e n t > {, < e l e m e n t > } I <empty>
<element> ::= < e x p r e s s i o n > I <expression> .. < e x p r e s s i o n >

The form m..n denotes the set of all elements i of the base type
such that m<=i<=n. If m > n , [ m . . n ] d e n o t e s t h e empty set.

Examples of set constructors :


[13]
[ i + j .i-J]
['A'..'Z" "0"..'9"]

The following operators are applicable on all objects with set


structure:

+ union
intersection
set difference (e.g. A-B denotes the set of all elements
of A t h a t a r e not also eIements of B . )

Relational operators applicable to set operands are:

= <> t e s t on ( i n ) e q u a l i t y
t e s t on s e t i n c l u s i o n
±a set membership. The first operand is a s c a l a r t y p e ,
the s e c o n d is of its a s s o c i a t e d set type; the result
is true w h e n t h e f i r s t is an e l e m e n t of the second.
otherwise false.
51

examples of declarations -and - assignments

tyoe primary = (red . y e l l o w .blue); hue1 := [red] ; h u e 2 := []7


color = ~ £J~ p r i m a r y ; hue2 := h u e 2 + [ s u c c ( r e d )]
vat hue1o'hue2 : color:

ch: char: chsetl := [ "d'.'a'°'g'] ;


chsetl,chset2: ~8_~ ~ Z "a'..'z'; chset2 := [ "a'..°z°]-[ch]

var opeode : &at g~ 0..7; add := [2",3] <= o p c o d e


add : Boolean:

Set operations are relatively fast and can be u s e d to eliminate


more complicated tests. A simpler test for:

~d~ ( c h = ' a ' ) o n ( e h = ° b ' ) o ~ ( e h = ' c ' ) o ~ ( c h = ' d ° ) g ~ ( c h = ' z ") ~hen s
is :
i_f£ ch i n [ "a°..'d °,'z "] &hen s

{ program 8.1
example of set operations }

program setop(output )

type d a y s = (m , t ,w , t h . f r , s a , s u ) ;
w e e k = se__t_t o f d a y s ;
varr wk,work,free : week;
d : days ;

~roce~w~ check(s : week): {procedures introduced in chapter 11}


~ d : days;
~eoin write(" ");
f o ~ d := m & ~ su ~
i ~ d i ~ s & h e n w r i t e ( ° x ") ~ I s e w r i t e ( ' o ' ) ;
writeln
end; { c h e c k }

beoin w o r k := [] ; f r e e := [] ;
wk := [ m . . s u ] :
d := sa; f r e e := [d] + f r e e +[su] ;
check (free) ;
w o r k := w k - f r e e ; check(work);
/j[ f r e e <= w k t h e n w r i t e ( ' o ° ) 7
~/[ w k >= w o r k t h e n w r i t e ( ' k ' ) ;
i f D o t (work >= f r e e ) the[l w r i t e ( " j a c k °):
i ~ [sa] <= w o r k ~ h e n w r i t e ( ' forget it');
writeln
end.

O0000XX
xxxxxoe
ok j a c k
52

On program development

Prooramming--in the sense of designing and formulating


algorithms--is in general a complicated process requiring the
mastery of numerous details and specific techniques. O n l y in
exceptional c a s e s w i l l t h e r e be a s i n g l e g o o d s o l u t i o n . Usually,
so many solutions e x i s t t h a t t h e c h o i c e of an o p t i m a l p r o g r a m
requires a thorough analysis not only of the available
algorithms and computers but also of the w a y in w h i c h t h e
program will most frequently be u s e d .

Consequently, the construction of an a l g o r i t h m should c o n s i s t of


a sequence of deliberations, investigations, and design
decisions. In the early stages, attention is b e s t c o n c e n t r a t e d
on the global problems, and the first draft of a solution may
pay little attention to details. As the design process
progresses, the problem can be split into subproblems, and
gradually more consideration given to the details of p r o b l e m
smecification a n d to t h e c h a r a c t e r i s t i c s of the availmb~e tools.
The terms ~ i s e ~efin_~ment [2] a n d ~ f ~ ~ m m i n ~ [4]
are associated with this approach.

The remainder of t h i s c h a p t e r illustrates the development of an


algorithm by rewording (to be c o n s i s t e n t with Pascal notation)
an example C .A.R. Hoare presents in ~ r u ~ ~rog_r_~in~
[4."Notes on O a t a S t r u c t u r i n g " ] .

The assignment is to g e n e r a t e the prime numbers f a l l i n g in t h e


range 2..n, where n>=2. After a comparison of t h e v a r i o u s
algorithms, t h a t of E r a t o a t h e n e s " s i e v e is c h o s e n b e c a u s e of i t s
simplicity (no m u l t i p l i c a t i o n s or d i v i s i o n s ) .

The first formulation is verbal.

I. Put a l l t h e n u m b e r s between 2 and n into t h e "'s~eve"


2. Select and remove the smallest number remainino in the
sieve.
3. Include this number in t h e "primes"
4. Step through the sieve, removing all multiples of this
number.
5. I f t h e s i e v e is not empty, repeat steps 2--5.

Although initialization of variables is t h e f i r s t s t e m in t h e


execution of a p r o g r a m , it is o f t e n t h e l a s t in t h e d e v e l o p m e n t
process. Full comprehension of t h e a l g o r i t h m is a p r e r e q u i s i t e
for making the proper initializations; updating of these
initializations with each program modification is n e c e s s a r y t o
keep the program runnino. (Unfortunately. updating is not a l w a y s
sufficient!)

Hoare chooses a set t y p e w i t h e l e m e n t s 2 , . n to r e p r e s e n t both


the sieve and the primes. The following is a s l i g h t v a r i a t i o n of
the program s k e t c h he p r e s e n t s .
53

const n = 10000:
va~ sieve,primes : set of 2..n;
next ,j : integer ;
{ initialize}
sieve := [2..n] : p r i m e s := [] : n e x t := 2;
~eoeat {find next prime}
while Dot(next in sieve) do n e x t := s u c c ( n e x t ) :
primes := p r i m e s + [next] :
j := n e x t ;
~hile j < = n d~l { e l i m i n a t e }
beoin sieve := s i e v e - [j] ; j := J + next
and
until sieve=[]

As an exercise Hoare makes the assignment to rewrite the


program, so that the sets only represent the odd numbers. The
following is one proposal. Note the close correlation with the
first solution.

cons~ n = 5000; {n' = n di~A 2}


sieve,primes : set o~ 2..n;
next ,j,c : integer ;
DJ&W~LJ3 { i n i t i a l i z e }
sieve := [2..n] : p r i m e s := [] ; n e x t := 2:
r_em~at { f i n d n e x t p r i m e }
~ h i l e DD_t(next in s i e v e ) d o n e x t := s u c c ( n e x t ) ;
primes := p r i m e s + [next] ;
c := 2 * n e x t - I; {c = n e w p r i m e }
j := n e x t ;
mile j < = n ~LQ { e l i m i n a t e }
~eoin sieve := s i e v e - [J] ; j := j+c
and
unt$~ sieve=[]
end .

It is desirable that all basic set operations ere relatively


fast. Many implementations restrict the maximum s i z e of s e t s
according to their " w o r d l e n g t h ~. so t h a t e a c h e l e m e n t of t h e
b a s e s e t is r e p r e s e n t e d b y o n e b i t (0 m e a n i n g absence, I meaning
presence). Most implementations would therefore not accept a set
with 1Oo000 elements. These considerations lead to an adjustment
in t h e d a t a r e p r e s e n t a t i o n , as s h o w n in p r o g r a m U.2.

A large set can be represented as an array of smaller sets such


that each "fits" into one word (implementation dependent). The
following program uses the second sketch as an abstract model of
the algorithm. The sieve and the primes are redefined as arrays
of sets: next is defined as a record. The output is left
undeveloped.
54

{ program 8.2
generate the primes between 3..I0000 using a
sieve containing o d d i n t e g e r s in t h i s r a n g e . }

oroQram primes (output);

const wdlenoth = 59; { i m p l e m e n t a t i o n dependent}


m a x b i t = 58;
w = 634; {w : n div w d l e n g t h div 2}
sieve ,primes : ~rrav[O..w] ~J~ : e t ~ f O . . m a x b i t ;
next : rec0rd word,bit :inteoer
end ;
j , k . t ,e : i n t e g e r ; empty : boolean;
beoin {initialize}
ZDJZ t :-- 0 ~j~ w do
beein sieve[t] := [ O . o m a x b i t ] ; p r i m e s [ t ] := [] e n d ;
sieve[O] := s i e v e [ O ] - [0] ; n e x t . w o r d := O;
next.bit := I; e m p t y := f a l s e ;

~ h next do
r_~3J~ { find next prime }
~h~ n o t ( b i t in s i e v e [ w o r d ] ) d o bit := s u c e ( b i t ) ;
primes[word] := p r i m e s [ w o r d ] + [bit] ;
c := 2 * b i t + 1;
j := b i t ; k := w o r d ;
~b.ile k < = w do { e l i m i n a t e }
b~liil s i e v e [ k ] := s i e v e [ k ] - [j] ;
k := k + w o r d * 2 ; j := j + c:
while j>maxbit do
begin k := k + 1 ; j := J - w d l e n g t h
end
end ;
if sieve[word]=[] then
be__qin e m p t y := t r u e ; bit := 0
--end ;
while empty ~ (word<w) d~
_b-e.q~ w o r d := w o r d + l ; e m p t y := s i e v e [ w o r d ] = [ ]
and
up~il empty ; {ends with}

and.
9
EZLE T~_EE~

In m a n y w a y s t h e s i m p l e s t s t r u c t u r i n g m e t h o d is t h e s e q u e n c e , In
the data processing profession t h e g e n e r a l l y a c c e p t e d t e r m to
d e s c r i b e a s e q u e n c e is a ~_~ouential ~ i l e . P a s c a l u s e s s i m p l y t h e
word LJ,~J: to specify a structure consisting of a s e q u e n c e of
components--all of w h i c h a r e of t h e s a m e t y p e .

A natural ordering of the components is d e f i n e d t h r o u g h t h e


sequence, and at any i n s t a n c e o n l y one c o m p o n e n t is d i r e c t l y
accessible. The other components are a c c e s s i b l e by p r o g r e s s i n g
sequentially t h r o u g h t h e f i l e . T h e n u m b e r of c o m p o n e n t s , called
the I en at~h of the file, is not fixed by the file type
definition, T h i s is a c h a r a c t e r i s t i c which clearly distinguishes
t h e f i l e f r o m t h e a r r a y . A f i l e w i t h no c o m p o n e n t s is s a i d to be
_em_~.

type <identifier> : ~i1~ ~ <type>;

The declaration of every file variable f automatically


introduces a buffer wariablg, d e n o t e d by ft, of t h e c o m p o n e n t
type. It can be considered as a ~ i n d o w t h r o u g h w h i c h o n e c a n
either inspect (read) e x i s t i n g c o m p o n e n t s or a p p e n d (write) n e w
components, and which is automatically m o v e d by c e r t a i n f i l e
operators.

The s e q u e n t i a l p r o c e s s i n g a n d t h e e x i s t e n c e of a b u f f e r v a r i a b l e
suggest t h a t f i l e s may be a s s o c i a t e d with ~condarv ~to=a~e and
~erioheral~. Exactly how the components are allocated is
implementation dependent, but w e a s s u m e t h a t o n l y s o m e of t h e
components are present in p r i m a r y s t o r e at a n y o n e t i m e , a n d
o n l y t h e c o m p o n e n t i n d i c a t e d by ft is d i r e c t l y a c c e s s i b l e .

When the window ft is moved beyond the gnd gf a ~ile f, t h e


standard Boolean function eof(f) returns the value true,
otherwise f a l s e . The b a s i c f i l e - h a n d l i n g operators are:

r e s e t (f) resets the f i l e w i n d o w to t h e b e g i n n i n g for t h e


purpose of r e a d i n G , i.e. a s s i g n s to ft t h e v a l u e
of the f i r s t e l e m e n t of f. e o f ( f ) b e c o m e s f a l s e
if f is not e m p t y ; o t h e r w i s e , ft is not d e f i n e d ,
and eof(f) remains true.

r e w r i t e (f) precedes the rewriting of t h e f i l e f. The c u r r e n t


value of f is replaced with the empty file.
eof(f) becomes true, and a new file may be
written.

get(f) advances the file window to the next component;


i.e. assigns t h e v a l u e of t h i s c o m p o n e n t to t h e
buffer variable ft . If no n e x t c o m p o n e n t e x i s t s ,
then eof(f) becomes true. and the resulting value
of ft is not d e f i n e d . T h e e f f e c t of g e t ( f ) is
56

defined only if eof(f) is false prior to its


execution.

put(f) appends the value of the buffer variable ft t o


the file f. T h e e f f e c t is d e f i n e d o n l y if p r i o r
to execution the predicate eof(f) is t r u e . e o f ( f )
r e m a i n s t r u e , a n d ft b e c o m e s u n d e f i n e d ,

examples of declarations -and- statements with files

yam d a t a : ~J~J~ ~L~ i n t e g e r ; a := s q r ( d a t a ~ );


a : integer; get (data)

~ar club : ~ile ~L~ p e r s o n ; clubt := p :


p : person; put (club)

Program parts :

I. R e e d a file of r e a l numbers and compute their sum S.

{ program part
c o m p u t e sum }

S := O; reset(f);
w_j31]e n o t e o f (f) d ~
be~ S := S + ft ; g e t ( f )
en_g

2, The following program fragment operates on two files of


ordered sequences of i n t e g e r s
fl,f2 ..... fm end g l g2 . . . . . gn

s u c h t h a t f ( i + 1 ) >= fi and g(i+1) >= gi, f o r a l l i.j


a n d _merges t h e m i n t o o n e o r d e r e d file h such that
h(k+1) >= h ( k ) f o r k = 1,2 . . . . . (re+n-l).

It uses the following variables:


endfg : Boolean;
fog,h : ~ile ~ integer
57

{ program part
merge f and g into h }

bea~3 reset(f): reset(g); rewrite(h);


endfg := s o l ( f ) or eof(g);
~hile D o t e n d f g do
bw~in if ft <gt t h e n
benin ht := ft ; g e t ( f ) ;
endfg := e o f ( f )
#J3~ e l s e
b e n i n ht := gt ; g e t (g);
e n d f g := e o f ( g )
end ;
out(h)
-eng ;

while not eof(g) do


basin ht := g t ; p u t ( h ) ;
get (g)
end :
while not eof(f) do
beqin h t := f t ; p u t ( h ) ;
get (f)
_end

--end

Files may be local to a p r o g r a m (or l o c a l t o a p r o c e d u r e ) or


they may already exist outside the program. The latter are
called e~ternal ~les. External files e r e p a s s e d as p a r a m e t e r s
in the program heading (see chapter 13) i n t o the program.

A. Textfiles

Files whose components are characters are called ~extfil~.


Accordingly, the standard type t e x t is d e f i n e d as f o l l o w s :

type text = ~ile D~ char;

Texts are usually subdivided into l ~ . A straight-forward


method of indicating the separation of two consecutive lines is
by using control characters. For instance, in the ASCII
character set the two characters ~ (carriage return) and l~
(line feed) are used to mark the end of a line. H o w e v e r . many
computer installations use a character set devoid of such
control characters; this implies that other methods for
indicating t h e end o f a l i n e m u s t be e m p l o y e d .

We may consider the t y p e t e x t as b e i n g d e f i n e d over the base


type char (containing printable characters only) extended by a
(hypothetical) line separator character. This control character
cannot be assigned to v a r i a b l e s of t y p e c h a r , b u t c a n be b o t h
58

recognized and generated by the following special textfile


operators:

writeln(x) terminate the current line of the textfile x

readln(x) skip to the beginning of the next line of the


textfile x (xt b e c o m e s the first character of the
next line)

eoln(x) a Boolean function indicating whether t h e e n d of


the current line in the textfile x has b e e n
reached. (If t r u e . xt c o r r e s p o n d s to the position
of a line separator, b u t xt is a b l a n k . )

If f is a textfile a n d ch a c h a r a c t e r variable, the following


abbreviated notation may be u s e d in p l a c e of t h e g e n e r a l f i l e
operators°

abbreviated form expanded form

write (f .eh ) ft : = ch; p u t (f)

read (f .ch ) ch := ft : g e t (f)

The following program schemata use the above conventions to


demonstrate some typical operations performed on t e x t f i l e s .

I. Writing a text y. Assume that P(c) computes a (next)


character and assigns it t o p a r a m e t e r c. I f t h e c u r r e n t l i n e
is to be terminated, a Boolean variable p is s e t to t r u e ;
a n d if t h e t e x t is t o be t e r m i n a t e d , q is s e t t o t r u e .

rewrite(y);
~eeeat
£eo eat P(c); w r i t e (y~.'h)
wntil p ;
wr it e l n (y)
until q

2. Reading a text x. Assume that Q(c) denotes the processing of


a (next) character c. R denotes an action to be executed
upon encountering the end of a line.

r e s e t (x) ;
while opt eof(x) do
be~in
wb_~ ~ eoln(x) do
b#_q/J3 r e a d (x .'c ) ; Q ( c )
end ;
R ; readln(x)
~nd
59

3. Copying a text x to a text y, w h i l e preserving the line


s t r u c t u r e of x.

reset(x); rewrite(y);
w h i l e #3&J2 e o f ( x ) d o
b e q i n {copy a line}
while not eoln(x) do
beain read(x,c); write(y,c)
end ;
readln(x); writeln(y)
end

B . The s t a n d a r d files "input" and "output"

The textfiles "input" and "output" usually represent the


standard I/O media of a computer installation (such as the card
reader and the line printer). Hence, they are the principal
communication line between the computer and its human user.

Because these two files are used very frequently, they are
considered as "default v a l u e s " in t e x t f i l e o p e r a t i o n s when the
t e x t f i l e f is not e x p l i c i t e l y i n d i c a t e d . That is

is e q u i v a l e n t to

write(ch) write (output ,eh)

read(ch) read (input,ch)

writeln writeln (output)

r eadln r e a d l n (input)

eof e a r (input)

eoln eoln (input)

Note: The s t a n d a r d f u n c t i o n s ~se& (rewrite) must not be a p p l i e d


to the f i l e i n p u t (output).

Accordingly, for the case where x is "input" and y is "output"


the first two of the program schemata can be expressed as
follows: (assume ~ar sh: char)

Writing a text on file "output";

~leo eat
~ e o e a ~ P (ch); w r i t e ( c h )
unti~, p ;
writeln
uDtil q
60

Reading a text from file "input":

wh~ no#- e o f d o
b~,q~& { p r o c e s s a line}
w h i l £ no#_ e o l n d o
beain read(ch); Q (oh)
ond ;
R ; readln
end

Further extensions of the procedures write and read (for the


convenient handling of legible input and output date) are
described in c h a p t e r 12.

The next two examples of programs show the use of the textfiles
input and output, (Consider what changes would be necessary if
only get and put° not read and write° are to be used.)

{ program 9.1 -- frequency count of letters in input file }

orosr~d~ fcount (input .output);

~ar ch: char ;


count : ~r_~av[ "a'..'z "] of integer:
letter : set of "a'..°z';
beRin letter : = [ °a ° . . ' z ' ] ;
f o e c h : = "a ° J&£ "z ° d o c o u n t [ c h ] := O;
wh~ not eof do
D#qia
~hile no& eoln do
b~g~3 read (ch) : write(ch) ;
if ch &n letter J2h£.J3 c o u n t [ c h ] := count[ch]+l
end ;
wrtteln; readln
e~
end.

In some installations when a textfile is s e n t t o a o r i n t e r , the


first character of each line is used as a p r i n t e r control
character; i.e. this first character is n o t o r i n t e o , but i n s t e a d
interpreted as controlling the paper feed mechanism of t h e
printer. The following conventions a r e in w i d e u s e :

blank : feed one llne space before printing


: feed double space before printing
r I "
: s k i p to t o p of n e x t p a g e b e f o r e p r i n t i n g
: no l i n e f e e d ( o v e r p r i n t )
61

The following program inserts a b l a n k at t h e b e g i n n i n # of each


llne, resulting in n o r m a l s i n g l e space printing.

{ program 9.2 --insert leading blank }

oroeram insert (input ,output):

vat ch: char;


begin
@D~i~J& D o t e o f d o
beoin write(" ');
~ h ~ l e n o t e o l n do
be~in r e a d (ch) : w r i t e ( c h )
eoj~ ;
wrlteln ; readln

end .

If read and write are used without indication of a file


parameter, the default convention specifies that the files input
and output are assumed: in w h i c h e a s e , t h e y m u s ~ be m e n t i o n e d in
the Paramenter l i s t of t h e p r o g r a m heading.
10
POINTER ~YPES

A ~ _ ~ ~#riable (staticly allocated) is o n e t h a t is d e c l a r e d


in a program and subsequently d e n o t e d by its i d e n t i f i e r . It is
called static, for it e x i s t s ( i . e . m e m o r y is a l l o c a t e d for it)
during the entire execution of the block to which it is l o c a l . A
variable may, on the other hand, be generated dynamically
(without any correlation to the static structure of the program)
by the procedure new. Such a variable is c o n s e q u e n t l y called a

Dynamic variables do not occur in an explicit variable


declaration and cannot be r e f e r e n c e d directly by i d e n t i f i e r s .
Instead, generation of a dynamic variable introduces a ~oi~_E~
Value (which is nothing other than the storage address of t h e
newly allocated variable). Hence, a pointer t y m e P c o n s i s t s of
an unbounded s e t of v a l u e s p o i n t i n g to elements of a given type
T. P is t h e n s a i d to be b o u n d to T . T h e v a l u e n i l is a l w a y s an
element o f P a n d p o i n t s t o no e l e m e n t at a l l ,

tvo~ <identifier> = ~ <type identifier>;

If p is a pointer variable bound to a type T. then p is a


reference to a variable of type T, and pf denotes that variable.

Pointers are a simple tool for the construction of complicated


and flexible data structures. If the type T is a record
structure that contains one or more fields of type fT. then
structures equivalent to arbitrary f i n i t e g r a p h s m a y be b u i l t .
where the T's represent the nodes, and the pointers are the
edges.

As an example, consider the construction of a "data bank""for a


given ~roup of people. Assume the persons are represented by
records as defined in chapter 7. One may then form a chain or
linked list of such records by adding a field of a pointer type
as shown below.

~ link = fperson;

person = ~ecord
. . ,

next : link:

~nd:

A linked list of n persons can be represented as in figure 10.a.


63

first
Figure 10.a Linked list

A variable of type llnk. called "first" points te t h e first


element of the list. The link of t h e last person is n i l .

If we assume that the file "input" contains n social security


numbers, then the following cede could have been used to
construct the above chain.

Va~, f i r s t , p : link; i: integer;

first := n i l :
for_ i := I ~o n do
be~zln mead(s); new(p);
Pt .next := f i r s t ;
pt .ss := s ;
first := p
a~

For purposes of access, ene intreduces anether variable, say pt.


of type l i n k a n d a l l o w s it t o m o v e f r e e l y t h r o u g h t h e l i s t . To
demenstrate selection, assume there is a person with social
security number equal to n and access this person. Th e s t r a t e g y
is te advance pt via link until the desired member is located:

pt := first;
~j~le ptt.ss <> n do pt := p t t . n e x t

In words this s a y s , "'Let pt p o i n t t o t h e f i r s t e l e m e n t . While


the social security number of the member pointed to (referenced)
by pt d o e s n e t e q u a l n, a d v a n c e pt to t h e v a r i a b l e indicated by
the link (also a pointer variable) of t h e r e c o r d w h i c h pt
currently references." N o t e in p a s s i n g that
f i r s t t .nextt .next
accesses the third person.

Note that this simple search statement w o r k s o n l y , if o n e is


s u r e t h a t t h e r e is at l e a s t o n e p e r s o n w i t h s e c u r i t y n u m b e r n on
the list. But is this realistic? A check against failing te
recognize t h e e n d of t h e l l s t is t h e r e f o r e mandatory. One might
first try the following solution:

pt := f i r s t ;
wh~l& (pt <> nil) and (ptt .ss <> n) d O pt := p t t .next

But recall section 4.A. If pt = nil, the variable ptt ,


referenced in the second factor of t h e t e r m i n a t i o n condition,
64

does not ~ x i s t at a l l . T h e f o l l o w i n g are two possible solutions


which treat this situation correctly:

(1) pt := f i r s t ; b := t r u e ;
~hile (pt <> nil) and b ~
~[ P t t .ss = n &h#~3 b := f a l s e else pt := p t t .next

(2) pt := f i r s t ;
~ h i l e pt <> ~ i l d ~
b ~ if p t f .ss = n ~ goto 13;
pt := pt~ .next
end

To p o s e a n o t h e r problem, say one wishes to add the sample person


to the bank. First a s p a c e i n memory m u s t be a l l o c a t e d , and a
reference created by m e a n s o f t h e s t a n d a r d procedure Dew.

new ( p ) allocates a new variable v and assigns


the pointer reference of v to the
pointer variable p. If t h e t y p e of v is
e record type with variants, then new(p)
allocates enough storage to accommodate
all variants. The form

n e w ( p ot 1 . . . . . tn) can be used to a l l o c a t e a variable of


the appropriate size for the variant
with tag field values equal to the
constants t 1...tn. The tag field values
must be listed contiguously a n d in t h e
order of their declaration. Any trailing
t a g f i e l d s m a y be o m i t t e d . T h i s ~W~,~& ~ o t
imply assignment to the tag fields.

Warning: if a record variable pt is c r e a t e d by t h e s e c o n d f o r m


of new, then this variable m u s t not c h a n g e its v a r i a n t during
program execution. Assignment to the entire variable is n o t
allowed; however one can assign to the components of pf.

I f n e w p is a v a r i a b l e o f t y p e l i n k (as d e f i n e d a b o v e ) , new(newp)
creates a new variable of t y p e p e r s o n e n d a s s i g n s the reference
to newp. The value of the new variable is u n d e f i n e d upon
allocation. A c c e s s is v i a t h e p o i n t e r .

examples:

newp~ . s s := 845680539 assignes a social security number

newpf := paul assigns the record paul to newpf

The procedure ~isoose is the "inverse" of i1~ and may h a v e


either the form

d i s p o s e (p)
or"
65

d l s p o s e ( p ,t I .... in)

relative to which form of new was u s e d to c r e a t e the v a r i a b l e


pointed to by p. D i s p o s e t h e n " e r a s e s " t h e v a r i a b l e r e f e r e n c e d
by p.

In t h e next step the new m e m b e r , r e f e r e n c e d by t h e p o i n t e r n e w p ,


must be i n s e r t e d after t h e m e m b e r r e f e r e n c e d by pt. See f i g u r e
10.b.

newp

Figure 10.b Before

Insertion is a s i m p l e matter of changing the pointers:

newpt .next := p i t . n e x t :
pt~ .next :: newp

Figure 10.e illustrates the result.

newp ~ I p n e e w #

Figure I0.c After


66

Deletion of the member following the auxiliary pointer pt is


accomplished in t h e s i n g l e instruction;

ptf .next := p t t . n e x t t . n e x t

It is often p r a c t i c a l t o p r o c e s s a list u s i n g 2 p o i n t e r s - - o n e
following t h e o t h e r . In t h e c a s e of d e l e t i o n , it is t h e n l i k e l y
that one pointer--say p l - - p r e c e d e s t h e m e m b e r to be d e l e t e o , a n d
p2 p o i n t s t o t h a t m e m b e r . D e l e t i o n can t h e n be e x p r e s s e d in t h e
single instruction:

p It ,next : = p2t . n e x t

One is, h o w e v e r , w a r n e d t h a t d e l e t i o n s in t h i s manner w i l l . in


most installations, r e s u l t in t h e l o s s of u s a b l e (free) store. A
p o s s i b l e r e m e d y is t h e f o l l o w i n g :

p l t . n e x t := p 2 t . n e x t ;
d i s p o s e (p 2 )

This provides the implementor with the opportunity to m a r k t h e


store defining the variable referenced by p 2 as f r e e . What
a c t u a l l y is done may g r e a t l y v a r y a m o n g i n s t a l l a t i o n s ; elaborate
"garbage collections" may be implemented, or the dispose
instruction may s i m p l y be i g n o r e d .

Li_~ ~liQcation is the most efficient representation for


inserting and deleting e l e m e n t s . A r r a y s r e q u i r e s h i f t i n g down
(up) of e v e r y e l e m e n t b e l o w t h e i n d e x in t h e c a s e of i n s e r t i o n
(deletion). and files require complete rewriting.

For an example involving a tree structure instead of a linear


l i s t . r e f e r to c h a p t e r 11 ( p r o g r a m 11.5).

A word to the wise

Pascal provides a w i d e v a r i e t y of date s t r u c t u r e s . It is left to


the programmer to e v a l u a t e his p r o b l e m in d e t a i l s u f f i c i e n t to
d e t e r m i n e t h e s t r u c t u r e b e s t s u i t e d to e x p r e s s t h e s i t u a t i o n and
to evaluate the algorithm. As indicated by t h e "data b a n k "
example, linked allocation is e s p e c i a l l y n i c e for i n s e r t i o n a n d
d e l e t i o n . If. h o w e v e r , t h e s e o p e r a t i o n s happen infrequently, but
instead efficient a c c e s s is m a n d a t o r y , t h e n t h e r e p r e s e n t a t i o n
of t h e data as an a r r a y of r e c o r d s is u s u a l l y m o r e a p p r o p r i a t e .
11
PROCE~RUS AND f U N C T I O N S

As one grows in the art of computer programming, one constructs


programs in a sequence of refinement ~tes&o At each step the
Programmer breaks his task into a number of subtaskso thereby
defining a number of partial programs. Although it is possible
to camouflage this structure, this is undesirable. The concegt
of the ~K~_~edure (or &wbroutine) allows the display o£ the
subtasks as explicit subprograms.

A, Procedures

The &roce~ur~ ~eclaration serves to d e f i n e a p r o g r a m p a r t a n d to


associate it w i t h an i d e n t i f i e r , s o t h a t it c a n b e a c t i v a t e d by
a orocedu~A statement. The declaration h a s t h e s a m e f o r m as a
program, e x c e p t it is i n t r o d u c e d by a & r _ ~ z e d u r e b e a d i D _ q i n s t e a d
of a p r o g r a m heading.

Recall the program part that found the minimum and maximum
values in a list of integers. As an extension, say that
increments of j 1 . . j n a r e a d d e d t o a[ I] . . . a [ n ] , t h e n m i n a n d m a x
are again computed. The resulting program, which employs a
Procedure to d e t e r m i n e min and max, follows.
68

{ program 11.1
extend program 6.1 }

program minmax2(input.output);

c o n s t n = 20;
vat a : ~rrav[1..n] ~j[ i n t e g e r ;
i,j : i n t e g e r ;
~rocedure minmax;
va~ i :1..n; u,v,min.max :integer;
~e~in rain := a[ I] : m a x := rain; i := 2;
wh i ] . ~ i<n d o
bagin u := a[i] : v := a l l + l ] ;
if u>v ~h~n
b~aio i f u > m a x th~d3 m a x := u ;
i f v < m i n t h e n rain := v
e~l~ @ l s e
h ~ ~ v > m a X t/3F,/1 m a x := v;
~C[ u < m i n tll@Xl rain := u
and :
i :: i + 2
and ;
i f i=n theo
i f a [ n ] >max t h e n m a x := a [ n ]
e l s e i f a[n] <rain /2hen rain := a [ n ] ;
w r i t e l n (min ,max) ; w r i t e l n
end: {minmax}

begin {read array}


for i := I ~a n do
be~i__~n r e a d ( a [ i ] )" w r i t e ( a [ i ] :3)
e~u3 :
writeln ;
minmax ,
f o r i := I t o n do
bw_~in r e a d ( j ) ; a [ i ] := a[i] +j" write(a[i] :3)
eo_~ :
wrlteln ;
minmax
an~.

-1 -3 4 7 8 54 23 - 5 3 9 9 9 -6 45 79 79 3 1 1 5
-6 79

44 40 7 15 9 88 15 - 4 7 43 12 17 - 7 48 59 39 9 ? 7 12
-7 88

Although simple, this program illustrates many points:

I. The simplest form of the PROCEDURE HEADING, namely:

~rocedure <identifier>:
69

2. LOCAL VARIABLES. Local to procedure minmax are the


variables i, u, v , m i n , a n d m a x . T h e s e m a y be r e f e r e n c e d
only within the scope of minmax; assignments to these
variables h a v e no e f f e c t on t h e p r o g r a m outside the scope
o£ m i n m a x .

3. GLOBAL VARIABLES. Global variables are a, i. and j: They


may b e r e f e r e n c e d throughout the program. (e.g. The first
assignment in m i n m a x is m i n := a [ i ] .)

4. NAME PRECEDENCE. Note that i is the name for both a


global and a local variable. These a r e not t h e s a m e
variable! A procedure may reference any variable global
to it, or it may choose to redefine t h e n a m e . If a
variable n a m e is r e d e f i n e d , the new name/tyoe association
is then valid f o r t h e s c o p e of t h e d e f i n i n g procedure,
and the global variable of t h a t n a m e ( u n l e s s p a s s e d as a
parameter) is no longer available within the procedure
scope. Assignment to t h e l o c a l i ( e . g . i := i + 2 ) h a s no
effect upon t h e g l o b a l i; a n d s i n c e i d e n o t e s the local
variable', the global variable i is effectively
inaccessible.

It is a good programming practice to declare every


identifier w h i c h is n o t r e f e r e n c e d outside the procedure.
as s t r i c l y l o c a l to t h a t p r o c e d u r e . N o t o n l y is t h i s g o o d
documentation, but it a l s o p r o v i d e s added security. For
example, i c o u l d h a v e b e e n l e f t as a g l o b a l v a r i a b l e ; but
then a later extension to the program which called
procedure mlnmax within a loop controlled by i w o u l d
cause incorrect computation.

5. The PROCEDURE STATEMENT. In this example the statement,


"minmax" in the main program activates the procedure.

Examining the last example in m o r e d e t a i l , o n e n o t e s t h a t m i n m a x


is called twice. By formulating the program part as a
procedure--i.e, by not explicitly writing this program part
twice--the programmer conserves not o n l y his t y p i n g t i m e , b u t
also space in m e m o r y . T h e s t a t i c c o d e is s t o r e d only once, and
space defining local variables is activated only during the
execution of the procedure.

One should not hesitate, however, from formulating an action as


a procedure--even when called only once--if doing so enhances
the readability. Defining development s t e p s as p r o c e d u r e s makes
a more communicable and verifiable program.

Often necessary with the decomposition of a problem into


subroutines is the introduction of n e w v a r i a b l e s to represent
the arguments and the results of t h e s u b r o u t i n e s . The purpose of
such variables s h o u l d be c l e a r f r o m t h e p r o g r a m text.

The following program extends the above example to compute the


m i n i m u m a n d m a x i m u m v a l u e of an a r r a y in a m o r e g e n e r a l sense.
70

{ program 11.2
extend program 11.1 }

groqram minmax3(input ,output) ;

const n = 20;
& y o e l i s t = aF_~av[ 1..n] of integer;
yam a . b : list ;
i.j,minl,min2,maxl.max2 : integer;

~ocedure m i n m a x ( v _ a r g : l i s t ; liar j,k : i n t e g e r ) ;


v a ~ i : 1..n; u ,v : i n t e g e r ;
beain j := g[ I] ; k := j; i := 2;
~hile i<n d~
~_~in u := g [ i ] ; v := g [ i + l ] ;
i £ u > v ~_hen
beajd3 if u > k J~h#O k := u ;
if v < j J 2 ~ J := V
end e ~
~.e_q.~.n i f v > k ..i~hen k := v ;
.i.f" u < j & h e n j : = u
.~ o.#. ;
i := i + 2
end :
if i =n ~b#d&
i f g [ n ] > k t h e n k := g[n]
e ~ & e if g[n] <j J&h~,d3 J := g[n] ;
end; {minmax}

~e~in {read array}


f o ~ i := 1 t o n do
beQi~ r e a d ( e l i ] ); write(a[i] :3) end;
writeln ;
minmex (a,minl.maxl);
writeln(minl,mexl,maxl-minl) ; writelm;
fort i := I & O n d o
be.qin r e a d ( b i l l ); write(b[i] :3) end:
writeln :
m i n m a x (b , m i n 2 . m a x 2 ) ;
writeln(min2,max2,mex2-min2) :
writeln(abs (mini-rain2) ,abs (max 1 - m a x 2 ) ) • w r i t e l n ;
~a i := 1 J2a n d ~
begicL e[i] := a[i] + b [ ~ ] : w r i t e ( a [ i ] :5) e~&d;
writeln ;
m i n m a x (a ,rain 1 , m a x l ) ;
wr it e l n (rain I ,max I .max l-rain I )
end.

-1 -3 4 ? U 54 23 -5 3 9 9 9 -6 45 79 79 3 1 1 5
-6 79 8b

45 43 3 ~ I 34 -8 I 4 34 3 d -I 3 -2 -4 6 6 6 7
-U 45 53
2 34

4.4 40 2 15 9 U8 15 - 4 7 43 12 17 - 7 4U 72 75 9 '7 7 12
-7 ~d8 9.5
71

In p r o g r a m 11.2 one encounters the second form of the procedure


heading:

procedure <identifier> ( <formal paramenter section>


{ ; <formal parameter section>} );

The formal parameter section lists the n a m e of each f o r m a l


parameter followed by its type. It is followed by the
declaration part, which introduces the o b j e c t s l o c a l to t h e
procedure.

The labels in the label definition part and all identifiers


introduced in t h e f o r m a l p a r a m e t e r part, the constant definition
part, the type definition part, the variable, procedure, or
function declaration parts are i~cal to the procedure
declaration w h i c h is c a l l e d t h e ~ c o o e of t h e s e o b j e c t s . T h e y a r e
not known o u t s i d e t h e i r s c o p e . In t h e c a s e of l o c a l v a r i a b l e s ,
their values are undefined at t h e b e g i n n i n g of t h e s t a t e m e n t
part.

Parameters provide a substitution mechanism that allows a


process to be r e p e a t e d w i t h a v a r i a t i o n of its a r g u m e n t s . (e.g.
minmex is called t w i c e t o s c a n a r r a y a a n d o n c e to s c a n a r r a y
b.)

One n o t e s a correspondence between the procedure heeding and the


procedure statement. The latter contains a list of ~ctu~J,
&arame~@~r_&, which are s u b s t i t u t e d for t h e c o r r e s p o n d i n g ~ormal
~arameters that are d e f i n e d in t h e p r o c e d u r e declaration. The
correspondence is established by the positioning of the
parameters in t h e l i s t s of a c t u a l and f o r m a l p a r a m e t e r s . There
exist four kinds of parameters: so-called value parameters,
variable parameters, procedure parameters (the a c t u a l p a r a m e t e r
is a procedure identifier), and function parameters (the a c t u a l
parameter is a f u n c t i o n i d e n t i f i e r ) .

Program 11.2 shows the case of the E#j~bl~ ~arameter. The


actual parcmeter ~ust ~ ~ E~riable; the corresponding formal
parameter m u s t be p r e c e d e d by t h e s y m b o l E#32 a n d r e p r e s e n t s this
actual variable during t h e e n t i r e e x e c u t i o n of t h e p r o c e d u r e .
Furthermore, if x 1 . . x n e r e t h e a c t u a l v a r i a b l e s that correspond
to the formal variable parameters v l . . v n , t h e n x l..xn s h o u l d be
~latincj~ v a r i a b l e s .

All address calculations a r e d o n e at t h e t i m e of t h e procedure


c a l l , H e n c e . if a v a r i a b l e is a c o m p o n e n t o f an a r r a y , its i n d e x
expression is e v a l u a t e d w h e n t h e p r o c e d u r e is c a l l e d .

To d e s c r i b e the memory allocation pictorially, o n e c o u l d d r a w en


arrow for each variable parameter f r o m t h e n a m e of t h e f o r m a l
Oarameter to the memory l o c a t i o n of t h e c o r r e s p o n d i n g actual
parameter. Any operation involving the formal parameter is t h e n
performed directly upon the actual parameter. Whenever the
parameter represents a ~ e s u l t of t h e p r o c e d u r e - - a s is t h e c a s e
with j and k a b o v e - - i t m u s t be d e f i n e d as a v a r i a b l e p a r a m e t e r .

When no s y m b o l heads the parameter section, the parameter(s) of


72

t h i s s e c t i o n a r e s a i d to be M~llw_~ ~i@J~eJr~J&~/~(s). In t h i s case t h e


actual parameter Wjistha an ~KD/~/~JJ3 (of w h i c h a v a r i a b l e is a
simple case). The corresponding formal parameter represents a
local variable in the called procedure. As its i n i t i a l v a l u e ,
this variable receives t h e c u r r e n t v a l u e of t h e c o r r e s p o n d i n g
actual parameter (i.e. t h e v a l u e of t h e e x p r e s s i o n at the t i m e
of the procedure call). The procedure may t h e n c h a n g e t h e v a l u e
of this variable by assigning to it; t h i s c a n n o t , h o w e v e r ,
affect the value of the actual parameter. Hence. a value
parameter can never represent a r e s u l t of a c o m p u t a t i o n ,

The difference in the effects of value and variable parameters


is shown in program 11.3.

{ program 11.3
procedure Parameters }

~roora~ parameters (output);

va~ a,b : integer;


~roced~r_e h(x : integer; ~/~ y : integer);
beqin x := x + 1 ; y := y+1;
writeln(x ,y)
end ;
~e~in e := 0; b := 0;
h(a.b);
writeln(a,b)
e~u~,

1 1
0 1

In program 11.2 none of the values in array g are altered; i.e.


g is not a result. Consequently g could have been defined as a
value parameter without affecting the end result. To understand
why this was not done. it is helpful to look at the
implementation.

A procedure c a l l a l l o c a t e s a n e w a r e a for e a c h v a l u e p a r a m e t e r ;
this represents the local v a r i a b l e . T h e c u r r e n t v a l u e of t h e
actual parameter is " c o p i e d " i n t o this l o c a t i o n ; exit from t h e
procedure simply releases this storage.

If a parameter is not used to transfer a result of the


procedure, a value parameter is generally preferred. The
referencing is then quicker, and one is protected against
mistakenly altering the data. However in the case where a
parameter is of a structured type (e.g. an array), one should be
cautious, for the copying operation is relatively expensive, and
the amount of storage needed to allocate t h e c o p y may b e l a r g e .
Because referencing of each element in the array occurs only
once, it is desirable to define the parameter as a variable
parameter.
73

One may change the d i m e n s i o n of the array simply by r e d e f i n i n g


n. To make the program applicable for an array of reals, one
needs only to change the type and variable definitions: the
statements are not dependent upon integer data.

The use of the procedure identifier w i t h i n the text of the


procedure itself implies Cenur@_~ve e x e c u t i o n of the p r o c e d u r e .
Problems whose definition is n a t u r a l l y r e c u r s i v e , often lend
themselves to r e c u r s i v e s o l u t i o n s . An e x a m p l e is the f o l l o w i n g
p r o g r a m . G i v e n as data ere the s y m b o l i c e x p r e s s i o n s :

(a+b) ~ (c-d)
a +b *c -d
( a + b)* c-d
a+b* (c-d)

b+c*(d+c*a*a )*b+a

which ere formed according to the syntax of figure 11.a. A


period t e r m i n a t e s the i n p u t .

expression

term

J factor I

if actor

factor

identifier

d
v I letter I

Figure 11.a Expressions


74

The task is to c o n s t r u c t a p r o g r a m to c o n v e r t t h e e x p r e s s i o n s
into postfix form (Polish notation). This is done by
constructing an individual conversion ~rocedure for each
syntactic construct (expression, term, factor). As these
syntactic constructs are defined recursively, their
corresponding procedures may activate themselves recursively,
75

{ program 11.4
conversion to postfix form }

mrooram postfix(input,output);

~ar ch : char ;

orocedure find;
beoin ~eoent read(oh)
until ( c h < > ° °) a n d ~ot eoln(input)
end ;

orooedure expression :
vat op : char:

or_~ure t erm :

oroce,~ure factor,
beoiQ i f o h = °(* t h e n
beoin find; expression; {oh = ) }
end #lse write(oh) :
find
end: {factor}

factor :
while oh= °*° do
benin find: factor: write('*')
end
~nd; {term}

beoin term ;
~hile (ch='+')oz(ch =°-') do
beqin op : = c h ; f i n d : term: write(op)
end
~.d331: { e x p r e s s i o n }

beoin find ;
~eoeat write(" ");
expression ;
writeln
until oh='."
end.

a b +cd - *
abc~+d-
ab+c*d-
abcd -*+
a a ~a {a ~
bcdca*a ~+*b*+a +

The binary JEr e e i s a d a t a s t r u c t u r e that is naturally defined in


r eeursive terms and processed by recursive algorithms. It
consists of a finite set of nodes t h a t is e i t h e r e m p t y or
consists of a n o d e (the r o o t ) w i t h t w o d i s j o i n t binary trees.
76

c a l l e d t h e left and r i g h t s u b t r e e s (6). R e c u r s i v e p r o c e d u r e s for


generating and traversing binary trees naturally reflect this
m o d e of d e f i n i t i o n .

Program 11.5 builds a binary t r e e and t r a v e r s e s it in p r e - ,


post- a n d e n d o r d e r . The t r e e is s p e c i f i e d in p r e o r d e r , i.e. by
listing t h e n o d e s ( s i n g l e l e t t e r s in this c a s e ) s t a r t i n g at t h e
root a n d f o l l o w i n g first t h e left and t h e n t h e r i g h t s u b t r e e s so
t h a t t h e i n p u t c o r r e s p o n d i n g to f i g u r e 11.b is:

abc..deo,fg...hi..jkt..m..n..

where a point signifies an e m p t y subtree.

Figure 11.b Binary tree s t r u c t u r e


77

{ program 11.5
binary tree traversal }

~ro0ra~ traversal(input.output):

tvae ptr = tnode;


node = ~ecord info : char:
llink.rlink : ptr
~nd;
~ar root : ptr; ch : char;

~roced~ preorder(p : ptr);


~eoin if p<>~3il then
~e~iE write (p~ .info ) ;
preorder(pf .llink):
preorder(pt .rlink)

e_~: {preorder}

~ro_~~ postorder(p : ptr);


beoin if p<>~il then
~eoin postorder(p~ ,llink):
write(pt .info);
postorder(p~ ~ l i n k )
and
~d3~; {postorder}

orocedw~ endorder(p : ptr):


~eoin i f p <>13~Ll J~hen
be~in endorder (p~ ,llink);
endorder(p~ .rlink);
write(p~ .info)
and
~nd: { endorder }

~ r o e e d u r ~ enter(yam p :ptr);
beoin read(oh); write(oh) :
if ch<>'.' _~P.O.
be~i~ new(p);
p? .info := ch;
e n t e r ( p t .llink);
e n t e r (pt .rlink) :
end
~dL~a p := n i l
end ;{ enter}

beain
write(" "); enter(root); writeln :
write(" "); preorder(root); writeln;
write(" ") : postorder(root); writeln;
write(" "); endorder (root); writeln
en__d.

abc.ode..fg...hi.,jkl..m..n..
abodefghijklmn
cbedgfaihlkmjn
cegfdbilmknjha
78

The reader is cautioned against applying reeursive techniques


indiscriminately. Although appearing "clever". they do not
always produce the most efficient solutions.

If a procedure P calls a procedure Q a n d Q a l s o c a l l s P. t h e n


either P or Q m u s t be " p r e - e n n o u n c e d " by a ~ o r w a r d ~eclaratioo
(section 11.C).

The &&andard ~rgcedures in A p p e n d i x A are predeclared in e v e r y


implementation of Pascal. Any implementation may feature
additional predeclared procedures. Since they are, as all
standard objects, a s s u m e d t o be d e c l a r e d in a scope surrounding
the user program, no conflict arises from a declaration
redefining the same identifier within the program. The standard
procedures get, put, read, write, reset, and rewrite were
introduced in c h a p t e r 9. R e e d a n d w r i t e a r e f u r t h e r discussed in
chapter 12.

B. Functions

Functions are program parts (in t h e s a m e s e n s e as p r o c e d u r e s )


which compute a single scalar or p o i n t e r v a l u e f o r u s e in t h e
evaluation o£ an e x p r e s s i o n . A ~wnction ~ . s ~ specifies the
activation of a function and consists of the identifier
designating the function a n d a l i s t of a c t u a l p a r a m e t e r s . The
parameters are variables, exoressions, procedures, or f u n c t i o n s
end are substituted for t h e c o r r e s p o n d i n g formal parameters.

The function declaration has the same form as t h e p r o g r a m , with


the exception of t h e _f_wj3c_t~DJ& ~ e a d i n o which has the form:

L~Oct~gJ3 <identifier> : <result type> :


-or-
L W ~ <identifier> ( <formal parameter section>
{ . <formal parameter section>}) : <result type> ;

As in t h e c a s e of p r o c e d u r e s , t h e l a b e l s in t h e l a b e l d e f i n i t i o n
part and all identifiers introduced in t h e f o r m a l p a r a m e t e r
part. the constant definition part. the type definition part,
the variable, procedure, or f u n c t i o n declaration parts are i~cal
to the function declaration, w h i c h is c a l l e d t h e ~ of t h e s e
objects. They a r e not k n o w n o u t s i d e their scope. T h e v a l u e s of
local variables are undefined at t h e b e g i n n l n B of the statement
pert.

The identifier specified in the function heading names the


function. T h e r e s u l t t y p e m u s t be a s c a l a r , subrange, or p o i n t e r
type. Within the function declaration t h e r e m u s t be an e x e c u t e d
assignment (of t h e r e s u l t t y p e ) to t h e f u n c t i o n identifier. This
assignment "returns" the result of t h e f u n c t i o n .
79

The examples to date have only dealt with variable and value
parameters. Also possible are procedure and function parameters.
Both must be introduced by a special symbol: the symbol
~rocedun~ signals a farmal procedure parameter: the symbol
function, a formal function parameter. The following program
finds a zero of a function by bisection: the function is
specified at t h e t i m e of t h e c e l l .

{ program 11.6
find zero of a function by bisection }

Pro~ra~ bisect (input, output):

const eps =1a-14;


v__ar_ x ,y :reaI;

function zero(J~unction f': real; a,b: real): real:


v a t x ,z : r e e l ; e :boalean;
b#oin s := f(a)<0;
~eoeat x := (a+b)/2.0;
z := f(x):
if (z<O):s then a :: x w l s e b := x
WJ3~Jil a b s ( a - b ) < e p s ;
zero :--- X
e a ~ : {zero}

beqin {main}
read(x,y); writeln(x.y,zero(sin,x ,y));
r e a d ( x ,y ) ; writeln(x ,y ,z e r o ( c o s , x , y ) )
~n~.

-l.000O00000000e+00 1.00O000000000e+00 -7.105427357601e-15


1.00O000000000e+00 2.000000000000e+00 1.570796326795e+00

An assignment (occurring in a function declaration) to a


non-local variable or taa variable parameter is called a ~i9_~
#f£ec~. Such occurrences often disguise the intent of the
program and greatly complicate the task of verification. (Some
implementations may e v e n a t t e m p t to forbid side effects.) Hence,
the use of functions producing side effects is strongly
discouraged.

As an example, consider program 11.7.


80

{ program 11.7
test side effect }

mro~ram sideffect (output);

v~ a,z : integer :
~wnction sneaky(x : integer): integer;
~e~in z := z - x ; { s i d e e f f e c t on z}
sneaky := s q r ( x )
end;
j~e~in
z := 10; a := s n e a k y ( z ) ; writeln(a,z);
z := I0; a := s n e a k y ( t 0 ) * sneaky(z); writeln(aoZ),
z := 10; a := s n e a k y ( z ) * sneaky(t0); writeln(a,z)
end.

100 0
0 0
10000 -10

The next example formulates the exponentiation a3gorithm of


program 4 . 6 as a f u n c t i o n declaration.
81

{ p r o g r a m 11.8
extent p r o g r a m 4.8 }

proora~ expon2(output);

~AE pi,spi: real;

~unetioIl p o w e r ( x :real; y :integer): real; {y>=O}


z: r e a l ;
benin z := I;
while y>O do
,be.q i_a
w_1~i~ n o t o dd ( y ) d o
b~_~n y := y div 2: x := s q r ( x )
end ;
y := y-l: z := x*z
end ;
power := Z
,@Pd: { p o w e r }

_be~In pi := 3.14159;
writeln(2.0o7,power(2.0,7));
spi := P o w e r ( p i , 2 ) :
w r i t e l n (pi ,2.spi );
writeln(spi~,~2,power(spl,2));
w r i t e l n ( p i , 4 , p o w e r (pi ,4) )

2.000000000000e+O0 7 1.2UOOOOOOOOOOe+02
3.141590000000e+00 2 9.869587728100e+00
9.86958772U100e+O0 2 9.740876192266e+01
3.141590000000e+00 4 9.740876192266e+01

The appearance of the function identifier in an e x p r e s s i o n


within the function itself implies ~eeursive e x e c u t i o n of the
function.
82

{ p r o g r a m 11.9
recursive formulation of g c d }

_Pro@ram r e c u r s i v e g c d ( o u t p u t ) ;

Mar x,y,n : integer;

~unction gcd(m,n: integer):integer;


h eoin i f n=0 ~b~J3 god := m
~ I s e god := g c d ( n , m m o d n)
end; {gcd}

&~#.~J~LwZ.~ t r y ( a , b :integer):
b~J3 w r i t e l n (a ,b ,god (e ,b ) )
end ;

beoin try(18,27);
try(312,2142);
t r y (61 ,53);
try (98,868)
end.

18 27 9
312 2142 6
61 53 1
98 868 14

F u n c t i o n c a l l s may o c c u r b e f o r e the function definition if t h e r e


is a ~ D J 2 j ~ ~eference (section 11.C).

The & J ~ ~unction8 of Appendix A ere assumed to be


predeclared in every implementation of Pascal. Any
implementation may feature additional predeclared functions.

C. Remarks

I. P r o c e d u r e (function) calls may o c c u r b e f o r e the p r o c e d u r e


(function) definition if t h e r e is a ~9J&_~J&~ ~ e f e r e n c e . The
form is as follows: (Notice that t h e p a r a m e t e r list and
eventual result type are written ~l~llv in the forward
reference.)
83

p r o g d#_~L~ Q (x : T ) ; forward;
~ro#edure P (y : T ) ,

Q(a)

~nd :
&~ocedure Q; {parameters are not repeated}
~e~in
P(b)

end :
b~zj~
P(a):
Q(b)
end.

2. Procedures and functions which are used as parameters to


other procedures and functions must have value 9arameters
only. (Consequently. it is not necessary to test at run time
whether a parameter is called by value or by address.)

3. A component of a packed structure must not appear as an


actual variable parameter. (Consequently. there is no need to
pass addresses of partwords, and to test at run time £or the
internal representation of the actual variable.)
12
~N PU T _AND # U T P U T

The problem of communication between man and c o m p u t e r was


a l r e a d y m e n t i o n e d in c h a p t e r 9. B o t h l e a r n to w ~ e r s t a n d through
what is t e r m e d p a t t e r n [ e c 0 ~ n i t ~ . Unfortunately, the patterns
recognized most e a s i l y by men ( d o m i n a n t l y t h o s e of p i c t u r e and
sound) are very d i f f e r e n t from t h o s e a c c e p t a b l e t o a c o m p u t e r
(electrical impulses). In fact, the expense of physically
t r a n s m i t t i n g d a t a - - i m p l y i n g a t r a n s l a t i o n of p a t t e r n s l e g i b l e to
man into o n e s l e g i b l e to a c o m p u t e r , a n d v i c e v e r s a - - c a n be as
Costly as the processing of the transmitted information.
(Consequently, much r e s e a r c h is d e v o t e d t o m i n i m i z i n g the cost
by "automatizing" or "'automating" m o r e of t h e t r a n s l a t i o n
process.) T h i s t a s k of c o m m u n i c a t i o n is c a l l e d i n p u t a n d o u t p u t
h a n d l ~ n g (I /O).

The h u m a n can s u b m i t his i n f o r m a t i o n v i a ~ O o u ~ ~ e v i c e & (e.g. key


punches, card readers, paper tapes, magnetic tapes, terminals)
and r e c e i v e his r e s u l t s v i a ~ A W ~ d e v i c e s (e,g. l l n e p r i n t e r s ,
card and paper tape punches, terminals, visual display units),
Common to b o t h - - a n d d e f i n e d by each i n d i v i d u a l i n s t a l l a t i o n - - I s
a set of legible characters (chapter 2). It is over t h i s
character set that Pascal defines the two standard textfile
variables (program parameters) ~OD~t and ~ (also see
c h a p t e r 9).

Textfiles may be a c c e s s e d t h r o u g h the s t a n d a r d f i l e p r o c e d u r e s


get and put. This can, of c o u r s e be q u i t e c u m b e r s o m e as t h e s e
procedures are defined for single character"manipulation. To
illustrate, consider one has a natural number s t o r e d in a
variable x a n d w i s h e s to p r i n t it on t h e file o u t p u t . Note that
t h e p a t t e r n of c h a r a c t e r s d e n o t i n g t h e d e c i m a l r e p r e s e n t a t i o n of
the value w i l l be q u i t e d i f f e r e n t from t h a t d e n o t i n g the v a l u e
written as a Roman numeral (see p r o o r a m 4 , 7 ) ° g u t as one is
usually interested in d e c i m a l n o t a t i o n , it a p p e a r s s e n s i b l e to
offer built-in standard transformation procedures that translate
a b s t r a c t n u m b e r s (from w h a t e v e r c o m p u t e r - i n t e r n a l representation
is u s e d ) i n t o s e q u e n c e s of d e c i m a l d i g i t s a n d v i c e v e r s a .

The two standard procedures ~ead and ~rite are thereby extended
to facilitate the analysis and the formation of textfiles. The
syntax for calling these procedures is non-standard, for they
can be used with a variable number of parameters whose types are
not fixed.

A. The procedure read

Let v l.v2 . . . . . vn denote variables of type char, integer, or


real, and let f denote a textfile.

I, read(v I ..... vn) stands for


r e a d (input,v I . . . . . vn)
85

2. read(f ,v 1 . . . . . vn) stands for


be~in r e a d ( f ,v l ) ; .,. ; read(f,vn) end

3. readln(vl ..... vn) stands for


readln(inPut.v 1 ..... vn)

4. readln(f,v I ..... vn) stands for


be~in r e a d ( f ,v l ) ; ... ; r e a d ( f . v n ) : readln(f) eild

The effect is that after vn is read (from the textfile f),


the remainder of the current line is skipped, (However ° the
values of v l...vn may s t r e t c h over several lines .)

5. If ch is a variable of type char. then


read(f,ch) stands for

begin oh := ff ; g e t (f) ~nd

If a parameter v is o f t y p e i n t e g e r (or m s u b r e n g e thereof)


or real° a sequence of characters, which represents an
integer or a r e e l n u m b e r a c c o r d i n g to the Pascal syntax, is
read. (Consecutive numbers m u s t be s e p a r a t e d by b l a n k s or
e n d s of l i n e s . )

examples:

Read and process a sequence of numbers where the last value


is immediately followed by an asterisk. Assume f to be a
textfile, x and ch to be variables of types integer (or real)
and char respectively.

reset (f) ;
reoe~& read(f,x,ch):
P(x)
~t~_~l ch :°*"

Perhaps a more common situation is w h e n o n e has no w a y o f


knowing how m a n y d a t a i t e m s a r e to be r e a d ° a n d t h e r e is no
special symbol that terminates the list. Two convenient
schemata f o l l o w . In t h e f i r s t , s i n g l e i t e m s a r e p r o c e s s e d .

reset(f); read(f,x):
~ n o t e o f ( f ) do
.b~:~d.a P ( x ) :
r e a d ( f ,x )
~nd

Notice that the last call of read, which causes eof(f) to


become true, does not return a defined value for x.

The second schema processes n-tuples of numbers :


86

reset(f); read(f ,xl);


~la not eof(f) do
beojJ3 read(f ,x2 ..... xn);
P(xl ..... xn);
read(f ,xl)
~nd

(For the above schema to function properly, the total number


of s i n g l e items m u s t be a m u l t i p l e of n.)

B. The p r o c e d u r e write

The procedure write appends character strings (one or m o r e


characters) to a textfile. L e t pl p 2 . . . . . p n be p a r a m e t e r s of
the form defined below (see 5), e n d let f be a t e x t f i l e . Then,
when writing o n t o t h e f i l e f:

1. w r i t e (p 1 . . . . . pn) stands for


write (output,p 1 ..... pn)

2. wrtte(f,p 1 ..... pn) stands for


be~i~ write(f,pl): ... ; write(f.pn) e~

3. wrlteln(p I ..... pn) stands for


writeln(output,p I ..... pn)

4. writeln (f.p 1 ..... pn) stands for


b~in write(f ,pl): ... ; write(f,pn): writeln(f) eo~

This has the effect of writing pl . . . . . pn end then


terminating the current l i n e of t h e textfile f.

5. Every parameter pi must be of one of the forms:

e
e : el
e : el : e2

where e, el, and e2 a r e expressions,

6. e is the valw_E t o be written a n d may be o f t y o e char,


integer, real, Boolean, o r i t may be a s t r i n g . In the first
case, write(f,c) stands for
ff := c; p u t ( f )

7. el--called the minimum field wldth--is an o p t i o n a l control.


It m u s t be a n a t u r a l n u m b e r and indicates the minimum number
of characters to be written. In g e n e r a l , t h e v a l u e e is
written w i t h el c h a r a c t e r s (with preceding blanks). If el is
"too small", m o r e s p a c e is a l l o c a t e d , ( R e a l s m u s t be w r i t t e n
w i t h at l e a s t o n e p r e c e d i n g blank; however, this restriction
does not apply to integer values.) I f no f i e l d l e n g t h is
specified, a default value (implementation dependent) is
assumed according to t h e t y p e of t h e e x p r e s s i o n e.
87

8. e2--called the ~raction i~o_q~--is an o p t i o n a l control and


is applicable only when e is of t y p e r e a l . It m u s t be a
natural n u m b e r and s p e c i f i e s t h e n u m b e r of d i g i t s to f o l l o w
t h e d e c i m a l p o i n t . (The n u m b e r is t h e n s a i d to be w r i t t e n in
fi×ed-~oint notation.) If no f r a c t i o n l e n g t h is s p e c i f i e d ,
t h e v a l u e is p r i n t e d in d e c i m a l f l o a t i n g - p o i n t form.

I0. If the value e is of type Booleen, then the standard


identifier true or f a l s e is w r i t t e n .
13
~ASOAL &000-3.4

The p u r p o s e of t h i s c h a p t e r is to i n t r o d u c e t h o s e f e a t u r e s t h a t
are peculiar to the implementation on t h e C o n t r o l D a t a 6 0 0 0
computers. The reader is w a r n e d t h a t r e l i a n c e u p o n a n y of t h e
characteristics peculiar to PASCAL 6000-3.4 may r e n d e r his
programs unacceptable to other implementations of P a s c a l . O n e
is, therefore, advised to use only features described as
~tandard £ a s c a l in t h e p r e v i o u s chapters whenever possible, and
certainly when writing "portable" programs.

The topics of t h i s chapter fall into four categories:

A) Extensions to the l a n g u a g e
B) Specifications left u n d e f i n e d in t h e p r e c e d i n g chapters
C) Restrictions
D) Additional predefined procedures, functions, and types

A. Extensions to the language Pascal

This section defines non-standard language constructs available


on the Pascal 6000-3,4 system. Although they may b e o r i e n t e d
toward the particular environment provided by the given
operating system, they a r e d e s c r i b e d a n d can be u n d e r s t o o d in
machine independent terms.

A.1 Segmented files

A file can be regarded as being subdivided into so-called


~e~ments, i.e. as a sequence of segments, e a c h of w h i c h is
itself a s e q u e n c e . P A S C A L 6 0 0 0 - 3 . 4 o f f e r s e f a c i l i t y to d e c l a r e
a file as b e i n g ~ e ~ m e n t L d , a n d to r e c o g n i z e s e g m e n t s a n d t h e i r
boundaries. E a c h s e g m e n t of s u c h a file is a " ~ w e i c a l r e c o r d " in
CDC S C O P E t e r m i n o l o g y .

declaration:
<file type> ::= & e ~ m e n t e ~ Lile ~ <type>

an example:
~vm~ T = Qeomented ~ile ~ char;

The p r e d i c a t e function

eas (x) returns the value true when the file x is


positioned st the end of a segment, otherwise
false.

The following two standard procedures are introduced:


89

putseg(x) m u s t be c a l l e d when the generation of a segment of


the file x has been completed, and

getseg(x) is called in o r d e r to i n i t i a t e t h e r e a d i n g of t h e
next segment of the file x . It a s s i g n s to t h e
buffer variable ~ the f i r s t c o m p o n e n t of t h a t
next segment. If no next s e g m e n t e x i s t s , e o f ( x )
becomes true; if a next s e g m e n t e x i s t s but is
empty, then eos(x) becomes true and xt is
undefined. S u b s e q u e n t c a l l s of w e t ~ x ) w i l l e i t h e r
step on t o t h e next c o m p o n e n t or, if it does not
exist, cause eos(x) to b e c o m e t r u e .

Get(x) must not be c a l l e d if e i t h e r eos(x) or e o f ( f ) is t r u e :


eof(x) always i m p l i e s e o s ( x ).

The advantages of a segmented file lie in the possibility of


positioning the reading or w r i t i n g head (relatively) quickly to
any. segment in the file. For the purposes of reading and
(re)writing a segmented file, the standard procedures getseg and
rewrite are extended to accept two arguments.

g e t s e g (x ,n ) initiates the reading of the nth segment


c o u n t i n g f r o m t h e c u r r e n t p o s i t i o n of t h e f i l e ,
n>O implies counting s e g m e n t s in t h e f o r w a r d
direction: n<O means counting them backwards:
and n=O indicates the c u r r e n t s e g m e n t . N o t e :
g e t s e g ( x , 1 ) is e q u i v a l e n t t o g e t s e g ( x ) .

rewrite(x ,n) i n i t i a t e s t h e ( r e ) w r i t i n g of x at t h e b e g i n n i n g
of the nth s e g m e n t c o u n t i n g f r o m the c u r r e n t
position. N o t e : r e w r i t e ( x , 1 ) is n £ & e q u i v a l e n t
to r e w r i t e ( x ) . The l a t t e r c a u s e s i n i t i a t i o n of
( r e ) w r i t i n g at t h e v e r y b e g i n n i n g of t h e e n t i r e
file .

Since files are organized for sequentfal (forward) processing,


one should not expect getseg and rewrite to b e as e f f i c i e n t for
n<=0 as they are for n>0.

The following two program schemas, with the parametric


statements W, R, and S, show the operations of sequential
writing and reading of a segmented file.

Writing a segmented file x :

r e w r i t e (x);
~eat {generate a segment}
~eo~ {generate a component}
W(xt): put(x)
un~2 p :
putseg (x)
w~til q
90

Note: this schem~ will never generate an empty file nor an empty
segment.

Reading a segmented file x :

reset (x):
~hila not eof(x) m~
bm.oin { p r o c e s s a segment}
abila Dot eos (x) d s
be_%i[1 { p r o c e s s a component}
S ( x t ); set(x)
en~ :
S: getseg(x)
end

The next example shows a procedure that reads a segmented


textfile f and copies the first n lines of e a c h s e g m e n t onto the
file output.

orocedure list ;
3&ar i.s : i n t e g e r ;
h~iO s := 0; reset(f):
~ b _ i l a zl#-~ e o f ( f ) do
hf..qin s := s + 1 : i := O;
writeln( ° segment 'os);
w__hil~ n o t e o s ( f ) a n d ( i < n ) d~
berlin i :='i+I; {copy a line}
w_hi/J~ D o t e o l n ( f ) do
b,#oin write(ff ): get(f) {next character}
eOJ~ :
writeln; readln(f) {next line}
eo_~ :
getseg(f) {next segment}
end
oo~

The s t a n d a r d procedures ~£~q.d and ~ r i t e can a l s o be a p p l i e d to


segmented textfiles.

A.2 External procedures

PASCAL 6000-3.4 provides a facility to access oxt~oal


oroeedures, i.e. procedures (functions) that exist outside the
user program and have been separately compi3ed. This enables the
Pascal programmer to a c c e s s p r o g r a m libraries. The declaration
of such a procedure consists of a p r o c e d u r e heading followed by
the word "extern" or "Fortran".
91

B. Specifications left undefined in the preceding chapters

8.1 The program heading and external files

A PASCAL file variable is implemented as a f i l e in t h e C D C


operating system. Local files are allocated on d i s c s t o r e or in
the Extended Core Store (ECS). Storage is a l l o c a t e d when they
are generated and automatically released when the block to which
t h e y a r e l o c a l is t e r m i n a t e d .

Files that exist outside the program ( i . e . b e f o r e or a f t e r


program execution) m a y be m a d e a v a i l a b l e to t h e p r o g r a m if t h e y
are specified as ~ c t u a I ~ a r a m e t e r s in t h e p r o g r a m ceil statement
(EXECUTE) of the control card record. They are called external
files and ere subsituted for the ~ l ~#ramE~WJ2~ specified in
t h e ~ro~j~am ~ a d i n o . The heeding has the following form:

~£~Oram <identifier> ( <program parameter>


{ , <program parameter>} ) ;

where a program parameter is either:

<file identifier> -or- <file identifier> *

The parameters are formal file identifiers; they must be


declared as file variables in t h e m a l n p r o g r a m in e x a c t l y the
same w a y a s a c t u a l local file varfables.

Files denoted by t h e f o r m a l p a r a m e t e r s inowJ2 a n d ~ u t o u ~ have a


somewhat special status. The following r u l e s m u s t be n o t e d :

I . The program heading ~L~S~ contain the formal parameter


output.
2, Contrary to all other external files, the two formal file
identifiers ~nout and ~-9_~ ~st Dot be defined in a
declaration, because their declaration is automatically
assumed t o be:

3&&~ i n p u t , output: text;

3. The procedures reset and rewrite have no effect if applied


to the actual files INPUT and OUTPUT.

examp le :
Proora~ P (output, x, y);

~_~_ x ,y text ;

If an actual parameter in t h e E X E C U T E statement of the control


card record is left empty, the corresponding formal parameter in
92

the p r o g r a m h e a d i n g i s t h e n a s s u m e d as t h e a c t u a l "logical file


n a m e " . For e x a m p l e , w h e n c e i l i n g a p r o g r a m w i t h t h e h e a d i n g :

p r or ~ p (output,f ,g);

then EXECUTE,(,X,) Ss e q u i v a l e n t t o E X E C U T E , P ( O U T P U T , X . G ) . The


full s p e c i f i c a t i o n of t h e f i l e p a r a m e t e r s is r e c o m m e n d e d b e c a u s e
reliance on default v a l u e s o f t e n l e a d s t o m i s t a k e s that c o u l d
easily have been avoided.

B.2 Representation of f i l e s

In the case of external files it is i m p o r t a n t t o know t h e


representation of files chosen by the P A S C A L c o m p i l e r . E v e r y
component of a P A S C A L - 6 0 0 0 file o c c u p i e s an i n t e g r a l n u m b e r of
~O-bit words, with t h e e x c e p t i o n of files w i t h c o m p o n e n t t y p e
~har (~extfiles). In t h i s c a s e P A S C A L f i l e s u s e t h e " s t a n d a r d "
representation imDosed by CDC°s text file conventions: 10
characters are packed into each word, implying that the
procedures put and get i n c l u d e p a c k i n g and u n p a c k i n g o p e r a t i o n s
when applied to t e x t f i l e s . The end of a l i n e is r e p r e s e n t e d by
at least 12, right-adjusted zero-bits in a word. Files
originating from ~ard ~ f o l l o w the s a m e g e n e r a l t e x t f i l e
conventions. Note that the operating system ~moves most (but
not necessary all) trailing blanks when reading cards, Hence,
such files do Dot necessarily c o n s i s t of 8 0 - c h a r a c t e r "card
images"

Files that are not s e g m e n t e d a r e w r i t t e n as a s i n g l e " l o o i c a l


record" (in SCOPE terminology). W h i l e r e a d i n g an u n s e g m e n t e d
external file, end-of-record marks are ignored [for an
exception, s e e p o i n t 3 b e l o w ] . In s e g m e n t e d f i l e s , each s e g m e n t
corresponds to a "logical record". T h e r e is no p r o v i s i o n to
specify a "record level".

Use of e x t e r n a l files

I. If an external file is t o be r e a d ( w r i t t e n ) , t h e n in t h e
Case of non~egmented files, reading (writing) must be
i n i t i a t e d by t h e s t a t e m e n t

r es et (x) (rewrite(x) )

and in t h e case of s e g m e n t e d files by

reset(x) (rewrite(x) > or


getseg(x.n) (rewrite(x.n) )

(This statement is automatically implied for the f i l e s


denoted by the f o r m a l p a r a m e t e r s j~omut a n d ~ u t n u t , and must
not be s p e c i f i e d by the p r o g r a m m e r . )

2. Every external file is a u t o m a t i c a l l y "opened" by a call of


93

the OPE r o u t i n e of t h e o p e r a t i n g s y s t e m . I f t h i s o p e n i n g is
to be r e s t r i c t e d to t h e r e a d f u n c t i o n - - e . g , in t h e c a s e of a
permanent file without write permission--then t h i s has t o be
i n d i c a t e d by an a s t e r i s k f o l l o w i n g t h e f i l e p a r a m e t e r in t h e
program heading. The asterisk itself constitutes no
protection a g a i n s t w r i t i n g on t h e f i l e .

example:
~r_Q_gram testdata (output. data*) :

~i d a t a : "f_ila ~ f real ;
r: r e a l ;

r :='data~ ; get(date)
o . .

3. If the a c t u a l file n a m e I N P U T is s u b s t i t u t e d corresponding


to a formal program parameter, s a y f, t h e n f is t h e c u r r e n t
s i n g l e l o g i c a l r e c o r d of t h e f i l e I N P U T .

B.3 The standard types

INTEGER

The standard identifier ~axint is defined as

~on~ maxint = 281474976710655; { = 2**48 - 1 }

The r e a d e r is c a u t i o n e d , h o w e v e r , that t h e CDC c o m p u t e r p r o v i d e s


no indication of o v e r f l o w . It is, t h e r e f o r e , the programmer's
responsibility to p r o v i d e a c h e c k w h e n e v e r t h i s m i g h t o c c u r .

Actually. the machine is c a p a b l e of s t o r i n g i n t e g e r s up t o an


absolute value of 2**59, but then ~ the operations of
addition (+), subtraction (-), taking the absolute value,
multiplication and division by powers of 2 ( i m p l e m e n t e d as
shifts), and comparisons a r e c o r r e c t l y e x e c u t e d in t h i s r a n g e
(as l o n g as no o v e r f l o w o c c u r s ) . In p a r t i c u l a r , one cannot even
p r i n t an i n t e g e r v a l u e i w h e n a b s ( i ) > m a x i n t . This does, however,
allow the following test:

if a b s ( i ) > maxint the• write(" too big')

REAL

The type ~al is d e f i n e d a c c o r d i n g to CDC 6 0 0 0 f l o a t i n g point


f o r m a t . P r o v i d e d is a m a n t i s s a w i t h 48 b i t s , c o r r e s m o n d i n g to 14
d e c i m a l d i g i t s . The m a x i m u m a b s o l u t e m a g n i t u d e is 1 0 - ' 3 2 2 .
94

CHAR

A v a l u e of t y p e ~ h a r is an e l e m e n t in t h e c h a r a c t e r set p r o v i d e d
by t h e p a r t i c u l a r i n s t a l l a t i o n . The f o l l o w i n g 3 v e r s i o n s e x i s t :

I) The C D C Scientific 64-character set


2) T h e C D C Scientific 63-character set
3) The CDC A S C I I 6 4 - c h a r a c t e r set

Table 13.a lists the a v a i l a b l e c h a r a c t e r s and i n d i c a t e s t h e i r


o r d e r i n g : ~ o t e : t h e CDC s p e c i f i c a t i o n i m p l i e s an o r d e r i n g of the
ASCII characters w h i c h d i f f e r s from the I n t e r n a t i o n a l S t a n d a r d
(ISO)!
95

CDC Scientific Character Set with 64 elements

j~ 1 2 3 4 5 6 7 8 9

: A a C O E F G H I
J K L M N 0 P q R S
T U V W X Y Z ~ I 2
3 4 5 6 7 8 9 + - *
/ ( ) $ = , -~ [
] ~ ~ m v ^ ~ ~ < >

Comments:
- ~ not used in 63-character set version
- 51 : in the 63-character set version
- 48 ' at ETH
- 53 t at ETH
- 57 t at ETH

ASCII Character 5et with CDC's ordering

1 2 3 4 5 6 7 8 9

: A B C D E F G H I
J K L M N 0 P q R 5
T U V W X Y Z ~ I 2
3 4 5 6 7 8 9 + *
/ ( ) , = , ~ [
: & ' ? < >
J ~ ,,

Figure 13.a CDC character sets


96

Based upon the above character sets, the following characters


are accepted as s y n o n y m s for t h e s t a n d a r d s y m b o l g i v e n in t h e
left c o l u m n :

5tandard Pascal CDC scientific ASCII

not -7
and A &
o__r V
<>
<= _<
>= >

Figure 13.b: Alternative representation of


standard symbols

B.4 The standard procedure "'write"

If no m i n i m u m f i e l d l e n g t h p a r a m e t e r is s p e c i f i e d , the following
default values are assumed.

type default

integer 10
real 22 ( w h e r e t h e e x p o n e n t is a l w a y s
e x p r e s s e d in t h e f o r m : E~999 )
Boolean 10
char 1
a string length of t h e string
a l f a (see D.I) 10

The end of each line in a textfile f m u s t be e x p l i c i t l y


indicated by writeln(f)o where writeln(output) m a y be w r i t t e n
simply as w r i t e l n . If a t e x t f i l e is to be s e n t to a p r i n t e r ~ no
line may c o n t a i n m o r e t h a n 136 c h a r a c t e r s . The f i r s t c h a r a c t e r
of each line is interpreted by the printer as a control
character and ~s not printed. The following characters are
interpreted to mean
°+"
no llne f e e d (overprinting)
blank single spacing
"0" double spacin~
triple spacing
s k i p to top of next p a o e b e f o r e printing

The procedure writeln(x) is u s e d to m a r k t h e end of a line on


fi&e x, The conventions of t h e C D C o p e r a t i n g s y s t e m r e o a r d i n g
textfile representation a r e s u c h that t h i s p r o c e d u r e is f o r c e d
to emit some extra blanks under certain circumstances. Hence,
97

upon reading, a textfile may c o n t a i n blanks at the end of lines


that were never explicitly written. (Sorry about this!)

C. Restrictions (as of March 1974)

1. The word se.qmented is r e s e r v e d .

2. T h e b a s e t y p e of a set m u s t be e i t h e r
a) a s c a l a r w i t h at m o s t 58 e l e m e n t s (or a s u b r a n g e thereof)
OF

b) a subrange with a minimum element greater than or equal


to zero, and a maximum element less than or equal to 58,
or
c) a subrange of the type chat with the maximum element less
than or equal to the value chr(58).

3. Standard (built-in) proeedrues or functions are not accepted


as actual parameters. For example, in order to run program
11.6 in PASCAL 6 0 0 0 - 3 . 4 , one would have to write auxiliary
functions as follows:

functi~233 s i n e ( x : r e e l ) : r e a l ;
beqin s i n e := s i n ( x ) ~U~:

fw43ction c o s i n e ( x : r e a l ) : r e a l :
b#~in c o s i n e := cos (x) ~3~;

fu~£~ zero(function f: r e a l ; e,b: real): real;


be~in ... e~ :
..•

be~in
read(x,y); writeln ( x .y , z e r o ( s i n e , x , y ) ) ;
r e a d ( x oy ) ; writeln (x ,y , z e r o ( c o s i n e , x , y )

4. It is not p o s s i b l e to c o n s t r u c t a file of f i l e s : h o w e v e r ,
records and a r r a y s w i t h f i l e s as c o m p o n e n t s a r e a l l o w e d .

D. Additional predefined types, procedures, and functions

D,I Additional predefined types

The t y p e ~ i f & is p r e d e f i n e d by:

~y~ ella = ~acked ~rrav[1..10] ~ char;

(Hence, a value of type ella is representable in exactly one


word.) The constants of this type are &trinqs of exactly 10
characters.
98

Applicable on operands of type ella are assignment (:=) a n d


comparison, where = a n d <> t e s t equality and <, <=, >=. and >
test order according to the underlying character set. Alfa
values may be p r i n t e d by t h e p r o c e d u r e write.

{ program 13.1
al£a values }

~roeram egalfa (output);

vat nl.n2: alfa;


beoin write(" names: ');
nl := "raymond ": n2 := "debby
if n2 < nl then writeln(n2.nl)
else writeln (nl,n2)
end.

nameS: debby raymond

Note: It is not possible to read alfa values directly; instead,


the following is s u g g e s t e d :

vi~ b u r : a r r a y [ I..10] of c h a r ;
a: a l f a ; i: i n t e g e r ;

E e l i :: I t o 10 d o r e a d ( b u f [ i ] );
pack(buf01.a ) {accomplishes read(a)}

D .2 A d d i t i o n a l predefined procedures and functions

~roce~ures

date (a) assigns to the alfa variable the current date.

halt terminates the execution of the program and


issues a poat-mortem dump.

linellmit(f,x) f is a t e × t f i l e a n d x is an i n t e g e r expression,
The effect is to cause the program to be
terminated, if m o r e t h a n x l i n e s a r e a s k e d t o be
written on f i l e f.

message (x) the string x is written into the dayfile.


(Hence. x should contain at most 40 characters.)

time (a) assigns to the alfa variable a the current time.

putseg . getseg, and the extensions to rewrite and reset are


99

discussed in s e c t i o n 13.A.I.

~unctlons

card(x) equals the cerdinal±ty of the s e t x (i.e. t h e


number of e l e m e n t s c o n t a i n e d in t h e s e t x.)

clock a function, without parameters, yielding an


i n t e g e r v a l u e e q u a l to the c e n t r a l p r o c e s s o r t i m e ,
expressed in milliseconds, already u s e d by t h e
job.

expo(x) yields an integer v a l u e e q u a l to t h e e x p o n e n t of


the floating~oint representation of the real
value x.

undefined(x) a Boolean function. Its value is t r u e w h e n t h e


real value x is "out of r a n g e " or " i n d e f i n i t e " ,
o t h e r w i s e f a l s e [ 7] .

cos(x) (discussed in s e c t i o n 13.A.1)


14
Uow ~ Use ~he ~ASCAL ~ 0 0 0 - 3 . 6 ~ v s t e m

A. C o n t r o l statements (for SCOPE 3.4)

A Pascal job u s u a l l y c o n s i s t s of f o u r s t e p s . F i r s t , the P a s c a l


compiler is loaded. The s e c o n d s t e p is t h e c o m p i l a t i o n step,
which yields a l i s t i n g of t h e s o u r c e p r o g r a m a n d t h e c o m p i l e d
code. In the third step t h e c o m p i l e d c o d e , d e p o s i t e d by t h e
compiler on secondary store, is loaded and linked with
precompiled routines for input and output handling, which are
provided on a "program l i b r a r y " f i l e . F i n a l l y , t h e c o m p i l e d a n d
loaded code is executed. These four s t e p s a r e i n i t i a t e d by
appropriate orders to the operating system in t h e f o r m of
~ontro~ Qtetements. The e x a c t f o r m of t h e s e s t a t e m e n t s and t h e i r
abbreviated f o r m s ( l o a d i n g a n d e x e c u t i o n c a n o f t e n be o r d e r e d by
a single statement) depend entirely upon the available operating
system, and must therefore be specified by the particular
installation.

The a c t u a l f i l e p a r a m e t e r s , w h i c h c o r r e s p o n d t o t h e f o r m a l file
identifiers l i s t e d in t h e p r o g r a m h e a d i n g , m u s t be s p e c i f i e d in
the statement initiating execution of the compiled program
( u s u a l l y an E X E C U T E c o m m a n d ) .

The compiler itself is a l s o a Pascal program. Its heading is

~r_~wJE~ Pascal(input.output,lgo);

The first formal parameter d e n o t e s t h e file r e p r e s e n t i n g the


source p r o g r a m to be c o m p i l e d ; t h e s e c o n d , t h e p r o g r a m l i s t i n g ;
end t h e t h i r d , the compiled "binary", relocatable code.

The COC operating systems allow the omission of a c t u a l


parameters in t h e c o n t r o l s t a t e m e n t s . If an a c t u a l f i l e name is
omitted, the Pascal convention on p r o g r a m p a r a m e t e r s specifies
t h a t t h e f o r m a l file i d e n t i f i e r be u s e d as t h e a c t u a l file n a m e .
Hence, the standard files INPUT, OUTPUT, and LGO are
automatically a s s u m e d as t h e d e f c u l t ~ i l e s for t h e s o u r c e file,
the listing, and the relooatable binary code respectively. Note,
however, that these roles m a y be a s s u m e d by o t h e r f i l e s w h e n
their names are entered as actual parameters. Note: actual
parameters m u s t c o n s i s t of at m o s t 7 c h a r a c t e r s .

B . Compiler options

The compiler may be i n s t r u c t e d to g e n e r a t e c o d e a c c o r d i n g to


c e r t a i n o p t i o n s ; in p a r t i c u l a r , it may be r e q u e s t e d to insert or
omit r u n - t i m e t e s t i n s t r u c t i o n s . Compiler directives are written
as comments and a r e d e s i g n a t e d as s u c h by a S - c h a r a c t e r as the
first c h a r a c t e r of t h e c o m m e n t :
101

{$<option sequence> <any comment> }

Example: {$T+.P+ }

The option sequence is a s e q u e n c e of i n s t r u c t i o n s separated by


commas. Each instruction consists of a l e t t e r , designating the
option, followed either by a p l u s (+) if t h e o p t i o n is t o be
activated or a m i n u s (-) if t h e o p t i o n is t o be p a s s ~ v a t e d 0 or
by a d i g i t (see X e n d B b e l o w ) .

The following options are presently available"

T include run-time tests that chock

a) all array indexing operations to insure that the index


lles within the specified array bounds.
b) all assignments to variables of s u b r a n g e t y p e s to m a k e
certain that the assigned value lies within the specified
range .
e) all divisions to i n s u r e a g a i n s t zero divisors
d) all automatic integer to reel conversions to a s s u r e t h a t
the converted value satisfies :
a b s (i) <= m s x i n t
e) all case statements to insure that the case selector
corresponds to one of the specified case labels.

default = T+

P generate the code necessary to write a complete Post~ortem


Dump (see section 14.C .2) in t h e c a s e of a r u n - t i m e error.

default = P+

X if a digit n (0 <= n <= 6) f o l l o w s t h e X. p a s s t h e f i r s t n


parameter discriptors in the registers XO to X(n-1) (the
first in XO, t h e s e c o n d in XI, etc',). O t h e r w i s e p a s s t h e m in
the locations with the addresses B6+3 to B6+n+2.

n>O reduces t h e s i z e of t h e c o d e p r o d u c e d by t h e c o m p i l e r and


probably also s l i g h t ly improves the code. However', the
programmer must be a w a r e t h a t w i t h n > O . t h e c o m p i l e r cannot
use the registers XO t o X m l n ( n - l , i - 2 ) for the passing of t h e
ith parameter. It is therefore possible t h a t for n > O , t h e
compiler gives the message " r u n n i n g o u t of r e g i s t e r s " : where
f o r n = O o it w o u l d n o t .

default = X4

E allows the programmer to control the symbols for the entry


points to the object code modules (procedures and functions)
that he declares in h i s p r o g r a m . . The following conventions
hold :

-- M o d u l e s declared as "extern" or "fortran" get an e n t r y


point name equal to the procedure identifier cut to the
first seven characters.
102

-- L o c a l modules get an entry point name depending on t h e


value of the E-option (at the moment of analyzing the
module name):

E- A u n i q u e s y m b o l is generated by the compiler


E+ The first seven characters of the module name ere
taken.

Whenever the cut module n a m e is t a k e n ( E + a n d " e x t e r n " or


"fortran"), it is t h e p r o g r a m m e r s responsibility to a v o i d t h e
occurrence of duplicate entry point symbols.

default : E-

L controls the listing of the program text.

default = L+

U a l l o w s t h e u s e r to r e s t r i c t t h e n u m b e r of r e l e v a n t characters
in every source line to ?2. T h e r e m a i n d e r o f t h e l i n e is
treated as a comment. With U- the number of relevant
characters is 120. T h e r e s t of t h e l i n e is t h e n t r e a t e d as a
comment.

default = U-

B used to s p e c i f y a T o w e r l i m i t for t h e s i z e of f i l e b u f f e r s .
If after the B a digit d (I<=d<=9) occurs, the bufffer size
S, computed by the compiler, is g u a r a n t e e d t o be S > 1 2 8 . d
words .

default = B4

As the compiler instructions may be w r i t t e n anywhere in t h e


program, it is p o s s i b l e to activate the options selectively over
specific parts of the program.

C. Error messages

0.1 Compiler

The compiler indicates a detected e r r o r by an arrow, pointino to


the relevant place in the text, followed by a n u m b c r , w h i c h
corresponds to the messaDes in A p p e n d i x E .

C.2 Run~ime (Post-Mortem Dump)

When the compiler o p t i o n P is t u r n e d on ( i . e . P + ) , t h e c o m p i l e r


generates code that c a n be u s e d t o p r i n t a r e a d a b l e "dump'" in
the case that a run-time error occurs. The dump includes the
103

following information:

a) the cause of the trap and where it occurred

b) a description of e a c h o f t h e p r o c e d u r e s (functions) that


is activated at t h e t i m e of t h e t r a p . T h e s e a p p e a r in t h e
reverse o r d e r of t h e i r c a l l s a n d c o n s i s t of:
I) t h e n a m e o f t h e p r o c e d u r e
2) t h e l o c a t i o n of its call
3) a list of t h e n a m e s a n d v a l u e s o f t h e l o c a l v a r i a b l e s
and parameters.

c) the values of t h e global variables in the main program.

Only variables and parameters of the types integer, real,


Boolean, char, and alfa. Pointers are either " n i l " or h a v e an
octal value (address). For other scaler variables, the ordinal
number of their current value is p r i n t e d , When, for any one
procedure, the option P is turned off (P-). t h e n o n l y t h e
procedure name and the location of its c a l l a p p e a r in t h e d u m p .

In the case of r e c u r s i v e procedure calls, only the last (most


recent) three occurrences of each procedure are listed,
104

References

1. N. Wirth, ~he ~r_@R~@mina L~u~ ~&cal. Acta


Informatica, 1, 3 5 - 6 3 . 1 9 7 1 .
~ ~roorammi~_~ L~noua~ ~aal (~,~zised
~p_~), Berichte der Fachgruppe
Computer-Wissenschaften ETH Z u r i c h , 5, 1973.

2. "Program Development by Stepwise Refinement",


~omm. ~CM ~ , 2 2 1 - 2 2 7 , April 1 9 7 1 .

3. System~J2~ ~roqrammino, Prentice-Hall, Inc,


1973.

4. O.d. Dahl, E .W. D i j k s t r e , C.A.R. Hoare.


~tructured ~£oorammiQ,&. Academic Press Inc.
1972.

5. C.A .R . H o a r e and N. W i r t h , "An A x i o m a t i c Definition of the


Programming Language Pascal", ~£ta
~nfo~ma~ica, 2, 335-355, 1973.

6, D . E . Knuth. ~ &r_~ ~ ~wmouter ~roa~&~_mino, vol 1


Edwj3~~ &3~gorithms, Addison-Wesley, 196U.
7. SCOPE R e f e r e n c e Manual, CDC 6000 V e r s i o n 4.3.1, Control Data
Corporation, 1973.
Appendix A
Standard ~&edures ~nd ~unctions

File handling procedures

put (f) appends the value of t h e b u f f e r v a r i a b l e f~ t o the


file f, and is applicable only if prior to
execution, e a r ( f ) is t r u e . e o f ( f ) r e m a i n s true, and
f~ b e c o m e s u n d e f i n e d ,

get(f) advances the current file position to the next


component, and assigns the value of this component
to the buffer variable f~ . I f no n e x t c o m p o n e n t
exists, then e o f ( f ) b e c o m e s t r u e , a n d t h e v a l u e of
ft is u n d e f i n e d . Applicable o n l y if e a r ( f ) is f a l s e
p r i o r t o its e x e c u t i o n .

reset (f) resets the current file p o s i t i o n to its b e g i n n i n g


for the purpose of reading, i.e. a s s i g n s to t h e
buffer variable f~ the value of the first element of
f. ear(f) becomes false if f is not empty:
otherwise, f~ is undefined and ear(f) remains true.

rewrite(f) replaces the current v a l u e of f w i t h t h e e m p t y f i l e .


eof(f) becomes true, a n d a new f i l e m a y be w r i t t e n .

page(f) instructs the printer to s k i p to t h e t o p of a n e w


page before printing the next l i n e of t h e textfile
f,

read, readln, write, writeln are applicable on textfiles and are


discussed in chapter 12.

Dynamic allocation procedure

new ( p ) allocates a new v a r i a b l e v and assigns the


pointer reference of v to the Pointer
variable p. If the type of v is a record
type with variants, the form

new(p ,t 1 ..... tn) can be used to a l l o c a t e a v a r i a b l e of t h e


variant with t a g f i e l d v a l u e s t 1 . . , t n . The
tag field values must be listed
contiguously and in the order of t h e i r
declaration.

dispose(p) returns the dynamic variable v that is


referenced by P .

dispose(p ,t I .... ,tn) returns the dynamic variable v t h a t is


referenced by p and was crested by the
second form of new. (Implementations may
106

choose to ignore this statement,)

Data transfer procedures

paek(a,i,z) if a i s an a r r a y variable of type


arrav[m..n] ~ T
and z is a variable of type
aacke~ ~_~Z[u..v] ~ T
w h e r e n-m >= v - ~ , then this is equivalent to

.f_or j := u J~D. v d o z [ j ] := e[j-u+i]

and
u n p a c k (z ,a , i ) is equivalent to

for j :: u to v do a[j-u+i] := z[j]

(In both c a s e s , j d e n o t e s an a u x i l i a r y variable


not o c c u r i n g e l s e w h e r e in t h e p r o g r a m . )

Arithmetic functions

abs ( x ) c o m p u t e s t h e a b s o l u t e v a l u e of x. The t y p e o f t h e r e s u l t
is t h e s a m e as t h a t of x, w h i c h m u s t be e i t h e r i n t e o e or
real.

sqr(x) c o m p u t e s x * x . T h e t y p e of t h e r e s u l t is t h e same as that


of x, w h i c h must be e i t h e r i n t e g e r or r e a l .

sin(x) for the f o l l o w i n g , t h e t y p e o f x m u s t be e i t h e r real or


integer. The t y p e o f t h e r e s u l t is always real.
cos(x)
aretan (x)
exp ( x )
ln(x) (natural logarithm)
sqrt(x) (square root)

Predicates (Boolean functions)

odd ( x ) the t y p e of x m u s t be i n t e g e r ; the result is t r u e if x


is o d d , o t h e r w i s e f a l s e .

eoln(f) returns the v a l u e t r u e w h e n , w h i l e r e a d i n g the t e x t f i l e


f, the end of t h e c u r r e n t line is r e a c h e d ; otherwise,
false.

eof(f) returns the value true when, while reading the file f.
the "end-of-file" is reached; otherwise, false.
107

Transfer functions

trunc(x) x must be of t y p e r e a l ; t h e r e s u l t is t h e g r e a t e s t
integer less than or equal to x for x > = O . a n d t h e
l e a s t i n t e g e r g r e a t e r or e q u a l to x for x < O .

round(x) x must be of t y p e r e a l : t h e r e s u l t , of t y p e integer,


is t h e v a l u e x r o u n d e d .
That is, r o u n d ( x ) = t r u n c ( x + 0 . 5 ) , for x > 0
trunc(x-O.5), for x < 0

ord (x) the ordinal number of the a r g u m e n t x in t h e set of


v a l u e s d e f i n e d by t h e t y p e of x.

chr(x) x must be of type integer, a n d t h e r e s u l t is t h e


c h a r a c t e r w h o s e o r d i n a l n u m b e r is x (if it e x i s t s ) .

Further standard functions

succ(x) x is of any s c a l a r t y p e (except r e a l ) , a n d the result


is t h e s u c c e s s o r v a l u e of x (if it e x i s t s ) .

pred(x) x is of any s c a l a r t y p e ( e x c e p t r e a l ) , a n d t h e result


is t h e predecessor v a l u e of x (if it e x i s t s ) .
108

Appendix B
~ / i v of Operations

go e r a ~ £oeration ~Yoe of # o e r a n d ( s ) KesN![


type

o_ assignment any t y p e e x c e p t ---


file t y p e s

arithmetic:
+ (unary) identity integer or r e a l same as

- (unary) sign inversion operand

+ addition integer or r e a l integer


subtraction or r e a l
multiplication

integer division integer integer


/ real division integer or real real
mad modulus integer integer

relational:
= equality scalar, string.
<> inequality s e t . or p o i n t e r

less t h a n scalar or s t r i n g
greater than
Boolean

<= less or e q u a l scalar or s t r i n g


-or-
set i n c l u s i o n set
>= greater or equal scalar or s t r i n g
-or-
set inclusion set

ia set membership f i r s t o p e r a n d is
any scalar, the
s e c o n d is its s e t
type

logical:
not negation
or disjunction Boolean Boolean
aad conjunction

set:
+ union
set difference any set type T
intersection
109

Appendix C
Yables

A. Table of standard identifiers

Constants :
false , true. maxint

Types:
integer, Boolean, real, char, text

Program parameters:
input, output

Functions:
abs, arctan, chr, cos, eof, eoln. exp, In, odd,
ord, pred, round, sin, sqr, sqrt, succ, trunc

Procedures :
get . n e w , pack. page, put , read, readln, reset ,
rewrite, unpack, write, writeln

B. Table of word-delimiters (reserved words)

m~ aad nil sa~


arraK file not thezl
~a_q~n for of ~o
y.~e ~unctioq ~E tvo~
~OJ3S t _onto o~Gk#d until
div if oroceduns var
do in o r o ~r a m ~bila
dow nt Q ~abe ~ rw_~or d ~ith
eIs e /nod r_e o w2.c~

C. Non-standard, predefined identifiers in PASCAL 6 0 0 0 - 3 . 4

Types:
alfa

Functions:
card, clock, eos, expo, undefined

Procedures:
data, getseg, halt, iinelimit, message, putseg, time
110

Appendix
~vnt ax

~_acku~_-N~_IZ~ F~Lm (RNF)

Note: the following symbols are mete-symbols belonginu to the


BNF formalism, a n d not symbols of t h e l a n o u a g e Pascal.

::= I { !
The curly brackets denote possible repetition of the enclosed
symbols z e r o or m o r e times. In g e n e r a l .

A : :: {B}

is a s h o r t form for the purely recursive rule:

A : := < e m p t y > [ AB

BNF

<program> : := < p r o g r a m heading> <block>

<program heading> ::= ~ o ~ r _ ~ <identifier> ( <file identifier>


{. < f i l e i d e n t i f i e r > } );

<file identifier> : := < i d e n t i f i e r >

<identifier> ::= <letter> {<letter or digit>}

<letter or digit> :'= <letter> ~ <digit>

<block> ::: <label declaration part> <constant definition part>


<type definition part> <variable declaration part>
<procedure and function declaration hart>
<statement part>

<label declaration part> ::= <empty> I


~abel <label> ~, <label>} ;

<label> ::= <unsigned integer>

<constant definition part> ::= <empty> I


c~nst <constant definition> { : <constant definition>} ;

<constant definition> ::= <identifier> = <constant>

<constant> ::: < u n s i g n e d number> I <sign> <unsigned number> I


<constant identifier> I <sign> <constant identifier> I
<strin~q>

<unsigned number> ::= <unsigned integer> I <unsigned real>


111

<unsigned integer> ::= <digit> {<digit>}

<unsigned reel> ::= < u n s i g n e d integer> . <digit> {<digit>} I


<unsigned integer> . <digit> {<digit>} E <scale factor>
<unsigned integer> E <scale factor>

<scale factor> ::= <unsigned integer> I <sign> <unsigned integer>

<sign> ::= + ~ -

<constant identifier> ::= <identifier>

<string> ::= ' <character> {<character>} °

<type definition part> ::-- < e m p t y > I


Jiyoe < t y p e d e f i n i t i o n > { ; <type definition>} ;

<type definition> : := < i d e n t i f i e r > = <type>

<type> ::= <simple type> I <structured type> I <pointer type>

<simple type> ::= < s c a l a r type> ~ <submange type> I


<type identifier>

<scalar type> ::= ( <identifier> { ° <identifier>} )

<subrange type> ::= <constant> .. < c o n s t a n t >

<type identifier> : := < i d e n t i f i e r >

<structured type> ::= <unpacked structured type> I


~Lecked <unpacked structured type>

<unpacked structured type> : := <array type> I <record type> l


<set type> I <file type>

<array type> ::= ~rev [ <index type> {, <index type>} ] of


<component type>

<index type> :'= <simple type>

<component type> : := < t y p e >

<record type> ::= D e c o r d <field list> ~J3~[

<field list> ::= < f i x e d part> ~ <fixed part> ; <variant part> I


<variant part>

<fixed part> ::= <record section> { ; <record section>}

<record section> ::= < f i e l d identifier> {. <field identifier>} :


<type> I <empty>

<variant part> ::= c a s e <tag field> <type identifier> of


<variant> { ; <variant>}
112

<tag field> ::= <field identifier> : ~ <empty>

<variant> ::= <case label list> : ( <field list> ) i <empty>

<case label list> : :-- < c a s e label> {, <case label>}

<case label> : := < c o n s t a n t >

<set type> : := s e t of <base type>

<base type> ::= <simple type>

<file type> ::= file ~ <type>

<pointer type> ::= ~ <type identifier>

<variable declaration part> ::= < e m p t y > I


~&WJ& < v a r i a b l e declaration> { ; <variable declaration>} ;

<variable declaration> : := < i d e n t i f i e r > {. <identifier>} : <type>

<procedure and function declaration part> ::=


{<procedure or function declaration> ;}

<procedure or f u n c t i o n declaration> ::= <procedure declaration> I


<function declaration>

<procedure declaration> ::= <procedure heading> <block>

<procedure heading> ::= & ~ w ~ e d u r e <identifier> ; I


&rocedure <identifier> ( <formal parameter section>
{ ; <formal parameter section>} ) ;

<formal parameter section> ::= <parameter ~rou#> I


va~ <parameter group> I ~unction <parameter group>
&~ocedure <identifier> {. <identifier>}

<parameter group> ::= <identifier> { ° <identifier>} :


<type identifier>

<function declaration> ::= <function heading> <block>

<function heading> ::= f u n c t i o n <identifier> : <result type> ; I


f~tio~ <identifier> ( <formal parameter section>
{ ; <formal parameter section>} ) : <result type> ;

<result type> ::= <type identifier>

<statement part> ::= <compound statement>

<statement> ::= < u n l a b e l l e d statement> I


<label> : <unlabelled statement>

<unlabelled statement> :'= < s i m p l e statement> I


<structured statement>

<simpIe statement> ::= <assignment statement> J


113

<Procedure statement> I <go to statement> I


<empty statement>

<assignment statement> ::= < v a r i a b l e > := < e x p r e s s i o n > I


<function identifier> := < e x p r e s s i o n >

<variable> ::-- < e n t i r e v a r i a b l e > I <component variable> I


<referenced variable>

<entire variable> ::= <variable identifier>

<variable identifier> ::= <identifier>

<component variable> ::= <indexed variable> I <field designator> I


<file buffer>

<indexed variable> ::= < a r r a y variable> [ <exmression>


{ , <expression>} ]

<array variable> : := < v a r i a b l e >

<field designator> ::= <record variable> . <field identifier>

<record variable> ::= <variable>

<field identifier> ::= <identifier>

<file buffer> : := < f i l e variable>

<file variable> ::= <variable>

<referenced variable> : := < p o i n t e r variable>

<pointer variable> ::= <variable>

<expression> ::= < s i m p l e e x p r e s s i o n > ~ <simple expression>


<relational operator> <simple expression>

<relational operator> ::= = I <> I < I <= I >= I > I in

<simple expression> ::= < t e r m > I <sion> <term> 1


<simple expression> <adding operator> <term>

<adding operator> ::= + I - I o~

<term> ::= <factor> I <term> <multiplyino operator> <factor>

<multiplying operator> ::= ~- ! / ~ d ~ I ~L~ I a~d

<factor> ::= < v a r i a b l e > I <unsigned constant> I ( <expressSon> )


<function designator> I <set> I not <factor>

<unsigned constant> ::= < u n s i g n e d number> I <string> I


<constant identifier> I nil

<function desionator> ::= < f u n c t i o n ~dentifier> I


<function identifier> ( <actual parameter>
114

{ , <actual parameter>} )

<function identifier> : := <identifier>

<set> ::= [ <element list> ]

<element list> ::= <element> [ . <element> } I <emmty>

<element> : := < e x m r e s s i o n > J <expression> ,. <expression>

<procedure statement> ::= < p r o c e d u r e identifier> I


<procedure identifier> ( <actual parameter>
{ . <actual parameter>} )

<Procedure identifier> ::= <identifier>

<actual parameter> ::= < e x p r e s s i o n > I <variable> I


<procedure identifier> I <function identifier>

<go to statement> ::= o o t o <label>

<empty statement> ::= <empty>

<empty> : :=

<structured statement> ::= < c o m p o u n d statement> l


<conditional statement> I <repetitive statement> I
<with statement>

<compound statement> ::= b ~ g ~ n <statement> [ ; <statement>} ond

<conditional statement> ::= <if statement> I <case statement>

<if statement> ::= if < e x p r e s s i o n > then <statement> I


if <expression> JZ~J~D < s t a t e m e n t > e3se <statement>

<case statement> : := c a s e <expression> of <case list element>


[ ; <case list element>} ~nd

<case list element> ::= <case label list> : <statement> I


<empty>

<case label list> ::= <case label> {, <case label> }

<repetitive statement> ::= <while statement> J <repeat statement} I


<for statement>

<while statement> ::= w h i l e <expression> do <statement>

<repeat statement> ::--z:e~_@_~JZ < s t a t e m e n t > { ; <statement>}


until <expression>

<for statement> ::= f o r <control variable> := <for list> do


<statement>

<for list> ::= < i n i t i a l v a l u e > t o < f i n a l v a l u e > J


<initial value> dgwnto <final value>
115

<control variable> ::= <identifier>

<initial value> ::= <expression>

<final value> ::= <expression>

<with statement> ::= ~ i t h <recomd variable list> ~ <statement>

<record variable list> ::= <record variable> { . <record variable>}


PASCAL simple type
.[type identifierI
identifier

unsigned integer
type
rm
unslgned number
"@--'Ityp°~

unsigned con~ta~tt O~

~ constant identifier] 1 ~ - ~ ~

field list

consta~l
I ~ G - - -®
F~q I 1.
~©~'ype'°o°','d'@
1
variable
simple expression

expression

oxp .... i o ~ ............


factor ----•simpio
~lu..~..°° ........ I
O©QQ@(~® ,I i
L i ~ ~ I l.[i~pt°oxp .......

parameter list ",4

~-<S i

term
statement

lock

~~-~~
i
0

. . . . . . ] =

rogtawl
119

Appendix E
~:~j~umber ~ummar~

I: e r r o r in s i m p l e t y p e
2: identifier expected
3: "program' expected
4: ")" expected
5: ":" expected
6: illegal symbol
7: e r r o r in p a r a m e t e r l i s t
8: "of" e x p e c t e d
9: "(° e x p e c t e d
10: e r r o r in t y p e
11: "[" e x p e c t e d
12: '] " e x p e c t e d
13: "end" e x p e c t e d
14: ";" e x p e c t e d
15: integer expected
16: "=" e x p e c t e d
I?: "begin" expected
18: e r r o r in d e c l a r a t i o n p a r t
19: e r r o r in f i e l d - l i s t
20: ' " expected
21: "~" expected

50: error in constant


51: ":=" expected
52: "then" expected
53: "until" expected
54: "do" expected
55: 'to'/'downte ° expected
56: "if" e x p e c t e d
57: "file" e x p e c t e d
58: e r r o r in f a c t o r
59: e r r o r in v a r i a b l e

101: identifier declared twice


102: low b o u n d e x c e e d s h i g h b o u n d
103: i d e n t i f i e r is not of a p p r o p r i a t e c l a s s
104: i d e n t i f i e r not d e c l a r e d
105: s i g n not a l l o w e d
106: number expected
I07: incompatible subran~e types
108: file not a l l o w e d h e r e
I09: t y p e m u s t not be r e a l
110: t a g f i e l d t y p e must be s c a l a r or s u b r a n g e
111: incompatible with tagfield type
112: i n d e x t y p e must not be r e a l
113: i n d e x t y p e must be s c a l a r or s u b r a n g e
114: b a s e t y p e must not be r e a l
115: b a s e t y p e must be s c a l e r or s u b r a n o e
116: e r r o r in t y p e of s t a n d a r d p r o c e d u r e p a r a m e t e r
117: unsatisfied forward reference
120

118: f o r w a r d r e f e r e n c e t y p e i d e n t i f i e r in v a r i a b l e d e c l a r a t i o n
119: f o r w a r d d e c l a r e d : r e p e t i t i o n of p a r a m e t e r list not a l l o w e d
120: f u n c t i o n r e s u l t t y p e must be s c a l a r , s u b r a n g e or p o i n t e r
121: file v a l u e p a r a m e t e r not a l l o w e d
122: f o r w a r d d e c l a r e d f u n c t i o n ; r e p e t i t i o n of r e s u l t t y p e not
allowed
123: m i s s i n g r e s u l t t y p e in f u n c t i o n d e c l a r a t i o n
124: F - f o r m a t for r e a l o n l y
125: e r r o r in t y p e of s t a n d a r d f u n c t i o n p a r a m e t e r
126: n u m b e r of p a r a m e t e r s does not a g r e e w i t h d e c l a r a t i o n
127: illegal parameter substitution
128: r e s u l t t y p e of p a r a m e t e r f u n c t i o n does not a g r e e w i t h
declaration
129: t y p e c o n f l i c t of o p e r a n d s
130: e x p r e s s i o n is not of set t y p e
131: t e s t s on e q u a l i t y a l l o w e d o n l y
132: s t r i c t i n c l u s i o n not a l l o w e d
133:. file c o m p a r i s o n not a l l o w e d
134: i l l e g a l t y p e of o p e r a n d ( s )
135: t y p e of o p e r a n d m u s t be B o o l e a n
136: set e l e m e n t t y p e m u s t be s c a l a r or s u b r e n g e
137: set e l e m e n t t y p e s not c o m p a t i b l e
138: t y p e of v a r i a b l e is not a r r a y
139: i n d e x t y p e is not c o m p a t i b l e w i t h d e c l a r a t i o n
140: t y p e of v a r i a b l e is not r e c o r d
141: t y p e of v a r i a b l e m u s t be file or p o i n t e r
142: illegal parameter substitution
143: i l l e g a l t y p e of loop c o n t r o l v a r i a b l e
144: i l l e g a l t y p e of e x p r e s s i o n
145: type conflict
146: a s s i g n m e n t of f i l e s not a l l o w e d
147: label type incompatible with selecting expression
148: s u b r a n g e bounds m u s t be s c a l a r
149: i n d e x t y p e must not be i n t e g e r
150: a s s i g n m e n t to s t a n d a r d f u n c t i o n is not a l l o w e d
151: a s s i g n m e n t to f o r m a l f u n c t i o n is not a l l o w e d
152: no s u c h f i e l d in t h i s r e c o r d
153: t y p e e r r o r in r e a d
154: a c t u a l p a r a m e t e r must be a v a r i a b l e
155: c o n t r o l v a r i a b l e m u s t not be f o r m a l
156: m u l t i d e f i n e d case l a b e l
157: too m a n y cases in c a s e s t a t e m e n t
158: missing corresponding variant declaration
159: r e a l or s t r i n g t a g f i e l d s not a l l o w e d
160: p r e v i o u s d e c l a r a t i o n w a s not f o r w a r d
161: again forward declared
162: p a r a m e t e r s i z e must be c o n s t a n t
163: m i s s i n g v a r i a n t in d e c l a r a t i o n
164: s u b s t i t u t i o n of s t a n d a r d p r o c / f u n c not a l l o w e d
165: multidefined label
166: mu!tideclared label
167: u n d e c l a r e d label
168: undefined label
169: e r r o r in b a s e set
170: value parameter expected
171: s t a n d a r d file was r e d e c l a r e d
172: undeclared external file
121

173: F o r t r a n p r o c e d u r e or f u n c t i o n e x p e c t e d
174: P a s c a l p r o c e d u r e or f u n c t i o n e x p e c t e d
175: m i s s i n g f i l e " i n p u t " in p r o g r a m h e a d i n g
176: m i s s i n g f i l e " o u t p u t " in p r o g r a m h e a d i n g

201: e r r o r in r e a l c o n s t a n t : digit expected


202: s t r i n g c o n s t a n t m u s t not e x c e e d s o u r c e line
203: integer constant exceeds range
204: 8 or 9 in o c t a l n u m b e r

250: too m a n y n e s t e d s c o p e s of i d e n t i f i e r s
251: too many nested procedures and/or functions
252: too many forward references of p r o c e d u r e entries
253: procedure too long
254: t o o m a n y long c o n s t a n t s in t h i s p r o c e d u r e
255: t o o m a n y e r r o r s on t h i s s o u r c e line
256: too many external references
257: too many externals
258: too many local files
259: expression too c o m p l i c a t e d

300: d i v i s i o n by z e r o
301: no c a s e p r o v i d e d for t h i s v a l u e
302: i n d e x e x p r e s s i o n out of b o u n d s
303: v a l u e to be a s s i g n e d is out of b o u n d s
304: e l e m e n t e x p r e s s i o n o u t of r a n g e

398: implementation restriction


399: variable dimension arrays not implemented
122

Appendix F
~roorammin~ ~amo~&

[ PASCAL 6000-3.4 }

~rocedure rdr(~Aar ?: t e x t ; ~A~r x: r e a l ) ;


{ r e a d r e a l n u m b e r s in " f r e e f o r m a t ' }
~onst t48 = 281494976910656;
limit = 56294995342131 ;
z = 27; { ord('O °) }
l i m l = 322; { maximum exponent }
lim2 = -292; { minimum exponent }
tvme posint = 0..323;
v a ~ c h : c h a r : y: r e a l ; a , i , e : integer:
s,ss: boolean: { signs }

function ten(e: posint): real; { = lO**e, 0<e<322 i


~ a r i: i n t e g e r ; t : real;
~agln i := O; t := 1 . 0 ;
~#~ea& ~ odd(e) ~hen
~ a i af
O: t := t * 1 . 0 e l ;
1: t := t * 1 . 0 e 2 :
2: t := t * 1 . 0 e 4 ;
3: t := t * 1 . 0 e 8 ;
4: t := t * 1.0e16;
5: t := t * 1 . 0 e 3 2 ;
6: t := t * 1.0e64;
9: t := t * 1.0e128;
8: t := t * 1,0e256
and :
e := e ~ i v 2; i := i+I
~ e = O;
ten := t
en~ ;

~eein
if eof(f) ~hen
be~in message ( "**tried to read past eos/eo?');
halt
end ;
{skip leading blanks}
while ( f t = • °) ~O_~ (not eof(f)) do get(f);
nDJl e o f ( f ) t h e n
begin
ch := ft ;
if ch = - ~hen
benin s := t r u e : get(f); ch := ft
and e l ~
b e o i n s := f a l s e ;
if eh = "+ ° t h e n
]~egi6 get (f): ch :-- f~
and
end :
123

i ~ i ~ o t (ch i n [ "0 ° . . ' 9 °] ) then


be~io message(°~*digi t expected'); halt;
end ;
a := O; e := O:
redeemS_ i f a < limit then a := l O ~ a + ord(ch)-z
else e := e + 1 ;
g e t (f); ch := ft
un£il not(ch ia ['0'..'9']),
if_ ch = ' . * J~heo
beoin { read fraction } get(f); ch := ff ;
Kbite eh i n [ °0°.. °9'] do
D~oin i f a < l i m i t tl3wJ&
b e a i n a := 1 0 ~ a + e r d ( e h ) - z ; e := e-1
end ;
get(f); ch := f T
end
ead ;
i £ c h = "e" t h e n
be~in { read scale factor } get(f): ch := fT ;
i := 0;
if ch = "-" thell
be-qill s s := t r u e ; get(f); c h := f~
and ela~
b a a / / & s s := f a l s e ; i f c h = "+" ~ a ~
h a ~ i z l g e t (f): c h := fT
and
end ;
~hil_~ ch in [ "0'..'9"] do
bg__qin i f i < l i m i t then
bemin i := I 0 - i + o r d ( c h ) - z e13~;
~ e t ( f ) ; ch := ft
and ;
i f s s i h a n e := e - i a ~ e := e + i ;
and ;
i f e < lira2 ~h@q
beqin a := O: e := 0
end ~lse
i f e > l i m l the[l
be&Lie m e s s a z e ( ' ~ ' ~ n u m b e r t o o l a r g e ' ) ; halt end;
{ 0 < a < 2"~'-49 }
i f a > = t q U ~ h e n y := ( ( a + 1 ) d i v 2) ~ 2 . 0
e l s e y := a ;
i f s t h e n y := - y :
e < 0 t h e n x := y / t e n ( - e )
i_f e < > 0 t_h2Jl x := y ~ t e n ( e ) e l s e x := y ;
and ;
end ;
124

[ PASCAL 6000-3.4 }

~racedure wre(var f:text; x: reel; n: i n t e g e r ) ;


{ w r i t e r e a l n u m b e r x in n c h a r a c t e r s }
~st t48 = 281474976710656; [ 2**48 }
realt48 = 2,81474976710656e14;
z = 27: { ord('O') }
tYPe posint : 0..323;
c,d.e,eO,el,e2,i: integer;

~WJ3~tio~ ten(e: posint): real; { 10**e. 0<e<322 }


~ i: i n t e g e r ; t : real;
~ i : : O; t : = 1 . 0 ;
~eoBat i ~ o d d ( e ) ~hg~_~
~ a s e i ~_[
O: t := t * 1 . 0 e l ;
I: t := t * 1 . 0 e 2 :
2: t := t * 1 . 0 e 4 ;
3: t := t * 1 . 0 e 8 ;
4: t :: t * 1.0e16;
5: t := t * 1 . 0 e 3 2 ;
6: t := t * 1 . 0 e 6 4 ;
7: t := t * 1 . 0 e 1 2 8 ;
8: t := t * 1 . 0 e 2 5 6
end ;
e := e ~J~3L 2; i := i+I
~ntil e : O;
t e n := t
end { ten } ;

bF..g.iZl { at l e a s t 10 c h a r a c t e r s needed: b+9,9e+999 }


if u n d e f i n e d ( x ) then
b e a i n f o r i : = 1 t ~ n - 1 d ~ b e a i n ft ::" "; p u t ( f ) end;
ft :='u °; p u t ( f )
end al~a
i f x =0 t h ~ a
h ~ ~Zhil: n > I W o
bg~w.in ft := " "; p u t ( f ) ; n : = n-1
end ;
ft : = ° 0 ° ; p u t ( f )

baaia
if n <= 10 ~ h e n n : = 3 eZ~_~ n : = n - 7 ;
react f~ := ° °; put(f); n := n-1
until n <: 14:
{ 2 <: n <: 14 : n u m b e r of d i g i t s t o be p r i n t e d }
beqin { test sign, then obtain exponent }
iJ[ x < 0 t h e n
• • • •
b e a i n ft .= - 0 put(f); x := -x
OD~ e l s e b~_q~n ft :: " "; ~ut(f) end;
e :: expo(x) * 7 7 d~AL 2 5 6 ; { "1og(2) }
if e >: 0 then
~eq!n e :: e+1; x := x/ten(e)
e n d e l s e x := x * t e n ( - e ) ;
w h i l e x < 0,I d o
b~_qin x := 1 0 . O - x ; e := e-1
end ;
125

{ 0 . 1 <= x < 1.0 }


case n o£ { rounding }
2: x := x+O.5e-2;
3: x := x+O.5e-3;
4 : x :-- x + O . 5 e - 4 ;
5: x := x + O . 5 e - 5 ;
6: x := x + O . S e - 6 ;
?: x := x + O . S e - ? ;
U: x := x + O . 5 e - 8 ;
9: x := x + O . S e - 9 ;
1 0 : x := x + O . S e - l O ;
11: x := x+O.Se-11;
12: x := x+O.5e-12;
13: x := x+O.5e-t3;
14: x := x+O.5e-14
e~ ;
if x >= 1.0 ~hen
pekin x := x * 0 . 1 ; e := e + 1
end ;
c := trunc(x~eait48);
c := 10-c; d := c d~v t48;
ff := chr(d+z); put(f);
ff := " "; put(f);
f o ~ i := 2 J&~ n d o
begLn c := ( c " - d . t 4 8 ) * 10; d := c d ~ v t 4 8 ;
f~ := c h r ( d + z ) ; put(f)
end ;
f~ := "e'; put(f); e := e - l ;
i£ e < 0 then
beoin f~ •. = " - : p u t ( f ) - ~ -= -~;
end else beoin ft := ~ "+ ; put(f) ~3d;
el := e * 205 div 2048; e2 := e - 10{-el;
eO : = e l * 2 0 5 d&3L 2 0 4 8 7 et := el - lO~eO;
ff := chr(eO+z); put(f),
ff := chr(el+z); put(f);
ff := chr(e2+z); put(f)
end
end
=rid { wr } ;
~NOEX

When a reference in this index is not a section name ( e . g .


Appendix A), then the #e£erence may be o f t h e f o l l o w i n g forms:

xl xl.x2 xl.x2.x3

xl is always the chapter n u m b e r , x 2 may b e a c a p i t a l letter in


which case i t may be f o l l o w e d by x3, a number, and refer~s to s
chapter section. When x 2 i s a s m a l l letter, the reference is a
figure; when x2 is a number, the reference is a program.

alfa (PASCAL 6 0 0 0 - 3 . 4 ) 13.D.1


array types 6
assignment statement 4.A
binary tree 11.A
block 0
BNF d e f i n i t i o n s Appendix D
Boolean 2.A
case statement 4.D.2
char 2.D
character sets 13.8.3
comment 1
compound statement 4.R
compile# error messages 14.C . 1 , Appendix E
compiler options (PASCAL 6 0 0 0 - 3 . 4 ) 13.B
conditional statements 4.D
constant declaration part 3.C
control statements (PASCAL 6 0 0 0 - 3 , 4 ) 14.A
control variable 4.C.3
data types 2
declaration part 3
empty statement 4.B
equivalence 2.A
expression 4.A
i d e n t i f i e r s , table of s t a n d a r d Appendix C
if s t a t e m e n t 4,D.I
implication 2.A
input 9.B
integer 2.B
I /0 12
field list ?
figures
after (list insertion) I0.C
a l t e r n a t i v e r e p r e s e n t a t i o n of standard symhols 13 .b
ASCII character set (with CDC's ordering) 13.a
before (list insertion) 10.b
block s t r u c t u r e O.b
binary tree structure 11.b
CDC s c i e n t i f i c character set (with 64 elements) 13 .a
expressions 11.a
identifier 1.a
linked list 10.a
syntax diagram of program structure O.a
127

two s a m p l e people 7.a


u n s i g n e d number 1.b
file types 9
e x t e r n a l files (PASCAL 0000-3.4) 13.B .1
s e q m e n t e d files (PASCAL 6 0 0 0 - 3 . 4 ) 13.A .1
r e p r e s e n t a t i o n in PASCAL 6 0 0 0 - 3 . 4 13 .D .2
textfiles 9.A
for s t a t e m e n t 4.C.3
forward r e f e r e n c e 11.C
functions 11.B
d e c l a r a t i o n part 3.F
designator 11,D
heading 11,B
p r e d e f i n e d (PASCAL 6000-3.4) 13.D.2)
s t a n d a r d , table of Appendix A
global v a r i a b l e s 11.A
goto s t a t e m e n t 4.E
labels
case 7.A
declaration part 3.B
goto 3 . B . 4~E
lists (linked) 10
local v a r i a b l e s 11.A
name p r e c e d e n c e 11.A
notation I
numbers I
operator p r e c e d e n c e 18,A
o p e r a t o r s , s u m m a r y of Appendix B
output 9.B
Pecked structures 6
parameters 11.A
PASCAL 6 0 0 0 - 3 . 4 13, 14
pointer types 10
procedures 11.A
d e c l a r a t i o n part 3.F
external p r o c e d u r e s (PASCAL 6000-3.4) 13 .A .2
heading 11.A
p r e d e f i n e d (PASCAL 6 0 0 0 - 3 . 4 ) 13.D.2
s t a n d a r d , table of Appendix A
p r o c e d u r e statement 11.A
programs and p r o g r a m parts
beginend 4.1
bisect 11.6
complex 7.1
convert 3.1
cosine 4.5
egalfa 13.1
egfor 4.4
egrepeat 4,3
egwhile 4.2
exponentiation 4.8
expon2 11.8
examples of goto 4.E
forward reference 11,C
frequency count 9.1
graph1 4.9
graph2 6.2
128

inflation 0.1
insert 9.2
matrixmul 6.3
minmax 6 .I
minmax2 11 ,I
minmax3 11 .2
parameters 11.3
primes 8.2
pointers, construction via 10
merge two files 9
postfix 11 .4
recursivegcd 1 1 .9
roman 4 .?
setop 8.1
sideffect 11 . 7
sum f i l e of real numbers 9
summing 4.6
tree traversal 11.5
program heading 3.A
(PASCAL 6 0 0 0 - 3 . 4 13.B.1)
record types ?
repet ire statements 4.C
reserved words--see word-delimiters
restrictions (PASCAL 6 0 0 0 - 3 . 4 ) 13.C
read. the standard procedure 12.A
real 2 .C
relational operator 2.A, 4 . A
repeat statement 4.0.2
run-time error messages 14.C .2
scalar types 5.A
schemata
read a text 9.A
read a text from "input" 9.A
reading arbitrary number of numerical
items from a textfile
reading a segmented file 1 3 . A .1
reed and write a se£;mented file 13.A.1
write a text 9.A
write a text x to y 9.A
write a text onto "output" 9.A
w r i t e a s e g m e n t e d file 13,A.I
scope O
separators I
set o p e r a t o r s 8
set t y p e s 8
side effect 11,B
standard identifiers Appendix C
string 1 6
subrange types 5,B
syntax diagrams Appendix D
tables
block structure 0
d e f a u l t v a l u e for f i e l d w i d t h 13.B.4
o p e r a t i o n s on t e x t f i l e s 9.A
printer control characters 9.B, 13.B .4
special symbols I
truth values 2.A
129

type--also see d a t a t y p e s
declaration part 3.D
predefined (PASCAL 6 0 0 0 - 3 . 4 ) 13 °D .1
s t a n d a r d (PASCAL 6 0 0 0 - 3 . 4 ) 13 .B .3
variable d e c l a r a t i o n part 3.E
vocabulary I
while s t a t e m e n t 4.C.I
with s t a t e m e n t 7.A
w o r d - d e l i m i t e r s , table of Appendix C
write, the s t a n d a r d p r o c e d u r e 12.B
(PASCAL 6000-3.4 13.8.4)
REPORT
~reface to t~e Revised Repot&

The language PASCAL has now been in use for several years,
during which considerable experience has been gained through its
use, its teaching, and its implementation. Although many r e a s o n s
suggest that a language should be k e p t u n c h a n g e d as s o o n as i t
has gained a user community, it w o u l d be u n w i s e to ignore this
experience and to refrain from making good use of it. This
Report therefore describes a revised language which included
some changes suggested by t h e w o r k o f t h e l a s t t w o y e a r s . It is
still of the form of the original definition, a n d in f a c t t h e
changes are very few and relatively minor. They concern the
following subjects :

- Constant parameters are replaced by value parameters (in the


sense of Algol 60).

- The class structure is eliminated: pointer variables are


bound to a data type instead of a c l a s s v a r i a b l e .

- The handling of files is changed such that the buffer


variable f~ always has a defined value except when t h e
condition e o f ( f ) is t r u e .

- Packed records end packed arrays are introduced. As a


consequence, the type ella becomes a special c~se of a
packed character array. The generalization has some
consequences on the denotation o£ s t r i n g s (formely called
alfa constants).

- Programs require a program heading with external files as


parameters.

- All labels require a declaration.

Moreover. there are a few minor syntactic changes, such as the


renaming of the powerset structure to set structure.

Implementation efforts on various computers have brought the


problem of portability and machine independence of s o f t w a r e
systems to our closer attention. Nlany of t h e a b o v e m e n t i o n e d
changes, and also some additional restrictions, were adopted and
imposed in the interest of program portability and machine
independent definability. T h e y m a d e ~t p o s s i b l e to define almost
the entire language by e set of abstract a x i o m s a n d r u l e s of
inference. Such a rigorous definition is n e c e s s a r y to be a b l e to
prove properties of proorams. This rigour and machine
independence has n o t a b l y b e e n a c h i e v e d without sacrifice in t h e
efficiency of program execution.

In o r d e r to p r o v i d e a c o m m o n b a s e f o r i m p l e m e n t a t i o n s on v a r i o u s
computers, the language defined in t h i s R e v i s e d R e p o r t is c a l l e d
~tand&r~ ~E~j&A~, This standard is d e f i n e d in t e r m s of t h e I S O
character set.

The chapter on PASCAL f o r the CDC 6 0 0 0 computer has been removed


134

from the Report and r e p l a c e d by a general chapter on s u g g e s t e d


standards for implementation and program interchange. This
standard specifies ways to represent programs in t e r m s of
a v a i l a b l e c h a r a c t e r s e t s , a n d l i s t s a n u m b e r of r e s t r i c t i o n s on
the language with the i n t e n t of s i m p l i f y i n g implementations.
Programs to be used on several computers where P A S C A L is
a v a i l a b l e s h o u l d a d h e r e to t h i s s t a n d a r d .

The two procedures Kead and ~rite together with the new
procedures readln, w r i t e l n , a n d eoln h a v e b e e n i n c l u d e d in t h e
set of standard procedures a n d a r e d e s c r i b e d in a new C h a p t e r
12. T h e y n o w c o n s t i t u t e a b i n d i n g s t a n d a r d for l e g i b l e input a n d
output. T h e l a t t e r t h r e e a r e u s e d to c o n t r o l t h e l i n e s t r u c t u r e
of text f i l e s , a n d h a v e b e c o m e n e c e s s a r y , b e c a u s e the S t a n d a r d
language cannot depend on the existence of a ~ine control
charccter (eol) in t h e c h a r a c t e r s e t .

~efereoces

N. W i r t h , "The Programming Language PASCAL". ACTA


INFORMATICA, 1. 3 5 - 6 3 , (1971)o

"Systematisches Programmieren", Teubner


Verlag, Stuttgart. 1972.

"Systematic Prooramming", Prentice-Hall,


Englewood Cliffs, 1973.

C .A .R . Hoare and N . Wlrth, "An Axiomatic Definition of t h e


Programming Language PASCAL", ACTA
INFORMATICA, 2, 335-355. (1973).

N. Wirth. "The Design o£ a PASCAL Compiler",


SOFTWARE-Practice and Experience, !, 309-333
(1971).
135

Contents

I. I n t r o d u c t i o n

2. Summary of the language

3. Notation, terminology, and vocabulary

4. I d e n t i f i e r s , Numbers, and Strings

5. Constant definitions

6. Data type definitions


6.1. Simple types
6.2. Structured types
6.3. Pointer types

7. D e c l a r a t i o n s and d e n o t a t i o n s of v a r i a b l e s
7.1. E n t i r e variables
7.2. Component v a r i a b l e s
7.3. Referenced v a r i a b I e s

8. E x p r e s s i o n s
8.1. Operators
U.2. F u n c t i o n d e s i g n a t o r s

9. S t a t e m e n t s
9.1. Simple statements
9.2. Structured s t a t e m e n t s

10. Procedure declarations


10.1. Standard procedures

11, Function declarations


11.1 Standard functions

12. Input and Output

13. Programs

14. A s t a n d a r d for i m p l e m e n t a t i o n and


p r o g r a m interehanoe

15. I n d e x
136

I. I n t r o d u c t i o n

The development of the l a n g u a g e ~ a s c a l is b a s e d on t w o p r i n c i p a l


aims. The first is t w o m a k e a v a i l a b l e a l a n g u a g e s u i t a b l e to
teach programming as a systematic discipline b a s e d on c e r t a i n
fundamental concepts clearly and naturally reflected by t h e
language. The second is to develop implementations of t h i s
language which are both reliable a n d e f f i c i e n t on p r e s e n t l y
available computers.

The desire for a new language for t h e p u r p o s e of t e a c h i n g


programming is due to my d i s s a t i s f a c t i o n with the present]y used
major languages w h o s e f e a t u r e s and c o n s t r u c t s too often cannot
be e x p l a i n e d logically and convincingly a n d w h i c h t o o o f t e n defy
systematic reasoning. Along with this dissatisfaction goes my
conviction that the l a n g u a g e in w h i c h t h e s t u d e n t is t a u g h t to
express his ideas profoundly influences his h a b i t s of t h o u g h t
and invention, and that the disorder governing these languaRes
directly imposes itself onto the programming style of t h e
students.

There is of course plenty of r e a s o n to be c a u t i o u s w i t h t h e


introduction of yet another programming language, and the
objection against teaching programming in a l a n g u a g e w h i c h is
not widely used and accepted has undoubtedly some
justification, at least based on short term commercial
reasoning. H o w e v e r , t h e c h o i c e of a l a n g u a g e for t e a c h i n g b a s e d
on its w i d e s p r e a d acceptance and availability, together with the
fact t h a t t h e l a n g u a g e m o s t w i d e Z y t a u g h t is t h e r e a f t e r g o i n g to
be the one most widely used, forms the s a f e s t r e c i p e for
stagnation in a s u b j e c t of s u c h p r o f o u n d p e d a g o g i c a l influence.
I consider it therefore well worth-while to m a k e an e f f o r t to
break this vicious circle,

Of course a new l a n g u a g e s h o u l d not be d e v e l o p e d just for t h e


sake of novelty; e x i s t i n g l a n g u a g e s s h o u l d be u s e d as a b a s i s
for d e v e l o p m e n t w h e r e v e r t h e y m e e t t h e c r i t e r i a m e n t i o n e d a n d do
not impede a systematic structure. In t h a t s e n s e A l g o l 60 w a s
used as a basis for P a s c a l , s i n c e it m e e t s t h e d e m a n d s w i t h
respect to teaching to a much h i g h e r d e g r e e t h a n any o t h e r
standard language. Thus the principles of s t r u c t u r i n g , a n d in
fact t h e f o r m of e x p r e s s i o n s , a r e c o p i e d f r o m A l g o l 60, It w a s ,
however not d e e m e d a p p r o p r i a t e to a d o p t A l g o l 60 as a s u b s e t of
Pascal: certain construction princ~p!e~.particulerly t h o s e of
declarations, w o u l d have b e e n i n c o m p a t i b l e with those allowin~ a
natural and convenient representation of t h e a d d i t i o n a l features
of P a s c a l ,

The main extensions relative to A l g o l 60 lie in t h e d o m a i n of


data structuring facilities, s i n c e t h e i r l a c k in A l g o l 60 was
considered as t h e p r i m e c a u s e for its r e l a t i v e l y n a r r o w r a n g e of
applicabil~ty. The introduction of r e c o r d and f i l e s t r u c t u r e s
should make it p o s s i b l e to s o l v e c o m m e r c i a l t y p e p r o b l e m s w i t h
Pascal, or at least to e m p l o y it s u c c e s s f u l l y to d e m o n s t r a t e
such problems in a p r o g r a m m i n g course.
137

2. Summary of the language

An algorithm or computer program consists of t w o e s s e n t i a l


parts, a description o£ ~tiw_o_& which are to be performed, and e
description of the #ata, which are manipulated by these actions.
Actions are described by so-called &~q~_Ements, and data are
described by so-called ~clara~on~ and ~#finitions.

The data are represented by values of variables. Every variable


occurring in a statement must be introduced by a ~ a r i a b l ~
~ ~ o n which associates an i d e n t i f i e r and a data type with
that variable. The ~at& ~voe essentially defines t h e s e t of
v a l u e s w h i c h m a y be a s s u m e d by t h a t v a r i a b l e . A d a t a t y p e m a y in
P a s c a l be e i t h e r d i r e c t l y described in t h e v a r i a b l e declaration,
or it m a y be r e f e r e n c e d by a t y p e i d e n t i f i e r , in w h i c h c a s e t h i s
identifier m u s t be d e s c r i b e d by an e x p l i c i t ~ v o e d e f i n i t i o n .

The basic data types are the &calar types. Their definition
indicates an o r d e r e d s e t of v a l u e s , i.e. introduces identifiers
standing for each value in t h e s e t . A p a r t f r o m t h e d e f i n a b l e
scalar types, there exist four ~tandar~ #asi& ~voes : ~oolean,
integer, ghar, and ~ealo Except for t h e t y p e B o o l e a n , their
values are not d e n o t e d by i d e n t i f i e r s , but instead by n u m b e r s
and quotations respectively. These are syntactically distinct
from identifiers. The set of values of type char is t h e
character set available on a p a r t i c u l a r installation.

A type may also be d e f i n e d as a subranoe of a scalar type by


indicating the smallest and the largest value of the subrange.

~truc[ured ty_&es are defined by d e s c r i b i n g t h e t y p e s of t h e i r


components and by i n d i c a t i n g a ~tructuriQ&__metho~. The various
structuring methods d i f f e r in t h e s e l e c t i o n mechanism s e r v i n g to
select the components of a v a r i a b l e of t h e s t r u c t u r e d t y p e . In
Pascal, there are four basic structuring methods available:
array structure, record structure, set structure, and file
structure.

In an ~_~av ~tructure, all components are of the same type. A


component is s e l e c t e d by an a r r a y s e l e c t o r , or ~ U ~ m u t a b l e i~!,
whose type is i n d i c a t e d in t h e a r r a y t y p e d e f i n i t i o n and which
must be s c a l a r . It is u s u a l l y a programmer-defined scalar type,
or a subrange of the type integer. Given a value of the index
type, an array selector yields a v a l u e of t h e c o m p o n e n t type.
Every array variable can therefore be r e g a r d e d as a m a p p i n g o f
the index type onto the component t y p e . T h e t i m e n e e d e d for a
selection d o e s not d e p e n d on t h e v a l u e of t h e s e l e c t o r (index).
The array structure is therefore celled a ~ando~-~£~_~ess
~Jj2u c t ur_~.

In a ~#_¢_~d s ~ _ ~ u c t u r e , the components (called ~ield&) are not


necessarily of the same type. In order t h a t t h e t y p e of a
selected component be evident from the program text (without
executing the program), a record selector is n o t a c o m p u t a b l e
value, but instead is an identifier uniquely denoting the
component to be selected. These component identifiers are
138

declared in the record type definition. Again, the time needed


to access a selected component does not d e p e n d on t h e s e l e c t o r ,
a n d t h e r e c o r d is t h e r e f o r e a l s o a r a n d o m - a c c e s s structure.

A record type may be specified as consisting of several


~ariaots. This i m p l i e s t h a t d i f f e r e n t v a r i a b l e s , althought said
to be of t h e s a m e t y p e . may a s s u m e s t r u c t u r e s w h i c h d i f f e r in a
c e r t a i n m a n n e r . T h e d i f f e r e n c e m a y c o n s i s t of a d i f f e r e n t n u m b e r
and d i f f e r e n t t y p e s of c o m p o n e n t s . The v a r i a n t w h i c h is a s s u m e d
by t h e c u r r e n t v a l u e of a r e c o r d v a r i a b l e m a y be i n d i c a t e d by a
component field which is c o m m o n to a l l v a r i a n t s a n d is c a l l e d
the ~aofie~. Usually. the p a r t c o m m o n t o all v a r i a n t s w i l l
c o n s i s t of s e v a r a l c o m p o n e n t s , including the tag field.

A ~ u c t u r e defines t h e s e t of v a l u e s w h i c h is t h e p o w e r s e t
of its b a s e t y p e . i.e. t h e s e t of a l l s u b s e t s of v a l u e s of t h e
base type. The base type m u s t be a s c a l a r t y p e . a n d w i l l u s u a l l y
be a programmer-defined s c a l a r t y p e or a s u b r a n g e of the t y p e
integer.

A ~ile_~ruc~_W~ is a ~W_g3&~O~e of c o m p o n e n t s of t h e s a m e t y p e . A
natural ordering of the components is defined t h r o u g h the
sequence. At any instance, only one component is d i r e c t l y
accessible. The other components are made accessible by
progressing sequentially t h r o u g h t h e f i l e . A f i l e is g e n e r a t e d
by sequentially a p p e n d i n g c o m p o n e n t s at its e n d . C o n s e q u e n t l y .
the file type definition does not determine t h e n u m b e r of
components,

Variables declared in e x p l i c i t d e c l a r a t i o n s are called ~atic,


The d e c l a r a t i o n a s s o c i a t e s an i d e n t i f i e r w i t h t h e v a r i a b l e w h i c h
is u s e d to r e f e r to t h e v a r i a b l e . In e o n s t r a t , v a r i a b l e s may be
generated by an e x e c u t a b l e s t a t e m e n t . Such a ~ m i ~ generation
yields a so-called ooi~te~ (a subtstitute for an e x p l i c i t
identifier) which subsequently s e r v e s to r e f e r to t h e v a r i a b l e .
This pointer may be assigned to other variables, namely
variables of type oointer. Every pointer variable may assume
values p o i n t i n g to v a r i a b l e s of t h e s a m e t y p e T o n l y , a n d it is
said to be h~d to t h i s t y p e T. It m a y , h o w e v e r , a l s o a s s u m e
the value ~ . which points to no v a r i a b l e . B e c a u s e p o i n t e r
variables may a l s o o c c u r as c o m p o n e n t s of s t r u c t u r e d variables,
which are themselves dynamically # e n e r a t e d , t h e u s e of p o i n t e r s
permits the representation of f i n i t e B r a p h s in full g e n e r a l i t y .

The most fundamental statement is the ~ i ~ m e n t _ ~ a t e m e n t . It


specifies t h a t a n e w l y c o m p u t e d v a l u e be a s s i g n e d to a v a r i a b l e
(or components of a variable). The value is obtained by
evaluating an ~xoressio~. Expressions consist of v a r i a b l e s ,
constants, sets, operators and functions operating on the
denoted quantities and producing new values. Variables,
constants, and f u n c t i o n s a r e e i t h e r d e c l a r e d in t h e p r o g r a m or
are standard e n t i t i e s . P a s c a l d e f i n e s a f i x e d s e t of o p e r a t o r s ,
each of w h i c h can be r e g a r d e d as d e s c r i b i n g a m a p p i n g from t h e
operand types into the result t y p e . The set of o p e r a t o r s is
subdivided i n t o g r o u p s of

I. ~r_~t hM @~ ie operators of addition, subtraction, sign


139

inversion, multiplication, division, and computinc the


remainder.

2. ~olean operators of negation, union (or), and conjunction


(and).

3. ~et ~oerators of u n i o n , intersection, and set difference.

4. ~e!atiQ~L~ Doerato~s of e q u a l i t y , i n e q u a l i t y , ordering, set


membership and set inclusion. The results of r e l a t i o n a l
operations are of t y p e ~ o o l e a ~ .

The & r o ~ ~ ~ causes the execution of the designated


procedure (see below). Assignment and procedure statements are
the components or building blocks of ~ u c t u r e d ~tatemeots,
which specify sequential, selective, or r e p e a t e d e x e c u t i o n of
their components. Sequential execution of statements is
specified by the ~omeo~ ~tatement, conditional or s e l e c t i v e
execution by the if &tatemen[ and the ~&~_~ ~ t a t e m e o t , and
repeated e x e c u t i o n by t h e ~ # # e ~ t ~ t a t e m e o t , t h e ~ h i ! e &tatemeoj~,
and the ~ &~atement. The if s t a t e m e n t s e r v e s to m a k e t h e
execution of a statement d e p e n d e n t on t h e v a l u e of a O o o l e a n
expression, and the case statement allows for the selection
among many statements according to t h e v a l u e of a s e l e c t o r . The
for statement is used when t h e n u m b e r of i t e r a t i o n s is k n o w
beforehand, and the repeat and while statements are used
otherwise.

A statement can be g i v e n a n a m e ( i d e n t i f i e r ) , a n d be r e f e r e n c e d
through that identifier. The statement is then called a
orocedur_~, and its d e c l a r a t i o n a ~rocedur~ ~laration. Such a
declaration may additionally contain a set of variable
declarations, type definitions and further procedure
declarations. The v a r i a b l e s , types end procedures thus declared
can be referenced only within the procedure ~tself, and are
therefore c a l l e d ~ o c a l to t h e p r o c e d u r e . Their identifiers have
significance only within the program text which constitutes the
procedure declaration and which is c a l l e d t h e & ~ o o e of t h e s e
identifiers. Since procedures may be d e c l a r e d l o c a l to o t h e r
procedures, s c o p e s may be n e s t e d . E n t i t i e s w h i c h a r e d e c l a r e d in
the main p r o g r a m , i.e. not l o c a l to s o m e p r o c e d u r e , are called
~lobal. A procedure has a f i x e d n u m b e r of p a r a m e t e r s , each of
which is denoted within the procedure by an i d e n t i f i e r called
the ~ a l ~arameter. Upon an activation of the procedure
statement, an actual quantity has to be i n d i c a t e d for e a c h
parameter which can be referenced from within the procedure
through the formal parameter. T h i s q u a n t i t y is c a l l e d t h e #ctu@.~
~aram~j~W_~, There are three kinds of parameters: value
parameters, variable parameters, and procedure or function
parameters. In the first case, the actual parameter is an
expression which is evaluated once. The formal parameter
represents a local variable to which the result of this
evaluation is a s s i g n e d b e f o r e t h e e x e c u t i o n of t h e p r o c e d u r e (or
function). In the case of a variable parameter, the actual
parameter is a v a r i a b l e a n d t h e f o r m a l p a r a m e t e r s t a n d s for t h i s
variable. P o s s i b l e i n d i c e s a r e e v a l u a t e d b e f o r e e x e c u t i o n of t h e
procedure (or function). In t h e c a s e of p r o c e d u r e or f u n c t i o n
140

parameters, the actual parameter is a procedure or function


identifier.

~unctions are declared analogously to procedures. The o n l y


difference lies in the fact that a function yields a result
which is confined to a scalar or p o i n t e r t y p e a n d must be
specified in t h e f u n c t i o n d e c l a r a t i o n . F u n c t i o n s may t h e r e f o r e
be used as c o n s t i t u e n t s of e x p r e s s i o n s . In o r d e r to e l i m i n a t e
side-effects, assignments to non-local variables should be
avoided within function declarations.

3. N o t a t i o n , terminology, and vocabulary

According to traditional Backus-Naur form. syntactic constructs


are denoted by English words enclosed between the angular
brackets < and > . These w o r d s also d e s c r i b e t h e n a t u r e or
meaning of the construct, and are used in the accompanying
description of s e m a n t i c s . P o s s i b l e r e p e t i t i o n of a c o n s t r u c t is
indicated by e n c l o s i n g t h e c o n s t r u c t w i t h i n m e t a b r a c k e t s { and
} . The s y m b o l < e m p t y > d e n o t e s t h e null s e q u e n c e of s y m b o l s .

The basic vocabulary of Pascal consists of basic symbols


classified into letters, digits, and special symbols.

<letter> ::= AI~IclDIEIF1GIHIIIJIKILIMINIOIPIQIRISlTlUlVl


wtxlYlZlalblcldlelflgthliljlklllmlnlolPlqlrl
sltlulvlwlxlylz

<digit> : : = OI 11213141 5161 7 1 8 1 9


<special symbol> : :=
+ I- I * I t I = 1 <> I < t > t <= t >= I ( I ) I
[ 1 ] 1 { I } I := I • 1, 1 : i : I " t f I di_v. I
mad I nil I i n I o n I a n d I not I i f I ~ a ~ n f a S s e i
ca.~e 1 o£ I r_eoeat I until I while I do I fan I to 1
dg_~DJ~o I b e a i n I e n d I wiJih I ~ o t o I ~ o n s t I v~j~ I
t y p e I a r r a y ~ ~#-~J&r-~ I s£.i I f i l e I f u n c t i o n I
P£_Qced~u~e I l a b e l I ~acd2#_~ ~ ~ m o ~ r a m

The c o n s t r u c t
{ < a n y s e q u e n c e of s y m b o l s not c o n t a i n i n g "}"> }
may be i n s e r t e d b e t w e e n a n y t w o i d e n t i f i e r s , n u m b e r s (cf. 4), or
special s y m b o l s . It is c a l l e d a ~ o m m e n t and may be r e m o v e d from
the p r o g r a m t e x t w i t h o u t a l t e r i n g its m e a n i n g . The s y m b o l s { and
} do not o c c u r o t h e r w i s e in t h e l a n g u a g e , a n d w h e n a p p e a r i n g in
syntactic descriptions they are m e t e - s y m b o l s l i k e I and ::=
The s y m b o l p a i r s (~ and *) a r e u s e d as s y n o n y m s for { a n d } .

4. I d e n t i f i e r s , Numbers, and Strings

Identifiers serve to denote constants, types, variables,


procedures and functions. Their association must be u n i q u e
within their scope of v a l i d i t y , i.e. w i t h i n t h e P r o c e d u r e or
function in which they are declared (of. 10 a n d 1 1 ) .
141

<identifier> ::= < l e t t e r > { < l e t t e r Or d i g i t > }


<letter or d i g i t > : := < l e t t e r > I <digit>

The usual decimal notation is u s e d for numbers, which are the


constants of the data types inteoer and ~&l (see 6.1.2.). The
letter E preceding the scale factor is p r o n o u n c e d as " t i m e s 10
to t h e p o w e r o f " .

<digit sequence> ::= < d i g i t > { < d i g i t > }


<unsigned integer> : := < d i g i t s e q u e n c e >
<unsigned real> ::= < u n s i g n e d integer>.<digit sequence>
<unsigned integer>.<digit sequence>E<scale factor>
<unsigned integer> E <scale factor>
<unsigned number> ::= < u n s i g n e d integer> I <unsigned real>
<scale factor> ::= < u n s i g n e d integer> I
<sign><unsigned integer>
<sign> ::= + I -

Examples :
1 100 O. I 5E-3 8 7 . 3 5 E +8

Sequences of characters enclosed by quote mmrks a r e c a l l e d


&~r_i~g.&. Strings consisting of a single character are the
constants of the standard type char (see 6.1.2). Strings
Consisting of n (>1) e n c l o s e d c h a r a c t e r s are the constants of
the types (see 6.2.1)

&ac~.~t array [1..hi aZ char

Note: I f t h e s t r i n g is t o c o n t a i n a quote mark, then this quote


m a r k is to be w r i t t e n twice.

<string> : := "<character>{ <character>} '

Examples :
"A' °; . . . . .
'PASCAL" °TI{IS IS A STRING "

5. C o n s t a n t definitions

A constant definition introduces an identifier as a synonym to a


constant.

<constant identifier> ::= < i d e n t i f i e r >


<constant> ::= < u n s i g n e d number> ~ <sign><unsigned number> I
<constant identifier> ] <sign><constant identifier> I
<string>
<constant definition> ::= < i d e n t i f i e r > = <constant>
142

6. D a t a type definitions

A data t y p e d e t e r m i n e s t h e set of v a l u e s w h i c h v a r i a b l e s of that


t y p e m a y a s s u m e and a s s o c i a t e s an i d e n t i f i e r w i t h t h e t y p e .

< t y p e > ::= < s i m p l e t y p e > I < s t r u c t u r e d t y p e > I <pointer type>


< t y p e d e f i n i t i o n > ::= < i d e n t i f i e r > = < t y p e >

6.1. S i m o Z ~ ~y_a_~

<simple type> ::= < s c a l a r t y p e > I < s u b r a n g e type> I


<type identifier>
<type i d e n t i f i e r > ::= < i d e n t i f i e r >

6.1.1. Sr~ar types

A scalar type defines an o r d e r e d s e t of v a l u e s by enumeration of


the identifiers which denote these values.

<scalar type> ::= (<identifier> {.<identifier>} )

Examples:
(red, o r a n g e , y e l l o w , g r e e n , blue)
(club. d i a m o n d , h e a r t , s p a d e )
(Monday j Tuesday, Wednesday, Thursday, Friday.
Saturday, Sunday)

Functions applying to all scalar types (except real) are :

SUCC t h e s u c c e e d i n g v a l u e (in t h e e n u m e r a t i o n )
pred t h e p r e c e d i n g v a l u e (in the e n u m e r a t i o n )

6.1.2. ~&&odard ~voes

The f o l l o w i n g types are s t a n d a r d in P a s c a l :

integer The values are a subset of t h e w h o l e n u m b e r s


d e f i n e d by i n d i v i d u a l i m p l e m e n t a t i o n s . Its v a l u e s
are the i n t e g e r s (see 4).

real Its values are a subset of the r e a l n u m b e r s


depending on the p a r t i c u Z a r i m p l e m e n t a t i o n . The
v a l u e s a r e d e n o t e d by r e a l n u m b e r s (see 4).

Boolean Its values are the truth values denoted by the


identifiers true and f a l s e .

char Its v a l u e s are a set of c h a r a c t e r s d e t e r m i n e d by


particular implementations. T h e y are d e n o t e d by
the c h a r a c t e r s t h e m s e l v e s e n c l o s e d w i t h i n q u o t e s .
t43

6.1.3. ~ubr~noe types

A type may be defined as a subrange of another scalar type by


indication of the least and the largest value in the subrange.
The first constant specifies the lower bound, and must not be
greater than the upper bound.

<subrange type> ::= <constant> .. <constant>

Examples: 1..100
-10 .. +10
Nonday .. Friday

6.2. ~#_gJ~red types

A structure type is characterised by the type(s) of its


components and by its structuring method. Moreover, a structured
type definition may c o n t a i n an indication of the preferred data
representation. If a definition is prefixed with the symbol
packed, this has no effect on the meaning of a program, but is a
!lint to the compiler that storage should be economised even at
the price of some loss in efficiency of access, and even if this
may expand the code necessary for expressin~ access to
components of the structure.

<structured type> ::= <unpacked structured type> I


lacked <unpacked structured type>
<unpacked structured type> ::= <array type> I
<record type> I <set type> I <file type>

6.2.1. ~rrav types

An array type is a structure consisting of a f i x e d n u m b e r of


components w h i c h a r e a l l of t h e s a m e t y p e , c a l l e d t h e ~£_W_EJ&oeot
~yoe. The elements of the array are designated by i n d i c e s ,
values belonging to the so-called index ~p_~. The array type
definition specifies the component t y p e as w e l l as t h e i n d e x
type.

<array type> ::= arra~ [<index type> {,<index type>}] ~


<component type>
<index type> ::= <simple type>
<component type> ::= <type>

If n index types ere specified, the array type is called


~-_~_j~O~&na~, and a component is designated by n indices.

Examples: #rray [ 1..100] of real


#r~av [1..10,1..20] of 0..99
#tray [Boolean] ~[ color
144

6.2.2. _Rec~L~__J~vo e&

A record type is a structure consisting of a fixed number of


components, possibly of different types. The record type
definition specifies for each component, called a _f~, its
type and an identifier which denotes i t . T h e s c o p e of t h e s e
so-called f ~ identifiers is t h e r e c o r d definition itself, and
they are also accessible within a field designator (ef. 7.2)
referring to a r e c o r d v a r i a b l e of t h i s t y p e .

A record type may have several variants, in w h i c h c a s e a c e r t a i n


field m a y be d e s i g n a t e d as t h e ]2~_q £ ~ e l d , w h o s e v a l u e i n d i c a t e s
which variant is a s s u m e d by t h e r e c o r d v a r i a b l e at a g i v e n t i m e .
Each variant structure is i d e n t i f i e d by a c a s e l a b e l w h i c h is a
constant o f t h e t y p e of t h e t a g f i e l d .

<record type> ::= E e e o r d <field list> end


<field list> ::= < f i x e d p a r t > I <fixed part>;<verient part> I
<variant part>
<fixed part> ::= < r e c o r d s e c t i o n > [ ;<record section>}
<record section> : :=
<field identifier>{ ,<field identifier>} : <type> I <empty>
<variant part> ::= ~ a s e < t a g f i e l d > < t y p e i d e n t i f i e r > of
<variant> { ;<variant>}
<variant> ::= < c a s e l a b e l l i s t > : (<field list>) I <empty>
<case label list> ::= <case label> { ,<case label>}
<ease label> : := <constant>
<tag field> ::= < i d e n t i f i e r > : I <empty>

Examples: rWcor~[ day: 1..31;


month: 1 ,,12;
y e a r : Inte~qer
en~

record name, firstname: alfa;


age : 0..99:
married: Boolean
and

record x,y : real;


area : real;
~O~ s : shape o~
trlangle: (side : real;
inclination, angle1, angle2: angle);
rectangle: (sidel, skde2: real;
skew, angle3: angle) ;
circle: (diameter: real)
and

6.2.3. _Se~,__t~y_p_es

A set type defines the range of values which is the oowerset of


its so-called ~ ~Y.~Za. Base types m u s t not be s t r u c t u r e d
types. Operators applicable to a l l s e t t y p e s a r e :
145

+ union
- set difference
* intersection
iS membership

The s e t d i f f e r e n c e x-y is d e f i n e d as the set of all elements of


x w h i c h a r e not m e m b e r s of y,

<set t y p e > ::= s e t o f < b a s e t y p e >


< b a s e t y p e > ::= < s i m p l e t y p e >

6.2.4. Lila__~yoes

A file type definition specifies a structure consisting of a


sequence of components which are all of t h e s a m e t y p e , T h e
number of components, called the J~@/3~th o f t h e f i l e , is not
fixed by t h e file t y p e d e f i n i t i o n , A file with 0 components is
called ~m&&Z.

<file type> ::= f i l ~ ~Z <type>

Files with component type char are called ~extfilw_&, end are a
special case insofar as t h e c o m p o n e n t r a n g e o f v a l u e s m u s t be
considered as extended by a m a r k e r d e n o t i n g the end of a line.
This marker allows textfiles t o be s u b s t r u c t u r e d into lines. The
t y p e ~82£~ i s a s t a n d a r d type predeclared as

~X~ text = ~ile ~f char

6.3. ~ o i n t ~ r tvoes

Variables which are declared in a program (see 7.) are


accessible by their identifier, They exist during the entire
execution process of the procedure (scope) to which the variable
is local, and these variables are therefore celled stati~ (or
statically allocated), In contrast, variables may also be
generated dynamically, i,e, without any correlation to t h e
structure of t h e p r o g r a m , T h e s e ~ z n a m i c v a r i a b l e s are generated
by the s t a n d a r d p r o c e d u r e ~#j~ (see 1 0 . I . 2 , ) : s i n c e t h e y do not
occur in an explicit variable declaration, they c a n n o t be
referred to by a name. Instead. access is achieved via a
so-called &D.i~ v a l u e w h i c h is p r o v i d e d u p o n g e n e r a t i o n of t h e
dynamic variable, A p o i n t e r t y p e t h u s c o n s i s t s o £ an u n b o u n d e d
set of values pointing to elements of the same t y p e , No
operations a r e d e f i n e d on p o i n t e r s e x c e p t t h e t e s t for e q u a l i t y ,

The pointer value ~ll belongs to every pointer type; it p o i n t s


to no e l e m e n t at all,

<pointer type> ::= ~ < t y p e identifier>


146

Examples of type definitions:

color = (red, yellow, green, blue)


sex = (male, female)
text -- f~le ~_~ c h a r
shape = (triangle, rectangle, circle)
card = ~rray [1..80] o~ char
alfa = ~ a e k e d ~&rJ~eJL [ 1 , . 1 0 ] of char
complex = record re.ira: real ~nd
p arson = 2ecord name, firstname: ella;
age : integer;
married :Boolean;
father, child, sibling: fperson:
case s : sex of
male: (enlisted, bold: Boolean);
female: (pregnant : Boolean;
size: arrav[1..3] of i n t e g e r )
ea~

7. D e c l a r a t i o n s and denotations of variables

Variable declarations consist of e l i s t of identifiers denoting


the new variables, followed by their type.

<variable declaration> ::= <identifier>{.<identifier>} : <type>

Every declaration of a f i l e v a r i a b l e f with components of t y p e T


implies the additional declaration of a so-called ~uffer
~ariable of type T. This buffer variable is d e n o t e d by ft a n d
serves to append components to t h e f i l e d u r i n g g e n e r a t i o n , and
to access the file during inspection (see ? . 2 . 3 . a n d I 0 . I . 1 . ) .

Examples:
X ,y ,Z : r e a l
u,v: complex
i,j: integer
k: 0 . . 9
p ,q : B o o l e a n
operator : (plus, minus, times)
a : ~rJ&~L[O,.63] ~ real
b: ~ r a y [ c o l o r , B o o l e a n ] £~ complex
c: c o l o r
f: f&l~ of char
huel,hue2: & a t O~ c o l o r
pl p2: ~person

Denotations of v a r i a b l e s either designate an e n t i r e v a r i a b l e , a


component of a variable, or a v a r i a b l e referenced by a p o i n t e r
(see 6.3). Variables occurring in examples in subsequent
chapters a r e a s s u m e d t o be d e c l a r e d as i n d i c a t e d above,

<variable> ::= <entire variable> I <component variable> I


<referenced variable>
147

7.I. Entlre~riables

An entire variable is denoted by its identifier.

<entire variable> ::= <variable identifier>


<variable identifier> ::= < i d e n t i f i e r >

7.2. C prop o n ~ n t _ _ ~ a r i a b l e ~

A component of a v a r i a b l e is d e n o t e d by t h e v a r i a b l e followed by
a selector specifying the component. The form of the selector
depends on the structuring type of the variable.

<component variable> ::= <indexed variable> I


<field designator> I <file buffer>

?.2.1. i~g~gd v~riables

A component of an n-dimensional array variable is denoted by the


variable followed by n i n d e x e x p r e s s i o n s .

<indexed variable> ::=


<array variable> [<expression> {,<expression>}]
<array variable> ::= < v a r i a b l e >

The types of the index expressions must correspond with the


index types declared in t h e definition of t h e a r r a y t y p e .

Examples :
a [ 12]
a[i+j]
b [ r e d ,true]

?.2.2. _Field desionator~

A component of a record variable is denoted by t h e r e c o r d


variable followed by t h e f i e l d i d e n t i f i e r of t h e c o m p o n e n t .

<field designator> ::= < r e c o r d variable>.<field identifier>


<record variable> ::= < v a r i a b l e >
<field identifier> ::= < i d e n t i f i e r >

Examples :
U .re
hired,true] .im
p 2~ .s i z e
148

?.2.3, Eile ~uffers

At any time, o n l y the one c o m p o n e n t d e t e r m i n e d by the c u r r e n t


file position (read/write head) is d i r e c t l y a c c e s s i b l e . This
component is called the current file component and is
r e p r e s e n t e d by t h e f i l e ' s ~ f _ ~ ~ariable.

<file b u f f e r > ::= <file v a r i a b l e > ~


<file v a r i a b l e > : := < v a r i a b l e >

7.3. ~ ~ e d variables

< r e f e r e n c e d v a r i a b l e > ::= < p o i n t e r variable>t


< p o i n t e r v a r i a b l e > ::= < v a r i a b l e >

If p is a pointer variable which is b o u n d to a t y p e T , p


denotes t h a t v a r i a b l e a n d its p o i n t e r v a l u e , w h e r e a s p~ d e n o t e s
the v a r i a b l e of t y p e T r e f e r e n c e d by m.

Examples:
p~ .father
p I~ .sibling~ .child

8. E x p r e s s i o n s

Expressions are constructs denoting r u l e s of c o m p u t a t i o n for


obtaining values of v a r i a b l e s and g e n e r a t i n g new v a l u e s by t h e
application of o p e r a t o r s . E x p r e s s i o n s c o n s i s t o f o p e r a n d s , i.e.
v a r i a b l e s and c o n s t a n t s , o p e r a t o r s , and f u n c t i o n s .

The r u l e s of c o m p o s i t i o n s p e c i f y o p e r a t o r ~ 2 ~ £ ~ # ~ & according


to four c l a s s e s of o p e r a t o r s . The o p e r a t o r ~ J 2 has the h i g h e s t
precedence, followed by the so-called multiplying operators,
then the so-called adding operators, and finally, with the
lowest precedence, the relational operators. Sequences of
operators of the same precedence are e x e c u t e d from left to
right. The rules of p r e c e d e n c e a r e r e f l e c t e d by the f o l l o w i n g
syntax:
149

<unsigned constant> ::= < u n s i g n e d number> I <string> I


<constant identifier> I ~il
<factor> ::= < v a r i a b l e > I <unsigned constant> 1
<function designator> I <set> I (<expression>) I
~ot <factor>
< s e t > ::= [ < e l e m e n t l i s t > ]
< e l e m e n t l i s t > ::= < e l e m e n t > {,<element>} I <empty>
<element> ::= < e x p r e s s i o n > I <expression>..<expression>
< t e r m > ::= < f a c t o r > I < t e r m > < m u l t i p l y i n g operator><factor>
<simple expression> ::= < t e r m > I
<simple expression> <adding operator><term> I
<adding operator><term>
<expression> ::= < s i m p l e e x p r e s s i o n > I
<simple expression><relational operator><simple expression>

Expressions which a r e m e m b e r s o f a s e t m u s t a l l be of t h e s a m e
type, which is t h e b a s e t y p e of t h e s e t . [] d e n o t e s t h e e m p t y
set, and [x.,y] d e n o t e s t h e s e t of a l l v a l u e s in t h e i n t e r v a l
X,o.y ,

Examples:

Factors: X
15
( x +y +z )
sin (x+y)
[ r e d .c , g r e e n ]
[ 1,5, 10.,19,23]
n~J& p

Terms : x *y
i/(1-i)
p nz q
(x<=y) ~[ (y < z )

Simple expressions : x +y
-x
hue 1 + hue2
i*j + I

Expressions: x = 1.5
p <=q
(i<j) = (j<k)
C in hue1

8.1. 09..~r3.tors

If both operands of the arithmetic operators of addition,


subtraction and multiplication are of type integer (or a
subrange thereof), t h e n t h e r e s u l t is of t y p e i n t e g e r . I f o n e o f
the oPerands is of t y p e r e a l , t h e n t h e r e s u l t is a l s o of t y p e
real.

8.1.1. The omer_wtor not


150

The operator ~ denotes negation of its Boolean operand.

8.1.2. ~ul&iolyinQ opera,Or&

<multiplying operator> ::= ~ I / I ~ ] m~L~t I ~ n d

Ioperatorl operation type of o p e r a n d s type of resultI

multiplication real. integer real, integer


set intersection any set type T T

/ division real, integer real

dlv division with integer integer


truncation

mod modulus integer integer

and logical "and" Boolean Boolean

8,1.3. ~ddio,~__~eretors

<addinc operator> ::= + I - I ~ Z

Ioperatorl operation I type of o p e r a n d s ) type of r e s u l t l


..............................................................
I I I I I
1 ÷ I addition I integer, real 1 integer, reel 1
I set union I any set type T ~ T I
I I I I I
I - I subtraction ] integer, real I integer, real I
I I set difference I a n y set t y p e T I T I
I I I I I
] logical "or" ] Ooolean I Boolean I
I I r I I

When used as operators with one operand only, - denotes sign


inversion, and + denotes the identity operation.

~3.I.4. _ R e l a t i o n a l ooeratQ~&

<relational operator> ::= = I <> I < I <= T >= I > I in


151

I operator I type of operands I result !

I = <> T I T
l < > l any scalar or subrange type I Boolean I
I <-->= ! l I
I T 1 i
I in I any scalar or s u b r a n g e t y p e I Boolean I
I I and its set type respectivelyl
I t t I

Notice that all scalar types define ordered sets of v a l u e s .

The operators <>. < = . >= s t a n d for unequal, less or equal,and


oreater or equal respectively.
The operators <= a n d >= may a l s o be used for comparing values of
set type. and then denote set inclusion.
tf p and q are Boolean expressions, p = q denotes their
equivalence, and p <= q denotes im#llcation of q by p. (Note
that f a l s e < t r u e )

The relational operators = <> < <= > >= may also be used to
compare (packed) arrays with components of type char (strings),
and then denote alphabetical ordering according to the collating
sequence of t h e u n d e r l y i n g set of c h a r a c t e r s .

8.2. E # n ~ t i o n dEsianator&

A f u n c t i o n d e s i g n a t o r s p e c i f i e s the a c t i v a t i o n of a f u n c t i o n . It
consists of t h e i d e n t i f i e r d e s i g n a t i n g t h e f u n c t i o n and a l i s t
of actual parameters. The parameters are variables, expressions.
procedures, and functions, and are substituted for the
corresponding formal parameters (cf. 9.1.2.o I0, a n d 11).

< f u n c t i o n d e s i g n a t o r > ::= < f u n c t i o n i d e n t i f i e r > I


<function identifier>(<actual parameter>{ ,<actual parameter>})
< f u n c t i o n i d e n t i f i e r > ::= < i d e n t i f i e r >

Examples : Sum(a .100)


SCD ( 1 4 7 , k )
s i n (x+y)
eof (f)
ord(ff )

9. S t a t e m e n t s

Statements denote algorithmic actions, and are said to be


#xec~&bi&. They may be prefixed by a label which can be
referenced by g o t o s t a t e m e n t s .
152

<statement>::=<unlabelled statement> I
<label>:<unlabelled statement>
<unlabelled statement> ::= < s i m p l e statement> I
<structured statement>
<label> ::= < u n s i g n e d integer>

9,1. ~&mo!e statements

A simple statement is a s t a t e m e n t of w h i c h no p a r t c o n s t i t u t e s
another statement. The empty statement consists of no s y m b o l s
and denotes no a c t i o n .

<simple statement> ::= < a s s i g n m e n t statement> I


<procedure statement> I <goto statement> I
<empty statement>
<empty statement> ::= < e m p t y >

9.1.1. Assignment statemeoJza

The assignment statement serves to r e p l a c e the current value of


a variable by a new v a l u e specified as an e x p r e s s i o n .

<assignment statement> ::= <variable> := < e x p r e s s i o n > I


<function identifier> := < e x p r e s s i o n >

The variable (or the function) and the expression m u s t be of


identical type, with the following exceptions being permitted:

I. t h e type of the variable is real. and the type of the


expression is i n t e g e r or a s u b r a n g e thereof.
2. t h e type of t h e e x p r e s s i o n is a s u b r a n g e of the type of the
variable, or v i c e - v e r s a .

Examples: x := y+z
p := ( I < = i ) a n d ( i < I 0 0 )
i := s q r ( k ) - (i'j)
hue := [ b l u e . s u c c (c )]

9.1.2, ~ro~_~dure statements

A procedure statement serves to execute the procedure denoted by


the procedure identifier, The procedure statement may c o n t a i n a
list of a~ual ~aramet~a which are substituted in place of
their corresponding ~orma~ ~ar~et@~S defined in the procedure
declaration (cf. 10). The correspondence is established by the
positions of the parameters in the lists of actual and formal
Parameters respectively, There exist four kinds of parameters:
so-called value parameters, variable parameters, procedure
parameters (the a c t u a l p a r a m e t e r is a P r o c e d u r e identifier), and
function parameters (the actual parameter is a function
identifier).

In the case of a ~alue ~jc_~m__~, the actual parameter m u s t be


an expression (of which a variable is a simple case). The
153

corresponding formal parameter represents a l o c a l v a r i a b l e of


t h e c a l l e d p r o c e d u r e , a n d t h e c u r r e n t v a l u e of t h e e x p r e s s i o n is
initially assigned to t h i s v a r i a b l e . In t h e c a s e of a ~ l e
~arameter, the actual parameter must be a v a r i a b l e , and the
corresponding formal parameter represents this actual variable
during the e n t i r e e x e c u t i o n of t h e p r o c e d u r e . If t h i s v a r i a b l e
is a component of an array, its index is evaluated when the
procedure is called, A variable parameter must be used whenever
the parameter represents a result of the procedure.

Components of a packed structure must not appear as actual


variable parameters.

<procedure statement> ::= < p r o c e d u r e i d e n t i f i e r > I


<procedure identifier> (<actual parameter>
{,<actual parameter>})
<procedure identifier> ::: < i d e n t i f i e r >
<actual parameter> ::: < e x p r e s s i o n > I <variable> I
<procedure identifier> I <function identifier>

Examples : next
Transpose(a on,m)
Bisect (fct.-1.0,+1.0.x)

9.1.3. _Onto s t a t e m e n t s

A gore statement serves to indicate that further processinc


should continue at a n o t h e r p a r t of the p r o g r a m t e x t , n a m e l y at
t h e p l a c e of t h e l a b e l .

<goto statement> ::: £ot£ <label>

The following restrictions hold concerninD the applicability of


labels:

I. T h e scope of a label is the procedure w i t h i n w h i c h it is


defined, it is therefore not possible to jump into a
procedure.

2. E v e r y label must be s p e c i f i e d in a l a b e l d e c l a r a t i o n in t h e
heading of the procedure in which the label marks a
statement.

9.2. ~r_w_~t~@3!~tatemen~:

Structured statements are constructs composed of other


statements which have to be executed either in sequence
(compound statement), conditionally (conditional statements), or
repeatedly (repetitive statements).

<structured statement> ::: < c o m p o u n d s t a t e m e n t > I


<conditional statement> I <repetitive statement> I
<with statement>
154

9,2.1, ~I~Ol&~Dd statements

The compound s t a t e m e n t s p e c i f i e s t h a t its c o m p o n e n t s t a t e m e n t s


are to be e x e c u t e d in t h e s a m e s e q u e n c e as t h e y a r e w r i t t e n . The
s y m b o l s ~ . ~ & ~ a n d ~031 act as s t a t e m e n t b r a c k e t s ,

<compound statement> ::= ~ _ E i n <statement> { ;<statement>} ~

Example: ~ z := x ; x := y; y := z ~

9.2.2, ~&~_~tional_~J~.O~

A conditional statement selects for execution a single one of


its c o m p o n e n t s t a t e m e n t s .

<conditional statement> ::=


<if s t a t e m e n t > I <case statement>

9 , 2 , 2 . I , If__~&~d~ements

The if s t a t e m e n t s p e c i f i e s t h a t a s t a t e m e n t be e x e c u t e d only if
a. c e r t a i n condition (Boolean expression) is t r u e . If it is
false, then either no statement is to be e x e c u t e d , or t h e
statement f o l l o w i n g t h e s y m b o l ~ I s ~ is to be e x e c u t e d .

<if statement> ::= & ~ < e x p r e s s i o n > &b~l! < s t a t e m e n t > I


~ <expression> ~ <statement> ~l&~ <statement>

The e x p r e s s i o n between the symbols ~ and ~ h e & must be of t y p e


Boolean.

~ote:
The syntactic ambiguity arising from the construct

i~ <expression-t> ~b_~i i L <expression-2> ~ <statement-l>


~ <statement-2>

is resolved by interpreting the construct as equivalent to

~ <expression-l> ~Uia
~e~iA i~ <expression-2> iheo <statement-l> ~.~ <statement-2>
end

Examples :
i~ x < 1 . 5 t h e n z := x + y e l s e z : = 1 .5
if p 1 <> n j ! &he~l p I : = p I f . f a t h e r

9,2,2,2. ~ e stat@ments

The case statement c o n s i s t s of an e x p r e s s i o n (the s e l e c t o r ) a n d


a list of s t a t e m e n t s , e a c h b e i n g l a b e l l e d by a c o n s t a n t of t h e
type of the selector, It s p e c i f i e s t h a t t h e o n e s t a t e m e n t be
executed whose label is equal to the c u r r e n t v a l u e of the
155

selector .

<case statement> ::= ~ , ~ <expression> D~


<case list element> { ;<case list element>~ ~A~
<case list element> ::= < c a s e l a b e l l i s t > : <sbatement> 1
<empty>
< c a s e l a b e l l i s t > ::= < c ~ s e l a b e l > { , < c a s e label> ]

E x a m p 1as :
c & s e o p e r a t o r of" case i of
plus : x := x+y ; 1: x := sin(x);
m i n u s : x := x - y ; 2: x := cos(x);
t i m e s : x := x * y 3: x := exp(x);
end 4: x := ln(x)
LoAd

9.2.3. ~#3z~ti~ve s ~ t ~ & ~

Repetitive statements specify that certain statements a r e t o be


executed repeatedly. If the number of repetitions is k n o w n
beforehand, i.e. before the repetitions are started, the for
statement is the appropriate construct to express this
situation; otherwise the while or r e p e a t s t a t e m e n t s h o u l d be
used .

<repetitive statement> ::= < w h ~ l e s t a t e m e n t > [


<repeat statement> [ <for statement>

9.2.3.1o ~ile__~tatemen~&

<while statement> ::= ~ , ~ i £ <expression> d£ <statement>

The expression controlling repetition m u s t be of t y p e g o o l e a n .


The statement is repeatedly executed until the expression
becomes false. I£ its value is f a l s e at t h e b e g i n n i n g , the
statement is not e x e c u t e d at a 1 1 . T h e w h i l e s t a t e m e n t

~hile B do S

is equivalent to

i£ B £hea
be£in 8 ;
wbile B do S
end
156

Examples:

whil~ a[i] <> x d o i := i + I

while i > O ~j&


beoin if odd(i) then z := z ' x ;
i := i ~ L ~ 2:
x := s q r ( x )
and

~]3ile ~ e e l ( f ) da
~W~O P ( f t ); g e t ( f )

9.2.3.2. ~931eat statemeoj&&

<repeat statement> : :=
reoeat <statement> { :<statement>} until <expression>

The expression controlling repetition m u s t be o f t y p e B o o l e a n .


The sequence of statements between t h e s y m b o l s [9~2eat a n d w n t i ~
is repeatedly executed (and at l e a s t o n c e ) u n t i l t h e e x p r e s s i o n
becomes true. The repeat statement

reoeat S ~il B

is equivalent to

beoin S :
i f no*. B t h e n
r~oeat S un~il B
end

E x a m p les :

zeoeal k
:= i m e ~ J:
i
:= j ;
j
:= k
~.~.~l j = O

z~ea~ P(ft); get(f)


~Dtil eof(f)

9.2.3.3. Egj~_~_~t e m e n t s

The for statement indicates that a statement is t o be r e p e a t e d l y


executed while a progression of v a l u e s is a s s i g n e d to a v a r i a b l e
w h i c h is c a l l e d t h e c o o ~ _ ~ ~r_iW_b~ of t h e f o r s t a t e m e n t .
157

<for statement> ::=


~ <control variable> := < f o r l i s t > ~ <statement>
<for list> ::= < i n i t i a l v a l u e > ~ O < f i n a l v a l u e > I
<initial value> ~owot~ <final value>
<control variable> ::= < i d e n t i f i e r >
<initial value> ::= < e x p r e s s i o n >
<final value> ::= < e x p r e s s i o n >

The control variable, the initial value, and the final value
must be o f t h e s a m e s e a l e r t y p e (or s u b r a n g e thereof), and must
n o t be a l t e r e d by t h e r e p e a t e d statement. T h e y c a n n o t be of t y p e
real.

A for statement of the form

for v := el to e2 do S

is equivalent to the sequence of statements

v := el; S; v := s u c c ( v ) ; S; .,. ; v := e2; S

and a for statement of the form

fD~l v := el do~jl~J& e 2 d o S

is equivalent to the statement

v := el; S; v := p r e d ( S ) ; S; ... ; v := e2; S

Examples :

for i := 2 t o 100 do ii a[i] > max tbru& m a x := a [ i ]

f e z i := I t o n #j&
fD~l J := I t o n d o
b e n i n x := 0 ;
for_ k := I t o n d o x := x + a [ i , k ] *b[k,j] ;
c[i.j] :-- x
an~

for c := r e d to blue do Q (c)

9.2.4. ~h_~tatements

<with statement> ::= ~ i t h < r e c o r d v a r i a b l e list> dO <statement>


<record variable l i s t > ::= < r e c o r d variable>{ ,<record variable>}

Within the component statement of the with statement, the


components ( f i e l d s ) of t h e r e c o r d variable specified by t h e w i t h
Clause can be denoted by their field identifier only, i.e.
without preceding them with the denotation of t h e e n t i r e r e c o r d
variable. The with clause effectively opens the scope
containing the field identifiers of the specified record
variable, so that the field identifiers m a y o c c u r as v a r i a b l e
identifiers.
158

Example:

~£~b date d~
~ m o n t h = 12 ~ e n
~ m o n t h := I; y e a r := y e a r + I

~se month := m o n t h + 1

is equivalent to

i~ date.month ~ 12 ~
~d~ date.month := 1; date.year := d a t e . y e a r + 1

~]_.~ d a t e . m o n t h := d a t e . m o n t h + l

No assignments may be m a d e in t h e q u a l i f i e d statement to any


elements of the record variable list. However, assignments are
possible to t h e c o m p o n e n t s of t h e s e v a r i a b l e s .

10. Procedure declarations

Procedure declarations s e r v e to d e f i n e p a r t s of programs and to


associate identifiers w i t h t h e m so t h a t they can be a c t i v a t e d by
procedure statements.

<procedure declaration> : := < p r o c e d u r e heading> <block>


<block> ::= < l a b e l d e c l a r a t i o n oart>
<constant definition part><type definition part>
<variable declaration part>
<procedure and function declaration part>
<statement part>

The ~ ~ ~ d ~ specifies the identifier naming the


procedure and the formal parameter identifiers (if ~ n y ) .
The parameters are either value-, variable-, procedure- or
function parameters (cf. also 9,1,2,), Procedures and functions
which are u s e d as p a r a m e t e r s to o t h e r p r o c e d u r e s end functions
must have value parameters only.

<procedure heading> ::= ~ 2 o c w ~ d u r e < i d e n t i f i e r > ; 1


~2~r~W~ <identifier> (<formal parameter section>
I ;<formal parameter section>}) :

<formal parameter section> ::=


<parameter group> 1
~&~ <parameter group>
~nc~on <parameter group>
~£ce~ur~ <identifier> { ,<identifier>t
<parameter group> ::= <identifier>{,<identifier>} :
<type identifier>

A parameter group without preceding specifier implies that its


constituents are value parameters.

The i ~ de~l~ration ~it specifies all labels which mark a


159

statement in the statement part.

<label declaration part> ::= < e m p t y > I


iah#3. < l a b e l > { . < l a b e l > } ;

The £~nsJi&Oj~ definition ~&~& contains all constant synonym


definitions l o c a l to t h e procedure.

<constant definition part> : := <empty> I


£~D_%& <constant definition> { ;<constant definition>} :

The ~ ~efioij~ion &arl contains all type definitions which are


local to the procedure declaration.

<type definition part> : := < e m p t y > I


ty_~ <type definition> { :<type definition> }:

The ~&r_i&hl~ ~cle£ation ~&~ contains all variable declarations


l o c a l to t h e procedure declaration.

<variable declaration part> ::= <empty> I


vat <variable declaration> { ;<variable declaration>t ;

The &2~£~r_@ &~_~ ~ o m ~&f_~Wd3 &&2& contains all


procedure and function declarations local to the procedure
declaration.

<procedure and function declaration part> ::=


{<procedure or f u n c t i o n declaration> :}
<procedure or f u n c t i o n declaration> ::=
<procedure declaration> ~ <function declaration>

The &~atg@~ELt ~r_J2 specifies the algorithmic actions to be


executed upon an activation of the procedure by a p r o c e d u r e
statement.

<statement part> ::= <compound statement>

All identifiers introduced in the formal parameter part, the


constant definition part, the type definition part, the
variable-, procedure or f u n c t i o n declaration parts are l~l to
the procedure declaration which is c a l l e d t h e &_q~Z~ of t h e s e
identifiers. T h e y a r e not k n o w n o u t s i d e t h e i r s c o p e . In t h e c a s e
of local variables, their values ere undefined at t h e b e c i n n i n ~
of t h e s t a t e m e n t part.

The use of the procedure identifier in a p r o c e d u r e statement


within its declaration implies recursive execution of the
procedure.
160

Examples of procedure declarations:

~ro_~dure readinteger (3Aor f : t e x t ; M a r x: integer) ;


va_~r i . j : i n t e g e r ;
~_qin w _ h i l e f¢ = " " d o g e t ( f ) : i := O:
.~bile f t ~O. [ ' 0 " . . "9"] do
begio j := o r d ( f ~ )- o r d ( ' O ' ) ;
i :: I 0 - i + j ;
g e t (f)
end ;
x := i
and

~rocedwj~e Bisect(functio~ f: real; a.b: real; Mar z: real);


va~ m: real;
beqin {assume f(a) < 0 and f(b) > 0 }
whi]j& abs (a-b) > IE-10*abs(a) do
b~g[in m :: ( a + b ) / 2 . 0 :
if f ( m ) < O Jzh~J3 a : = m e l s e b ::m
end ;
Z :: m
~d

or~.~e~_~re GCD (m 0 n : i n t e g e r ; v o ~ x 0y , z : i n t e g e r ) :
~ al,a2, bl,b2,c,d ,q , r : i n t e g e r ; {m>=O, n>O}
be~in {Greatest Common D i v i s o r x of m and n,
Extended Euclid's Algorithm}
al :: O; a 2 : = I ; b l ;:I; b 2 : = O;
e : : m: d : : n :
~hi~ d <> 0 d o
beo3J3 {a1*m + b1*n = d, a2*m + b2*n = e.
god(cod) : gcd(mon)}
q := c ~ d ; r :-- c rnDj~ d ;
a 2 := a 2 - q * a I; b 2 := b 2 - q * b l ;
o :: d; d := r :
r :: a l ; e l :: a 2 : a 2 :: r ;
r := b 1 : b l := b 2 ; b 2 := r
end ;
x :: c ; y := a 2 ; z := b 2
{ x = gcd(m,n) = y*m + z*n }
end

10.1. ~Zld~rd erocedwres

Standard procedures are supposed to be predeclared in every


implementation of Pascal. Any implementation may feature
additional predeclared procedures. Since they are, as all
standard quantities, assumed as declared in a scope surrounding
the program, no conflict arises from a declaration redefining
the same identifier within the program, The standard procedures
are l~sted and explained below.
161

10.1.1. Eile handling orocedur~

put(f) appends the value of the buffer variable f~ to the


file f. The effect is defined o n l y if p r i o r to
execution the predicate eof(f) is true. eof(f)
r e m a i n s t r u e . a n d t h e v a l u e of f~ b e c o m e s u n d e f i n e d .

get(f) a d v a n c e s the c u r r e n t f i l e p o s i t i o n (read/write head)


to t h e next c o m p o n e n t , and a s s i g n s t h e v a l u e of t h i s
component to the buffer variable ft . if no n e x t
component exists, then eof(f) becomes true. and the
value of ft is not d e f i n e d . T h e e f f e c t of g e t ( f ) is
defined only if ear(f) = false prior to its
execution. (see 1 1 . 1 . 2 )

reset (f) resets the current file p o s i t i o n to its b e g i n n i n g


end assigns to t h e b u f f e r v a r i a b l e f~ t h e v a l u e of
the first e l e m e n t of f. e o f ( f ) b e c o m e s f a l s e , if f
is not empty; otherwise f~ is not d e f i n e d , a n d
eof(f) remains true.

rewrite(f) d i s c a r d s t h e c u r r e n t v a l u e of f s u c h t h a t a new file


m a y be g e n e r a t e d , eof(f) becomes true.

Concerning the textfile procedures read, write, readln, writeln,


and page, see Chapter 12.

ne. (p) allocates a new variable v and assigns the pointer


to v to the p o i n t e r v a r i a b l e p. If t h e type of v is
a record type with variants, the form

new(p ,t 1 ..... tn) can be used to allocate a variable of the


variant with tag field values t 1 ..... t n .

dispose(p) a n d d i s p o s e ( p , t 1 , . . . . t n ) can be u s e d to i n d i c a t e t h a t
storaoe occupied by t h e v a r i a b l e r e f e r e n c e d by t h e
pointer p is no l o n g e r n e e d e d . (Implementations may
use this information to r e t r i e v e s t o r a g e , or t h e y
may i g n o r e it,)

Let the variables a and z be declared by

a: &~&~ [m..n] of T
z: oack~ azrav [u..v] of T

where n-m >= v-u o Then the statement pack (a ,i ,z ) means

~or j := u to v do z[j] := a[j-~+i]

and the statement unpack(z,a,i) means


162

f~l~ J := u t o v do a[J-u+i] :: z[j]

where j denotes an auxiliary variable not occurring elsewhere in


the program.

11. F u n c t i o n declarations

Function declarations serve to define parts of the program which


compute a scalar value or a pointer value. Functions are
activated by the evaluation of a f u n c t i o n designator (cf. 8 . 2 )
w h i c h is a c o n s t i t u e n t o f an e x p r e s s i o n .

<function declaration> ::: <function heading><block>

The function heading soecifies the identifier naming the


function, the formal parameters of the function, and the type of
the function.

<function heading> ::: ~WJl&~i9_~ < i d e n t i f i e r > : < r e s u l t type>; I


~tion <identifier> (<formal parameter section>
{ :<formal parameter section>}) : <result type> ;
<result type> ::= < t y p e i d e n t i f i e r >

The t y p e of t h e f u n c t i o n m u s t be a s c a l a r , s u b r a n g e , or p o i n t e r
type. Within the function declaration t h e r e m u s t be at l e a s t o n e
assignment statement assigning a value to the function
identifier. This assignment determines the result of the
function. Occurrence of the function identifier in a f u n c t i o n
designator within its d e c l a r a t i o n implies recursive execution of
the function.

Examples:

fu~l~J~ioa S q r t (x : r e a l ) : r e a l :
vgr xU.x1: real:
b~_~i~ x l := x; {x>l. Newton's method}
~e~zoat xO :: x l; x l := (xO+ x/xO)*O.5
until abs(xl-xO) < eps*xl ;
Sqrt := x 0
~nd

f u n ~ d ~ i o ~ N~ax(e : v e c t o r ; n: integer): real;


v~ x : r e a l ; i: i n t e g e r :
b a ~ i n x := a [ 1 ] :
_fD/i i := 2 t ~ n do
b.~lin_ {x = max(a[ I] ..... a[i-1])}
if X < a[i] ~h~ii x := a [ i ]
end :
{X = max(a[ I] ..... a[n] )}
I~!ax : = x
end
163

function G C D (m,n : i n t e g e r ) : i n t e g e r :
be~in if n = O J~hen G C D :-- m # ! s e G O D := G C O (n,m mod n)
en,~d

function Power(x: r e a l ; y: i n t e g e r ) : real : {y >= O}


v&r w.z : reel; i : integer ;
~.~in w : = x ; z := 1: i : = y ;
mhJJ.~ i > 0 d o

if odd(i) then z := z'w;


i := i d i v 2 ;
w := sqr(w)
end :
{Z = x ~ y }
Power := z
~nd

11.1. ~&ndard functig_Q&

Standard functions are supposed to be predeclered in every


implementation of Pascal. Any implementation may feature
additional predeclared functions (cf. also 10.1).

The standard functions are listed and explained below:

11.1.1, Ar_~_bmetic fg_ectiojl&

abs(x) computes the absolute value of x . The t y p e o f x


must be either ~Ca~ or iO~e~er, and the type of
the result is the type of x.

sqr (x) computes x~2. T h e t y p e o f x m u s t be either ~


or ~ n t e ~ e r , and t h e t y p e of t h e r e s u l t is t h e type
of x.

s in (x)
cos (x)
exp (x) the type of x m u s t be e i t h e r ~ e a l or 3 n t e g e r , and
ln(x ) the type of t h e r e s u l t is ~ e a l .
sqrt (x)
erctan (x)

11.1.2. ~e~_~tes

odd(x) the type of x m u s t be ~ o t e ~ w J : . e n d t h e result is


true, if x is odd, and false otherwise.

eof(f) eof(f) indicates, whether the file f is in the


end-of-file status.

eoln(f) indicates the end of a line in e textfile (see


chapter 12).
164

11.1.3. ~&sfer funct~j&~&

trune(x) the real value x is truncated to its integral


Part .

round (x) the real argument x is rounded to the nearest


integer.

ord (x) x m u s t be of a s c a l e r t y p e ( i n c l u d i n g Boolean and


char), and the result (of t y p e i n t e g e r ) is t h e
ordinal number of t h e v a l u e x in t h e s e t d e f i n e d
by t h e t y p e o f x.

chr (×) x m u s t be o f t y p e i n t e g e r , a n d the result (of t y p e


char) is the character whose ordinal n u m b e r is x
(if it e x i s t s ) .

11.1.4. Fw.~_he~ standard functions

succ(x) x is of any scalar or subrange type, and the


result is the successor v a l u e of x (if it e x i s t s ) .

pred(x) x is of any scalar or subrange type, and t h e


result is the predecessor value of x (if it
exists).

12. Input end output

The basis of l e g i b l e i n p u t a n d o u t p u t a r e t e x t f i l e s (cf.6.2.4)


that are passed as program parameters (cf. 13) t o a PASCAL
program and in its environment represent some i n p u t or output
device such as e terminal, a card reader, or a line printer. In
order to facilitate the handling of textfiles, the four standard
procedures ~£.~3~, writE, ~eadlo, and ~rite!~ are introduced in
addition t o ~e~ a n d ~W~. T h e y c a n be a p p l i e d to textfiles only;
however, these textfiles must not necessarily represent
input/output devices, but cam also be local files. The new
procedures are used with a non-standard syntax for their
parameter lists, allowing, among other things, for a v a r i a b l e
number of parameters. Moreover, the parameters must not
necessarily be of t y p e c h a r , b u t m a y a l s o be of c e r t a i n o t h e r
types, in which case the date transfer is a c c o m p a n i e d by an
implicit data conversion operation. If the first parameter is s
file variable, then this is t h e f i l e to be r e a d or w r i t t e n .
Otherwise, the standard files iO&ut and ~g~&~& are automatically
assumed as default values in t h e c a s e s of r e a d i n g and writing
respectively. These two files are predeclared as

vat input 0 output : text

Textfiles represent a special c a s e a m o n g f i l e t y p e s i n s o f a r as


texts are substructured into l i n e s by s o - c a l l e d line markers
(cf. b.2.4.). If, u p o n r e a d i n g a textfile f, t h e f i l e p o s i t i o n
165

is a d v a n c e d to a l i n e m a r k e r , t h a t is p a s t t h e l a s t c h a r a c t e r of
a line, then the value of the buffer variable ft becomes a
blank, and the standard function ~#_~O(~) ( ~ n d £ £ l i n e ) yields
the value true. Advancing the file position once more assigns to
f~ the first character of the next line, and eoln(f) yields
false (unless the next line consists of 0 c h a r a c t e r s ) . Line
markers, not being elements o f t y p e c h a r , c a n o n l y be g e n e r a t e d
by t h e p r o c e d u r e ~r~teln,

12.1. !~__~r_Q~edure r~d~

The following rules hold for the procedure ~ ; f denotes a


textfile and vl...vn denote variables of t h e t y p e s char, integer
(or s u b r a n g e of i n t e g e r ) , or r e a l .

1. r e a d ( v 1 .... ,vn) is equivalent to read(input,v 1 ..... vn)

2. read(f,v 1 ..... vn) is equivalent to read(f,v 1); ... ;


read(f,vn)

3. if v is a v a r i a b l e of type ~b&2, then read(f,v) is equivalent


to v := f? ; g e t ( f )

4. if v is a v a r i a b l e of type integer (or s u b r a n g e of integer)


or real, then read(f.v) implies the reading f r o m f of a
sequence of characters which form a number according to t h e
syntax of PASCAL (cf. 4.) a n d t h e a s s i g n m e n t of t h a t n u m b e r
to v. P r e c e d i n g blanks and line markers are skipped,

12.2. !~£,__2~&~ure r ~

1. r e a d l n ( v l ..... vn) is equivalent to readln(input,vl ..... vn)

2. readln(f,vl ..... vn) is equivalent to

read(f,v 1 ..... vn): readln (f)

3, r e a d l n ( f ) is equivalent to

~ h i ] d ~ D_~& e o l n ( f ) ~ get(f);
get(f)
Readln is used to read and subsequently skip to the beginning
of the next line,

12.3. !b~_~roe~dure~r_i~

The following rules hold for the procedure ~i~-~; f denotes a


textfile, p 1 ..... pn d e n o t e s o - c a l l e d write-parameters, e denotes
an e x p r e s s i o n , m and n denote expressions of t y p e i n t e g e r .

1. w r i t e ( p 1..... pn) is equivalent to write(output,p1 ..... pn)


166

2. w r i t e ( f , p 1 ..... pn) is equivalent to

write(f .pl); .., ; write(f,pn)

3. The write-parameters p have the followin~ forms:

e :m e :m : n e

e represents t h e v a l u e t o be " w r i t t e n " on t h e f i l e f, a n d m


and n are so-celled field width parameters. If t h e v a l u e e,
which is either a number, a character, a Boolean value, or e
string requires less than m characters for its
representation, then an a d e q u a t e n u m b e r of b l a n k s is i s s u e d
such that exactly m characters are written. If m is o m i t t e d ,
an implementation-defined d e f a u l t v a l u e w i l l be m s s u m e !. T h e
form with the width parameter n is applicable on~y if e is of
type reel (see r u l e 6 ) .

4. if e is of t y p e c h a r , t h e n
write(fo e : m ) is e q u i v a l e n t to
f~ := °: put(f); (repeated m-1 times)
ft : = e ; p u t (f)
N o t ~ : t h e d e f a u l t v a l u e f o r m is in t h i s c a s e I,

5. If e is of t y p e i~_~_W_g~ (or a s u b r a n o e of i n t e g e r ) , then the


decimal representation of t h e n u m b e r e w i l l he w r i t t e n on t h e
file f, preceded by an appropriate number of b l a n k s as
specified by m.

6. If e is of t y p e f e e l , a decimal representation of t h e n u m b e r
e is w r i t t e n on t h e f i l e f, p r e c e d e d by a n a p p r o p r i a t e number
of blanks as specified by m. If t h e p a r a m e t e r n is m i s s i n g
( s e e r u l e 3), e f l o a t i n o ~ o i n t representation eonsistino of a
coefficient and a s c a l e f a c t o r w i l l be c h o s e n . Otherwise a
fixed~oint representation with n dioits after the decimal
p o i n t is o b t a i n e d .

2. if e is of type ~ i ~ , then t h e w o r d s T R U E or FALSE are


written on t h e f i l e f, p r e c e d e d by an a p p r o p r i a t e n u m b e r of
b l a n k s as s p e c i f i e d by m,

C. if e is an (packed) array of characters, then the string e is


written on the file f.

12.4. T h e _ ~ r o c e d u r e _ ~ r it e l a

I. w r i t e l n (p I ..... pn) is equivalent to writeln ( o u t p u t ,p I ..... p n )

2. w r i t e l n ( f , p 1,...,pn) ~s equivalent to write(f,p I ..... p n ) ;


w r i t e l n (f)

3. w r i t e l n ( f ) appends a line marker (cf.6.2,4) to the file f.


167

12.5. ~ditional_grocedu~es

page(f) causes skipping to the top of a new page. when the


t e x t f i l e f is p r i n t e d .

13. Programs

A Pascal program has the form of a procedure declaration except


for its heading.

<program> ::= <program heading> <block> .

<program heading> ::=


~raoram <identifier> (<program parameters>) ;

<program parameters> ::= <identifier> { , <identifier> }

The identifier f o l l o w i n g t h e s y m b o l ~roorej~ is t h e p r o g r a m n a m e ;


it has no f u r t h e r s i g n i f i c a n c e inside the program. The p r o o r a m
parameters denote entities that exist outside the program, and
through which the program communicates w i t h its e n v i r o n m e n t .
These entities (usually files) are called ~9/:~ , a n d must be
declared in the block which constitutes the program like
ordinary local variables.
The two standard files i~&W_~ a n d ~ u t o u ~ m u s t not be d e c l a r e d
(cf. 12)0 but have to be l i s t e d as p a r a m e t e r s in t h e p r o g r a m
heading, if they are used. The initialising statements
reset(input) and r e w r i t e ( o u t p u t ) are automatically generated and
must not be s p e c i f i e d by t h e p r o g r a m m e r .

Examples:

or~g~, c o p y (f ,~ ) ;
v_all f.g: _file o f r e a l ;
~ e o i ~ r e s e t (f); r e w r i t e ( g ) ;
w h i l e n o t e o f ( f ) do
~eoio gT : = f ~ ; p u t ( g ) ; get(f)

oad •

orw_qram e o p y t e x t (input , o u t p u t ) ,
)jar oh : c h a r ;
begin
w~LiiO D o t e o f ( i n p u t ) lip
ba_q~a
while not eoln(input) do
bwlljJ3 r e a d ( c h ) ; w r i t e ( c h )
ea~ ;
readln; writeln
eo/
168

14. A s t a n d a r d for implementation and program interchange

A primary motivation for the development of P A S C A L w a s t h e n e e d


for a powerful end flexible l a n g u a g e t h a t c o u l d be r e a s o n a b l y
efficiently implemented on m o s t c o m p u t e r s . Its features w e r e to
be defined without reference to any particular machine in o r d e r
to facilitate the interchange of programs. The following s e t of
Proposed restrictions is designed as a guideline for
implementors and for programmers who anticipate that their
programs be used on d i f f e r e n t computers. The purpose of t h e s e
standards is to increase the likelihood that different
implementations will be compatible, and that programs are
transferable from one installation to a n o t h e r .

1. I d e n t i f i e r s denoting distinct objects must differ over their


first U characters.

2. L a b e l s consist of at most 4 digits.

3. The implementor may s e t a l i m i t to t h e s i z e of a b a s e t y p e


o v e r w h i c h a s e t can be d e f i n e d . (Consequently, a bit pattern
representation may reasonably be u s e d f o r s e t s . )

4. The first character on each line of printfiles m a y be


interpreted as a p r i n t e r control character with the following
meanings:
blank : single spacing
°0' : double spacing
"1" : print on top of next page

Representations of P A S C A L in t e r m s of available character sets


should obey the following rules:

5. Word symbols - such as ~eqiA, ~nd, etc. - are written as a


sequence of letters (without surrounding escape characters).
They may not be used as identifiers.

6. Blanks, ends of lines, and comments are considered as


separators. An arbitrary number of separators may occur
between any two consecutive PASCAL symbols with the following
restriction: no separators must occur within ~dentifiers,
numbers, and word symbols.

7. At least one separator must occur between any pair of


consecutive identifiers, numbers, or w o r l d s y m b o l s .
169

15. I n d e x

actual parameter 9.1.2.


adding operator 8.1.3
array type 6.2.1
array variable 7.2.1
assignment statement 9.1.1
base t y p e 6.2.3
block 10.
case l a b e l 6.2.2
c a s e l a b e l list 9.2.2.2 and 6.2.2
case l i s t e l e m e n t 9.2.2.2
case statement 9.2.2.2
component type 6.2.1
component variable 7.2
compound statement 9,2.1
conditional statement 9,2.2
constant 5.
constant definition 5.
c o n s t a n t d e f i n i t i o n part 10.
constant identifier 5.
control v a r i a b l e 9.2.3.3
digit 3.
digit s e q u e n c e 4.
element 8.
e l e m e n t list 8,
empty statement 9.1
entire variable 7.I
expression 8.
factor 8.
field designator 7.2.2
field identifier 72.2
f i e l d list 62.2
file b u f f e r 72.3
file t y p e 62.4
file v a r i a b l e 72.3
final v a l u e 92.3.]
fixed p a r t 62.2
for list 92.3.3
formal Parameter section 10.
for s t a t e m e n t 9.2.3.3
function declaration 11.
function designator 8.2
furct ton h e a d i n g 11.
function identifier B.2
goto s t a t e m e n t 9.1.3
identifier 4.
if s t a t e m e n t 9.2.2.1
index t y p e 0.2.1
indexed variable 7.2,1
initial value 9.2.3.3
label 9.
label d e c l a r a t i o n part 10.
letter 3.
l e t t e r or digit 4.
multiplying operator 8.1.2
170

p a r a m e t e r group 10.
pointer variable ?.~
pointer t y p e 6.3
p r o c e d u r e and function
d e c l a r a t i o n part 10.
Procedure declaration 10.
procedure heading 10.
p r o c e d u r e identifier 9.1.2
p r o c e d u r e or function d e c l a r a t i o n 10.
p r o c e d u r e statement 9.1.2
program 13.
program h e a d i n g 13.
program P a r a m e t e r s 13.
record s e c t i o n 6.2.2
record type 6.2.2
record variable 7.2.2
record variable list 9.2.4
referenced variable 7.3
r e l a t i o n a l operator 8.1.4
repeat s t a t e m e n t 9.2.3.2
repetitive statement 9.2.3
result type 11.
scale factor 4.
scalar t y p e 6.1 .t
set
set t y p e 6.2.3
sign 4.
simple e x p r e s s i o n U.
simple s t a t e m e n t 9.1
simple type 6.1
special symbol 3.
statement 9.
statement part 10.
string 4.
structured statement 9.2
structured type 6.2
subrange type 6.1 .3
tag field 6.2.2
term 8.
type 6.
type d e f i n i t i o n 6.
type d e f i n i t i o n pert 10.
type i d e n t i f i e r 6.1
variable 7.
variable d e c l a r a t i o n 7.
variable d e c l a r a t i o n part 10.
variable i d e n t i f i e r 7.1
variant 6.2.2
variant part 6.2.2
u n l a b e l l e d statement 9.
unpacked s t r u c t u r e d type 6.2
unsigned constant 8.
unsigned integer 4.
unsigned number 4.
unsigned real 4.
with s t a t e m e n t 9.2.4
while s t a t e m e n t 9.2.3.1

Potrebbero piacerti anche