Not-small C program with noweb


From:     Eric Prestemon
Date: 12 Oct 1994
I am making my first foray into writing a more-than-trivial C program and my first foray into noweb. I have a few questions. Are there examples of finished C/noweb programs out there, beyond what is in the noweb distribution? Specifically, I am interested in how people write functions and function calls cleanly (rather than inlining a chunk, which seems the more natural *WEB way to do things).

Is noweb less usable than, say, CWEB or FWEB for this kind of thing? The printed output isn't too critical to me; only I and whoever maintains the program after I stop working on it will ever see it. I don't plan to spend much (any) time customizing the output. Also, a DOS implementation is handy, though much of my development is on a Unix machine. For these reasons, and my plan to use multiple output files, I think noweb is best, but I am open to other ideas. (As an aside, I am not much of DOS guru, and the dosnoweb-2.5a distribution didn't include a cpif, as far as I can tell. Is there an easy way to do such a thing in DOS? Between having multiple output files and a slow PC, only compiling the source files that change would save me a lot of time.)

Is there a good emacs mode for editing noweb files? I have been using C mode, which I guess is okay, but it's not perfect. Diving into the cc-mode.el code to make changes seems too difficult. This isn't critical, cc-mode gets the C parts right, and I just have to convince it to let me enter certain things in the text. Thanks for any help or any other comments,


From:     Norman Ramsey
Date: 13 Oct 1994
Eric Prestemon writes: Are there examples of finished C/noweb programs out there, beyond what is in the noweb distribution?

This is a good question. A while back (last year?) there was some idle talk about setting up an archive of literate programs for people to read. This would let people read some literate programs and compare tools and techniques. George Greenwade even said he might be able to provide space for such an archive after his new disk arrived. This is a task worth doing, but it would take nontrivial work to set up and maintain such an archive. (For example, do you keep source? .TeX? postscript? all of the above.) Any volunteers?

To answer your specific question, I don't know of any well-written C programs using noweb that are readily available. The best-written noweb program I have seen is about to be published as a book, but you can't get it yet:

  @book{fraser:retargetable:book,
    author="Christopher W. Fraser and David R. Hanson",
    title="A Retargetable {C} Compiler: Design and Implementation",
    publisher="Benjamin/Cummings", address="Redwood City, CA",
    year=1995, note="ISBN 0-8053-1670-1, in press"
  }
Don Knuth's latest book, The Stanford GraphBase, contains a number of CWEB programs that are worth some study. If you're learning C and literate programming, reading CWEB and writing noweb should present you no problems (writing CWEB is another story). Marcus Speh has a few examples on the WWW; try http://info.desy.de/user/projects/LitProg.html.

Is noweb less usable than, say, CWEB or FWEB for this kind of thing?

I claim noweb is more usable than CWEB or FWEB. See my articles in the September 1994 IEEE Software or the July 1991 Software---Practice & Experience.

Is there a good emacs mode for editing noweb files?

There are some emacs modes, including one distributed with noweb, but I think the world is still waiting for a good one...


From:     Lee Wittenberg
Date: 13 Oct 1994
Eric Prestemon writes: Are there examples of finished C/noweb programs out there, beyond what is in the noweb distribution? Specifically, I am interested in how people write functions and function calls cleanly (rather than inlining a chunk, which seems the more natural *WEB way to do things).

I don't know of any, unless you count the C++ programs in the DOS distribution. One of these days I hope to get some more samples to pub/leew/samples.LP at bart.kean.edu, but as of yet, all the C examples there use CWEB. The technique I generally use for C programs is an overall structure something like (I use this in CWEB, too):

<<[[#include]] lines>>
<<Global variables & constants>>
<<Function prototypes>>
<<Function definitions>>

Then, whenever I define a function, I write 2 chunks. For example:

<<Function prototypes>>=
int func(int, int);
<<Function definitions>>=
int func(int x, int y)
{
	return x + y;	/* what, you want a real function? */
}
I find this technique works very well, particularly since the prototype and the function definition stay in close proximity in the web. If one changes, the appropriate change is easy to make to the other.

Is noweb less usable than, say, CWEB or FWEB for this kind of thing? The printed output isn't too critical to me; only I and whoever maintains the program after I stop working on it will ever see it. I don't plan to spend much (any) time customizing the output. Also, a DOS implementation is handy, though much of my development is on a Unix machine. For these reasons, and my plan to use multiple output files, I think noweb is best, but I am open to other ideas.

In my experience, they are all equally usable. They each have their strengths and weaknesses. You pays your money and you takes your choice. noweb is probably the easiest of the three to learn, however.

(As an aside, I am not much of DOS guru, and the dosnoweb-2.5a distribution didn't include a cpif, as far as I can tell. Is there an easy way to do such a thing in DOS? Between having multiple output files and a slow PC, only compiling the source files that change would save me a lot of time.)

Barry Schwartz has written one for DOS, it just hasn't made it into the official DOS package yet (mea culpa). By the way, the current DOS package supports version 2.6c, and I recommend the upgrade.


From:     Barry Schwartz
Date: 14 Oct 1994
Eric Prestemon writes: Are there examples of finished C/noweb programs out there, beyond what is in the noweb distribution? Specifically, I am interested in how people write functions and function calls cleanly (rather than inlining a chunk, which seems the more natural *WEB way to do things).

I haven't yet written any large programs with noweb, but even in small programs I don't depend too much on the chunk mechanism, unless the language makes subroutines ungainly. (Perl may exhibit this property.) I like to put C subprograms in two main chunks. First I present a prototype, and then the definition, adjacent to each other. The prototype goes into a chunk that gets tangled to near the top of the C file, and (in a multiple-file program) to a header file. The definition can go almost anywhere in the C file.

Some languages are much easier to handle. In Icon, for instance, globals can go anywhere, and there's no need for prototypes or the like. This works out very well; you can just put all globals in the root chunk, or wherever you like.

(As an aside, I am not much of DOS guru, and the dosnoweb-2.5a distribution didn't include a cpif, as far as I can tell.

I have written a cpif in C/noweb, for MS-DOS, but it hasn't been released yet. However, there is nothing outlandish about the code; all I did was duplicate the functions of the shell script.