54

Running Bison on this file:

%{
    #include <iostream>
    int yylex();
    void yyerror(const char*);
%}


%union
{
    char    name[100];
    int     val;
}

%token NUM ID
%right '='
%left '+' '-'
%left '*'

%%

exp :   NUM     {$$.val = $1.val;}
    | ID        {$$.val = vars[$1.name];}
    | exp '+' exp   {$$.val = $1.val + $3.val;}
    | ID '=' exp    {$$.val = vars[$1.name] = $3.val;}
;

%%

Leads to warnings of the kind of:

warning: $$ of 'exp' has no declared type.

What does it mean and how do I solve it?

svick
  • 236,525
  • 50
  • 385
  • 514
Asaf R
  • 6,880
  • 9
  • 47
  • 69
  • 25
    +1: for appearing first when googling `bison error has no declared type` – INS Jun 24 '10 at 10:28
  • Just a small clarity. I have `%union { int intValue; int floatValue; }` but it does not allow me to use `$$.intValue` or `$1.intValue`. It says `error: request for member ‘floatValue’ in something not a structure or union`. Why so? – Shashwat Nov 03 '13 at 10:21

2 Answers2

51

The union (%union) defined is not intended to be used directly. Rather, you need to tell Bison which member of the union is used by which expression.

This is done with the %type directive.

A fixed version of the code is:

%{
    #include <iostream>
    int yylex();
    void yyerror(const char*);
%}


%union
{
    char    name[100];
    int     val;
}

%token NUM ID
%right '='
%left '+' '-'
%left '*'

%type<val> exp NUM
%type<name> ID

%%

exp :   NUM     {$$ = $1;}
    | ID        {$$ = vars[$1];}
    | exp '+' exp   {$$ = $1 + $3;}
    | ID '=' exp    {$$ = vars[$1] = $3;}
;

%%
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
Asaf R
  • 6,880
  • 9
  • 47
  • 69
  • 2
    A small point: the notation `%type exp NUM` does not mean that the particular reduction `exp NUM` has type `val`; it means that `exp` has type `VAL` and `NUM` has type `val`. This posted answer, btw, is more useful, than the official documentation for the type directive, which has no examples. – Reb.Cabin Jun 10 '17 at 02:01
  • @Reb.Cabin where is `VAL` defined? Or did you mean `val`? – Michael Dorst Mar 12 '20 at 15:18
8

As a further thought, if you want to be more explicit with your reductions (if you are doing AST annoation, this can be handy) then you can make your stack values pointers and then handle type values yourself. Much like scalar types with:

struct myScalar {
    union {
        int num;
        char *id;
        char *float_lexeme;
    }payload;

    enum {
        TYPE_NUM,
        TYPE_IDENTIFIER,
        TYPE_FLOAT_CHAR
    } type;
    char *orig_lexeme;
};

And have a typedef and scalar_val *val for the stack.

When you move onto more complex compiler front-ends, it can help to build your AST like this so that when you traverse the tree you have better meta-data and you can also augment the translation with translations for pre-semantic types. Then it boils down to your leaf productions such as ID to shuffle the lexeme into the right scalar payload.

Not a complete explanation, but you get the idea.

Hope this helps with your future Bison/Lex front-ends and ...

Good Luck

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119