I am new in this, so I wondered why do I need to use these directives
%option nounput
%option noinput
Yeah, I am aware that otherwise I'd have these warnings:
lex.yy.c:1237:17: warning: ‘yyunput’ defined but not used [-Wunused-function]
static void yyunput (int c, register char * yy_bp )
^
lex.yy.c:1278:16: warning: ‘input’ defined but not used [-Wunused-function]
static int input (void)
^
What is actually the matter with these directives in flex? In which case I'd be able to use these functions (what for):
static void yyunput (int c, register char * yy_bp );
static int input (void);
What are the for?
You certainly don't need to use those options. You should use them if you don't use the corresponding functions, in order to avoid the compiler warnings. (If you don't care about compiler warnings, you don't need the options either. But you should care about compiler warnings :) )
The functions are rarely used; they are mostly needed if you want to bypass lexical analysis in some context, and directly read input (input
), or if you want to fabricate input text to be scanned (unput
).
input()
can be used in an action to return the next character from the input stream. (The character read is removed from the input stream, so it will not be scanned when the action finishes.) Since input()
only returns a single character, it is not very efficient but there are times when it is unavoidable. One example is tokens preceded by explicit lengths. (On the whole, flex is not the ideal tool to lexically scan an input stream where all tokens have explicit lengths.)
unput(c)
can be used to insert a character into the input stream, so that the character will be scanned when the action finishes. The Flex manual contains a rather contrived example where the token just read is reinserted into the input stream surrounded by parentheses.
You should not use unput
to simply rescan all or part of a token; the yyless
macro is much more efficient for that purpose.
Most lexical scanners do not require either input
or unput
, and so it is common to use %option
to avoid generating code for them.
If you are just learning flex, I recommend that you start with the normal flex idioms, leaving things like input
and unput
aside until you have a clear need for them (if ever). Also, you should read the Flex manual, which will answer many of your questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With