Search code examples
c++bisonflex-lexerbisonc++

How to get yy::parser::token from yy::parser::symbol_type in Bison c++ variant?


For example, I return yy::parser::symbol_type in flex rule via:

[a-zA-Z][a-zA-Z0-9_]*  return yy::parser::make_ID(yytext);

where ID is a token I defined in bison, it will generate the yy::parser::token structure.

Now I want to do some unit test just for flex's token.l, when I invoke the yy::parser::symbol_type yylex() function, I didn't see any API to get yy::parser::token from yy::parser::symbol_type in Bison c++ variant manual.

And by the way, in bison manual, return yy::parser::symbol_type via yy::parser::make_XXX APIs in flex rule is a recommanded.

Or is there no such API to do this job ? I need to use the symbol_type.kind() API to get something like yy::parser::symbol_type_kind::S_T_ID ?


Solution

  • There is no object of type yy::parser::token. That struct exists only to hold the enum token_kind_type enumeration (not a member with that value; the enumeration itself). See the Bison manual outline of the C++ API.

    I don't really understand the motivation for this, but I suppose it comes from a desire to allow ancient versions of the C++ standard which didn't have enum class.

    In any case, I am almost certain that the value you want is indeed the return value of symbol_type.kind().