Jensen K., Wirth N. PASCAL User Manual and Report.pdf

Share Embed Donate


Short Description

Download Jensen K., Wirth N. PASCAL User Manual and Report.pdf...

Description

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, 1949PASCAL: 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 photocopying 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.

June

1974

Kathleen densen Niklaus Wirth ETH Zurich Switzerland

Table

of Contents

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

Introduction

I. N o t a t i o n

and

. . . . . . . . . . . . . . . . . . . . . . . .

3

Vocabulary . . . . . . . . . . . . . . . . . . .

9

2. T h e A. B. C. D.

Concept The type The type The type T h e type

of D a t a Boolean integer real . char .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . . . . . . . ..... . . . . . . . . . .

3. T h e A. B. C. D. E. F.

Program Heading and the Declaration Part . . . . . . . . The program heading . . . . . . . . . . . . . . . . . . . The label declaration part . . . . . . . . . . . . . . . . The constant definition part . . . . . . . . . . . . . . . The type definition part . . . . . . . . . . . . . . . . . The variable declaration part . . . . . . . . . . . . . . The procedure and function declaration part . . . . . . .

12 12 13 14 14 16 16 16 16 18 18 19

4. T h e C o n c e p t of A c t i o n . . . . . . . . . . . . . . . . . . . . . A. T h e a s s i g n m e n t s t a t e m e n t . . . . . . . . . . . . . . . . . B. T h e c o m p o u n d s t a t e m e n t . . . . . . . . . . . . . . . . . . . C. R e p e t i t i v e statements . . . . . . . . . . . . . . . . . . C.I T h e w h i l e s t a t e m e n t . . . . . . . . . . . . . . . . . C.2 The repeat statement . . . . . . . . . . . . . . . . . C.3 The for statement . . . . . . . . . . . . . . . . . . D. C o n d i t i o n a l statements . . . . . . . . . . . . . . . . . . D.I T h e if s t a t e m e n t . . . . . . . . . . . . . . . . . . . D.2 The case statement . . . . . . . . . . . . . . . . . . E. T h e g o t o s t a t e m e n t . . . . . . . . . . . . . . . . . . . .

2O 20 21 22 22 23 23 26 26 31 31

5. S c a l a r a n d S u b r a n g e T y p e s . . . . . . . . . . . . . . . . . . A. S c a l a r t y p e s . . . . . . . . . . . . . . . . . . . . . . . B. S u b r a n g e t y p e s . . . . . . . . . . . . . . . . . . . . . .

34 34 35

6.

Structured

7. R e c o r d A. T h e 8.

Set

Types

in G e n e r a l

-- T h e

Array

in Particular

.

Types . . . . . . . . . . . . . . . . . . . . . . . . with statement . . . . . . . . . . . . . . . . . . . .

Types . . . . . . . . . . . . . . . . . . . . . . . . . .

9. ~ i l e T y p e s . . . . . . . . . . . . . . . . . . . . . . . . . A. T e x t f i l e s . . . . . . . . . . . . . . . . . . . . . . . . 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 " . . . . . . . . . 10.

Pointer

11.

Procedures and A. P r o c e d u r e s . B. F u n c t i o n s . C. R e m a r k s . .

Types . . . . . . . . . . . . . . . . . . . . . . . .

12.

Input and Output A. T h e p r o c e d u r e B. T h e p r o c e d u r e

13.

PASCAL 6000-3.4 . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . A.I S e g m e n t e d f i l e s . . . . . . . . . . . . . . . . . . .

36 42 47 5O 55 57 59 62

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67 67 78 82

. . . . . . . . . . . . . . . . . . . . . . read . . . . . . . . . . . . . . . . . . . . write . . . . . . . . . . . . . . . . . . .

84 84 86

Functions . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . .

. . . .

. . . .

. . . .

88 88 88

VI

A.2 External procedures . . . . . . . . . . . . . . . . . Specifications left undefined in the preceding chapters 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 . . . . . . . . B.2 Representation of files . . . . . . . . . . . . . . . B.3 The standard types . . . . . . . . . . . . . . . . . . B.4 The standard procedure write . . . . . . . . . . . . . C. R e s t r i c t i o n s . . . . . . . . . . . . . . . . . . . . . . . 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 . 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 . . . . . . . . . . . . . D.2 Additional predefined procedures and functions.. B.

14.

How to Use the PASCAL 6000-3.4 System . . . . . A. C o n t r o l s t a t e m e n t s . . . . . . . . . . . . . . B. C o m p i l e r o p t i o n s . . . . . . . . . . . . . . . C. E r r o r m e s s a g e s . . . . . . . . . . . . . . . . C.I C o m p i l e r . . . . . . . . . . . . . . . . . C.2 Run-time . . . . . . . . . . . . . . . . . References Appendix Appendix Appendix Appendix Appendix Appendix

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . . . . . . . . . . . . . . . . . Standard Procedures S u m m a r y of O p e r a t o r s Tables . . . . . . . . Syntax . . . . . . . . Error Number Summary Programming Examples

A B C D E F

and . . . . . . . . . .

Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . .

. . . . . .

. . . . . .

.

. . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

9O 91 91 92 93 96 97 97 97 98 I00 100 100 I O2 102 I02 I04 I05 I O8 I09 110 119 122 126

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . .

by N. Wirth Preface

to

the

1. I n t r o d u c t i o n 2.

Summary

3. N o t a t i o n ,

Revised

Report

of the

language . . . . . . . . . . . . . . . . . . .

terminology,

4.

Identifiers,

5.

Constant

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

Numbers

definitions

and and

vocabulary . . . . . . . . . . . .

Strings

. . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . .

133 136 137 140 140 141

. . . .

. . . .

. . . .

. . . .

. . . .

142 142 143 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 . . . . . 7.1. E n t i r e v a r i a b l e s . . . . . . . . . . . . . . . . 7.2. C o m p o n e n t v a r i a b l e s . . . . . . . . . . . . . 7.3. R e f e r e n c e d v a r i a b l e s . . . . . . . . . . . . . .

. . . .

. . . .

. . . .

. . . . .

146 147 147 148

6. D a t a 6.1. 6.2. 6.3.

type definitions . Simple types . . . Structured types . Pointer types . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

8.

Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 8.1. O p e r a t o r s . . . . . . . . . . . . . . . . . . . . . . . 8.2. F u n c t i o n d e s i g n a t o r s . . . . . . . . . . . . . . . . . .

148 149 151

9.

Statements . . . . . . . . . . . . . . . . . . . . . . . . . 9.1. S i m p l e s t a t e m e n t s . . . . . . . . . . . . . . . . . . . 9.2. S t r u c t u r e d s t a t e m e n t s . . . . . . . . . . . . . . . . .

151 152 153

10.

