Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
INTRODUCTION
The Pascal Report is part of The Pascal User Manual and Report by Jensen and Wlrth [JENs74].
Permission to copy without fee all or part of this material is granted provided t h a t the copies are not made or
dlstmbuted for direct commercial advantage, the ACM copyright notice and the title of the pubhcatlon and its
date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To
copy otherwise, or to republish, requires a fee and/or specific permission.
1982 ACM 0010-4892/82/0300-0073 $00.75
Computing Surveys, Vol. 14, No. 1, March 1982
74
CONTENTS
INTRODUCTION
1 THESIS
2, LANGUAGE HISTORY AND DESIGN PHILOSOPHY
2.1 Type Philosophy
3 SUMMARY AND COMPARISON OF
FEATURES
3,1 Sample Programs
3.2 Data Types
3.3 Expressions
3 4 Statements
3 5 Routmes and Program Structure
3 6 Input/Output
3 7 The C Preprocessor
4 DOMAINS OF APPLICATION
4.1 Business Data Processing
4.2 Scmntlfic Programming
4.3 Programming for Operating Systems
4 4 Programming for System Ut]hties
5. SUMMARY
ACKNOWLEDGMENTS
REFERENCES
C and PASCAL are both fairly small languages. Compared to other languages designed in the late 1960s such as P L / I and
ALGOL 68, both C and PASCAL have relatively few facilities and can easily be implemented on small machines.
The lineage for C includes BCPL
[RICH69] and B [JOHN73]. B and BCPL
have typically been used for implementing
operating systems and language processors.
Because their underlying model closely resembles that of commonly used computing
equipment, they provide the programmer
with an efficient interface to computer
hardware. The major advance of C was the
addition of typed variables; both B and
BCPL are typeless.
C was designed and implemented in 1972
by Dennis Ritchie. It has been used to
implement a wide variety of applications,
mostly under the U N I X T M 4 operating system running on the DEC pDpTM-11 ~ series
of computers. In 1977 development began
on a machine-independent version of the C
compiler, known as the Portable C Compiler [JOHN78], to simplify the task of moving the C compiler to new environments.
As a result, compatible versions of C now
run on upward of 15 different machines
from micro- to maxicomputers. This effort
has led to modifications of C to allow easy
portability of C programs.
PASCAL was influenced by ALGOL 60
[NAUR63] and ALGOL W [WmT66]. It was
designed by Niklaus Wirth in 1969 as a
direct response to the size and complexity
of ALGOL 68. PASCAL was to be a highlevel language that would [WIRT71a]
(1) allow the systematic and precise
expression of programming concepts
and structures;
(2) allow systematic program development;
(3) demonstrate that a language with a rich
set of flexible data and program structuring facilities can be implemented efficiently;
4 U N I X is a t r a d e m a r k of Bell Laboratories.
P D P Is a t r a d e m a r k of Digital E q u i p m e n t Corporation.
75
UNIX operating system and all of its system utilities {including several PASCAL
compilers) have been written in C.
A precise definition of a programming
language is vital to users and implementors,
and requisite to verifying program correctness. From the start, a precise specification
of the PASCAL syntax was available
[WIRT71b], and in 1973 Hoare and Wirth
produced an axiomatic definition for most
of the PASCAL semantics [HOAR73b]. To
date, a complete definition of the C syntax
has not been published. Sethi, in 1980, published a denotational definition for the semantics of most of C [SETI-I80]. Unlike axiomatic definitions, denotational definitions
are not very useful in demonstrating program correctness. In addition, the programming style encouraged by C {e.g., side effects in expressions, use of pointers, interchangeable use of arrays and pointers)
makes programs harder to verify.
2.1 Type Philosophy
76
A. R. F e u e r a n d N. H. G e h a n i
Facility
Declarations
constant
type
variable
array
record
77
Summary of Syntax
PASCAL
c o n s t name value
type name -- type
v a r vartables" type
v a r name array[index-type . . . . ] of ale.
merit-type
ftxed-part; variant-part e n d
pointer
routine
Referencing
array element
record field
pointer object
Statements
assignment
compound
conditional
selection
v a r name Ttype
function name
[(formal-parameters)I: result-type;
local-declarattons;
compound-statement
p r o c e d u r e name
[(formal-parameters)];
local-declarations;
compound-statement
array-name [t,]...[t,,]
record-name field-name
*p
vartable= expresston;
( vartable-declarattons statements}
if(expresston) statement~
else statement2
switch(expressmn) {
labeled-statements
vartable := expression
begin statements e n d
if Boolean then statement~
e l s e statement2
c a s e expression of
labeled-statements
pT
while loop
repeat loop
for loop
end
while(expresston) statement
do statement while (expresston)
for(intttahzatton, expression; remtttahzatton)
statement
The programming examples in this and following sections are presented in the style
used by the corresponding standard texts
(e.g., keywords are boldface in PASCAL
but not in C).
311C
T h e r o u t i n e is c o d e d i n C a s a n i n t e g e r
function named BinarySearch.
BinarySearch takes three arguments: an array A
o f i t e m s , t h e n u m b e r o f i t e m s n, a n d t h e
k e y k t o b e s e a r c h e d for. I t r e t u r n s e i t h e r
the index of the item that matches the key
o r - 1 if t h e k e y is n o t m a t c h e d . T h e t y p e
i t e m is d e f i n e d t o b e a s t r u c t u r e w i t h t w o
elements, key and value.
S t a t e m e n t s in C a r e t e r m i n a t e d b y s e m i colons, a n d b l o c k s a r e s u r r o u n d e d b y
braces. Comments are surrounded by the
p a i r " / * */". L i n e s b e g i n n i n g w i t h " # " a r e
ComputingSurveys,Vol. 14, No 1, March 1982
78
C preprocessor instructions. " = " is the ass i g n m e n t operator, "[ ]" is the a r r a y indexing operator, and "." is the field selector for
structures. In the example below, the include instruction is replaced b y the contents of the file "stdio.h" and the define
instruction defines the m a c r o " F A I L U R E "
to be the string " ( - 1 ) " . T h e typedef statem e n t defines the t y p e "item." T h e while
loop executes as long as the condition "low
< = high" r e m a i n s true. " A [ m i d ] . k e y " accesses the " k e y " field f r o m the " m i d " elem e n t of the a r r a y "A".
#include (stdio.h)
#define FAILURE (-1)
typedef struct (
int key;
char ,value;
) item;
int BinarySearch(A, n, k)
/* return index of Item m A with key k,
return FAILURE if k is not in A */
item A[ ];
int n, k;
return (FAILURE);
3. 1.2 PASCAL
T h e routine is i m p l e m e n t e d as an integer
function in P A S C A L . As with the C implem e n t a t i o n , the P A S C A L function accepts
t h r e e a r g u m e n t s and r e t u r n s either the index of the i t e m t h a t m a t c h e s the k e y or
failure.
C o m p o u n d s t a t e m e n t s in P A S C A L are
s u r r o u n d e d b y the pair " b e g i n e n d " . Comm e n t s are enclosed in braces. A semicolon
is used as a s t a t e m e n t separator. Declarations in a P A S C A L p r o g r a m always a p p e a r
in the s a m e order: labels, constants, types,
variables, and t h e n p r o c e d u r e s and functions. ":=" is t h e a s s i g n m e n t operator,
"[ ]" is used to subscript arrays, and "." is
the field selector for records. " d i v " is the
division o p e r a t o r for integers. T h e following
Computing Surveys, Vol 14, No. 1, March 1982
type
max = 100;
{max size of the array}
failure -- -1;
index = 1..max;
string = p a c k e d array[1..20] of char;
item = record
key: integer;
value: string;
end,
data = array[1..max] of item,
D e c l a r a t i o n s in C h a v e the f o r m
79
80
name value
are accessed b y
3.2.4 Structured Types
sales[mon], sales[rue],
and so on.
T h e P A S C A L compiler m a y be instructed to store an array compactly by
defining the array to be p a c k e d . This usually results in b e t t e r storage utilization, but
slower access time. T h e procedures pack
and unpack are provided so t h a t the
representation of an array can be
changed dynamically. Strings are packed
one-dimensional character arrays of fixed
length.
P A S C A L records have the form
};
};
P A S C A L has four data structuring mechanisms: arrays, records, sets, and files. T h e
Computing Surveys, Vol 14, No 1, March 1982
record
fixed.part;
vartant-part
end
area: real;
c a s e shape : (rect, circle, point) of
rect: (a, b: real);
circle: (r :real);
point: ( )
end
81
sp = &instance-of-date
Operators defined on addresses are comparison, subtraction, and addition with an
integer. Arithmetic on addresses is performed in units of the storage size of the
base type for the pointer. Thus, using cp
and sp from the previous example, cp + 1
points to the next character in m e m o r y and
sp + 1 points to the next instance of date.
Pointers and arrays are closely related in
C. An array name is a pointer to the first
element of the array. T h u s if A is an array,
the first element m a y be accessed either by
A[0] or by *A. Similarly, if cp points to a
character in a string, cp[0] yields the character pointed to by cp, c p [ - 1 ] yields the
previous character, and cp[1] yields the
next character.
Unlike those in C, pointer variables in
P A S C A L can only access dynamic variables of a specific type. T h e y cannot point
to explicitly declared variables. T h e declarations
var
p : ~integer;
f: 1'figure
82
A. R. F e u e r a n d N. H. G e h a n i
A Comparison of the P r o g r a m m i n g L a n g u a g e s C a n d P A S C A L
83
As with data types, the operators in C reflect the underlying hardware, while the
operators in PASCAL reflect the higher
level PASCAL machine. Thus, while C has
operators to manipulate the bits of a word,
PASCAL has operators to manipulate sets.
C and PASCAL also differ widely in their
view of side effects. By allowing assignments within expressions C encourages side
effects, which make programs less reliable
and modifiable. T h e ability to perform assignment within an expression, though,
tends to make C programs more compact
than the corresponding PASCAL programs. For example, compare the C and
PASCAL versions of reading characters
into an array until end-of-me.
(1) C:
while((s[i + +] = getchar()) !-- EOF);
(2) PASCAL:
while not eof do
begin
s[i] := getchar( );
i:=i+l
end
Computing Surveys, Vol 14, No 1, March 1982
84
A. R. Feuer a n d N. H. Gehani
instead of simply
x>0andy>0.
C suffers s o m e w h a t from the opposite malady; it has 11 comparable levels of precedence. With so m a n y levels, parentheses
are often needed for readability.
T h e logical operators in P A S C A L are
unconditional, while those in C are conditional (the bit operators "&" and "l" can
sometimes be used to get the effect of unconditional logical operators in C8). T h e use
of conditional operators can lead to b e t t e r
programs [DIJK76], although unconditional
operators m a y sometimes be necessary in
an expression with side effects. T h e lack of
conditional logical operators in P A S C A L
results in the linear search program fragment
T h e assignment s t a t e m e n t in C is the
assignment expression followed b y a semicolon. Assignment is p e r m i t t e d between all
types except arrays. 9 Because assignment is
an expression, multiple assignments can
take place in one statement, for example,
i--j--k=0;
T h e basic selection construct in C is the
if statement:
if(condition) statement1
else statement2
T h e condition is an expression t h a t evaluates to false if it is zero, otherwise to true.
Statement1 is executed if the condition
evaluates to true; otherwise, statement2, if
given, is executed.
T h e switch s t a t e m e n t is used for multiple-way selection based on the value of an
integral expression. If has the form
switch(expression) (
case constant-integral-expression1 :
statement-hstl;
case constant-integral-expression2 :
statement-hst2;
default: default-statement-hst;
found := false;
w h i l e (i <ffi n) a n d ( n o t found) d o
begin
found :-- x[i] = key;
i'=i+l
end;
i f found t h e n i := i - 1
8Using the bitwise logical operators as wordwise logical operators can be dangerous. For instance, the
bltwlse AND of binary 10 and 01 yields 00, or false
The wordwlse logical AND of 10 and 01 yields true.
while(condition) statement
loop-mittahzatton;
whde(condttion) {
statement
loop-remitiahzation:
85
T h e case s t a t e m e n t selects one statem e n t to be executed from a set of alternatives, depending upon the value of the case
expression. If the expression does not
m a t c h any of the case-labels, an error results. T h e case s t a t e m e n t has the form
case expression of
i f condition
then statement~
else statement2
where condition is a Boolean expression.
3.4.1 Discussion
86
A. R. Feuer a n d N. H. Gehani
A C program may consist of multiple routines spread across multiple source files. A
program source file consists of external objects, data and functions, that may be declared to be local to the file or global to all
files. All executable statements are contained within blocks, which may be nested
to arbitrary depth. Variables may be declared at the beginning of any block. Most
program source files begin with a prelude
of preprocessor statements. Standard preludes are included by means of the inclusion
facility of the preprocessor (see Section
3.7}. By convention, every program has a
function named m a i n where execution begins. All routines in C are functions. Every
function returns a value, which can be of
any type except array. Parameters are
passed by value, except for arrays. For arrays, the value passed is the address of the
first element.
C functions return after executing the
last statement or after executing a return
statement. T h e return statement is also
used to return the function value.
A PASCAL program consists of a main
procedure with the following form:
p r o g r a m name( hst of file identifiers);
label declarations
constant definitions
type definitions
variable defmttmns
procedure and functton defmtttons
program body.
T h e normal scope rules for objects in block
structured languages apply in PASCAL.
Names are local to the routine in which
they are declared. A name is global to routines contained within the routine in which
the name is declared.
87
88
A. R. F e u e r a n d N. H. G e h a n i
(
}
89
4. DOMAINS OF APPLICATION
90
A. R. F e u e r a n d N. H. G e h a n i
91
5. SUMMARY
92
DIJK72
DIJK76
DOD80
A . R . F e u e r a n d N. H. G e h a n i
DIJKSTRA, E . W . "Notes on structured
programming," in O. J. Dahl, E. W. Dijkstra, and C. A. R. Hoare, Structured programming, Academic Press, New York,
1972.
DIJKSTRA, E. W. A discipline of programming, Prentice-Hall, Englewood
Chffs, N.J., 1976.
DEPARTMENT OF DEFENSE. Reference
KERN78
KERN79
LISK74
manual for the Ada programming language, U.S. Government Printing Office,
GANN75
GANN77
GEHA77
GEHA80
HOAE73a
LINK76
HORN79
ISO81
JENS74
JOHN73
JOHN78
NAUR63
RICH69
SETH80
WELS77
WIRT66
WIRT71a
WIRT71b
WIRT75
WULF73
M.