/** * Read in from a specified file and * print out a list of all the integers in the file * * flex -o test.yy.c test.yy * gcc -o test test.yy.c -ll */ int count = 0; // the whitespace here is important.(the declaration is tabbed in) DIGIT [0-9] %% {DIGIT}+ { printf("number: %10d\n", atoi(yytext)); /*yytex a special character available to the C code contains the text corresponding to the current token: the text matched by the regular expression */ } \n count++; . /* skip all other input */ %% int main( int argc, char *argv[] ) { if (argc > 1) yyin = fopen( argv[1], "r" ); //where yylex reads its input yylex(); // a function of flex that read input till it is exhausted printf("There are %d lines in the file.\n", count); return 0; }