/** * Read in from a specified file and * print out a list of all the integers in the file * * flex -o count_ints.c count_ints.yy * gcc -o count_ints count_ints.yy.c -ll Preliminary solution. Make a rule for finding just one digit: [0-9] printf( "found a digit: %s\n", yytext ); Alternate solutions 1) Use this alternate definition for digit DIGIT [0-9] {DIGIT}+ printf( "found an integer: %s\n", yytext ); 2) Skip the definition altogether and use the expression directly [0-9]+ printf( "found an integer: %s\n", yytext ); 3) Use the built-in class [:digit:] [[:digit:]]+ printf( "found an integer: %s\n", yytext ); We tested it with the file: blah 1324oih hi3 cat cad catdog 10298 097 this is an interesting file! Also, we learned that pattern that matches the longest string wins. If multiple patterns match the same string (i.e. strings of the same length), then the first pattern in the list is used. (so if we put the . before the {DIGIT}+ then we ignore the 3 in the third line of our test. */ int count = 0; // Tab over C code DIGIT 0|1|2|3|4|5|6|7|8|9 %% {DIGIT}+ printf( "found an integer: %s\n", yytext ); . \n count++; %% 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( "Found %d lines\n", count ); return 0; }