Procedure declarations . . . . . . . . . . . . . . . . . . . . 10.1.Standard procedures . . . . . . . . . . . . . . . . . .

158 160

11.

Function declarations . . . . . . . . . . . . . . . . . . . . 11.1.Standard functions . . . . . . . . . . . . . . . . . . .

163

162

VII

12. Input and Output 13. P r o g r a m s

. . . . . . . . . . . . . . . . . . . . .

164 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.

15. I n d e x . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

168 169

0 ~NTRODUCTION

Much

of

grasp of e

the

following

text

of computer terminology program. The purpose

assumes

the

reader

and a "feeling" of this section

for is

has

a minimal

the structure to spark that

intuition.

{ program assuming find the sterling, 1. 2 . . . . ~rocram

0.1 annual inflation rates o f 9 , 8 , a n d 10 p e r c e n t . factor by which the frank, dollar, pound m a r k , or g u i l d e r will have been devalued in n years.} inflation(output);

#onst n = 10 ; var i : integer; wl.w2~oW3 becin i := O; w l := 1.0; r_@~.~at i := i + I ; w I

:=

w I

*

: real; w 2 := 1.0;

w3

:=

1.0;

1.07;

w2 w3

:= w2 * 1.08; := W3 * 1 . 1 0 ; w r i t e l n (i .w l , w 2 . w 3 ) i=n end.

1 2 3 4 5 6

1.090000000000e+00 1.144900000000e+00 1.225043000000e+00 1.310796010000e+00 1.402551730700e+00 1.500730351849e+00

1.080000000000e+00 1.166400000000e+00 1.2597t2000000e+00 1.360488960000e+00 1.469328076UOOe+00 1.586874322944e+00

1.100000000000e+00 1.210000000000e+00 1.331000000000e+00 1.464100000000e+00 1.610510000000e+00 1.771561000000e+00

7

1.bObTU1476478e+O0

1.713824268779e+00

1.948717100000e+00

9 10

1.718186179832e+00 1.838459212420e+00 1.96915t359290e+00

1.850930210282e+00 1.999004627104e+00 2.158924997293e+00

2.14358~810000e+00 2.357947691000e+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 described The

are described by so-called

program

is

by so-called ~tatements, ~cl~ra~L~zns end ~L~finitions.

divided

into

a

~eadin~

and

end

a body,

data

is

called

b3ock. The heading gives the program s name a n d l i s t s parameters. (These ere (file) variables and represent arguments and results of the computation. See chapter 13.) file "output" is e compulsory parameter. The b l o c k consists six sections, where any except the lest may be e m p t y . I n required order they ere:

a

its the The of the



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-

9

j ~ I ~~G-o~ ~-~PROCEDUII~~--I

Figure O.a

p..... ter list I

Syntax diagrams defining the general structure of a program

"i

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 of f i g u r e O.a is d e f i n e d by the following formulas, called "productions": ::= < p r o g r a m heading> . ::~ ~ r o m r a ~ ( { . } ) ; ::= < 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

QF--q

I

Figure

O.b

Block

structure

level level level level

0 I 2 3

: = : :

M P, A, B

Q R,

S

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 A B

P ,A ,B A.B B

Q

Q ,R ,S

R S

R 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. 2.

3. 4.

5. 6.

7.

8. 9.

10.

Declaration of variables is mandatory, Certain key words (e.g. ~eain, ~nd, ~eoeat) ere "reserved" and cannot be used as identifiers. In this manual they are underlined. 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). 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. 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 " . Arrays may be of arbitrary dimension with arbitrary bounds; the array bounds are constant. (i.e. There are no dynamic arrays.) As in FORTRAN. ALGOL, and PL/I, there is a go to statement. Labels are unsigned integers and m u s t be declared. 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 . 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.

The

for

statement,

corresponding

F O R T R A N . may o n l y have steps is executed only as long variable lles within the controlled s t a t e m e n t m a y not

to

the

DO

loop of

of 1 (~g) or -1 (~ownto) and as t h e v a l u e o f t h e c o n t r o l limits. Consequently. the be e x e c u t e d at a l l .

11. 12. 13. t4. 15.

16.

There are no conditional expressions a n d no m u l t i p l e assignments. 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 . 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 ) . Parameters are called either by value or by reference: there i s no c a l l by name. 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. 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

The basic wocabularv consists letters, digits, and special operators and delimiters:

VOCABULARY

of b a s i c symbols.

symbols classified into The ~ . ~ ~ymbols are

+ -

: "

( )

and Drrav

and _f..il~

ail aot

/



]

case

function

~

• ,

: >

} % ..

~iv ~a downto 91se

iZ in /~l mod

£rocedure ~r_Qar.am ~ Keoea~

~hen

~ar ahila ~ith

~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: {}

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

~

Figure

l

1.a

e

t

t

e

r

~

Identifier

examples of legal identifiers: sum root3 pi h4g x thisisaverylongbutneverthelesslegalidentifier thisissverylongbutprobablythesameidentifierasabove illegal 3rd

identifiers: 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 digit must precede and succeed occur in a number.

unsigned 3

numbers : 03 6272844

0.6

a decimal point, the point. Also.

5E-8

49.22E +08

at least one no comma may

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 celled ~nos. the quote mark

characters enclosed To i n c l u d e a q u o t e twice.

examples of strings: "a' ";" "3 ° * t h i s s t r i n g has

by mark

single quote marks are in s s t r i n g , o n e w r i t e s

'begln" "don'°t " 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 the predefined identifiers false The following applied to Operators.)

and or no__t

logical Boolean

logical logical logical

operators operands:

logical truth and true. yield e (Appendix

values

denoted

Boolean value B summarizes

by

when all

conjunction disjunction 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 equivalence e x c l u s i v e OR

as as as

p = 0 t r u n c ( x + 0 . 5 ) , and for x < 0 t r u n c ( x - 0 . 5 )

Notes: abs and argument is also integer, then succ(1) yields pred(i) yields This is, h o w e v e r , i+1 and

a go

an

the

when:

14

a b s (a o@. b ) abs (a) abs (b)

C . The

type

{ ,

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



A l a b e l is d e f i n e d most 4 digits. example: i~el

C.

}; to

be

an

unsigned

integer,

end

consists

of

at

~,1~;

Constant

A ~&tao~ a constant. part, which f,Ji~&~

{,

definition

part

~efinition introduces an i d e n t i f i e r as a s y n o n y m for The symbol ~ introduces the constant definition has t h e g e n e r a l form:



= ;

{

= ;}

17

where a (possibly

constant signed),

is either" or a s t r i n g .

a

number,

a constant

identifier

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,

program example

~roaram #onst

consider

3.1 of constant convert

addin

the

following

definition

pert

program:

}

(output);

= 32;

mulby

=

1.8;

low

= O;

high

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 if odd(degree) then writeln end : writeln ; writeln (separator) and.

0c 2c 4c 6c 8c I0c 12c 14c 16c IBC 20C 22c 24c 26c 28c 30c 32c 34c 36c 38c

32f 36f 39f 43f 46f 50f 54f 57f 61f 64f 68f 72f 75f 79f 82f 86f 90f 93f 97f 100f

Ic 3c 5c 7c 9c 11c 13c 15c 17e 19c 21c 23c 25c 27c 29c 31c 33c 35c 37c 39c

34f 37f 41f 45f 48f 52f 55f 59f 63f 66f 70f 73f 77f 81f 84f 88f 91f 95f 99f I02f

= 39;

+ addin),'f')"

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



