Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!mailrus!rutgers!iuvax!pur-ee!uiucdcs!uiucdcsb!kenny From: kenny@uiucdcsb.cs.uiuc.edu Newsgroups: comp.lang.c Subject: Re: Put your code... (was Re: gotos Message-ID: <165600040@uiucdcsb> Date: 25 Apr 88 23:56:00 GMT References: <2597@ttrdc.UUCP> Lines: 58 Nf-ID: #R:ttrdc.UUCP:2597:uiucdcsb:165600040:000:1308 Nf-From: uiucdcsb.cs.uiuc.edu!kenny Apr 25 18:56:00 1988 [Part 2: Finite-state recognizers and pushback] EXAMPLE 4 shows how _goto_s may be used to good effect in finite-state recognizers for processing text. Here we see, however, how programming tools have improved since Knuth's day. Most experienced C programmers, at least from among those workig on UN*X, would code EXAMPLE 4 with Lex [Lesk75]: %{ int column = 0; /* Column in the output */ %} %% '//' { putchar ('\n'); column = 0; } '/' { column = tabulate (column); } '.' { ECHO; putchar (' '); column += 2; } \n { ; } . { ECHO; ++column; } %% int tabulate (column) int column; { .... } Without Lex, there is another possibility, which is to use the `ungetc' feature of the standard I/O library. In this case, the procedure winds up being: c = getchar (); switch (c) { case EOF: return; case '\n': break; case '/': if ((c = getchar ()) == '/') { putchar ('\n'); column = 0; } else column = tabulate (column); break; case '.': putchar (c); putchar (' '); column += 2; break; default: putchar (c); column += 1; break; } Only if ungetc is unavailable do we need to resort to Knuth's examples. In this case, I actually prefer the use of the temporary Boolean; it may be interpreted as `there is a character in the ungetc buffer.'