new copyright; att/bsd/shared
[unix-history] / usr / src / usr.bin / pascal / USD.doc / puman3.n
CommitLineData
3edcb7c8
KB
1.\" Copyright (c) 1980 The Regents of the University of California.
2.\" All rights reserved.
e3d088d7 3.\"
3edcb7c8
KB
4.\" %sccs.include.redist.man%
5.\"
6.\" @(#)puman3.n 6.3 (Berkeley) %G%
e3d088d7 7.\"
e3d088d7
KM
8.if !\n(xx \{\
9.so tmac.p \}
10.if n 'ND
11.nr H1 2
12.NH
13Error diagnostics
14.PP
15This section of the
16.UM
17discusses the error diagnostics of the programs
18.PI ,
19.PC
20and
21.X .
22.I Pix
23is a simple but useful program which invokes
24.PI
25and
26.X
27to do all the real processing.
28See its manual section
29.IX
30(1)
31and section 5.2 below for more details.
32All the diagnostics given by
33.PI
34will also be given by
35.PC .
36.NH 2
37Translator syntax errors
38.PP
39A few comments on the general nature of the syntax errors usually
40made by Pascal programmers
41and the recovery mechanisms of the current translator may help in using
42the system.
43.SH
44Illegal characters
45.PP
46Characters such as `$', `!', and `@' are not part of the language Pascal.
47If they are found in the source program,
48and are not part of a constant string, a constant character, or a comment,
49they are considered to be
50`illegal characters'.
51This can happen if you leave off an opening string quote `\(aa'.
52Note that the character `"', although used in English to quote strings,
53is not used to quote strings in Pascal.
54Most non-printing characters in your input are also illegal except
55in character constants and character strings.
56Except for the tab and form feed characters,
57which are used to ease formatting of the program,
58non-printing characters in the input file print as the character `?'
59so that they will show in your listing.
60.SH
61String errors
62.PP
63There is no character string of length 0 in Pascal.
64Consequently the input `\(aa\(aa' is not acceptable.
65Similarly, encountering an end-of-line after an opening string quote `\(aa'
66without encountering the matching closing quote yields the diagnostic
67``Unmatched \(aa for string''.
68It is permissible to use the character `#'
69instead of `\''
70to delimit character and constant strings for portability reasons.
71For this reason, a spuriously placed `#' sometimes causes the diagnostic
72about unbalanced quotes.
73Similarly, a `#' in column one is used when preparing programs which are to
74be kept in multiple files.
75See section 5.11 for details.
76.SH
77Comments in a comment, non-terminated comments
78.PP
79As we saw above, these errors are usually caused by leaving off a comment
80delimiter.
81You can convert parts of your program to comments
82without generating this diagnostic
83since there are two different kinds of comments \- those delimited by
84`{' and `}', and those delimited by `(*' and `*)'.
85Thus consider:
86.LS
87{ This is a comment enclosing a piece of program
88a := functioncall; (* comment within comment *)
89procedurecall;
90lhs := rhs; (* another comment *)
91}
92.LE
93.PP
94By using one kind of comment exclusively in your program you can use
95the other delimiters when you need to
96``comment out''
97parts of your program\*(dg.
98.FS
99\*(dgIf you wish to transport your program,
100especially to the 6000-3.4 implementation,
101you should use the character sequence `(*' to delimit comments.
102For transportation over the
103.I rcslink
104to Pascal 6000-3.4, the character `#' should be used to delimit characters
105and constant strings.
106.FE
107In this way you will also allow the translator to help by detecting
108statements accidentally placed within comments.
109.PP
110If a comment does not terminate before the end of the input file,
111the translator will point to the beginning of the comment,
112indicating that the comment is not terminated.
113In this case processing will terminate immediately.
114See the discussion of ``QUIT'' below.
115.SH
116Digits in numbers
117.PP
118This part of the language is a minor nuisance.
119Pascal requires digits in real numbers both before and after the decimal
120point.
121Thus the following statements, which look quite reasonable to
122.SM
123FORTRAN
124.NL
125users, generate diagnostics in Pascal:
126.LS
127.so digitsout
128.LE
129These same constructs are also illegal as input to the Pascal interpreter
130.I px .
131.SH
132Replacements, insertions, and deletions
133.PP
134When a syntax error is encountered in the input text,
135the parser invokes an error recovery procedure.
136This procedure examines the input text immediately after the point
137of error and considers a set of simple corrections to see whether they
138will allow the analysis to continue.
139These corrections involve replacing an input token with a different
140token,
141inserting a token,
142or replacing an input token with a different token.
143Most of these changes will not cause fatal syntax errors.
144The exception is the insertion of or replacement with a symbol
145such as an identifier or a number;
146in this case the recovery makes no attempt to determine
147.I which
148identifier or
149.I what
150number should be inserted,
151hence these are considered fatal syntax errors.
152.PP
153Consider the following example.
154.LS
155% \*bpix -l synerr.p\fR
156.tr --
157.so synerrout
158%
159.LE
160The only surprise here may be that Pascal does not have an exponentiation
161operator, hence the complaint about `**'.
162This error illustrates that, if you assume that the language has a feature
163which it does not, the translator diagnostic may not indicate this,
164as the translator is unlikely to recognize the construct you supply.
165.SH
166Undefined or improper identifiers
167.PP
168If an identifier is encountered in the input but is undefined,
169the error recovery will replace it with an identifier of the
170appropriate class.
171Further references to this identifier will be summarized at the
172end of the containing
173.B procedure
174or
175.B function
176or at the end of the
177.B program
178if the reference occurred in the main program.
179Similarly,
180if an identifier is used in an inappropriate way,
181e.g. if a
182.B type
183identifier is used in an assignment statement,
184or if a simple variable
185is used where a
186.B record
187variable is required,
188a diagnostic will be produced and an identifier of the appropriate
189type inserted.
190Further incorrect references to this identifier will be flagged only
191if they involve incorrect use in a different way,
192with all incorrect uses being summarized in the same way as undefined
193variable uses are.
194.SH
195Expected symbols, malformed constructs
196.PP
197If none of the above mentioned corrections appear reasonable, the
198error recovery will examine the input
199to the left of the point of error to see if there is only one symbol
200which can follow this input.
201If this is the case, the recovery will print a diagnostic which
202indicates that the given symbol was `Expected'.
203.PP
204In cases where none of these corrections resolve the problems
205in the input,
206the recovery may issue a diagnostic that indicates that the
207input is ``malformed''.
208If necessary, the translator may then skip forward in the input to
209a place where analysis can continue.
210This process may cause some errors in the text to be missed.
211.PP
212Consider the following example:
213.LS
214% \*bpix -l synerr2.p\fR
215.so synerr2out
216%
217.LE
218Here we misspelled
219.I output
220and gave a
221.SM FORTRAN
222style variable declaration
223which the translator diagnosed as a `Malformed declaration'.
224When, on line 6, we used `(' and `)' for subscripting
225(as in
226.SM FORTRAN )
227rather than the `[' and `]' which are used in Pascal,
228the translator noted that
229.I a
230was not defined as a
231.B procedure .
232This occurred because
233.B procedure
234and
235.B function
236argument lists are delimited by parentheses in Pascal.
237As it is not permissible to assign to procedure calls the translator
238diagnosed a malformed statement at the point of assignment.
239.SH
240Expected and unexpected end-of-file, ``QUIT''
241.PP
242If the translator finds a complete program, but there is more non-comment text
243in the input file, then it will indicate that an end-of-file was expected.
244This situation may occur after a bracketing error, or if too many
245.B end s
246are present in the input.
247The message may appear
248after the recovery says that it
249``Expected \`.\'\|''
250since `.' is the symbol that terminates a program.
251.PP
252If severe errors in the input prohibit further processing
253the translator may produce a diagnostic followed by ``QUIT''.
254One example of this was given above \-
255a non-terminated comment;
256another example is a line which is longer than 160
257characters.
258Consider also the following example.
259.LS
260% \*bpix -l mism.p\fR
261.so mismout
262%
263.LE
264.NH 2
265Translator semantic errors
266.PP
267The extremely large number of semantic diagnostic messages which the translator
268produces make it unreasonable to discuss each message or group of messages
269in detail.
270The messages are, however, very informative.
271We will here explain the typical formats and the terminology used in the error
272messages so that you will be able to make sense out of them.
273In any case in which a diagnostic is not completely comprehensible you can
274refer to the
275.I "User Manual"
276by Jensen and Wirth for examples.
277.SH
278Format of the error diagnostics
279.PP
280As we saw in the example program above, the error diagnostics from
281the Pascal translator include the number of a line in the text of the program
282as well as the text of the error message.
283While this number is most often the line where the error occurred, it
284is occasionally the number of a line containing a bracketing keyword
285like
286.B end
287or
288.B until .
289In this case, the diagnostic may refer to the previous statement.
290This occurs because of the method the translator uses for sampling line
291numbers.
292The absence of a trailing `;' in the previous statement causes the line
293number corresponding to the
294.B end
295or
296.B until .
297to become associated with the statement.
298As Pascal is a free-format language, the line number associations
299can only be approximate and may seem arbitrary to some users.
300This is the only notable exception, however, to reasonable associations.
301.SH
302Incompatible types
303.PP
304Since Pascal is a strongly typed language, many semantic errors manifest
305themselves as type errors.
306These are called `type clashes' by the translator.
307The types allowed for various operators in the language are summarized on page
308108 of the
309Jensen-Wirth
310.I "User Manual" .
311It is important to know that the Pascal translator, in its diagnostics,
312distinguishes between the following type `classes':
313.br
314.ne 8
315.TS
316center;
317lew(10) le le le le.
318array Boolean char file integer
319pointer real record scalar string
320.TE
321These words are plugged into a great number of error messages.
322Thus, if you tried to assign an
323.I integer
324value to a
325.I char
326variable you would receive a diagnostic like the following:
327.LS
328.so clashout
329.LE
330In this case, one error produced a two line error message.
331If the same error occurs more than once, the same explanatory
332diagnostic will be given each time.
333.SH
334Scalar
335.PP
336The only class whose meaning is not self-explanatory is
337`scalar'.
338Scalar has a precise meaning in the
339Jensen-Wirth
340.I "User Manual"
341where, in fact, it refers to
342.I char ,
343.I integer ,
344.I real ,
345and
346.I Boolean
347types as well as the enumerated types.
348For the purposes of the Pascal translator,
349scalar
350in an error message refers to a user-defined, enumerated
351type, such as
352.I ops
353in the example above or
354.I color
355in
356.LS
357\*btype\fP color = (red, green, blue)
358.LE
359For integers, the more explicit denotation
360.I integer
361is used.
362Although it would be correct, in the context of the
363.I "User Manual"
364to refer to an integer variable as a
365.I scalar
366variable
367.PI
368prefers the more specific identification.
369.SH
370Function and procedure type errors
371.PP
372For built-in procedures and functions, two kinds of errors occur.
373If the routines are called with the wrong number of arguments a message similar to:
374.LS
375.so sinout1
376.LE
377is given.
378If the type of the argument is wrong, a message like
379.LS
380.so sinout2
381.LE
382is produced.
383A few functions and procedures implemented in Pascal 6000-3.4 are
384diagnosed as unimplemented in
385Berkeley
386Pascal, notably those related to
387.B segmented
388files.
389.SH
390Can't read and write scalars, etc.
391.PP
392The messages which state that scalar (user-defined) types
393cannot be written to and from files are often mysterious.
394It is in fact the case that if you define
395.LS
396\*btype\fP color = (red, green, blue)
397.LE
398``standard'' Pascal does not associate these constants with the strings
399`red', `green', and `blue' in any way.
400An extension has been added which allows enumerated types
401to be read and written,
402however if the program is to be portable,
403you will have to write your own routines to perform these functions.
404Standard Pascal only allows the reading of characters, integers and real
405numbers from text files.
406You cannot read strings or Booleans.
407It is possible to make a
408.LS
409\*bfile of\fP color
410.LE
411but the representation is binary rather than string.
412.SH
413Expression diagnostics
414.PP
415The diagnostics for semantically ill-formed expressions are very explicit.
416Consider this sample translation:
417.LS
418% \*bpi -l expr.p\fP
419.so exprout
420%
421.LE
422This example is admittedly far-fetched, but illustrates that the error
423messages are sufficiently clear to allow easy determination of the
424problem in the expressions.
425.SH
426Type equivalence
427.PP
428Several diagnostics produced by the Pascal translator complain about
429`non-equivalent types'.
430In general,
431Berkeley
432Pascal considers variables to have the same type only if they were
433declared with the same constructed type or with the same type identifier.
434Thus, the variables
435.I x
436and
437.I y
438declared as
439.LS
440\*bvar\fP
441 x: ^ integer;
442 y: ^ integer;
443.LE
444do not have the same type.
445The assignment
446.LS
447x := y
448.LE
449thus produces the diagnostics:
450.LS
451.so typequout
452.LE
453Thus it is always necessary to declare a type such as
454.LS
455\*btype\fP intptr = ^ integer;
456.LE
457and use it to declare
458.LS
459\*bvar\fP x: intptr; y: intptr;
460.LE
461Note that if we had initially declared
462.LS
463\*bvar\fP x, y: ^ integer;
464.LE
465then the assignment statement would have worked.
466The statement
467.LS
468x^ := y^
469.LE
470is allowed in either case.
471Since the parameter to a
472.B procedure
473or
474.B function
475must be declared with a
476type identifier rather than a constructed type,
477it is always necessary, in practice,
478to declare any type which will be used in this way.
479.SH
480Unreachable statements
481.PP
482Berkeley
483Pascal flags unreachable statements.
484Such statements usually correspond to errors in the program logic.
485Note that a statement is considered to be reachable
486if there is a potential path of control,
487even if it can never be taken.
488Thus, no diagnostic is produced for the statement:
489.LS
490\*bif\fP false \*bthen\fP
491 writeln('impossible!')
492.LE
493.SH
494Goto's into structured statements
495.PP
496The translator detects and complains about
497.B goto
498statements which transfer control into structured statements (\c
499.B for ,
500.B while ,
501etc.)
502It does not allow such jumps, nor does it allow branching from the
503.B then
504part of an
505.B if
506statement into the
507.B else
508part.
509Such checks are made only within the body of a single procedure or
510function.
511.SH
512Unused variables, never set variables
513.PP
514Although
515.PI
516always clears variables to 0 at
517.B procedure
518and
519.B function
520entry,
521.PC
522does not unless runtime checking is enabled using the
523.B C
524option.
525It is
526.B not
527good programming practice to rely on this initialization.
528To discourage this practice, and to help detect errors in program logic,
529.PI
530flags as a `w' warning error:
531.IP
532.RS
533.HP 1)
534Use of a variable which is never assigned a value.
535.IP 2)
536A variable which is declared but never used, distinguishing
537between those variables for which values are computed but which are never
538used, and those completely unused.
539.RE
540.LP
541In fact, these diagnostics are applied to all declared items.
542Thus a
543.B const
544or a
545.B procedure
546which is declared but never used is flagged.
547The
548.B w
549option of
550.PI
551may be used to suppress these warnings;
552see sections 5.1 and 5.2.
553.NH 2
554Translator panics, i/o errors
555.SH
556Panics
557.PP
558One class of error which rarely occurs, but which causes termination
559of all processing when it does is a panic.
560A panic indicates a translator-detected internal inconsistency.
561A typical panic message is:
562.LS
563snark (rvalue) line=110 yyline=109
564Snark in pi
565.LE
566If you receive such a message, the translation will be quickly and perhaps
567ungracefully terminated.
568You should contact a teaching assistant or a member of the system staff,
569after saving a copy of your program for later inspection.
570If you were making changes to an existing program when the problem
571occurred, you may
572be able to work around the problem by ascertaining which change caused the
573.I snark
574and making a different change or correcting an error in the program.
575A small number of panics are possible in
576.X .
577All panics should be reported to a teaching assistant or systems
578staff so that they can be fixed.
579.SH
580Out of memory
581.PP
582The only other error which will abort translation when no errors are
583detected is running out of memory.
584All tables in the translator, with the exception of the parse stack,
585are dynamically allocated, and can grow to take up the full available
586process space of 64000 bytes on the \s-2PDP\s0-11. On the \s-2VAX\s0-11,
587table sizes are extremely generous and very large (25000) line programs
588have been easily accommodated. For the \s-2PDP\s0\-11, it is
589generally true that
590the size of the largest translatable program is directly related to
591.B procedure
592and
593.B function
594size.
595A number of non-trivial Pascal programs, including
596some with more than 2000 lines and 2500 statements
597have been translated and interpreted using
598Berkeley
599Pascal on \s-2PDP\s0-11's.
600Notable among these are the Pascal-S
601interpreter,
602a large set of programs for automated generation of
603code generators,
604and a general context-free parsing program which has been used to
605parse sentences with a grammar for a superset of English.
606In general,
607very large programs should be translated using
608.PC
609and the separate compilation facility.
610.PP
611If you receive an out of space message from the translator
612during translation of a large
613.B procedure
614or
615.B function
616or one containing a large number of string constants
617you may yet be able
618to translate your program if you break this one
619.B procedure
620or
621.B function
622into several routines.
623.SH
624I/O errors
625.PP
626Other errors which you may encounter when running
627.PI
628relate to input-output.
629If
630.PI
631cannot open the file you specify,
632or if the file is empty,
633you will be so informed.
634.NH 2
635Run-time errors
636.PP
637We saw, in our second example, a run-time error.
638We here give the general description of run-time errors.
639The more unusual interpreter error messages are explained
640briefly in the manual section for
641.I px
642(1).
643.SH
644Start-up errors
645.PP
646These errors occur when the object file to be executed is not available
647or appropriate.
648Typical errors here are caused by the specified object file not existing,
649not being a Pascal object, or being inaccessible to the user.
650.SH
651Program execution errors
652.PP
653These errors occur when the program interacts with the Pascal runtime
654environment in an inappropriate way.
655Typical errors are values or subscripts out of range,
656bad arguments to built-in functions,
657exceeding the statement limit because of an infinite loop,
658or running out of memory\*(dd.
659.FS
660\*(ddThe checks for running out of memory are not foolproof and there
661is a chance that the interpreter will fault, producing a core image
662when it runs out of memory.
663This situation occurs very rarely.
664.FE
665The interpreter will produce a backtrace after the error occurs,
666showing all the active routine calls,
667unless the
668.B p
669option was disabled when the program was translated.
670Unfortunately, no variable values are given and no way of extracting them
671is available.*
672.FS
673* On the \s-2VAX\s0-11, each variable
674is restricted to allocate at most 65000 bytes of storage (this is a
675\s-2PDP\s0-11ism that has survived to the \s-2VAX\s0.)
676.FE
677.PP
678As an example of such an error, assume that we have accidentally
679declared the constant
680.I n1
681to be 6, instead of 7
682on line 2 of the program primes as given in section 2.6 above.
683If we run this program we get the following response.
684.LS
685% \*bpix primes.p\fP
686.so primeout3
687%
688.LE
689.PP
690Here the interpreter indicates that the program terminated
691abnormally due to a subscript out of range near line 14,
692which is eight lines into the body of the program primes.
693.SH
694Interrupts
695.PP
696If the program is interrupted while executing
697and the
698.B p
699option was not specified,
700then a backtrace will be printed.\*(dg
701.FS
702\*(dgOccasionally, the Pascal system will be in an inconsistent
703state when this occurs,
704e.g. when an interrupt terminates a
705.B procedure
706or
707.B function
708entry or exit.
709In this case, the backtrace will only contain the current line.
710A reverse call order list of procedures will not be given.
711.FE
712The file
713.I pmon.out
714of profile information will be written if the program was translated
715with the
716.B z
717option enabled to
718.PI
719or
720.IX .
721.SH
722I/O interaction errors
723.PP
724The final class of interpreter errors results from inappropriate
725interactions with files, including the user's terminal.
726Included here are bad formats for integer and real numbers (such as
727no digits after the decimal point) when reading.