How to solve Bison warning “… has no declared type”

前端 未结 2 886
死守一世寂寞
死守一世寂寞 2021-02-06 20:45

Running Bison on this file:

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


%union
{
    char    name[100];
    int     val         


        
2条回答
  •  时光说笑
    2021-02-06 21:22

    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 
        int yylex();
        void yyerror(const char*);
    %}
    
    
    %union
    {
        char    name[100];
        int     val;
    }
    
    %token NUM ID
    %right '='
    %left '+' '-'
    %left '*'
    
    %type exp NUM
    %type ID
    
    %%
    
    exp :   NUM     {$$ = $1;}
        | ID        {$$ = vars[$1];}
        | exp '+' exp   {$$ = $1 + $3;}
        | ID '=' exp    {$$ = vars[$1] = $3;}
    ;
    
    %%
    

提交回复
热议问题