Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!rutgers!rochester!pt!ius1.cs.cmu.edu!edw
From: edw@ius1.cs.cmu.edu (Eddie Wyatt)
Newsgroups: comp.lang.c
Subject: Re: goto's in C: an opinion...
Message-ID: <1023@ius1.cs.cmu.edu>
Date: Mon, 27-Jul-87 10:01:07 EDT
Article-I.D.: ius1.1023
Posted: Mon Jul 27 10:01:07 1987
Date-Received: Tue, 28-Jul-87 02:14:35 EDT
References: <3289@bigburd.PRC.Unisys.COM> <7571@beta.UUCP> <1237@ius2.cs.cmu.edu> <162@pyuxf.UUCP>
Organization: Carnegie-Mellon University, CS/RI
Lines: 77

In article <162@pyuxf.UUCP>, asg@pyuxf.UUCP (alan geller) writes:
> In article <1237@ius2.cs.cmu.edu>, edw@ius2.cs.cmu.edu.UUCP writes:
> > 
> > In an article by Skip Egdorf (hwe@beta.UUCP), Skip sez,
> > > There are NO legitimate uses for the goto statement...
> > 
> >    I sez..
> >    There is a whole class of problems that map very nicely into goto contructs.
> > They are simulation of NFAs and DFAs (ie finite state machines).
> > 
> >    States map very nicely to labels and transitions map very nicely into
> > if (input == ?) goto label.
> > 
> >    The most readable way one can represent the NFA/DFA is through a mesh
> > of gotos with a diagram of the machine in comments :-).
> 
> I disagree -- the most readable way to present a finite state machine
> is with a transition matrix (array), an action matrix (array), and a
> driver routine that looks like:
> 
> 		state = INITIAL_STATE;
> 		while (state != ACCEPT_STATE) {
> 			input = getInput();
> 			performAction(actionMatrix[state][input]);
> 			state = transitionMatrix[state][input];
> 		}
> or something along those lines.  This produces code that is more easily
> maintained, more compact, and often faster than using a bushel of goto's.

	    START : 
	        input = getchar();
		if (isdigit(input) goto INTEGER;
		if (input == '.')  goto FLOAT ;
		if (input == '+')  goto START ;
		if (input == '-')  goto START ;
		goto ERROR;

	    INTEGER :
	        input = getchar();
		if (isdigit(input) goto INTEGER;
		if (input == '.')  goto FLOAT2;
		ungetc(stdin,input);
		return(INT_TOKEN);

	    FLOAT :
	        input = getchar();
		if (isdigit(input) goto FLOAT2;
		goto ERROR;

	    FLOAT2 :
	        input = getchar();
		if (isdigit(input) goto FLOAT2;
		ungetc(stdin,input);
		return(FLOAT_TOKEN);

	    ERROR :
		(void) fprintf(stderr,"lex error\n");
		goto START; /* try again */

     Is the above code really that hard to understand?  BTW I know
there are much more efficient ways of coding the above - this is just
an example.

     I would say that transitional tables are no more readable than this
approach, but I guess this is all a matter of taste.

   Also, the transitional tables have one very good advance over this approach
which is, it requires the programmer to enumerate all posible actions
associated with every state-input pair.  But transitional tables are
wasteful if the matrix is sparse (ie. more error transitions than good
transitions).

-- 

					Eddie Wyatt

e-mail: edw@ius1.cs.cmu.eduo