flex 2.5.35
论文写到此处,遇到点麻烦,随手翻了本书,说下flex中的注释问题。中文版的35页有点问题,所以纠正下。
下面是p31示例 fb2_2.l
1 /* 读取多个文件 */ 2 %option noyywrap 3 4 %{ 5 int chars = 0; 6 int words = 0; 7 int lines = 0; 8 9 int totchars = 0; 10 int totwords = 0; 11 int totlines = 0; 12 %} 13 14 %% 15 ♥//匹配字符串 ♥ 表示空格 16 [a-zA-Z]+ { //习惯C风格,所以写成了这样 17 { //原因:参见中文版 p9 倒数第9行中括号内的文字 18 words++; chars += strlen(yytext); 19 } 20 } 21 22 ♥ /*匹配 23 ♥ *换行符 24 ♥ */ 25 { 26 { 27 chars++; lines++; 28 } 29 } 30 31 ♥ /* 其它 */ 32 . { 33 { 34 chars++; 35 } 36 } 37 38 %% 39 40 int main(int argc, char **argv) 41 { 42 int i; 43 if (argc < 2) 44 { 45 yylex(); 46 printf("lines: %8d " 47 "words: %8d " 48 "chars: %8d ", 49 lines, words, chars); 50 return 0; 51 } 52 53 for (i = 1; i < argc; i++) 54 { 55 FILE *f = fopen(argv[i], "r"); 56 57 if (!f) 58 { 59 perror(argv[i]); 60 return 1; 61 } 62 yyrestart(f); 63 yylex(); 64 fclose(f); 65 printf("lines: %8d " 66 "words: %8d " 67 "chars: %8d ", 68 lines, words, chars); 69 totchars += chars; 70 totwords += words; 71 totlines += lines; 72 chars = 0; 73 words = 0; 74 lines = 0; 75 } 76 if (argc > 1) 77 { 78 printf("totlines: %8d " 79 "totwords: %8d " 80 "totchars: %8d ", 81 totlines, totwords, totchars); 82 } 83 return 0; 84 }