More gotos...

David Burhans dwb at sppy00.UUCP
Tue Sep 12 02:42:27 AEST 1989


In article <10353 at phoenix.Princeton.EDU> tbrakitz at phoenix.Princeton.EDU (Byron Rakitzis) writes:
>I came across what I thought was a good application for a goto:
>say you're scanning an input stream for tokens, and that you
>skip over whitespace. Furthermore, suppose that there is a pair
>of comment-delimiter characters. You also want to skip over comments.
>So:
>
>token_grabbing_function()
> {
>
> get_a_token:
>
>   while (is_white_space(c = getchar()); /* skip it */
>
>   /* So, c contains a non-whitespace character */
>
>   if (c == comment_delimiter)
>    { 
>    while (c = getchar() != other_comment_delimiter); /* skip it */
>    goto get_a_token;
>    }
>   else
>    {
>    deal_with_the_token_properly_and_return;
>    }
> }
>
>Can anyone see a goto-less version which is as concise?
>(I'm ignoring EOF errors and the like for the moment)

  How's this:

  token_grabbing_function()
  {
    while(1)
    {
      while(is_white_space(c = getchar()); /* skip it */

      if (c == comment_delimiter)
	while( (c = getchar()) != other_comment_delimiter);
      else
      {
	/* deal with token and return */
      }
    }
  }

  -DwB
-- 
dwb at sppy00	{att|killer|pyramid}!osu-cis!sppy00!dwb	
David Burhans/6565 Frantz Rd./Columbus, Oh 43017
  ****  Views expressed above were randomly generated with a six
  	sided die and as such are not the views of my employer.   *****



More information about the Comp.lang.c mailing list