How should is use yy_scan_buffer in (.y) file( lex and yacc). The return type of yy_scan_buffer is YY_BUFFER_STATE which is in lex.yy.c .
1 Answers
For background: it is an special function called from yy_scan_bytes
, which in turn is called from yy_scan_string
(likewise can be overridden).
According to String input to flex lexer, the return-type corresponds to a handle which should be deleted using yy_delete_buffer
, but that yy_scan_buffer
does the deletion. However (looking at the generated code), that does not appear to be correct — perhaps this depends upon the version of flex which is used.
According to these questions, you might want to use yy_scan_string
, etc., in writing reentrant code (although the functions predate any work on flex
to provide reentrancy):
- how to use
yy_scan_string(const char *str)
(generated by lex yacc) in a separated file - how to use yy_scan_string in lex
Flex's current documentation mentions its use for multiple input buffers:
Some scanners (such as those which support “include” files) require reading from several input streams. As flex scanners do a large amount of buffering, one cannot control where the next input will be read from by simply writing a
YY_INPUT()
which is sensitive to the scanning context.YY_INPUT()
is only called when the scanner reaches the end of its buffer, which may be a long time after scanning a statement such as an include statement which requires switching the input source.
The documentation goes on to provide examples of usage. Depending on what you want to do, those may be useful.

- 234,347
- 28
- 237
- 341

- 51,086
- 7
- 70
- 105