I once wrote a C program on Linux, using Lex to do lexical analysis to simultaneously count the number of characters, words, and lines in n-text files. Makes me think Lex is really interesting. It's true that Lex is very powerful, and it's good to do small lexical analysis. This procedure refers to an example in Lex and YACC.
%{unsigned int char_count = 0, Word_count = 0, Line_Count = 0;
%%}% [^/t/n]+ {word_count++; char_count+=yyleng;};
/n {char_count++; line_count++;}; .
char_count++;
%% char **file_list;
unsigned int current_file = 0;
unsigned int total_file = 0;
unsigned int total_cc = 0;
unsigned int total_wc = 0;
unsigned int total_lc = 0;
typedef struct file_info{unsigned int c;
unsigned int w;
unsigned int l;
Char *name;
}info;
INFO **all;
int create_info (int num) {info *tmp;
int i;
if (num <= 0) {return-1;
all = (INFO * *) malloc (sizeof (int *) *num);
for (i = 0; i < num i++) {tmp = (info *) malloc (sizeof (info));
tmp->c = 0;
tmp->w = 0;
tmp->l = 0;
Tmp->name = NULL;
All[i] = tmp;
return 1;
int delete_info (int num) {int i;
if ((all = = (INFO * *) 0) | | | num <= 0) {return-1;
for (i = 0; i < Num. i++) {free (all[i]);
Free (all);
return 1;int set_info (int pos) {int length = 0;
if (POS < 0) {return-1;
} all[pos]->c = Char_count;
All[pos]->w = Word_count;
All[pos]->l = Line_Count;
All[pos]->name = File_list[pos];
return 1;
int main (int argc, char** argv) {FILE *file;
int position = 0;
int i;
File_list = argv + 1;
Total_file = argc-1;
Current_file = 0;
printf ("--------------------------------------------------------------/n", total_file); if (argc > 1) {if (Create_info (total_file) = = 1) {fprintf (stderr, "%s/n", "encounter a error when malloc m
Emory. ");
Exit (1);
} if (argc = 2) {File=fopen (argv[1], "R");
if (!file) {fprintf (stderr, "could not open%s./n", argv[1]);
Delete_info (Total_file);
Exit (1);
} yyin = file;
} yywrap ();
Yylex ();
if (argc > 1) {total_cc + = Char_count; TOTAL_WC + + Word_count;
TOTAL_LC + = Line_Count;
if (Set_info (current_file-1) = = 1) {fprintf (stderr, "%s/n", "encounter a error when set information to info.");
Delete_info (Total_file);
Exit (1); for (i = 0; i < total_file i++) {printf ("Char:%-8lu word:%-8lu line:%-8lu file name:%s/n", all
[i]-> C, All[i]->w, All[i]->l, File_list[i]);
printf ("-----------------------Total--------------------------------/n");
printf ("Chars:%-8lu words:%-8lu lines:%-8lu files:%d/n", total_cc, tot AL_WC, TOTAL_LC, total_file);
}else{printf ("Char:%-8lu word:%-8lu line:%-8lu/n", Char_count, Word_count, L ine_count);
} delete_info (Total_file);
return 0;
} yywrap () {FILE *file = NULL; if ((Current_file > 0) && (Current_file < Total_file) && (Total_file > 1)) {TOTAL_CC + = ch
Ar_count;
TOTAL_WC + = Word_count;
TOTAL_LC + = Line_Count; If(Set_info (current_file-1) = = 1)
{fprintf (stderr, "%s/n", "encounter a error when set information to INFO.");
Delete_info (Total_file);
Exit (1);
} char_count = Word_count = Line_Count = 0;
Fclose (Yyin); while ((File_list[current_file)!= (char *) 0) && (Current_file < Total_fil e)) {file = fopen (f
ile_list[current_file++], "R");
if (!file) {fprintf (stderr, "could not open%s.", File_list[current_file-1]);
}else{yyin = file;
Break
} return (file. 0:1);
}