tbrakitz@phoenix.Princeton.EDU (Byron Rakitzis) (09/11/89)
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) Cheers. (ducks for the flames) -- "C Code." "C Code run." "Run, Code, run!" Byron Rakitzis. (tbrakitz@phoenix.princeton.edu ---- tbrakitz@pucc.bitnet)
dwb@sppy00.UUCP (David Burhans) (09/11/89)
In article <10353@phoenix.Princeton.EDU> tbrakitz@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@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. *****
darcy@bbm.UUCP (D'Arcy Cain) (09/12/89)
In article <10353@phoenix.Princeton.EDU> tbrakitz@phoenix.Princeton.EDU (Byron Rakitzis) writes: >I came across what I thought was a good application for a goto: >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? OK how about; token_grabbing_function() { for (;;) { 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 */ else { deal_with_the_token_properly_and_return; } } } D'Arcy J.M. Cain (darcy@bbm, darcy@cain)