[comp.lang.c] ANSI -> K&R translator

rober@attila.WEITEK.COM (Don Rober) (08/25/90)

Does anyone know of an ANSI-to-K&R translator? Specifically, I need to
de-prototype thousands of lines of code and any mechanical translator
is less error-prone than I.

-- 
----------------------------------------------------------------------------
Don Rober				       UUCP: {pyramid}!weitek!rober
Weitek Corporation	1060 East Arques		Sunnyvale, CA 94086

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (08/26/90)

In article <907@attila.WEITEK.COM>, rober@attila.WEITEK.COM (Don Rober) writes:
> Does anyone know of an ANSI-to-K&R translator? Specifically, I need to
> de-prototype thousands of lines of code and any mechanical translator
> is less error-prone than I.

There is a set of patches to GCC, which you can get from the same
places (such as prep.ai.mit.edu) that you can get GCC, that yield
protoize/deprotoize or whatever they are called.

I have a couple of m4 files I have been playing with, in order to go
the other way.  The rest of this message is a script.  The m4 files
have been written for the least capable version of m4 I could find.

% cat classic.m4		# definitions for "Classic C"
define(PROTO,`()')
define(M4w,
`ifelse(index($1,` '),-1,ifelse(index($1,`*'),-1,$1,
`M4w(substr($1,incr(index($1,`*'))))'),
`M4w(substr($1,incr(index($1,` '))))')')dnl
define(_,
`ifelse(
$1,,`()',
$2,,`(M4w($1))
    $1;',
$3,,`(M4w($1)`,' M4w($2))
    $1;
    $2;',
$4,,`(M4w($1)`,' M4w($2)`,' M4w($3))
    $1;
    $2;
    $3;',
$5,,`(M4w($1)`,' M4w($2)`,' M4w($3)`,' M4w($4))
    $1;
    $2;
    $3;
    $4;',
$6,,`(M4w($1)`,' M4w($2)`,' M4w($3)`,' M4w($4)`,' M4w($5))
    $1;
    $2;
    $3;
    $4;
    $5;',
$7,,`(M4w($1)`,' M4w($2)`,' M4w($3)`,' M4w($4)`,' M4w($5)`,' M4w($6))
    $1;
    $2;
    $3;
    $4;
    $5;
    $6;',
$8,,`(M4w($1)`,' M4w($2)`,' M4w($3)`,' M4w($4)`,' M4w($5)`,' M4w($6)`,' M4w($7))
    $1;
    $2;
    $3;
    $4;
    $5;
    $6;
    $7;',
$9,,`(M4w($1)`,' M4w($2)`,' M4w($3)`,' M4w($4)`,' M4w($5)`,' M4w($6)`,' M4w($7)`,' M4w($8))
    $1;
    $2;
    $3;
    $4;
    $5;
    $6;
    $7;
    $8;',
`errprint(Too many arguments)')')dnl
% 
% cat ansi.m4			# definitions for "ANSI C"
define(PROTO,`$1')
define(_,
`ifelse(
$1,,`()',
$2,,`($1)',
$3,,`($1`,' $2)',
$4,,`($1`,' $2`,' $3)',
$5,,`($1`,' $2`,' $3`,' $4)',
$6,,`($1`,' $2`,' $3`,' $4`,' $5)',
$7,,`($1`,' $2`,' $3`,' $4`,' $5`,' $6)',
$8,,`($1`,' $2`,' $3`,' $4`,' $5`,' $6`,' $7)',
$9,,`($1`,' $2`,' $3`,' $4`,' $5`,' $6`,' $7`,' $8)',
`errprint(Too many arguments)')')dnl
% cat test.c			# Example of a source file
extern int  scanf  PROTO((char *, ...));
extern int  printf PROTO((char *, ...));
extern void exit   PROTO((int));

main _(int argc, char **argv)	/* note the space before the underscore */
    {
        double x;

        while (scanf("%lg", &x) == 1) printf("%g\n", x);
        exit(1);
    }

% /bin/m4 classic.m4 test.c	# produce the "Classic C" version of test.c

extern int  scanf  ();
extern int  printf ();
extern void exit   ();

main (argc, argv)
    int argc;
    char **argv;
    {
        double x;

        while (scanf("%lg", &x) == 1) printf("%g\n", x);
        exit(1);
    }

% /bin/m4 ansi.m4 test.c	# produce the "ANSI C" version of test.c

extern int  scanf  (char *, ...);
extern int  printf (char *, ...);
extern void exit   (int);

main (int argc, char **argv)
    {
        double x;

        while (scanf("%lg", &x) == 1) printf("%g\n", x);
        exit(1);
    }

% exit

Not foolproof, but rather fun.
-- 
The taxonomy of Pleistocene equids is in a state of confusion.

peter@ficc.ferranti.com (Peter da Silva) (08/29/90)

The one problem with M4 is that it's got this annoying tendency to do
replacements that aren't intended (what happens if you have a variable
called "dnl"?). When I did a version of MACRO (m4 in Ratfor) for RSX, I
made all the builtind start with "$".
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com

thomas@uplog.se (Thomas Tornblom) (08/29/90)

In article <FRI5NYC@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:

   The one problem with M4 is that it's got this annoying tendency to do
   replacements that aren't intended (what happens if you have a variable
   called "dnl"?). When I did a version of MACRO (m4 in Ratfor) for RSX, I
   made all the builtind start with "$".
   -- 
   Peter da Silva.   `-_-'
   +1 713 274 5180.   'U`
   peter@ferranti.com

If you're using system V m4 you can always redefine the m4 builtins to
something harmless:

define(`_m4_dnl', defn(`dnl'))dnl
undefine(`dnl')_m4_dnl
-- 
Real life:	Thomas Tornblom		Email:	thomas@uplog.se
Snail mail:	TeleLOGIC Uppsala AB		Phone:	+46 18 189406
		Box 1218			Fax:	+46 18 132039
		S - 751 42 Uppsala, Sweden

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (08/29/90)

In article <FRI5NYC@xds13.ferranti.com>, peter@ficc.ferranti.com (Peter da Silva) writes:
> The one problem with M4 is that it's got this annoying tendency to do
> replacements that aren't intended (what happens if you have a variable
> called "dnl"?).

The M4 I *really* use is Ozan Yigit's PD version, modelled on Sys V M4.
In System V M4 and the PD one, you can say
	define(M4__dnl,defn(`dnl'))undefine(`dnl')M4__dnl
The files I posted are the totally stripped down BSD versions.
-- 
You can lie with statistics ... but not to a statistician.

emcmanus@cs.tcd.ie (Eamonn McManus) (08/31/90)

peter@ficc.ferranti.com (Peter da Silva) writes:
>The one problem with M4 is that it's got this annoying tendency to do
>replacements that aren't intended (what happens if you have a variable
>called "dnl"?). When I did a version of MACRO (m4 in Ratfor) for RSX, I
>made all the builtind start with "$".

Some versions of m4, System V for example, provide a builtin `defn'.  The
manual says:

defn	returns the quoted definition of its argument(s).  It is useful
	for renaming macros, especially built-ins.

Unfortunately, this is a lie.  `defn' doesn't expand to anything if given
a builtin as its argument.  If it worked, you could rename all the m4
built-ins to begin with the string `m4' or some such.  It ought to be
quite easy to make defn work properly, for instance by having
defn(`somebuiltin') expand to `m4builtin(something)'.  Thus defn(`define')
might expand to m4builtin(1), and m4builtin(1) would behave exactly like
define: one could write m4builtin(1)(`foo', `bar').  Then only the word
`m4builtin' would be reserved by m4.  Other builtins could be renamed by a
sequence like:
	define(`m4define', defn(`define'))
	undefine(`define')
--
Eamonn McManus <emcmanus@cs.tcd.ie>	<emcmanus%cs.tcd.ie@cunyvm.cuny.edu>
			Fingers are for fuguing.

emcmanus@cs.tcd.ie (Eamonn McManus) (09/03/90)

I wrote about System V m4:
> `defn' doesn't expand to anything if given a builtin as its argument.
and of course I was wrong.  In fact it expands to a control character with
bit 7 set, which is why I hadn't seen anything when I'd tried it
interactively.  This is a rather ugly approach, but I suppose it's faster
than what I suggested.
--
Eamonn McManus <emcmanus@cs.tcd.ie>	<emcmanus%cs.tcd.ie@cunyvm.cuny.edu>
			Fingers are for fuguing.