/* * Count words -- interactively */ white = [\n\t ]; /* End of a word */ eol = [\0]; /* End of input line */ any = [!-~]; /* All printing char's */ illegal = [\0-\377]; /* Skip over junk */ %{ char line[133]; char *linep = &line; int is_eof = 0; int wordct = 0; #define T_EOL 1 main() { register int i; while ((i = yylex()) != 0) { /* * If the "end-of-line" token is returned * AND we're really at the end of a line, * read the next line. Note that T_EOL is * returned twice when the program starts * because of the nature of the look-ahead * algorithms. */ if (i == T_EOL && !is_eof && *linep == 0) { printf("* "); fflush(stdout); getline(); } } printf("%d words\n", wordct); } %} %% any(any)* { /* * Write each word on a seperate line */ lexecho(stdout); printf("\n"); wordct++; return(LEXSKIP); } eol { return(T_EOL); } white(white)* { return(LEXSKIP); } %% getline() /* * Read a line for lexgetc() */ { is_eof = (fgets(line, sizeof line, stdin) == NULL); linep = &line; } lexgetc() /* * Homemade lexgetc -- return zero while at the end of an * input line or EOF at end of file. If more on this line, * return it. */ { return((is_eof) ? EOF : (*linep == 0) ? 0 : *linep++); }