How to solve Bison warning "... has no declared type"

Asaf R picture Asaf R · Jun 18, 2009 · Viewed 32.1k times · Source

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?

Answer

Asaf R picture Asaf R · Jun 18, 2009

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

%%