Using std::variant as YYSTYPE/yylval in gnu bison for C++17

My blang project, which is a BASIC interpreter, uses  top-down recursive-descent parsing. I have been wondering if I could obtain better mileage from using flex/bison.

Bison is quite old now, having last being updated in 2015 at version 3.0.5. So it is not up to speed with the latest C++ standards.

Although bison does support C++, and even it’s own spin on variant values for YYSTYPE/yylval, I found the documentation impenetrable. It almost seems to work, but I couldn’t get it to do quite what I wanted. You seem to be faced with the alternative with adding a whole mass of infrastructure to accommodate bison, or else do things the C way. The documentation on using C++ in bison is opaque.

My real problem stems from being able to express the type of the parse node. The C way is to use a union of pointers: not the sort of thing we want when programming in C++.

Now, it turns out that you can use a variant for YYSTYPE. I present below a simple skeleton file (variant.yy):

%{
#include <string>
#include <variant>
typedef std::variant<std::string, double> value_t;
#define YYSTYPE value_t
extern YYSTYPE yylval;
int yylex();
void yyerror(char const *s);
int main();
%}

%token FOO

%%

top:
 FOO { $$ = $1; }

%%

int yylex()
{
 yylval = std::string("hello");
 return FOO;
}

void yyerror(char const *s)
{
}

int main()
{
 return 0;
}

It is available as a gist .

There’s a lot wrong with this example, of course. yylex() doesn’t do very much, for example, and main() doesn’t even call yyparse(). But that’s not the point. The point is to show how you set up a parser to generate variants as heterogeneous intermediate branches of the parse tree.

I am only at the experimental stage of rewriting my blang parser, but I am hopeful that the code I write using bison will be shorter than my top-down implementation. I shall report back my findings when they are available.

Advertisements

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s