flex/examples/manual/example_er.lex
Eric S. Raymond 8d0162b80a Update all the examples to use the new API elements.
Add a fully reentrant example.  And update to TODO file.
2020-10-12 21:07:14 -04:00

36 lines
706 B
Plaintext

/* 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 );
}