ANSI -> K&R translator

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Sun Aug 26 22:21:13 AEST 1990


In article <907 at attila.WEITEK.COM>, rober at 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.



More information about the Comp.lang.c mailing list