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