Examples chapters.

E.

Variable

of

type

= : definitions

declaration

Every variable ~ar!ab!e ~ ~ a n y u s e of t h e

[ ere

found

in

the

subsequent

part

occurring in a s t a t e m e n t declaration . This variable.

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

= :}

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

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

m u s t be d e c l a r e d in a must textually Precede

identifier and a data type the identifier followed by variable declaration part.

: : : i}

reali

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:

:= < 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 . relational operator =. The statement current value of a is replaced with ~ecomes 5".

not t o be c o n f u s e d w i t h t h e "a := 5" is pronounced "the the value 5", or simply, "a

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 15 ~ 4 * 4 B0/5/3 4/2 *3 sqrt (sqr(3)+11*5) The syntax The reader

of is

= = = =

(2*3) (15 ~iv (80/5)/3 (4/2)*3

(4*5) 4)*4

Appendix D reflects the recommended to reference

= -14 = 12 = 5.333 = 6.000 = 8.000

exact rules of precedence. it whenever in doubt.

Boolean expressions have the property thst their value known before the entire expression has been evaluated. for example, that x=O. Then (x>O) is

~

already

may b e Assume

(xlO)

~

O,

program

the

(a[x]=O) will

refer

to

an

element

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 root I root 1 root3 found

assignments : := p i * x / y := - r o o t I := ( r o o t l + r o o t 2 ) * ( 1 . 0 := 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

+ y)

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 ~ro~ram

statement

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 The

while

while while

statement

statement

has



the

~g

form:



The expression controlling the repetition must 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 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) Qroeram

=

I +

1/2 +

egwhile(input,

I/3

+

o..

+ I/n

be care

of t y p e must be

}

output):

varn ~ealn

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

10

n

:= n-1

2.928968253968e+00

The statement executed statement in the above becomes false. If its

by the while statement (a c o m p o u n d 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 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

C.2

The

is

not

repeat

The repeat

has



The sequence is executed the Boolean iteration.

{ program 4.3 compute h(n)

at

all.

statement

statement

reoeat

orooram

executed

the

form:

{ ; }

until



of at

statements between the symbols ~eat and until least once. Repeated execution is c o n t r o l l e d by expression, which is evaluated after every

=

I +

I/2

egrepeat(input,

+

I/3

+

...

+

I/n

}

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

The above happens if correct for

2.928968253968e+00

program performs correctly nO, Consider what the same program is

statements that pair ~e~!~...~nd

the repeat w o u l d be

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



:= < i n i t i a l value> do < s t a t e m e n t >

to

do < s t a t e m e n t >

downto

value>

(or)

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 := writeln(h) end.

10

+

1/i;

2.928968253968e+00

{ program 4.5 compute the cosine using cos(x) = 1 -x*'2/(2"I) ~rQoram

h

cosine

(input,

the expansion: + x*'4/(4"3"2"I)

-

...

}

output);

const vat

eps = Ie-14; 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 ~DJ~ abs(t) > eps*abs(s) do ~e~D k :~ k + 2; t := -t*sx/(k*(k-1)); :=

S

end

:~ s q r ( x ) ;

s+t

;

writeln

(x ,s ,k d i v

2)

end end.

1.534622222233e-01 3.333333333333e-01 5.000000000000e-01 1.000000000000e+O0 3.141592653590e+00

9.882477647614e-01 9.449569463147e-01 8.V75825618904e-01 5.403023058681e-01 -1.000000000000e+O0

5 6 ? 9 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

for is

v

of t h e

:= el & g

equivalent

to

e2 ~g the

j~ e1=e2 then beain v := el; ea~ {at this point,

example

of

:=

of t h e

#ownto to

S

sequence

S;

point,

:=

form:

4 ways. then

subtract

then

subtract}

(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 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.930971830599e-01

6.930971830612e-01 6.930991830601e-01

:=

O;

26

Why

do t h e

four

D. Conditional

"identical"

sums

differ?

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

if

for an

if s t a t e m e n t

are:



~hen





~hen



(or) if

else



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

is i n c o r r e c t . if

p then;

$1;

Perhaps ~#ain

Here. the statement between the then statement following

S2;

even

$1;

S2;

$3 end;

more

else

$4

deceptive

is t h e

text:

$3 ~nd

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 . and the semicolon; hence, the compound 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 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 iL ~hen ~eain if ~hen else

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 statement can be very c o s t l y . Take t h e e x a m p l e n-mutually exclusive conditions, c1.,.Cno each

to

f o r m u l a t e d if w h e r e one has instigating a

27

distinct action, si. Let P ( c i ) be t h e true, and say that P(ci)>=P(cj) for i < j . s e q u e n c e o f if c l a u s e s is : if

probability of ci b e i n g Then the most efficient

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

The fulfillment of statement completes remaining tests.

if

c(n-1)

~hen

s(n-1)

e!se

a condition and the execution of the if statement, thereby bypassing

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 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 A much

J~b~.J3 f o u n d

simpler

found

:= a = b

sn

:= t r u e

statement

is:

:lse

found

:= f a l s e

frequent

its the

abuse

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°): if x>=500 then bea~j3 w r i t e ( ' d ") ;

"): x

:= x - l O 0 0

~n~:

x

:= x - 5 0 0

end :

x

:= x - l O 0

end:

x

:= x - 5 0

~nd:

x

:= x - t 0

eZld;

x

:= x - 5

end;

x

:= x - 1

end;

w_h_Ale x>=lO0 d o if

b~gin write('c°): x > = 5 0 t, hen beEi_.on w r i t e ( ' l ' ) :

w_hi!e x>=10 d~ be_~i~n

write('x'):

if x > : 5 ~hsn beoln x >= be~i[z writeln;

~h_~le

write('v'); 1 ~l write('i'): y : : 2*y

until y>5000 and.

I 2 4 U 16 32 64 128 256 512 1024 2048 4096

i ii iiii viii xvi xxxii lxiiii cxxviii cclvi dxii mxxiiii mmxxxxviii 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 #j~oera~

with

natural

exponent

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

b ~

{z*u**e

do

while ~_~ b~_~in

en~. ; := e - l ;

e a~; writeln(z)

= x**y,

z {z

e>O}

odd(e) do e :-- e d i v

2:

u

:= s q r ( u )

:= u * 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 f(x) = exp(-x) * sin(2*pi*x)

&re,ram

function }

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~ ~Z

=

., < 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;

The associated assignments a

:= b;

c

b:

0..30;

scalar

:= b;

b:=

type

c:20..30; for

a.

b,

and

e is

integer.

Hence

the

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): 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 }

{scalar

type}

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.)

STRUCTURED TYPES

IN

6 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 the

definition of an a r r a y s p e c i f i e s 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]

both

the

component

of

variable

declarations

memory : ~.~L~[O..max] sick : ~r_~[days] £~ (Of c o u r s e these identifiers.)

and

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 scalar type (where typos integer and real are index types); a n d T 2 is a n y t y p e .

examples

type

examples

-and-

~L integer Boolean assume

the

sample

t y p e a n d is a not allowable

assignments

memory[i+j] := x sick[men] := t r u e definition

of

the

auxiliary

37

{ program find the mrooram

part 6.1 largest and

minmax(input,

smallest

number

in a g i v e n

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 , contains the values: 35 6 8 94 7 88 -5 -3 - 6 3 0 -2 74 88 52 4 3 5 4} rain := a[ while i < beoiQ u _if u > v

I] ; m a x := rain; i := 2; n do := a]~i] ; v := a [ i + 1 ] ; 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 ] 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] O implies counting s e g m e n t s in t h e f o r w a r d direction: n or (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 ~i

testdata (output.

d a t a : "f_ila ~ f r: r e a l ; r

:='data~ ;

data*) :

real ;

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 ~on~

identifier

maxint

=

~axint

is

281474976710655;

defined {

as

= 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 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 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 .

point to 14

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 2) T h e C D C 3) The CDC

