Next: Complete Symbols, Up: C++ Scanner Interface [Contents][Index]
The interface is as follows.
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::INTEGER;
}
[a-z]+ {
yylval.sval = new std::string (yytext);
return yy::parser::IDENTIFIER;
}
Using variants, yylval is already constructed, but it is not
initialized. So the code would look like:
[0-9]+ {
yylval.build<int>() = text_to_int (yytext);
return yy::parser::INTEGER;
}
[a-z]+ {
yylval.build<std::string> = yytext;
return yy::parser::IDENTIFIER;
}
or
[0-9]+ {
yylval.build(text_to_int (yytext));
return yy::parser::INTEGER;
}
[a-z]+ {
yylval.build(yytext);
return yy::parser::IDENTIFIER;
}