4 * wc.lex : A simple example of using FLEX
5 * to create a wc-like utility.
7 * See MISC/fastwc/ in the flex distribution for examples
8 * of how to write this scanner for maximum performance.
19 * rules start from here
26 [\n] { numchars++; numlines++; }
28 [^ \t\n]+ { numwords++; numchars += yyleng; }
34 * additional C code start from here. This supplies
35 * all the argument processing etc.
38 int main(int argc, char *argv[])
41 int lflag = 0; /* 1 if we count # of lines */
42 int wflag = 0; /* 1 if we count # of words */
43 int cflag = 0; /* 1 if we count # of characters */
44 int fflag = 0; /* 1 if we have a file name */
46 for(loop=1; loop<argc; loop++){
47 if(argv[loop][0] == '-'){
48 switch(argv[loop][1]){
59 fprintf(stderr,"unknown option -%c\n",
64 if(lflag == 0 && wflag == 0 && cflag == 0){
65 lflag = wflag = cflag = 1; /* default to all on */
68 for(loop=1; loop<argc; loop++){
69 if(argv[loop][0] != '-'){
71 numlines = numchars = numwords = 0;
72 if((yyin = fopen(argv[loop],"rb")) != NULL){
83 printf("file : %25s :",argv[loop]) ;
85 fprintf(stdout,"lines %5d ",numlines);
88 fprintf(stdout,"characters %5d ",numchars);
91 fprintf(stdout,"words %5d ",numwords);
95 fprintf(stderr,"wc : file not found %s\n",argv[loop]);
100 fprintf(stderr,"usage : wc [-l -w -c] file [file...]\n");
101 fprintf(stderr,"-l = count lines\n");
102 fprintf(stderr,"-c = count characters\n");
103 fprintf(stderr,"-w = count words\n");
106 for(loop=0;loop<79; loop++){
109 fprintf(stdout,"\n");
110 fprintf(stdout,"total : %25s ","") ;
112 fprintf(stdout,"lines %5d ",totlines);
115 fprintf(stdout,"characters %5d ",totchars);
118 fprintf(stdout,"words %5d ",totwords);
120 fprintf(stdout,"\n");