blob: 284031c9c9e21442e8aff006f205221613834e48 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/* basic example, fully reentrant thread-safe version */
%{
struct stats {
int num_lines;
int num_chars;
};
%}
%option reentrant noyywrap
%option extra-type="struct stats"
%%
\n {
struct stats ns = yyget_extra(yyscanner);
++ns.num_lines; ++ns.num_chars;
yyset_extra(ns, yyscanner);
}
. {
struct stats ns = yyget_extra(yyscanner);
++ns.num_chars;
yyset_extra(ns, yyscanner);
}
%%
int main() {
yyscan_t scanner;
struct stats ns;
yylex_init ( &scanner );
yylex ( scanner );
ns = yyget_extra(scanner);
printf( "# of lines = %d, # of chars = %d\n",
ns.num_lines, ns.num_chars);
yylex_destroy ( scanner );
}
|