]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/lex/word_count_functor.flex
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / lex / word_count_functor.flex
1 %{
2 // Copyright (c) 2001-2009 Hartmut Kaiser
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <boost/timer.hpp>
8 #if defined(_WIN32)
9 #include <io.h>
10 #endif
11 #define ID_WORD 1000
12 #define ID_EOL 1001
13 #define ID_CHAR 1002
14 %}
15
16 %%
17 [^ \t\n]+ { return ID_WORD; }
18 \n { return ID_EOL; }
19 . { return ID_CHAR; }
20 %%
21
22 bool count(int tok, int* c, int* w, int* l)
23 {
24 switch (tok) {
25 case ID_WORD: ++*w; *c += yyleng; break;
26 case ID_EOL: ++*l; ++*c; break;
27 case ID_CHAR: ++*c; break;
28 default:
29 return false;
30 }
31 return true;
32 }
33
34 int main(int argc, char* argv[])
35 {
36 int tok = EOF;
37 int c = 0, w = 0, l = 0;
38 yyin = fopen(1 == argc ? "word_count.input" : argv[1], "r");
39 if (NULL == yyin) {
40 fprintf(stderr, "Couldn't open input file!\n");
41 exit(-1);
42 }
43
44 boost::timer tim;
45 do {
46 tok = yylex();
47 if (!count(tok, &c, &w, &l))
48 break;
49 } while (EOF != tok);
50 printf("lines: %d, words: %d, characters: %d\n", l, w, c);
51 fclose(yyin);
52 return 0;
53 }
54
55 extern "C" int yywrap()
56 {
57 return 1;
58 }
59