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

前端 未结 2 883
死守一世寂寞
死守一世寂寞 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 <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;}
    ;
    
    %%
    
    0 讨论(0)
  • 2021-02-06 21:24

    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

    0 讨论(0)
提交回复
热议问题