home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / share / doc / ps1 / 04.pascal / puman2.n < prev    next >
Encoding:
Text File  |  1991-04-17  |  22.2 KB  |  859 lines

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