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