Scientific 64-character Scientific 63-character A S C I I 6 4 - c h a r a c t e r set

set 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

~

<

>

not

used

set

version

, ^

~

Comments: -

~

- 51

:

in

the

- 48

'

at

ETH

-

53

t

at

ETH

-

57

t

at

ETH

ASCII

Character

1

in

63-character

5et

2

63-character

with

set

CDC's

version

ordering

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 -7

not and o__r = Figure

B.4

The

13.b:

standard

_< > Alternative representation standard symbols

of

"'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 default values are assumed.

is s p e c i f i e d ,

the

following

default

integer real

10 22

Boolean

10

char

a string a l f a (see

&

A V

procedure

type

ASCII

( 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

)

1

length D.I)

of t h e

string

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 °+"

blank "0"

no llne f e e d (overprinting) single spacing 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 that

reading, a textfile were never explicitly

(as

may c o n t a i n blanks at the end written. (Sorry about this!)

C.

Restrictions

of

1.

The word

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

se.qmented

March

of

lines

1974)

is r e s e r v e d .

thereof)

OF

b)

c)

3.

a subrange with a minimum element greater than or equal to zero, and a maximum element less than or equal to 58, or a subrange of the type chat with the maximum element less than or equal to the value chr(58).

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 ... e~ :

be~in

f: r e a l ;

e,b:

real):

real;

..•

be~in read(x,y); r e a d ( x oy ) ;

4.

It is records

not and

D. Additional

D,I

Additional

The t y p e ~ i f & ~y~

ella

writeln writeln

( x .y , z e r o ( s i n e , x , y ) ) ; (x ,y , z e r o ( c o s i n e , x , y )

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 , 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 .

predefined

types,

predefined

procedures,

by:

= ~acked ~rrav[1..10] type of

functions

types

is p r e d e f i n e d

(Hence, a value of word.) The constants characters.

and

ella this

~

char;

is representable type are &trinqs

in exactly of exactly

one 10

98

Applicable comparison, test order values may

on operands of type ella are assignment where = a n d t e s t equality and set. Alfa

{ 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

Note: It is the following vi~

raymond

not possible to is s u g g e s t e d :

read

alfa

values

directly;

instead,

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

halt

terminates the execution 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 (Hence. x should contain at

time (a)

assigns

putseg

.

getseg,

and

to

to the

the

the

alfa

alfa

extensions

variable

variable to

the of

current the

date.

program

and

into the dayfile. most 40 characters.) a the rewrite

current and

reset

time. are

99

discussed

in s e c t i o n

13.A.I.

~unctlons

card(x)

equals number

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 the floating~oint value x.

undefined(x)

a Boolean function. Its value real value x is "out of r a n g e " o t h e r w i s e f a l s e [ 7] .

cos(x)

(discussed

the cerdinal±ty of the s e t x (i.e. t h e 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.)

v a l u e e q u a l to t h e e x p o n e n t of representation of the real

in s e c t i o n

13.A.1)

is t r u e w h e n t h e or " i n d e f i n i t e " ,

14 Use ~he ~ASCAL ~ 0 0 0 - 3 . 6 ~ v s t e m

Uow ~ 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 ~r_~wJE~

itself

is a l s o

a Pascal

program.

Its

heading

is

Pascal(input.output,lgo);

The first formal source p r o g r a m to end t h e t h i r d , the

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 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 ; 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

{$

{$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 T

following include a) b)

e) d)

e)

tests

presently that

available"

chock

= T+

generate Dump (see default

X

run-time

are

all array indexing operations to insure that the index lles within the specified array bounds. 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 . all divisions to i n s u r e a g a i n s t zero divisors 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) 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

E

= X4

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 point name equal to first seven characters.

"extern" or the procedure

"fortran" get an e n t r y identifier cut to the

102

-- L o c a l value module EE+

modules of the name):

get an E-option

A u n i q u e s y m b o l is The first seven taken.

