Next: , Up: C++ Scanner Interface   [Contents][Index]


10.1.7.1 Split Symbols

The generated parser expects yylex to have the following prototype.

Function: int yylex (value_type* yylval, location_type* yylloc, type1 arg1, …)
Function: int yylex (value_type* yylval, type1 arg1, …)

Return the next token. Its kind is the return value, its semantic value and location (if enabled) being yylval and yylloc. Invocations of ‘%lex-param {type1 arg1}’ yield additional arguments.

Note that when using variants, the interface for yylex is the same, but yylval is handled differently.

Regular union-based code in Lex scanner typically looks like:

[0-9]+   {
           yylval->ival = text_to_int (yytext);
           return yy::parser::token::INTEGER;
         }
[a-z]+   {
           yylval->sval = new std::string (yytext);
           return yy::parser::token::IDENTIFIER;
         }

Using variants, yylval is already constructed, but it is not initialized. So the code would look like:

[0-9]+   {
           yylval->emplace<int> () = text_to_int (yytext);
           return yy::parser::token::INTEGER;
         }
[a-z]+   {
           yylval->emplace<std::string> () = yytext;
           return yy::parser::token::IDENTIFIER;
         }

or

[0-9]+   {
           yylval->emplace (text_to_int (yytext));
           return yy::parser::token::INTEGER;
         }
[a-z]+   {
           yylval->emplace (yytext);
           return yy::parser::token::IDENTIFIER;
         }