8000 minix/man/man1/flexdoc.1 at R3.2.0 · cppfool/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2442 lines (2229 loc) · 63.9 KB

flexdoc.1

File metadata and controls

2442 lines (2229 loc) · 63.9 KB
A set of rules contingent on the same exclusive start condition
describe a scanner which is independent of any of the other rules in the
.I flex
input. Because of this,
exclusive start conditions make it easy to specify "mini-scanners"
which scan portions of the input that are syntactically different
from the rest (e.g., comments).
.LP
If the distinction between inclusive and exclusive start conditions
is still a little vague, here's a simple example illustrating the
connection between the two. The set of rules:
.nf
%s example
%%
<example>foo /* do something */
.fi
is equivalent to
.nf
%x example
%%
<INITIAL,example>foo /* do something */
.fi
.LP
The default rule (to
.B ECHO
any unmatched character) remains active in start conditions.
.LP
.B BEGIN(0)
returns to the original state where only the rules with
no start conditions are active. This state can also be
referred to as the start-condition "INITIAL", so
.B BEGIN(INITIAL)
is equivalent to
.B BEGIN(0).
(The parentheses around the start condition name are not required but
are considered good style.)
.LP
.B BEGIN
actions can also be given as indented code at the beginning
of the rules section. For example, the following will cause
the scanner to enter the "SPECIAL" start condition whenever
.I yylex()
is called and the global variable
.I enter_special
is true:
.nf
int enter_special;
%x SPECIAL
%%
if ( enter_special )
BEGIN(SPECIAL);
<SPECIAL>blahblahblah
...more rules follow...
.fi
.LP
To illustrate the uses of start conditions,
here is a scanner which provides two different interpretations
of a string like "123.456". By default it will treat it as
as three tokens, the integer "123", a dot ('.'), and the integer "456".
But if the string is preceded earlier in the line by the string
"expect-floats"
it will treat it as a single token, the floating-point number
123.456:
.nf
%{
#include <math.h>
%}
%s expect
%%
expect-floats BEGIN(expect);
<expect>[0-9]+"."[0-9]+ {
printf( "found a float, = %f\\n",
atof( yytext ) );
}
<expect>\\n {
/* that's the end of the line, so
* we need another "expect-number"
* before we'll recognize any more
* numbers
*/
BEGIN(INITIAL);
}
[0-9]+ {
printf( "found an integer, = %d\\n",
atoi( yytext ) );
}
"." printf( "found a dot\\n" );
.fi
Here is a scanner which recognizes (and discards) C comments while
maintaining a count of the current input line.
.nf
%x comment
%%
int line_num = 1;
"/*" BEGIN(comment);
<comment>[^*\\n]* /* eat anything that's not a '*' */
<comment>"*"+[^*/\\n]* /* eat up '*'s not followed by '/'s */
<comment>\\n ++line_num;
<comment>"*"+"/" BEGIN(INITIAL);
.fi
Note that start-conditions names are really integer values and
can be stored as such. Thus, the above could be extended in the
following fashion:
0