Next: Values Available To the User, Previous: End-of-File Rules, Up: flex [Contents][Index]
This option can be set to provide an code fragment
which is always executed prior to the matched rule’s action. For
example, it could be set to call a routine to convert yytext to
lower-case. When the code fragment is invoked, the variable
yy_act gives the number of the matched rule (rules are numbered
starting with 1). Suppose you want to profile how often each of your
rules is matched. The following would do the trick:
%option pre-action="++ctr[yy_act]"
where ctr is an array to hold the counts for the different
rules. Note that the public constant YY_NUM_RULES (a macro in
the default C/C++ back end) gives the total number of rules (including
the default rule), even if you use ‘-s)’, so a correct
declaration for ctr is:
int ctr[YY_NUM_RULES];
This option may be defined to provide an action which is always executed before the first scan (and before the scanner’s internal initializations are done). For example, it could be used to call a routine to read in a data table or open a logging file.
The entry point yy_set_interactive(is_interactive) can be used to
control whether the current buffer is considered interactive. An
interactive buffer is processed more slowly, but must be used when the
scanner’s input source is indeed interactive to avoid problems due to
waiting to fill buffers (see the discussion of the ‘-I’ flag in
Scanner Options). Passing a boolean true (in C/C++, non-zero) value marks
the buffer as interactive, a zero value as non-interactive. Note that
use of this entry point overrides %option always-interactive or
%option never-interactive (see Scanner Options).
yy_set_interactive() must be invoked prior to beginning to scan
the buffer that is (or is not) to be considered interactive.
The rule hook yysetbol(at_bol) can be used to control whether the
current buffer’s scanning context for the next token match is done as
though at the beginning of a line. A non-zero argument makes
rules anchored with ‘^’ active, while a zero argument makes
‘^’ rules inactive.
The rule hook yyatbol() returns true if the next token scanned from
the current buffer will have ‘^’ rules active, false otherwise.
In the generated scanner, the actions are all gathered in one large
switch statement and separated using a post-action fragment, which
may be redefined. By default, in C it is simply a break, to
separate each rule’s action from the following rule’s. Other target
languages may have different defaults for this action, often an empty
string. If a target language has no case statement this option will
probably be ineffective.
Setting a post-action allows, for example, C++ users to suppress the
trailing break (while being very careful that every rule ends with a
break or a return!) to avoid suffering from unreachable
statement warnings where because a rule’s action ends with
return, the break is inaccessible.
Next: Values Available To the User, Previous: End-of-File Rules, Up: flex [Contents][Index]