Chapter 6: Actions

As described in Section 4.2, the second section of the flexc++ input file contains series of rules: pairs of patterns and actions.

Specifications of patterns end at the first unescaped white space character; the action then starts at the first non-white space character. It usually contains C++ code, with two exceptions: the empty and the bar (|) action (see below). If the C++ code starts with a brace ({), the action can span multiple lines until the matching closing brace (}) is encountered. Flexc++ will correctly handle braces in strings and comments.

Actions can also be empty (i.e. only white space characters were encountered until the end of the line). Empty actions discard the matched pattern. We discourage the use of empty actions however, and advise to place a simple comment stating that the input is ignored.

The bar action is an action containing only a single vertical bar (|). This tells flexc++ to use the action of the next rule. This can be repeated so the following rules all use the same action:


a   |
b   |
c   std::cout << "Matched " << match() << "\n";

Actions can return an int value, usually used for returning tokens to a parser. This return value is then returned from Scanner::lex(). The next call to lex() then starts off where the previous call left off.

The following functions can be called from actions (or methods called from those actions):

ECHO()
Copies the current match to the scanner's output (default cin).

begin(startCondition)
Places the scanner in the specified start condition.

more()
Tells the scanner not to discard the current match. This means that the next match will be appended to the current match. See for example the following lexer file:


%%
super-      ECHO(); more();
flex        ECHO();

When presented with the input `super-flex', the scanner will output `super-super-flex'.

less(n)
Returns all but the first n characters back to the input buffer, which will then be used for further matches. Take the following example:


%%
fubar       ECHO(); less(2);
bar         std::cout << " Matched bar\n";

When presented with the input `fubar', it will output `fubar Matched bar'.

The parameter n is optional. When left out, it is interpreted as 0 which means the entire token is put back on the input stream.

Unless you have taken means to change the matching process (by changing to a different start condition for example), this results in an eternal loop.