Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
Kathleen densen
Niklaus Wirth
ETH Zurich
June 1974 Switzerland
Table of Contents
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
References . . . . . . . . . . . . . . . . . . . . . . . . . I04
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
by N. Wirth
{ 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.
block
~1 .... i~.di,l,~o~ I-
j ~ I ~~G-o~
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,)
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
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 construct:
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 . )
~ l e t t e r ~
illegal identifiers:
3rd array level.4 root-3
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
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
A. T h e type Boolean
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
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
* 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
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
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 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
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.
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
examples:
'~" 'G" '3 ° "°°' °X °
One sees immediately that ord and chr are inverse functions.
i.e.
chr(ord(c)) = c -and- ord(chr(i)) = i
A. Program heading
example:
i~el ~,1~;
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 . )
{ program 3.1
example of constant definition pert }
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
example :
v~ rootl,root2.root3: reali
count,i: integer ;
found : Boolean;
filler : chari
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
(x>O) ~ (x<lO)
x := O:
~eoeat x := x+1 ~ntil (x>lO) ~ (a[x]=O)
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 )
{ program 4.1
the compound statement }
~ro~ram beginend(output);
B -8
22
C. R e p e t i t i v e statements
C .1 T h e while statement
{ program 4.2
compute h(n) = I + 1/2 + I/3 + o.. + I/n }
10 2.928968253968e+00
{ program 4.3
compute h(n) = I + I/2 + I/3 + ... + I/n }
10 2.928968253968e+00
{ 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) - ... }
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
for v := el & g e2 ~g S
j~ e1<=e2 &hen
~eain v := el; S; v := succ(v); S: ...; v := e2;
~or v := el #ownto e2 ~ g S
if el>=e2 then
beain v := el; S; v := pred(v); S; ...; v := e2; S
ea~
{at this point, v is undefined}
{ 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}
~.930991830595e-01 6.930971830612e-01
6.930971830599e-01 6.930991830601e-01
26
D. Conditional statements
D .I The if s t a t e m e n t
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
if ci t h e n sl
e l s e i f c2 S b e n s 2
else ...
found := a = b
28
{ program 4.7
write roman numerals }
~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 .
{ program 4.B
exponentiation with natural exponent }
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
{ orogram 4.9
graphic representation of e function
f(x) = exp(-x) * sin(2*pi*x) }
95
31
cas~ <expression> ~Z
<case label list> : <statement>;
~ot~ <label>
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
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. 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.
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 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.
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 := b; c := b; b:= c;
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 }
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 .
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);
!
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 . )
vat M : grrav[a..b] of a r r e v [ c . . d ] of T;
and
M [ i ] [j]
va____rr M : ~r_~av[a..b,c.,d] ~ T;
and
M[i.j]
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:
{ program 6.3
matrix multiplication 1
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
~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]
Applying these rules, one can state the following definition and
declaration:
a toy as :
family= (father,mother.childl,child2,child3);
~L~d~ v a c c i n e : g~[family] £L date:
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 }
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~
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.
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
ms : maritelstetus
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
woodyard
A)
edward
i i lliH
845680539 B)
iiiiii i
male c)
~ug i 30 i1941 D)
t
1 H,,,,, ,,
E)
single
F)
true
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
Likewise.
_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
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 )
with vaccine[child3] d~
b e Q i ~ mo := a p t : day := 23: year := 1973
grid
w__ith r do S
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;
y_~ a : integer ;
b : ~eoo~_~[ a: r e a l ; b : Boolean
end ;
wi~h b ~ g S
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.
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.
+ 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 . )
= <> 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
~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 ;
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
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=[]
{ 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 . }
~ 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 .
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 .
Program parts :
{ 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
{ program part
merge f and g into h }
--end
A. Textfiles
rewrite(y);
~eeeat
£eo eat P(c); w r i t e (y~.'h)
wntil p ;
wr it e l n (y)
until q
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
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
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
r eadln r e a d l n (input)
eof e a r (input)
~leo eat
~ e o e a ~ P (ch); w r i t e ( c h )
unti~, p ;
writeln
uDtil q
60
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
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.)
end .
~ link = fperson;
person = ~ecord
. . ,
next : link:
~nd:
first
Figure 10.a Linked list
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~
pt := first;
~j~le ptt.ss <> n do pt := p t t . n e x t
pt := f i r s t ;
wh~l& (pt <> nil) and (ptt .ss <> n) d O pt := p t t .next
(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
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:
d i s p o s e (p)
or"
65
d l s p o s e ( p ,t I .... in)
newp
newpt .next := p i t . n e x t :
pt~ .next :: newp
newp ~ I p n e e w #
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
p l t . n e x t := p 2 t . n e x t ;
d i s p o s e (p 2 )
A, Procedures
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}
-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
~rocedure <identifier>:
69
{ program 11.2
extend program 11.1 }
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;
-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
{ program 11.3
procedure Parameters }
1 1
0 1
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.
(a+b) ~ (c-d)
a +b *c -d
( a + b)* c-d
a+b* (c-d)
b+c*(d+c*a*a )*b+a
expression
term
J factor I
if actor
factor
identifier
d
v I letter I
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 +
abc..deo,fg...hi..jkt..m..n..
{ program 11.5
binary tree traversal }
~ro0ra~ traversal(input.output):
e_~: {preorder}
~ 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
B. Functions
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 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 }
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~.
{ program 11.7
test side effect }
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
{ p r o g r a m 11.8
extent p r o g r a m 4.8 }
proora~ expon2(output);
_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
{ 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 ) ;
&~#.~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
C. Remarks
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.
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.
examples:
reset (f) ;
reoe~& read(f,x,ch):
P(x)
~t~_~l ch :°*"
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
B. The p r o c e d u r e write
e
e : el
e : el : e2
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.
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
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
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 .
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 .
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.
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
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~
examp le :
Proora~ P (output, x, y);
~_~_ x ,y text ;
p r or ~ p (output,f ,g);
B.2 Representation of f i l e s
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) )
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 . .
INTEGER
REAL
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 :
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
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 ~ ,,
not -7
and A &
o__r V
<>
<= _<
>= >
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
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
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~;
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 .
{ program 13.1
al£a values }
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)}
~roce~ures
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.
discussed in s e c t i o n 13.A.I.
~unctlons
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 ) .
~r_~wJE~ Pascal(input.output,lgo);
B . Compiler options
Example: {$T+.P+ }
default = T+
default = P+
default = X4
default : E-
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
C. Error messages
0.1 Compiler
following information:
References
and
u n p a c k (z ,a , i ) is equivalent to
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.
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 .
Appendix B
~ / i v of Operations
arithmetic:
+ (unary) identity integer or r e a l same as
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
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
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
Types:
alfa
Functions:
card, clock, eos, expo, undefined
Procedures:
data, getseg, halt, iinelimit, message, putseg, time
110
Appendix
~vnt ax
::= 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}
A : := < e m p t y > [ AB
BNF
<sign> ::= + ~ -
{ , <actual parameter>} )
<empty> : :=
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
~-<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
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
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
Appendix F
~roorammin~ ~amo~&
[ PASCAL 6000-3.4 }
~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
[ PASCAL 6000-3.4 }
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
xl xl.x2 xl.x2.x3
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 :
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 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
Contents
I. I n t r o d u c t i o n
5. Constant definitions
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
13. Programs
15. I n d e x
136
I. I n t r o d u c 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 ~ 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,
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
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 } .
Examples :
1 100 O. I 5E-3 8 7 . 3 5 E +8
Examples :
"A' °; . . . . .
'PASCAL" °TI{IS IS A STRING "
5. C o n s t a n t definitions
6. D a t a type definitions
6.1. S i m o Z ~ ~y_a_~
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)
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 )
Examples: 1..100
-10 .. +10
Nonday .. Friday
6.2.3. _Se~,__t~y_p_es
+ union
- set difference
* intersection
iS membership
6.2.4. Lila__~yoes
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
6.3. ~ o i n t ~ r tvoes
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
7.I. Entlre~riables
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.
Examples :
a [ 12]
a[i+j]
b [ r e d ,true]
Examples :
U .re
hired,true] .im
p 2~ .s i z e
148
7.3. ~ ~ e d variables
Examples:
p~ .father
p I~ .sibling~ .child
8. E x p r e s s i o n s
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
8,1.3. ~ddio,~__~eretors
~3.I.4. _ R e l a t i o n a l ooeratQ~&
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
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).
9. S t a t e m e n t s
<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>
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 .
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 )]
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
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~:
Example: ~ z := x ; x := y; y := z ~
9.2.2, ~&~_~tional_~J~.O~
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 .
~ote:
The syntactic ambiguity arising from the construct
~ <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
selector .
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.1o ~ile__~tatemen~&
~hile B do S
is equivalent to
i£ B £hea
be£in 8 ;
wbile B do S
end
156
Examples:
~]3ile ~ e e l ( f ) da
~W~O P ( f t ); g e t ( f )
<repeat statement> : :=
reoeat <statement> { :<statement>} until <expression>
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
9.2.3.3. Egj~_~_~t e m e n t s
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.
for v := el to e2 do S
fD~l v := el do~jl~J& e 2 d o S
Examples :
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~
9.2.4. ~h_~tatements
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
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
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,)
a: &~&~ [m..n] of T
z: oack~ azrav [u..v] of T
11. F u n c t i o n declarations
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
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
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
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.2. !~£,__2~&~ure r ~
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~
e :m e :m : n e
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,
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 .
12.4. T h e _ ~ r o c e d u r e _ ~ r it e l a
12.5. ~ditional_grocedu~es
13. Programs
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
15. I n d e x
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