goto's in 'C'

John Chambers jc at mit-athena.ARPA
Sat Jan 26 00:37:43 AEST 1985


> The incidence of the "goto" in C code is so rare anyway, I dare say
> we could abolish it altogether (replacing it with BLISS's "leave")
> and not miss the loss. 

Don't you dare!  There are some of us who need to write "pre-processors"
to translate some funny (usually special-purpose) language into something
compilable.  C is a popular target for such translators.  The main
reason is that C's grammar is flexible enough that it's relatively
easy to generate it.  Without gotos, it would be much harder.  Have
you ever tried to write a routine to generate properly structured
code when the semantics of the languages don't quite line up?  The
only practical way I know of is with labels and gotos.

Granted, gotos may be undesirable with human-generated code.  I use
them, but normally only to exit from deep nesting, and even this can
be done with only a small loss of performance by writing subroutines 
and using return instead of goto.  But generating structured code
automatically?  Does anyone know a clean way to do this?

If you want a test case to play with, consider translating a C for
loop into the goto-less language of your choice.  Take an example
like:
    for (i=0,p=&x[j]; *p && p->f && p->f[i]; i++, p=p->next) {
       ...
       if (p->x >= p->f[i]) break;
       ...
    }
Note that I'm not asking how you would express this in your goto-less
language.  That's easy.  I'm asking how you would write a translator
that takes this and produces code that is semantically exactly the
same in your goto-less language.  I contend that, with gotos it is
in general easy; without gotos it is in general difficult.

			John Chambers



More information about the Comp.lang.c mailing list