1 /* ---------------------------------------------------
 2    Compilers: Fall 2011                  Aug. 24, 2011
 3
 4    flex: generates a regex pattern matcher for you.
 5     (flex.sourceforge.net/manual/ or 'info flex')
 6
 7
 8    syntax of a flex file:
 9
10         -------------------
11         %{
12             declarations
13         %}
14             definitions
15         %%                  * (required)
16             rules           * (required)
17         %%
18             user routines
19         -------------------
20         
21     rules occurring earlier in the list receive
22     preference.
24
25     any text not meant for flex gets passed on to the
26     generated C file.
27
28     if you know what Perl regexes are, you won't have
29     them here -- just POSIX.
30
31     regular expressions. google is your friend.
32
33     don't forget to tell gcc to use the flex library
34     with '-lfl'
35
36     global variables:
37         yytext      null-terminated lexeme; overwritten
38                     by subsequent token
39         yyleng      integer; length of lexeme
40   
41    --------------------------------------------------- */
42
43
44 %{
45     int chars    = 0,
46         newlines = 0,
47         words    = 0;
48 %} 
49
50 %%
51
52 \n              { newlines++; chars++; }
53 [[:graph:]]+    { chars += yyleng; words++; }
54 .               { chars++; }
55
56 %%
57
58 int main( int argc, char **argv )
59 {
60     // process input
61     yylex();
62
63     // no more input to process
64     printf("words\tchars\tlines\n");
65     printf("%d\t%d\t%d\n",words,chars,newlines);
66
67     exit( EXIT_SUCCESS );
68 }