I heard that ss
uses Bison to parse command line arguments. Bison takes input from output Flex. Flex takes input from stdin by default. Where does ss
set up flex input to come from command line arguments?
1 Answers
Bison calls yylex()
whenever it needs a token. The return value of yylex()
is a small integer, which is either one of the enumeration constants declared with %token
in the bison grammar, or a character constant other than 0, or 0 to indicate EOF.
yylex()
must also:
Save the token's semantic value in the global variable
yylval
, if the token has a semantic value; andSave the token's location in the input stream in the global variable
yylloc
, if the parser actions make use of location information.
You can build a scanner using the flex
command line tool. But you are under no obligation to do so. You could use a different tool to generate a scanner. Or you could define a yylex()
function yourself. The interface for the scanner has been kept as simple as possible to simplify implementation.
Many tools, including ss
, use bison with a hand-coded lexical analyzer. So the answer to your question is that ss
never sets up the flex input because it doesn't use flex. (The fact that this is possible does not necessarily make it a good idea. Many hand-written scanners are significantly slower than flex
, and the code is verbose and hard to reason about.)
Other intetfaces
The above is the default interface between yyparse
and yylex
. There are alternatives:
You can define
yylex
to take additional arguments, if youryylex
implementation requires additional data. Normally, these additional arguments will be provided toyyparse()
so that the parser can use them in its call toyylex()
. See the bison manual section on the%param
directive.You can tell bison to generate a pure parser which does not rely on global variables, in which case it will call
yylex
with a pointer to a semantic object and, if location information is used, a pointer to a location object. See the bison manual chapter on pure parser interface.You can request a "push parser", which is called by the scanner every time a token becomes available. See the bison manual chapter on push parsers.