[comp.lang.c] I need to create ANSI C function prototypes.

dcw@doc.ic.ac.uk (Duncan C White) (02/18/88)

Hi there everyone,

Several times in the last six months, in various newsgroups, there
have been requests such as Lloyd Zusman's ( fxgrp!ljz@ames.arpa ) :

> I'm looking for a program that will take old-fashioned C code and
> produce ANSI C function prototypes.
> ...
> This program would produce something similar to the following:
> 
> 	char *foo(char *a, int b, int c);

Sometime last year, I started using my Atari ST for weekend development -
for fairly small programs.
The compiler I have allows prototypes, so I thought that a tool to
automagically whip up suitable external declarations might be useful.

In consequence, over one such weekend [and subsequent spare time]
I have written a first approximation :-) of such a utility.

It has, however, the following restrictions:

1).	each line is parsed to see if it "looks like" a function
	declaration.... so the entire decln [return type, name,
	param list] must be placed all on one line...
	anything else was too complex for a weekend :-)

2).	I didn't bother putting in arrays cos I always use pointers..

3).	It will not accept "complex" declarations like
		char *(**f)()
	[ie. pointers to functions..]

4).	It will not accept something like:
		struct fred *zippo();
	because it scans for a SINGLE id, followed by any no of stars...
	[reason: I always typedef or #define struct fred as FRED]

In article <445@spectrix.UUCP> clewis@spectrix.UUCP (Chris R. Lewis) writes:
>This is probably not to hard to do with awk.  Swallow a whole function
>definition until the leading brace and then rewrite it.

Hmm... very UNIX specific...

>.... With YACC (this
>will probably work much better): find a yacc grammar for C.  In the
>parameter declaration portion, insert actions to reformat the text and
>spit it out.  Don't emit anything else.
>

This is obviously the way to go....  anyone got a PD grammar for C ?
YACC if necessary, recursive descent preferred...


If anyone's interested, I also wrote a makefile builder, which analyses the
sources of a multi-module C program, and attempts to build the relationships
contained therein:
it only works, of course, if you program in a MODULAR fashion ( ie. a file x.h
is an "interface" for module x, and all users of x - and x.c itself - must
#include x.h )

The tool emits makefiles for any one of UNIX, Lattice C on Atari ST, MicroSoft C
for IBM PC...

[ Don't tell me about make depend.... that's very UNIX specific ]


If anyone is interested in either of these, mail me: they're about 8k source
each.  They are public domain [in the non-strictly-legal, USENET sense, ie.
don't use it commercially, I accept no responsibility etc]

I should mention, however, that our mailer experiences difficulties sending mail
to people with UUCP mail addresses...  i think incoming mail is ok, but I may
not be able to reply to it!
Perhaps it would be better if I simply post either/both to the net ?

[Say ten more "yes, post it **please**" than "arrgh, no..." responses?]

	Duncan.

----------------------------------------------------------------------------
Duncan White,           |       Flying is the art of aiming oneself
Dept. Of Computing,     |       at the ground and missing.
Imperial College,       |               -- Douglas Adams, So Long and Thanks
London SW7, England     |                  for all the fish.