An Example of noweb

The following short program illustrates the use of noweb,
a low-tech tool for literate programming.
The purpose of the program is to provide a basis for
comparing WEB and noweb, so I have used a program that has
been published before;
the text, code, and presentation are taken
from Chapter 12 of D. E. Knuth,
Literate Programming
(volume 27 of Center for the Study of
Language and Information Lecture Notes,
Stanford Univ., 1992).

The notable differences are:

When displaying source code,
noweb uses different typography.
In particular, WEB makes good use of multiple fonts
and the ablity to typeset mathematics, and it may use
mathematical symbols in place of C symbols (e.g.
a logical ``and'' symbol for ``&&'').
noweb uses a single fixed-width font for code.

noweb can work with HTML, and I have used HTML in this example.

noweb has no numbered ``sections.''
When cross-referencing is needed, noweb uses hypertext links or page
numbers.

noweb has no special support for macros.
In the sample program, I have used
a ``Definitions'' chunk to hold
macro definitions.

noweb's index of identifiers is less accurate than WEB's,
because it uses a language-independent heuristic to find identifiers.
This heuristic may erroneously find ``uses'' of identifiers
in string literals or comments.
Although noweb does have a language-dependent algorithm for finding
definitions of identifiers, that algorithm is less reliable than CWEB's,
because noweb does not really parse C code.

The CWEB version of this program has semicolons following most uses
of <...&gt.
WEB needs the semicolon or its equivalent to make
its prettyprinting come out right.
Because it does not attempt prettyprinting, noweb needs no semicolons.

This example, based on a program by Klaus Guntermann and
Joachim Schrod (`WEB adapted to C.'
TUGboat7(3):134-7, Oct. 1986)
and a program by Silvio Levy and
D. E. Knuth (Ch. 12 of Literate Programming),
presents the ``word count''
program from Unix, rewritten in noweb to demonstrate
literate programming using noweb.
The level of detail in this document is intentionally high, for
didactic purposes; many of the things spelled out here don't need to
be explained in other programs.

The purpose of wc is to count lines, words, and/or characters in
a list of files.
The number of lines in a file is the number of newline characters it
contains.
The number of characters is the file length in bytes.
A ``word'' is a maximal sequence of consecutive characters other than
newline, space, or tab, containing at least one visible ASCII code.
(We assume that the standard ASCII code is in use.)

Most literate C programs share a common structure.
It's probably a good idea to state the overall structure explicitly at
the outset, even though the various parts could all be introduced in
chunks named <*> if we wanted to add them piecemeal.

Here, then, is an overview of the file wc.c that is defined by
the noweb program wc.nw:

If the first argument begins with a `-', the
user is choosing the desired counts and specifying
the order in which they should be displayed.
Each selection is given by the
initial character (lines, words, or characters).
For example, `-cl' would cause just the
number of characters and the number of lines to
be printed, in that order.

We do not process this string now; we simply remember where it is.
It will be used to control the formatting at output time.

Incidentally, a test of this program against the system wc
command on a SPARCstation showed that the ``official'' wc was
slightly slower.
Furthermore, although that wc gave an appropriate error message
for the options `-abc', it made no complaints about the options
`-labc'!
Dare we suggest that the system routine might have been better if its
programmer had used a more literate approach?