/usr/share/doc/flex-old-doc/flex_11.html is in flex-old-doc 2.5.4a-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | html2
<html>
<!-- Created on May 24, 2011 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>Flex - a scanner generator: 11. Start conditions</title>
<meta name="description" content="Flex - a scanner generator: 11. Start conditions">
<meta name="keywords" content="Flex - a scanner generator: 11. Start conditions">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Start-conditions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="flex_10.html#Generated-scanner" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="flex_12.html#Multiple-buffers" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="flex_10.html#Generated-scanner" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="flex.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="flex_12.html#Multiple-buffers" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="flex.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="flex_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[Index]</td>
<td valign="middle" align="left">[<a href="flex_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Start-conditions-1"></a>
<h1 class="chapter">11. Start conditions</h1>
<p><code>flex</code> provides a mechanism for conditionally activating
rules. Any rule whose pattern is prefixed with "<sc>"
will only be active when the scanner is in the start
condition named "sc". For example,
</p>
<table><tr><td> </td><td><pre class="example"><STRING>[^"]* { /* eat up the string body ... */
…
}
</pre></td></tr></table>
<p>will be active only when the scanner is in the "STRING"
start condition, and
</p>
<table><tr><td> </td><td><pre class="example"><INITIAL,STRING,QUOTE>\. { /* handle an escape ... */
…
}
</pre></td></tr></table>
<p>will be active only when the current start condition is
either "INITIAL", "STRING", or "QUOTE".
</p>
<p>Start conditions are declared in the definitions (first)
section of the input using unindented lines beginning with
either ‘<samp>%s</samp>’ or ‘<samp>%x</samp>’ followed by a list of names. The former
declares <em>inclusive</em> start conditions, the latter <em>exclusive</em>
start conditions. A start condition is activated using
the <code>BEGIN</code> action. Until the next <code>BEGIN</code> action is
executed, rules with the given start condition will be active
and rules with other start conditions will be inactive.
If the start condition is <em>inclusive</em>, then rules with no
start conditions at all will also be active. If it is
<em>exclusive</em>, then <em>only</em> rules qualified with the start
condition will be active. 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 <code>flex</code> 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).
</p>
<p>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:
</p>
<table><tr><td> </td><td><pre class="example">%s example
%%
<example>foo do_something();
bar something_else();
</pre></td></tr></table>
<p>is equivalent to
</p>
<table><tr><td> </td><td><pre class="example">%x example
%%
<example>foo do_something();
<INITIAL,example>bar something_else();
</pre></td></tr></table>
<p>Without the ‘<samp><INITIAL,example></samp>’ qualifier, the ‘<samp>bar</samp>’ pattern
in the second example wouldn’t be active (i.e., couldn’t match) when
in start condition ‘<samp>example</samp>’. If we just used ‘<samp><example></samp>’
to qualify ‘<samp>bar</samp>’, though, then it would only be active in
‘<samp>example</samp>’ and not in <code>INITIAL</code>, while in the first example
it’s active in both, because in the first example the ‘<samp>example</samp>’
starting condition is an <em>inclusive</em> (‘<samp>%s</samp>’) start condition.
</p>
<p>Also note that the special start-condition specifier ‘<samp><*></samp>’
matches every start condition. Thus, the above example
could also have been written;
</p>
<table><tr><td> </td><td><pre class="example">%x example
%%
<example>foo do_something();
<*>bar something_else();
</pre></td></tr></table>
<p>The default rule (to ‘<samp>ECHO</samp>’ any unmatched character) remains
active in start conditions. It is equivalent to:
</p>
<table><tr><td> </td><td><pre class="example"><*>.|\\n ECHO;
</pre></td></tr></table>
<p>‘<samp>BEGIN(0)</samp>’ 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
‘<samp>BEGIN(INITIAL)</samp>’ is equivalent to ‘<samp>BEGIN(0)</samp>’. (The
parentheses around the start condition name are not required but
are considered good style.)
</p>
<p><code>BEGIN</code> 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 ‘<samp>yylex()</samp>’ is called and the global
variable <code>enter_special</code> is true:
</p>
<table><tr><td> </td><td><pre class="example"> int enter_special;
%x SPECIAL
%%
if ( enter_special )
BEGIN(SPECIAL);
<SPECIAL>blahblahblah
…more rules follow…
</pre></td></tr></table>
<p>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:
</p>
<table><tr><td> </td><td><pre class="example">%{
#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]+ {
Version 2.5 December 1994 18
printf( "found an integer, = %d\n",
atoi( yytext ) );
}
"." printf( "found a dot\n" );
</pre></td></tr></table>
<p>Here is a scanner which recognizes (and discards) C
comments while maintaining a count of the current input line.
</p>
<table><tr><td> </td><td><pre class="example">%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);
</pre></td></tr></table>
<p>This scanner goes to a bit of trouble to match as much
text as possible with each rule. In general, when
attempting to write a high-speed scanner try to match as
much possible in each rule, as it’s a big win.
</p>
<p>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:
</p>
<table><tr><td> </td><td><pre class="example">%x comment foo
%%
int line_num = 1;
int comment_caller;
"/*" {
comment_caller = INITIAL;
BEGIN(comment);
}
…
<foo>"/*" {
comment_caller = foo;
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(comment_caller);
</pre></td></tr></table>
<p>Furthermore, you can access the current start condition
using the integer-valued <code>YY_START</code> macro. For example, the
above assignments to <code>comment_caller</code> could instead be
written
</p>
<table><tr><td> </td><td><pre class="example">comment_caller = YY_START;
</pre></td></tr></table>
<p>Flex provides <code>YYSTATE</code> as an alias for <code>YY_START</code> (since that
is what’s used by AT&T <code>lex</code>).
</p>
<p>Note that start conditions do not have their own
name-space; %s’s and %x’s declare names in the same fashion as
#define’s.
</p>
<p>Finally, here’s an example of how to match C-style quoted
strings using exclusive start conditions, including
expanded escape sequences (but not including checking for
a string that’s too long):
</p>
<table><tr><td> </td><td><pre class="example">%x str
%%
char string_buf[MAX_STR_CONST];
char *string_buf_ptr;
\" string_buf_ptr = string_buf; BEGIN(str);
<str>\" { /* saw closing quote - all done */
BEGIN(INITIAL);
*string_buf_ptr = '\0';
/* return string constant token type and
* value to parser
*/
}
<str>\n {
/* error - unterminated string constant */
/* generate error message */
}
<str>\\[0-7]{1,3} {
/* octal escape sequence */
int result;
(void) sscanf( yytext + 1, "%o", &result );
if ( result > 0xff )
/* error, constant is out-of-bounds */
*string_buf_ptr++ = result;
}
<str>\\[0-9]+ {
/* generate error - bad escape sequence; something
* like '\48' or '\0777777'
*/
}
<str>\\n *string_buf_ptr++ = '\n';
<str>\\t *string_buf_ptr++ = '\t';
<str>\\r *string_buf_ptr++ = '\r';
<str>\\b *string_buf_ptr++ = '\b';
<str>\\f *string_buf_ptr++ = '\f';
<str>\\(.|\n) *string_buf_ptr++ = yytext[1];
<str>[^\\\n\"]+ {
char *yptr = yytext;
while ( *yptr )
*string_buf_ptr++ = *yptr++;
}
</pre></td></tr></table>
<p>Often, such as in some of the examples above, you wind up
writing a whole bunch of rules all preceded by the same
start condition(s). Flex makes this a little easier and
cleaner by introducing a notion of start condition <em>scope</em>.
A start condition scope is begun with:
</p>
<table><tr><td> </td><td><pre class="example"><SCs>{
</pre></td></tr></table>
<p>where SCs is a list of one or more start conditions.
Inside the start condition scope, every rule automatically
has the prefix ‘<samp><SCs></samp>’ applied to it, until a ‘<samp>}</samp>’ which
matches the initial ‘<samp>{</samp>’. So, for example,
</p>
<table><tr><td> </td><td><pre class="example"><ESC>{
"\\n" return '\n';
"\\r" return '\r';
"\\f" return '\f';
"\\0" return '\0';
}
</pre></td></tr></table>
<p>is equivalent to:
</p>
<table><tr><td> </td><td><pre class="example"><ESC>"\\n" return '\n';
<ESC>"\\r" return '\r';
<ESC>"\\f" return '\f';
<ESC>"\\0" return '\0';
</pre></td></tr></table>
<p>Start condition scopes may be nested.
</p>
<p>Three routines are available for manipulating stacks of
start conditions:
</p>
<dl compact="compact">
<dt> ‘<samp>void yy_push_state(int new_state)</samp>’</dt>
<dd><p>pushes the current start condition onto the top of
the start condition stack and switches to <var>new_state</var>
as though you had used ‘<samp>BEGIN new_state</samp>’ (recall that
start condition names are also integers).
</p>
</dd>
<dt> ‘<samp>void yy_pop_state()</samp>’</dt>
<dd><p>pops the top of the stack and switches to it via
<code>BEGIN</code>.
</p>
</dd>
<dt> ‘<samp>int yy_top_state()</samp>’</dt>
<dd><p>returns the top of the stack without altering the
stack’s contents.
</p></dd>
</dl>
<p>The start condition stack grows dynamically and so has no
built-in size limitation. If memory is exhausted, program
execution aborts.
</p>
<p>To use start condition stacks, your scanner must include a
‘<samp>%option stack</samp>’ directive (see Options below).
</p>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="flex_10.html#Generated-scanner" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="flex_12.html#Multiple-buffers" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="flex.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="flex_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[Index]</td>
<td valign="middle" align="left">[<a href="flex_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated on <i>May 24, 2011</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
</font>
<br>
</p>
</body>
</html>
|