Use Lex Statistical Text File Characters

xiaoxiao2021-03-06  126

I used to write a C program written in Linux, using the LEX to make a word, the number, the number of words, and the number of lines of the number of words. Let me think Lex is really interesting. It does LEX's function is very powerful, and it is very suitable for compact lexical analysis. It is also very easy to use. This program refers to an example of "LEX and YACC".

% {unsigned int car_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 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 c = char_count; all [pOS] -> w = word_count; all [pOS] -> L = line_count; all = 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 memory"); 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 (); yylexi (); 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 ("----------------------------------------------- -------- / 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 1)) {TOTAL_CC = char_count; Total_WC

转载请注明原文地址:https://www.9cbs.com/read-99774.html

New Post(0)