Next: Complete Symbols, Up: C++ Scanner Interface [Contents][Index]
The generated parser expects yylex
to have the following prototype.
Return the next token. Its type 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 look 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; }