Open System Services Shell and Utilities Reference Manual (G06.27+, H06.04+)
flex(1) OSS Shell and Utilities Reference Manual
dashes ( - ) are not permitted. Unlike regexp names, state names share the C
#define namespace. In the rules section, states are recognized as <state> (state
within angle brackets).
The %x directive names exclusive states. When a scanner is in an exclusive state,
only rules prefixed with that state are active. Inclusive states are named with the
%s directive.
%{ or %} When placed on lines by themselves, enclose C code to be passed verbatim into
the global definitions of the output file. Such lines commonly include preproces-
sor directives and declarations of external variables and functions.
space or tab Appear at the beginning of lines in the definitions section that are to be passed
directly into the lex.yy.c output file, as part of the initial global definitions.
The rules section follows the definitions, separated by a line consisting of %%. The rules section
contains rules for matching input and taking actions, in the following format:
pattern [ action ]
pattern starts in the first column of the line and extends until the first nonescaped white space
character. The command attempts to find the pattern that matches the longest input sequence and
execute the associated action. If two or more patterns match the same input, the one that appears
first in the rules section is chosen. If no action exists, the matched input is discarded. If no pat-
tern matches the input, the default action is to copy it to the standard output file.
All action code is placed in the yylex() function. Text (C code or declarations) placed at the
beginning of the rules section is copied to the beginning of the yylex() function and can be used in
actions. This text must begin with a space or a tab (to distinguish it from rules). In addition, any
input (beginning with a space or within %{ and %} delimiter lines) appearing at the beginning of
the rules section before any rules are specified is written to file lex.yy.c after the declarations of
variables for the yylex() function and before the first line of code in yylex().
Elements of each rule are:
state A pattern can begin with a comma-separated list of state names enclosed by angle
brackets (< state [,state...] >). These states are entered through the BEGIN state-
ment. If a pattern begins with a state, the scanner can recognize it only when in
that state. The initial state is 0 (zero).
regexp A pattern can be a regular expression to match against the input stream. The reg-
ular expressions in the flex command provide a rich character-matching syntax.
The following characters, shown in order of decreasing precedence, have special
meanings:
x Matches the character x.
(double quotes)
Enclose characters and treat them as literal strings. For example, "*+"
is treated as the asterisk character followed by the plus character.
\str (backslash)
If str is one of the characters a, b, f, n, r, t,orv, then represents the
ANSI C interpretation (for example, \n is a newline). If str is a string of
octal digits, it is interpreted as a character with octal value str.Ifstr is a
string of hexadecimal digits with a leading x, it is interpreted as a char-
acter with that value. Otherwise, it is interpreted literally with no spe-
cial meaning. For example, x\*yz represents the four characters x*yz.
3−146 Hewlett-Packard Company 527188-004