yylval and union

bison, lexical-analysis, parsing, yacc

Solution

The purpose of the `union` is to allow storing different kind of objects into nodes emitted by flex.

To explain better you can have for example:

%union
{
    int intValue;
    float floatValue;
    char *stringValue;
}

in `.y` if you want to provide basic support for `int`, `float` and `string` types. What can you do with this?

Two things:

First, you can automatically set right values when generating tokens. Think about `.l` file of the previous example, you can have:

[a-zA-Z][a-zA-Z0-9]* {
 yylval.stringValue = strdup(yytext);
 return IDENTIFIER;
}

[0-9]+ { 
 yylval.intValue = atoi(yytext);
 return INTEGER;
}

[0-9]*\.[0-9]+"f"? {
    yylval.floatValue = new atof(yytext);
 return FLOAT;
}

In addition you can use value directly in your flex grammar:

nexp: nexp '+' nexp { $<floatValue>$ = $<floatValue>1 + $<floatValue>3 }

Finally if you plan to use an OOP syntax tree you can define union as

%union
{
    class ASTNode *node;
}

in which `ASTNode` is the ancestor class of any kind of syntax node.

Problem

What is the purpose of union in the yacc file? Is it directly related to yylval in the flex file? If you don't use yylval, then you don't need to use union?

Original source