Start development on 386BSD 0.0
[unix-history] / .ref-BSD-4_3_Net_2 / usr / src / share / doc / ps1 / 04.pascal / puman2.n
CommitLineData
3edcb7c8
KB
1.\" Copyright (c) 1980 The Regents of the University of California.
2.\" All rights reserved.
86d6ee2d 3.\"
af359dea
C
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
3edcb7c8 19.\"
af359dea
C
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)puman2.n 6.3 (Berkeley) 4/17/91
86d6ee2d 33.\"
86d6ee2d
KM
34.if !\n(xx \{\
35.so tmac.p \}
36'if n 'ND
37.nr H1 1
38.NH
39Basic UNIX Pascal
40.PP
41The following sections
42explain the basics of using
43.UP .
44In examples here we use the text editor
45.I ex
46(1).
47Users of the text editor
48.I ed
49should have little trouble following these examples,
50as
51.I ex
52is similar to
53.I ed .
54We use
55.I ex
56because it
57allows us to make clearer examples.\(dg
58.FS
59\(dg Users with \s-2CRT\s0 terminals should find the editor
60.I vi
61more pleasant to use;
62we do not show its use here because its display oriented nature
63makes it difficult to illustrate.
64.FE
65The new
66.UX
67user will find it helpful to read one of the text editor documents
68described in section 1.4 before continuing with this section.
69.NH 2
70A first program
71.PP
72To prepare a program for
73.UP
74we first need to have an account on
75.UX
76and to `login'
77to the system on this account.
78These procedures are described in the documents
79.I "Communicating with UNIX"
80and
81.I "UNIX for Beginners".
82.PP
83Once we are logged in we need to choose a name for our program;
84let us call it `first' as this is the first example.
85We must also choose a name for the file in which the program will be stored.
86The
87.UP
88system requires that programs reside in files which have names ending with
89the sequence `.p' so we will call our file `first.p'.
90.PP
91A sample editing session to create this file would begin:
92.LS
93% \*bex first.p\fR
94"first.p" [New file]
95:
96.LE
97We didn't expect the file to exist, so the error diagnostic doesn't
98bother us.
99The editor now knows the name of the file we are creating.
100The `:' prompt indicates that it is ready for command input.
101We can add the text for our program using the `append'
102command as follows.
103.LS
104:\*bappend\fR
105.B
106program first(output)
107begin
108 writeln('Hello, world!')
109end.
110\&.
111.R
112:
113.LE
114The line containing the single `\*b.\fR' character here indicated
115the end of the appended text.
116The `:' prompt indicates that
117.I ex
118is ready for another command.
119As the editor operates in a temporary work space we must now store the contents
120of this work space in the file `first.p'
121so we can use the Pascal
122translator and executor
123.IX
124on it.
125.LS
126:\*bwrite\fR
127"first.p" [New file] 4 lines, 59 characters
128:\*bquit\fR
129%
130.LE
131We wrote out the file from the edit buffer here with the
132`write'
133command, and
134.I ex
135indicated the number of lines and characters written.
136We then quit the editor, and now have a prompt from the shell.\(dd
137.FS
138\(dd Our examples here assume you are using
139.I csh.
140.FE
141.KS
142.PP
143We are ready to try
144to translate and execute our program.
145.DS
146.tr '\(aa^\(ua
147% \*bpix first.p\fR
148.so firstout
149.tr ''^^
150%
151.DE
152.KE
153.PP
154The translator first printed a syntax error diagnostic.
155The number 2 here indicates that the rest of the line is an image
156of the second line of our program.
157The translator is saying that it expected to find a `;' before the
158keyword
159.B begin
160on this line.
161If we look at the Pascal syntax charts in the Jensen-Wirth
162.I "User Manual" ,
163or at some of the sample programs therein, we will see that
164we have omitted the terminating `;' of the
165.B program
166statement on the first
167line of our program.
168.PP
169One other thing to notice about the error diagnostic is the letter `e'
170at the beginning.
171It stands for `error',
172indicating that our input was not legal Pascal.
173The fact that it is an `e' rather than an `E'
174indicates that the translator managed to recover from this error well
175enough that generation of code and execution could take place.
176Execution is possible whenever no fatal `E' errors
177occur during translation.
178The other classes of diagnostics are `w' warnings,
179which do not necessarily indicate errors in the program,
180but point out inconsistencies which are likely to be due to program bugs,
181and `s' standard-Pascal violations.\*(dg
182.FS
183\*(dgThe standard Pascal warnings occur only when the associated
184.B s
185translator option is enabled.
186The
187.B s
188option is discussed in sections 5.1 and A.6 below.
189Warning diagnostics are discussed at the end of section 3.2,
190the associated
191.B w
192option is described in section 5.2.
193.FE
194.PP
195After completing the translation of the program to interpretive code,
196the Pascal system indicates that execution of the translated program began.
197The output from the execution of the program then appeared.
198At program termination, the Pascal runtime system indicated the
199number of statements executed, and the amount of cpu time
200used, with the resolution of the latter being 1/60'th of a second.
201.PP
202Let us now fix the error in the program and translate it to a permanent
203object code file
204.I obj
205using
206.PI .
207The program
208.PI
209translates Pascal programs but stores the object code instead of executing it\*(dd.
210.FS
211\*(ddThis script indicates some other useful approaches to debugging
212Pascal programs.
213As in
214.I ed
215we can shorten commands in
216.I ex
217to an initial prefix of the command name as we did
218with the
219.I substitute
220command here.
221We have also used the `!' shell escape command here to execute other
222commands with a shell without leaving the editor.
223.FE
224.LS
225% \*bex first.p\fR
226"first.p" 4 lines, 59 characters
227:\*b1 print\fR
228program first(output)
229:\*bs/$/;\fR
230program first(output);
231:\*bwrite\fR
232"first.p" 4 lines, 60 characters
233:\*bquit\fR
234% \*bpi first.p\fR
235%
236.LE
237If we now use the
238.UX
239.I ls
240list files command we can see what files we have:
241.LS
242% \*bls\fR
243first.p
244obj
245%
246.LE
247The file `obj' here contains the Pascal interpreter code.
248We can execute this by typing:
249.LS
250% \*bpx obj\fR
251.so firstobjout
252%
253.LE
254Alternatively, the command:
255.LS
256% \*bobj\fR
257.LE
258will have the same effect.
259Some examples of different ways to execute the program follow.
260.LS
261% \*bpx\fR
262.so firstobjout
263% \*bpi -p first.p\fR
264% \*bpx obj\fR
265.so firstobjout2
266% \*bpix -p first.p\fR
267.so firstobjout2
268%
269.LE
270.PP
271Note that
272.I px
273will assume that `obj' is the file we wish to execute
274if we don't tell it otherwise.
275The last two translations use the
276.B \-p
277no-post-mortem option to eliminate
278execution statistics and
279`Execution begins'
280and
281`Execution terminated'
282messages.
283See section 5.2 for more details.
284If we now look at the files in our directory we will see:
285.LS
286% \*bls\fR
287first.p
288obj
289%
290.LE
291We can give our object program a name other than `obj' by using the move
292command
293.I mv
294(1).
295Thus to name our program `hello':
296.LS
297% \*bmv obj hello\fR
298% \*bhello\fR
299Hello, world!
300% \*bls\fR
301first.p
302hello
303%
304.LE
305Finally we can get rid of the Pascal object code by using the
306.I rm
307(1) remove file command, e.g.:
308.LS
309% \*brm hello\fR
310% \*bls\fR
311first.p
312%
313.LE
314.PP
315For small programs which are being developed
316.IX
317tends to be more convenient to use than
318.PI
319and
320.X .
321Except for absence of the
322.I obj
323file after a
324.IX
325run,
326a
327.IX
328command is equivalent to a
329.PI
330command followed by a
331.X
332command.
333For larger programs,
334where a number of runs testing different parts of the program are
335to be made,
336.PI
337is useful as this
338.I obj
339file can be executed any desired number of times.
340.. >>> INSERT SECTION FOR PC <<<
341.NH 2
342A larger program
343.PP
344Suppose that we have used the editor to put a larger program
345in the file `bigger.p'.
346We can list this program with line numbers by using the program
347.I cat -n
348i.e.:
349.LS
350% \*bcat -n bigger.p\fR
351.so bigger3.p
352%
353.LE
354This program is similar to program 4.9 on page 30 of the
355Jensen-Wirth
356.I "User Manual" .
357A number of problems have been introduced into this example for
358pedagogical reasons.
359.br
360.PP
361If we attempt to translate and execute the program using
362.IX
363we get the following response:
364.LS
365% \*bpix bigger.p\fR
366.so bigout1
367%
368.LE
369.PP
370Since there were fatal `E' errors in our program,
371no code was generated and execution was necessarily suppressed.
372One thing which would be useful at this point is a listing of the
373program with the error messages.
374We can get this by using the command:
375.LS
376% \*bpi -l bigger.p\fR
377.LE
378There is no point in using
379.IX
380here, since we know there are fatal errors in the program.
381This command will produce the output at our terminal.
382If we are at a terminal which does not produce a hard copy
383we may wish to print this
384listing off-line on a line printer.
385We can do this with the command:
386.LS
387% \*bpi -l bigger.p | lpr\fR
388.LE
389.PP
390In the next few sections we will illustrate various aspects of the
391Berkeley
392Pascal system by correcting this program.
393.NH 2
394Correcting the first errors
395.PP
396Most of the errors which occurred in this program were
397.I syntactic
398errors, those in the format and structure of the program rather than
399its content.
400Syntax errors are flagged by printing the offending line, and then a line
401which flags the location at which an error was detected.
402The flag line also gives an explanation
403stating either a possible cause of the error,
404a simple action which can be taken to recover from the error so
405as to be able to continue the analysis,
406a symbol which was expected at the point of error,
407or an indication that the input was `malformed'.
408In the last case, the recovery may skip ahead in the input
409to a point where analysis of the program can continue.
410.PP
411In this example,
412the first error diagnostic indicates that the translator detected
413a comment within a comment.
414While this is not considered an error in `standard'
415Pascal, it usually corresponds to an error in the program which
416is being translated.
417In this case, we have accidentally omitted the trailing `*)' of the comment
418on line 8.
419We can begin an editor session to correct this problem by doing:
420.LS
421% \*bex bigger.p\fR
422"bigger.p" 24 lines, 512 characters
423:\*b8s/$/ *)\fR
424 s = 32; (* 32 character width for interval [x, x+1] *)
425:
426.LE
427.PP
428The second diagnostic, given after line 16,
429indicates that the keyword
430.B do
431was expected before the keyword
432.B begin
433in the
434.B for
435statement.
436If we examine the
437.I statement
438syntax chart on page 118 of the
439Jensen-Wirth
440.I "User Manual"
441we will discover that
442.B do
443is a necessary part of the
444.B for
445statement.
446Similarly, we could have referred to section C.3 of the
447Jensen-Wirth
448.I "User Manual"
449to learn about the
450.B for
451statement and gotten the same information there.
452It is often useful to refer to these syntax charts and to the
453relevant sections of this book.
454.PP
455We can correct this problem by first scanning for the keyword
456.B for
457in the file and then substituting the keyword
458.B do
459to appear in front of the keyword
460.B begin
461there.
462Thus:
463.LS
464:\*b/for\fR
465 for i := 0 to lim begin
466:\*bs/begin/do &\fR
467 for i := 0 to lim do begin
468:
469.LE
470The next error in the program is easy to pinpoint.
471On line 18, we didn't hit the shift key and got a `9'
472instead of a `)'.
473The translator diagnosed that `x9'
474was an undefined variable and, later,
475that a `)' was missing in the statement.
476It should be stressed that
477.PI
478is not suggesting that you should insert a `)' before the `;'.
479It is only indicating that making this change will help it to be able to
480continue analyzing the program so as to be able to diagnose further
481errors.
482You must then determine the true cause of the error and make the
483appropriate correction to the source text.
484.PP
485This error also illustrates the fact that one error in the input may lead
486to multiple error diagnostics.
487.I Pi
488attempts
489to give only one diagnostic for each error,
490but single errors in the input sometimes appear to be more than
491one error.
492It is also the case that
493.PI
494may not detect an error when it occurs, but may detect it later in
495the input.
496This would have happened
497in this example if we had typed `x' instead of `x9'.
498.PP
499The translator next detected, on line 19, that the function
500.I Round
501and the variable
502.I h
503were undefined.
504It does not know about
505.I Round
506because
507.UP
508normally distinguishes between upper and lower case.\*(dg
509.FS
510\*(dgIn ``standard'' Pascal no distinction is made based on case.
511.FE
512On
513.UX
514lower-case is preferred\*(dd,
515.FS
516\*(ddOne good reason for using lower-case is that it is easier to type.
517.FE
518and all keywords and built-in
519.B procedure
520and
521.B function
522names are composed of lower-case letters,
523just as they are in the Jensen-Wirth
524.I "Pascal Report" .
525Thus we need to use the function
526.I round
527here.
528As far as
529.I h
530is concerned,
531we can see why it is undefined if we look back to line 9
532and note that its definition was lost in the non-terminated
533comment.
534This diagnostic need not, therefore, concern us.
535.PP
536The next error which occurred in the program caused the translator
537to insert a `;' before the statement calling
538.I writeln
539on line 23.
540If we examine the program around the point of error we will see
541that the actual error is that the keyword
542.B until
543and an associated expression have been omitted here.
544Note that the diagnostic from the translator does not indicate the actual
545error, and is somewhat misleading.
546The translator made the correction which seemed to be most plausible.
547As the omission of a `;' character is a common mistake,
548the translator chose to indicate this as a possible fix here.
549It later detected that the keyword
550.B until
551was missing, but not until it saw the keyword
552.B end
553on line 24.
554The combination of these diagnostics indicate to us the true problem.
555.PP
556The final syntactic error message indicates that the translator needed an
557.B end
558keyword to match the
559.B begin
560at line 15.
561Since the
562.B end
563at line 24 is supposed to match this
564.B begin ,
565we can infer that another
566.B begin
567must have been mismatched, and have matched this
568.B end .
569Thus we see that we need an
570.B end
571to match the
572.B begin
573at line 16,
574and to appear before the final
575.B end .
576We can make these corrections:
577.LS
578:\*b/x9/s//x)\fR
579 y := exp(-x) * sin(i * x);
580:\*b+s/Round/round\fR
581 n := round(s * y) + h;
582:\*b/write\fR
583 write(' ');
584:\*b/\fR
585 writeln('*')
586:\*binsert\fR
587 \*buntil n = 0;\fR
588\&\*b.\fR
589:\*b$\fR
590end.
591:\*binsert\fR
592 \*bend\fR
593\&\*b.\fR
594:
595.LE
596.PP
597At the end of each
598.B procedure
599or
600.B function
601and the end of the
602.B program
603the translator summarizes references to undefined variables
604and improper usages of variables.
605It also gives
606warnings about potential errors.
607In our program, the summary errors do not indicate any further problems
608but the warning that
609.I c
610is unused is somewhat suspicious.
611Examining the program we see that the constant was intended
612to be used in the expression which is an argument to
613.I sin ,
614so we can correct this expression, and translate the program.
615We have now made a correction for each diagnosed error
616in our program.
617.LS
618:\*b?i ?s//c /\fR
619 y := exp(-x) * sin(c * x);
620:\*bwrite\fR
621"bigger.p" 26 lines, 538 characters
622:\*bquit\fR
623% \*bpi bigger.p\fR
624%
625.LE
626It should be noted that the translator suppresses warning
627diagnostics for a particular
628.B procedure ,
629.B function
630or the main
631.B program
632when it finds severe syntax errors in that part of the source
633text.
634This is to prevent possibly confusing and
635incorrect warning diagnostics from being produced.
636Thus these warning diagnostics may not appear in a program with
637bad syntax errors until these errors are corrected.
638.KS
639.PP
640We are now ready to execute our program for the first
641time.
642We will do so in the next section after giving a listing
643of the corrected program for reference purposes.
644.LS
645% \*bcat -n bigger.p\fR
646.so bigger6.p
647%
648.LE
649.NH 2
650Executing the second example
651.PP
652We are now ready to execute the second example.
653The following output was produced by our first run.
654.LS
655% \*bpx\fR
656.so bigout2
657%
658.LE
659Here the interpreter is presenting us with a runtime error diagnostic.
660It detected a `division by zero' at line 17.
661Examining line 17, we see that we have written
662the statement `x := d / i' instead of `x := d * i'.
663We can correct this and rerun the program:
664.LS
665% \*bex bigger.p\fR
666"bigger.p" 26 lines, 538 characters
667:\*b17\fR
668 x := d / i
669:\*bs'/'*\fR
670 x := d * i
671:\*bwrite\fR
672"bigger.p" 26 lines, 538 characters
673:\*bq\fR
674% \*bpix bigger.p\fR
675.so bigout3
676%
677.LE
678.KS
679.PP
680This appears to be the output we wanted.
681We could now save the output in a file if we wished by using the shell
682to redirect the output:
683.LS
684% \*bpx > graph\fR
685.LE
686.KE
687We can use
688.I cat
689(1) to see the contents of the file graph.
690We can also make a listing of the graph on the line printer without
691putting it into a file, e.g.
692.LS
693% \*bpx | lpr\fR
694.so bigout4
695%
696.LE
697Note here that the statistics lines came out on our terminal.
698The statistics line comes out on the diagnostic output (unit 2.)
699There are two ways to get rid of the statistics line.
700We can redirect the statistics message to the printer using the
701syntax `|\|&' to the shell rather than `|', i.e.:
702.LS
703% \*bpx |\|& lpr\fR
704%
705.LE
706or we can translate the program with the
707.B p
708option disabled on the command line as we did above.
709This will disable all post-mortem dumping including the statistics line,
710thus:
711.LS
712% \*bpi -p bigger.p\fR
713% \*bpx | lpr\fR
714%
715.LE
716This option also disables the statement limit which normally guards
717against infinite looping.
718You should not use it until your program is debugged.
719Also if
720.B p
721is specified and an error occurs, you will
722not get run time diagnostic information to help you
723determine what the problem is.
724.NH 2
725Formatting the program listing
726.PP
727It is possible to use special lines within the source text of a program
728to format the program listing.
729An empty line (one with no characters on it) corresponds to a
730`space' macro in an assembler, leaving a completely blank line
731without a line number.
732A line containing only a control-l (form-feed) character
733will cause a page eject in the listing with the corresponding line number
734suppressed.
735This corresponds to an `eject' pseudo-instruction.
736See also section 5.2 for details on the
737.B n
738and
739.B i
740options of
741.PI .
742.NH 2
743Execution profiling
744.PP
745An execution profile consists of a structured listing of (all or part of)
746a program with information about the number of times each statement in
747the program was executed for a particular run of the program.
748These profiles can be used for several purposes.
749In a program which was abnormally terminated due to excessive looping
750or recursion or by a program fault, the counts can facilitate location
751of the error.
752Zero counts mark portions of the program which were not executed;
753during the early debugging stages they should prompt new test data or
754a re-examination of the program logic.
755The profile is perhaps most valuable, however, in drawing
756attention to the (typically small)
757portions of the program that dominate execution time.
758This information can be used for source level optimization.
759.SH
760An example
761.PP
762A prime number is a number which is divisible only by itself and the
763number one.
764The program
765.I primes ,
766written by Niklaus Wirth,
767determines the first few prime numbers.
768In translating the program we have specified the
769.B z
770option to
771.IX .
772This option causes the translator to generate counters and count instructions
773sufficient in number to determine the number of times each statement in the
774program was executed.\*(dg
775.FS
776\*(dgThe counts
777are completely accurate only in the absence of runtime errors and nonlocal
778.B goto
779statements.
780This is not generally a problem, however, as in structured programs
781nonlocal
782.B goto
783statements occur infrequently,
784and counts are incorrect after abnormal termination only when the
785.I "upward look"
786described below to get a count passes a suspended call point.
787.FE
788When execution of the program completes, either normally or abnormally,
789this count data is written to the file
790.I pmon.out
791in the current directory.\*(dd
792.FS
793\*(dd\c
794.I Pmon.out
795has a name similar to
796.I mon.out
797the monitor file produced by the profiling facility of the C compiler
798.I cc
799(1).
800See
801.I prof
802(1) for a discussion of the C compiler profiling facilities.
803.FE
804It is then possible to prepare an execution profile by giving
805.XP
806the name of the file associated with this data, as was done in the following
807example.
808.LS
809% \*bpix -l -z primes.p\fR
810.so primeout1
811%
812.LE
813.SH
814Discussion
815.PP
816The header lines of the outputs of
817.IX
818and
819.XP
820in this example indicate the version of the translator and execution
821profiler in use at the time this example was prepared.
822The time given with the file name (also on the header line)
823indicates the time of last modification of the program source file.
824This time serves to
825.I "version stamp"
826the input program.
827.I Pxp
828also indicates the time at which the profile data was gathered.
829.LS
830% \*bpxp -z primes.p\fR
831.so primeout2
832%
833.LE
834.KE
835.PP
836To determine the number of times a statement was executed,
837one looks to the left of the statement and finds the corresponding
838vertical bar `|'.
839If this vertical bar is labelled with a count then that count gives the
840number of times the statement was executed.
841If the bar is not labelled, we look up in the listing to find the first
842`|' which directly above the original one which has a count and that
843is the answer.
844Thus, in our example,
845.I k
846was incremented 157 times on line 18,
847while the
848.I write
849procedure call on line 24 was executed 48 times as given by the count
850on the
851.B repeat .
852.PP
853More information on
854.I pxp
855can be found in its manual section
856.XP
857(1)
858and in sections 5.4, 5.5 and 5.10.