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