flex 用於統計檔案字元、詞以及行數
阿新 • • 發佈:2018-12-18
* 安裝bison/flex
sudo apt-get install flex
sudo yum install flex
* words.l
%option noyywrap %{ int chars = 0; int words = 0; int lines = 0; %} %% [a-zA-Z]+ { words++; chars += strlen(yytext); } \n { chars++; lines++; } . { chars++; } %% main(int argc, char **argv) { if(argc > 1) { if(!(yyin = fopen(argv[1], "r"))) { perror(argv[1]); return (1); } yylex(); printf("%8d%8d%8d\n", lines, words, chars); } }
* compile
flex words.l
gcc lex.yy.c -o words
* run
./words /etc/passwd
29 220 1381
http://www.php-internals.com/book/?p=chapt02/02-03-01-lex-and-yacc