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


10.1.5.1 Split Symbols

The interface is as follows.

Method on parser: int yylex (semantic_type* yylval, location_type* yylloc, type1 arg1, ...)
Method on parser: int yylex (semantic_type* yylval, type1 arg1, ...)

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;
         }