entry (at

point name the moment

generated characters

by

depending on t h e of analyzing the

the compiler of the module

name

ere

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 L

U

:

controls

the

default

= L+

listing

of

the

program

text.

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 Uthe 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

B

E-

= U-

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= v - ~ , then this is equivalent .f_or j

u n p a c k (z ,a , i )

:= u J~D. v d o z [ j ]

:=

and is equivalent

to

for

v do a[j-u+i]

j

::

u to

e[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 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

to

variable

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 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 integer.

same

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 The t y p e o f t h e r e s u l t is always real.

as

that

real

or

cos(x) aretan

(x)

exp ( x ) ln(x) sqrt(x)

(natural (square

Predicates

logarithm) root)

(Boolean

functions)

odd ( x )

the t y p e of x m u s t be i n t e g e r ; is o d d , o t h e r w i s e f a l s e .

eoln(f)

returns f, the false.

eof(f)

returns the value true when, the "end-of-file" is reached;

the

result

is t r u e

if x

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 end of t h e c u r r e n t line is r e a c h e d ; otherwise,

while reading the otherwise, false.

file

f.

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 , 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 trunc(x-O.5), for

of t y p e

integer,

x > 0 x < 0

ord (x)

the ordinal number v a l u e s d e f i n e d by t h e

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

of the a r g u m e n t t y p e of x.

x

in t h e

set

of

functions

succ(x)

x is is t h e

of any s c a l a r t y p e (except r e a l ) , a n d s u c c e s s o r v a l u e of x (if it e x i s t s ) .

the

result

pred(x)

x is is t h e

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 predecessor v a l u e of x (if it e x i s t s ) .

result

108

~ / i v

go e r a ~

o_

arithmetic: + (unary) - (unary) +

/ mad

Appendix B of Operations

£oeration

~Yoe

of # o e r a n d ( s )

assignment

any t y p e e x c e p t file t y p e s

identity sign inversion

integer

addition subtraction multiplication

integer

or r e a l

integer division real division modulus

integer integer integer

or

equality inequality

scalar, string. s e t . or p o i n t e r

less t h a n greater than

scalar

or r e a l

KesN![ type ---

same

as

operand

real

integer or r e a l

integer real integer

relational: =

or s t r i n g Boolean

less or e q u a l -orset i n c l u s i o n greater or equal

=

scalar

or s t r i n g

set scalar

or s t r i n g

-or-

ia

logical: not or

aad

set

inclusion

set

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

negation disjunction conjunction

Boolean

union set difference intersection

any

Boolean

set: +

set

type

T

109 Appendix Yables

A.

Table

of

standard

Constants : false , true.

Types: integer,

C

identifiers

maxint

Boolean,

real,

char,

text

Program parameters: input, output

Functions: abs, arctan, ord, pred,

chr, round,

Procedures : get . n e w , pack. rewrite, unpack,

B.

Table

of

cos, sin,

eof, sqr,

page, write,

exp, succ,

put , read, writeln

word-delimiters

m~ arraK ~a_q~n

eoln. sqrt,

(reserved

readln,

nil not of

y.~e

~unctioq

~E

tvo~

~OJ3S t div do dow nt Q eIs e

_onto if in ~abe ~ /nod

o~Gk#d oroceduns o r o ~r a m rw_~or d r_e o w2.c~

until var ~bila ~ith

predefined

reset ,

words)

aad file for

C. Non-standard,

In, odd, trunc

sa~ thezl ~o

identifiers

in

PASCAL 6 0 0 0 - 3 . 4

Types: alfa

Functions: card,

clock,

Procedures: data, getseg,

eos, halt,

expo,

undefined

iinelimit,

message,

putseg,

time

110 Appendix ~vnt ax

~_acku~_-N~_IZ~

F~Lm

(RNF)

Note: the following BNF formalism, a n d not

I

::=

{

: ::

belonginu Pascal.

to

the

denote possible repetition times. In g e n e r a l .

of

the

enclosed

{B}

is a s h o r t A

are mete-symbols of t h e l a n o u a g e

!

The curly brackets symbols z e r o or m o r e A

symbols symbols

form

for

: := < e m p t y >

the

purely

recursive

rule:

[ AB

BNF



: := < p r o g r a m





::=

::= ~ o ~ r _ ~

(

identifier> } );

: := < i d e n t i f i e r > :'=

{}



~

declaration part> ::= ~abel ~, } ;



::=

I I I I number>

::=



I

;

111







::=

::= ~rev [

type>

type>

.. < c o n s t a n t >

I ~&WJ& < v a r i a b l e declaration> { ;

group>

:



heading> ::= f u n c t i o n : f~tio~ ( { ; } ) : ; type>







::= < u n l a b e l l e d statement> I : :'= < s i m p l e

statement>

I



J

statement>

::=

::= & ~ w ~ e d u r e ; I &rocedure (

::= function declaration>

parameter section> ::= va~ &~ocedure

::= ::=

identifier>







.

: := < p o i n t e r

::=

variable>



::= < s i m p l e e x p r e s s i o n > ~

::=

=

I

I <

I

=

::= < t e r m > I





::=



::=

I

>

in

I

1

I o~ I ~L~

I a~d



::= < v a r i a b l e > I I I not

I (

::= < u n s i g n e d number> identifier> I nil



I (

I

I

)

114

{ , }



: :=



[

::=







identifier>

parameter> I I identifier> I

to

statement>

::= o o t o



::=



]

statement> ::= < p r o c e d u r e identifier> I ( { . } )

e3se

: := c a s e [ ; } ~nd





::=

::=



statement> ::--z:e~_@_~JZ < s t a t e m e n t > until variable>

list

element>

:

I W o bg~w.in ft := " "; p u t ( f ) ; n : = n-1 end ; ft : = ° 0 ° ; p u t ( f )

} end;

baaia if n } 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

::= < l e t t e r > { < l e t t e r Or d i g i t > } : := < l e t t e r > I The usual decimal notation is u s e d constants of the data types inteoer letter E preceding the scale factor to t h e p o w e r o f " .

for numbers, which are the and ~&l (see 6.1.2.). The is p r o n o u n c e d as " t i m e s 10

::= < d i g i t > { < d i g i t > } : := < d i g i t s e q u e n c e > ::= < u n s i g n e d integer>. .E E ::= < u n s i g n e d integer> I ::= < u n s i g n e d integer> I ::= + I Examples 1

:

100

Sequences &~r_i~g.&. constants Consisting the types

O. I

8 7 . 3 5 E +8

of characters enclosed by quote mmrks a r e c a l l e d Strings consisting of a single character are the of the standard type char (see 6.1.2). Strings 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 (see 6.2.1)

&ac~.~t array Note:

5E-3

[1..hi

aZ

char

I f t h e s t r i n g is t o c o n t a i n a quote m a r k is to be w r i t t e n twice.

Examples

: :=

"{

mark,

}

then

this

quote

'

: "A' °; . . . . . 'PASCAL" °TI{IS

5. C o n s t a n t

A constant constant.

IS

A STRING

"

definitions

definition

introduces

an

identifier

as

a synonym

::= < i d e n t i f i e r > ::= < u n s i g n e d number> ~ ] I ::= < i d e n t i f i e r > =

to

a

I

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 t y p e m a y a s s u m e and a s s o c i a t e s

of v a l u e s w h i c h v a r i a b l e s of that 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 > < 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.

type>

S i m o Z ~ ~y_a_~

I < s u b r a n g e i d e n t i f i e r > ::= < i d e n t i f i e r >

Sr~ar

type defines identifiers which



::=

an o r d e r e d s e t of v a l u e s denote these values. (

I

to all

scalar

enumeration

types

of

)

Friday.

(except

real)

are

:

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 ) 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 )

SUCC

pred 6.1.2.

applying

by

{.}

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, Saturday, Sunday) Functions

type>

types

A scalar

the

I I ; ::= < r e c o r d s e c t i o n > [ ;} : := { ,} : I ::= ~ 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 { ;} ::= < c a s e l a b e l l i s t > : () I ::= { ,} : := ::= < i d e n t i f i e r > : I

Examples:

rWcor~[

I

day: 1..31; month: 1 ,,12; y e a r : Inte~qer

en~ record

name, firstname: age : 0..99: married: Boolean

alfa;

and record

x,y : real; area : real; ~O~ s : shape o~ trlangle: (side : real; inclination, angle1, angle2: rectangle: (sidel, skde2: real; skew, angle3: angle) ; circle: (diameter: real) and

6.2.3.

angle);

_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 membership

-

* iS

The s e t d i f f e r e n c e x-y x w h i c h a r e not m e m b e r s

is d e f i n e d of y,

as

the

set

of

all

elements

of

::= 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 sequence of components which are number of components, called the fixed by t h e file t y p e d e f i n i t i o n , called ~m&&Z.

::= f i l ~

~Z

a structure consisting of a all of t h e s a m e t y p e , T h e J~@/3~th o f t h e f i l e , is not A file with 0 components is



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~

6.3.

text

= ~ile

~f

char

~ 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 to no e l e m e n t

belongs

::= ~ < t y p e

to

every

identifier>

pointer

type;

it p o i n t s

146

Examples

of

type

color sex text shape card alfa complex p arson

= = -= = = = =

definitions: (red, yellow, green, blue) (male, female) f~le ~_~ c h a r (triangle, rectangle, circle) ~rray [1..80] o~ char ~ a e k e d ~&rJ~eJL [ 1 , . 1 0 ] of char record re.ira: real ~nd 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

denotations

of variables

Variable declarations consist the new variables, followed by

of e l i s t of their type.



::=

identifiers

{.}

denoting

:

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, a : ~rJ&~L[O,.63] ~ real b: ~ r a y [ c o l o r , B o o l e a n ] c: c o l o r f: f&l~ of char huel,hue2: & a t O~ c o l o r pl p2: ~person

times) £~

complex

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,

::=

I

I

147

7.I. An

Entlre~riables

entire

variable

is

denoted

::=

7.2.

by

its

identifier.

::= < i d e n t i f i e r >

C prop o n ~ n t _ _ ~ a r i a b l e ~

A component a selector depends on

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 specifying the component. The form of the structuring type of the variable.

::=

?.2.1.

i~g~gd



n-dimensional array variable by n i n d e x e x p r e s s i o n s .

::= [ ::= < v a r i a b l e >

Examples

I

v~riables

A component of an variable followed

The types index types

followed by the selector

of the declared

index in t h e

expressions definition

is

denoted

by

the

must correspond with of t h e a r r a y t y p e .

the

{,}]

:

a [ 12] a[i+j] b [ r e d ,true]

?.2.2.

_Field

desionator~

A component of variable followed

a record variable is by t h e f i e l d i d e n t i f i e r

Examples

:

U .re

hired,true] p 2~ .s i z e

.im

denoted by t h e r e c o r d of t h e c o m p o n e n t .

::= < r e c o r d variable>. ::= < i d e n t i f i e r >

identifier>

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. : := < 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 < p o i n t e r v a r i a b l e > ::= < v a r i a b l e >

variable>t

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



::= < u n s i g n e d number> I I I ~il ::= < v a r i a b l e > I 1 I I () I ~ot < 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 > {,} I ::= < e x p r e s s i o n > I .. < 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> ::= < t e r m > I I ::= < s i m p l e e x p r e s s i o n > I 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 x *y

Terms :

i/(1-i) p nz q (x ::= < f u n c t i o n i d e n t i f i e r > I ({ , ::= < i d e n t i f i e r >

expressions. for the

11).

parameter>})

Sum(a .100) SCD ( 1 4 7 , k )

Examples :

s i n (x+y) eof (f) ord(ff

)

9. S t a t e m e n t s

Statements #xec~&bi&. referenced

denote algorithmic actions, They may be prefixed by by g o t o s t a t e m e n t s .

a

and are label

said which

to can

be be

152

::= I : ::= < s i m p l e statement>

9,1.

~&mo!e

I statement>

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 . ::= < a s s i g n m e n t I 9.1.1.

Assignment

The variable (or the identical type, with the

x

~ro~_~dure

:=

value

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

function) following

of

I

and the expression m u s t be exceptions being permitted:

of the variable is real. and is i n t e g e r or a s u b r a n g e thereof. 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 or v i c e - v e r s a .

p i hue

9.1.2,

I

serves to r e p l a c e the current specified as an e x p r e s s i o n .

::=

Examples:

I

statemeoJza

The assignment statement a variable by a new v a l u e

I. t h e type expression 2. t h e type variable,

statement> statement>

of

the

type

of

the

the

type

of

the

y+z

:= ( I < = i ) a n d ( i < I 0 0 ) := s q r ( k ) - (i'j) := [ b l u e . s u c c (c )]

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 an

the case expression

of

a ~alue ~jc_~m__~, the (of which a variable

actual parameter m u s t be 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 variable parameters.

structure

must

not

appear

as

actual

::= < p r o c e d u r e i d e n t i f i e r > I ( {,}) ::: < i d e n t i f i e r > ::: < e x p r e s s i o n > I I I Examples :

9.1.3.

next Transpose(a on,m) Bisect (fct.-1.0,+1.0.x)

_Onto s t a t e m e n t s

A gore statement serves to indicate should continue at a n o t h e r p a r t of the t h e p l a c e of t h e l a b e l .

The following labels:

:::

£ot£

restrictions

hold

I. T h e scope of a label is defined, it is therefore procedure. 2. E v e r y label heading of statement.

9.2.

must the

that further processinc p r o g r a m t e x t , n a m e l y at

concerninD

the not

the

procedure possible

applicability

w i t h i n w h i c h it to jump into

of

is a

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 procedure in which the label marks a

~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). ::: < c o m p o u n d s t a t e m e n t > I I

I

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 ,

Example: 9.2.2,

~

z

::= ~ _ E i n := x

; x



:= y;

y

{ ;}

~

:= z ~

~&~_~tional_~J~.O~

A conditional statement its c o m p o n e n t s t a t e m e n t s .

selects

for

execution

a single

one

of

::= I

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 . ::= & ~ ~ ~

The e x p r e s s i o n Boolean.

between

~ote: The syntactic i~

is

< e x p r e s s i o n > &b~l! < s t a t e m e n t > I ~l&~

the

ambiguity

symbols

arising

~b_~i i L ~

resolved

by

interpreting

~ ~Uia ~e~iA i~ end

~

from

and ~ h e &

the

iheo

construct

be of t y p e

construct



the

must

~

as





equivalent

~.~

to



Examples : i~ if

9,2,2,2.

~ e

x < 1 . 5 t h e n z := x + y e l s e z : = p 1 n j ! &he~l p I : = p I f . f a t h e r

1 .5

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

.

::= ~ , ~ D~ { ;~ ~A~ ::= < c a s e l a b e l l i s t > : < 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" plus : x := x+y ; m i n u s : x := x - y ; t i m e s : x := x * y end

case 1: 2: 3: 4:

i of x := x := x := x :=

1

sin(x); cos(x); exp(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 . ::= < w h ~ l e s t a t e m e n t > [

9.2.3.1o

[

~ile__~tatemen~&



::= ~ , ~ i £







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 is

B do

equivalent



S

to

B £hea be£in 8 ; wbile end

B do

S

156 Examples: whil~

a[i]

while beoin

i > O ~j& if odd(i) then i := i ~ L ~ 2:

x

x d o

i

:= i + I

z

:= z ' x ;

:= s q r ( x )

and ~]3ile ~ e e l ( f ) da P ( f t ); g e t ( f )

~W~O

9.2.3.2.

~931eat

statemeoj&&

: := reoeat

{ :}

until



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 is

S ~il

equivalent

B

to

beoin S : i f no*. B t h e n r~oeat S un~il end

B

E x a m p les : zeoeal

~.~.~l

:= i m e ~ := j ; := k j = O

z~ea~ ~Dtil

eof(f)

9.2.3.3.

k i j

P(ft);

J:

get(f)

Egj~_~_~t e m e n t s

The for statement indicates executed while a progression w h i c h is c a l l e d t h e c o o ~ _ ~

that a statement is t o be r e p e a t e d l y 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 ~r_iW_b~ of t h e f o r s t a t e m e n t .

157

~ 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 ~owot~ ::= < i d e n t i f i e r > ::= < e x p r e s s i o n > ::= < 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

for

is

and

v

equivalent v

:= el;

a

for

fD~l v is

:=

:= el

:= el;

Examples

for

the

to

e2 do

to

the

S; v

form

of

:= s u c c ( v ) ;

S;

of

the

do~jl~J& e 2 d o to S;

the v

9.2.4.

statements .,.

; v

:= e2;

S

...

; v

:= e2;

S

form

S

statement

:= p r e d ( S ) ;

S;

:

i

:= 2 t o

100

do ii

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 c[i.j] :-- x an~ for

S

sequence

statement

equivalent v

el

of

c

:= r e d

to

blue

x

do

a[i]

> max

:= x + a [ i , k ]

tbru& m a x

:= a [ i ]

*b[k,j] ;

Q (c)

~h_~tatements

dO l i s t > ::= < r e c o r d 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 := ~se is

month

equivalent i~

I; y e a r

:= y e a r

+

I

:= m o n t h + 1 to

date.month ~ 12 ~ ~d~ date.month :=

~]_.~ d a t e . m o n t h

1;

date.year

:= d a t e . y e a r + 1

:= 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 elements of the record variable list. However, assignments 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

Procedure associate procedure

any are

declarations

declarations s e r v e to d e f i n e identifiers w i t h t h e m so t h a t statements.

p a r t s of they can

programs and be a c t i v a t e d

: := < p r o c e d u r e heading> ::= < l a b e l d e c l a r a t i o n oart>

The ~ ~ ~ d ~ specifies the identifier naming the procedure and the formal parameter identifiers (if ~ n y ) . The parameters are either value-, variable-, procedureor 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. ::= ~ 2 o c w ~ d u r e < i d e n t i f i e r > ; 1 ~2~r~W~ ( I ;}) : ::= 1 ~&~ ~nc~on ~£ce~ur~ { ,t ::= {,} A parameter constituents The

i ~

group without preceding are value parameters. de~l~ration

~it

:

specifier

specifies

all

implies

labels

that

which

its

mark

a

159

statement ::= < e m p t y > iah#3. < l a b e l > { . < l a b e l > } ;

The £~nsJi&Oj~ definition definitions l o c a l to t h e : := < e m p t y > I definition> { :

~cle£ation procedure

The &2~£~r_@ procedure and declaration.

all

I { ;=

~or

j

the

writeln,

variable

v

and

used

tag

to

field

allocate

values

assigns

the

If t h e form

type

a

variable

pointer

of v

of

is

the

t 1 ..... t n .

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,)

variables

where

readln,

file

12.

to v to the p o i n t e r v a r i a b l e p. a record type with variants, the new(p

a new

v-u :=

statement

of

be

v

the do

declared

by

T

[u..v]

o Then u to

z

of

T

statement z[j]

unpack(z,a,i)

:=

pack (a ,i ,z )

a[j-~+i] means

means

162

f~l~ J

:= u t o

where j denotes the program.

11. F u n c t i o n

an

v do

a[J-u+i]

auxiliary

:: z[j]

variable

not

occurring

elsewhere

in

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 .

The function heading function, the formal the function.

:::



identifier naming the function, and

the the

type

::: ~WJl&~i9_~ < i d e n t i f i e r > : < r e s u l t type>; ~tion ( { :}) : ; ::= < t y p e i d e n t i f i e r >

of

I

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: 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_ if

{x X

= max(a[ I] ..... < a[i]

end : {X = max(a[ I~!ax : = x end

integer):

I] .....

~h~ii x a[n]

a[i-1])} := a [ i ] )}

real;

163

function be~in if en,~d

G C D (m,n : i n t e g e r ) : i n t e g e r : n = O J~hen G C D :-- m # ! s e G O D

:= G C O (n,m

function Power(x: r e a l ; y: i n t e g e r ) : v&r w.z : reel; i : integer ; ~.~in w : = x ; z := 1: i : = y ; mhJJ.~ i > 0 d o if odd(i) then i := i d i v 2 ; w := sqr(w) end : {Z = x ~ y } Power := z

z

:=

real

: {y

mod

n)

>=

O}

z'w;

~nd

11.1.

~&ndard

functig_Q&

Standard functions are supposed to be predeclered implementation of Pascal. Any implementation may additional predeclared functions (cf. also 10.1).

The

standard

11.1.1,

Ar_~_bmetic

abs(x)

sqr

are

listed

the absolute either ~Ca~ is the type

computes x~2. or ~ n t e ~ e r , and of x.

s in (x) cos (x) exp (x) ln(x ) sqrt (x) erctan (x)

the the

and

explained

below:

fg_ectiojl&

computes must be the result

(x)

11.1.2.

functions

in every feature

type type

or of

value of iO~e~er, x.

x . The t y p e o f x and the type of

T h e t y p e o f x m u s t be t h e t y p e of t h e r e s u l t

of x m u s t be e i t h e r ~ e a l of t h e r e s u l t is ~ e a l .

either is t h e

or 3 n t e g e r ,

~ type

and

~e~_~tes

odd(x)

the true,

type if x

of is

x m u s t be ~ o t e ~ w J : . e n d t h e odd, and false otherwise.

eof(f)

eof(f) indicates, end-of-file status.

eoln(f)

indicates the chapter 12).

end

whether

of

a

the

line

file

in

result

f is

e textfile

in

is

the

(see

164

11.1.3.

~&sfer

trune(x)

the Part

(x)

x

is

truncated

argument

x

is

to

rounded

x m u s t be o f t y p e i n t e g e r , a n d char) is the character whose (if it e x i s t s ) .

Fw.~_he~

standard

succ(x)

x is result

of is

pred(x)

x is result exists).

of is

12.

value

its

integral

to

the

nearest

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 (×)

11.1.4.

real .

the real integer.

round (x)

ord

funct~j&~&

Input

end

the result (of t y p e ordinal n u m b e r is x

functions any scalar the successor any the

or subrange type, and the v a l u e of x (if it e x i s t s ) .

scalar or predecessor

subrange value

type, of x

and t h e (if it

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 textfile and vl...vn denote variables (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

if to

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,

!~£,__2~&~ure

1. r e a d l n ( v l 2.

.....

readln(f,vl

of

vn)

.....

is

~b&2,

then

read(f,v

read(f,v)

1);

is

...

;

equivalent

r ~

is

vn)

read(f,v 3, r e a d l n ( f )

type

to

vn)

3.

is a v a r i a b l e := f? ; g e t ( f )

equivalent

1 .....

read(f,v 1 ..... read(f,vn)

12.2.

is

read(input,v

f denotes a char, integer

2.

v v

vn)

to

procedure ~ ; of t h e t y p e s

equivalent is

equivalent

1 .....

equivalent

to

vn):

readln(input,vl

.....

vn)

to readln

(f)

to

~ h i ] d ~ D_~& e o l n ( f )

~

get(f);

get(f) Readln of the

12.3.

is used to next line,

read

and

subsequently

skip

to

the

beginning

!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) write(f

3.

The

is

equivalent

.pl);

..,

write-parameters e :m

p

;

write(f,pn)

have

e :m : n

to

the

followin~

forms:

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 written on t h e f i l e f, p r e c e d e d b l a n k s as s p e c i f i e d by m,

C.

if e is written

12.4.

an on

(packed) the file

array f.

of

t h e w o r d s T R U E or by an a p p r o p r i a t e

characters,

then

the

FALSE are n u m b e r of

string

e is

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

2. w r i t e l n ( f , p w r i t e l n (f)

1,...,pn)

3. w r i t e l n ( f )

appends

a

equivalent ~s

line

to

equivalent

marker

writeln to

( o u t p u t ,p I ..... p n )

write(f,p

(cf.6.2,4)

to

the

I ..... p n ) ;

file

f.

167

12.5.

~ditional_grocedu~es

page(f)

13.

top

causes skipping to the t e x t f i l e f is p r i n t e d .

of

a

new

page.

when

the

Programs

A Pascal program for its heading.



::=

declaration

.

()



;

}

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 ) ; 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/

get(f)

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 first U characters. 2. L a b e l s

consist

of

at

distinct

most

objects

must

differ

over

their

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 should obey the

of P A S C A L in t e r m s following rules:

of

available

5.

Word symbols sequence of They may not

such as ~eqiA, ~nd, etc. letters (without surrounding 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 consecutive

- are escape

character

sets

written as characters).

one separator must occur between any identifiers, numbers, or w o r l d s y m b o l s .

pair

a

of

169

15. I n d e x

actual parameter adding operator array type array variable assignment statement base t y p e block case l a b e l c a s e l a b e l list case l i s t e l e m e n t case statement component type component variable compound statement conditional statement constant constant definition c o n s t a n t d e f i n i t i o n part constant identifier control v a r i a b l e digit digit s e q u e n c e element e l e m e n t list empty statement entire variable expression factor field designator field identifier f i e l d list file b u f f e r file t y p e file v a r i a b l e final v a l u e fixed p a r t for list formal Parameter section for s t a t e m e n t function declaration function designator furct ton h e a d i n g function identifier goto s t a t e m e n t identifier if s t a t e m e n t index t y p e indexed variable initial value label label d e c l a r a t i o n part letter l e t t e r or digit multiplying operator

9.1.2. 8.1.3 6.2.1 7.2.1 9.1.1 6.2.3 10. 6.2.2 9.2.2.2 9.2.2.2 9.2.2.2 6.2.1 7.2 9,2.1 9,2.2 5. 5. 10. 5. 9.2.3.3 3. 4. 8. 8, 9.1 7.I 8. 8. 7.2.2 72.2 62.2 72.3 62.4 72.3 92.3.] 62.2 92.3.3 10. 9.2.3.3 11. 8.2 11. B.2 9.1.3 4. 9.2.2.1 0.2.1 7.2,1 9.2.3.3 9. 10. 3. 4. 8.1.2

and

6.2.2

170 p a r a m e t e r group pointer variable pointer t y p e p r o c e d u r e and function d e c l a r a t i o n part Procedure declaration procedure heading p r o c e d u r e identifier p r o c e d u r e or function d e c l a r a t i o n p r o c e d u r e statement program program h e a d i n g program P a r a m e t e r s record s e c t i o n record type record variable record variable list referenced variable r e l a t i o n a l operator repeat s t a t e m e n t repetitive statement result type scale factor scalar t y p e set set t y p e sign simple e x p r e s s i o n simple s t a t e m e n t simple type special symbol statement statement part string structured statement structured type subrange type tag field term type type d e f i n i t i o n type d e f i n i t i o n pert type i d e n t i f i e r variable variable d e c l a r a t i o n variable d e c l a r a t i o n part variable i d e n t i f i e r variant variant part u n l a b e l l e d statement unpacked s t r u c t u r e d type unsigned constant unsigned integer unsigned number unsigned real with s t a t e m e n t while s t a t e m e n t

10. ?.~ 6.3 10. 10. 10. 9.1.2 10. 9.1.2 13. 13. 13. 6.2.2 6.2.2 7.2.2 9.2.4 7.3 8.1.4 9.2.3.2 9.2.3 11. 4. 6.1 .t 6.2.3 4. U. 9.1 6.1 3. 9. 10. 4. 9.2 6.2 6.1 .3

6.2.2 8. 6. 6. 10. 6.1 7. 7. 10. 7.1 6.2.2 6.2.2 9. 6.2 8. 4. 4. 4. 9.2.4 9.2.3.1

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF