The output of flex
is the file `lex.yy.c', which contains
the scanning routine yylex
, a number of tables used by it for
matching tokens, and a number of auxiliary routines and macros. By
default, yylex
is declared as follows:
int yylex() { ... various definitions and the actions in here ... }
(If your environment supports function prototypes, then it will be
`int yylex( void )'.) This definition may be changed by redefining
the YY_DECL
macro. For example, you could use:
#undef YY_DECL #define YY_DECL float lexscan( a, b ) float a, b;
to give the scanning routine the name lexscan
, returning a
float
, and taking two float
values as arguments. Note
that if you give arguments to the scanning routine using a
K&R-style/non-prototyped function declaration, you must terminate the
definition with a semicolon (`;').
Whenever yylex
is called, it scans tokens from the global input
file `yyin' (which defaults to `stdin'). It continues until
it either reaches an end-of-file (at which point it returns the value 0)
or one of its actions executes a return statement. In the former case,
when called again the scanner will immediately return unless
yyrestart
is called to point `yyin' at the new input file.
(yyrestart
takes one argument, a `FILE *' pointer.) In the
latter case (i.e., when an action executes a return), the scanner may
then be called again and it will resume scanning where it left off.
By default (and for efficiency), the scanner uses block-reads rather
than simple getc
calls to read characters from `yyin'. You
can control how it gets input by redefining the YY_INPUT
macro.
YY_INPUT
's calling sequence is
`YY_INPUT(buf,result,max_size)'. Its action is
to place up to max_size characters in the character array
buf and return in the integer variable result either the number of
characters read or the constant YY_NULL
(0 on Unix systems) to
indicate EOF. The default YY_INPUT
reads from the global
file-pointer `yyin'.
A sample redefinition of YY_INPUT
(in the definitions section
of the input file):
%{ #undef YY_INPUT #define YY_INPUT(buf,result,max_size) \ { \ int c = getchar(); \ result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \ } %}
This definition will change the input processing to occur one character at a time.
You also can add in things like keeping track of the input line number this way; but don't expect your scanner to go very fast.
When the scanner receives an end-of-file indication from
YY_INPUT
, it then checks the yywrap
function. If
yywrap
returns false (zero), then it is assumed that the function
has gone ahead and set up `yyin' to point to another input file,
and scanning continues. If it returns true (non-zero), then the
scanner terminates, returning 0 to its caller.
The default yywrap
always returns 1. At present, to redefine it
you must first `#undef yywrap', as it is currently implemented as a
macro. As indicated by the hedging in the previous sentence, it may be
changed to a true function in the near future.
The scanner writes its ECHO
output to the `yyout' global
(default, `stdout'), which may be redefined by the user simply
by assigning it to some other FILE
pointer.