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