Question

Referring to this blog post

There is a union defined as below:

%union {
    Node *node;
    NBlock *block;
    NExpression *expr;
    NStatement *stmt;
    NIdentifier *ident;
    NVariableDeclaration *var_decl;
    std::vector<NVariableDeclaration*> *varvec;
    std::vector<NExpression*> *exprvec;
    std::string *string;
    int token;
}

My question is: How could this union be used when it is anonymous and how was it actually used?

Was it helpful?

Solution

Don't confuse bison's %union directive with the C/C++ syntax.

In the code generated by bison, there will be a C/C++ union, whose body is copied from the %union directive, but it is certainly not anonymous: its name is YYSTYPE. However, you hardly ever need to know that, since the only exposed variable with that type is yylval. (The internal bison stack is also composed of elements of type YYSTYPE but it is not exposed to user code.)

There are various other possible ways to define YYSTYPE, as described by the bison manual. For example, you can create your own C/C++ union (or struct) type, as described here. However, these advanced bison features are rarely required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top