简体   繁体   中英

Flex & Bison make problems when using struct pointers as a union type

I am in the process of trying to make a file parser using Flex & Bison. I wan't to use a struct pointer in the yacc file (union) and am unable to get it to build. In my yacc file eddl.y

%{

#include <stdio.h>     /* C declarations used in actions */
#include <stdlib.h>
#include <string.h>

#include "eddl_data_type.h"

int yylex(void);
void yyerror (char *s);
%}

%union {
        float dec; 
        char* ptr;
        int num; 
        char str[100];
        eddl_variable_t* var;     //<----- Troublemaker
        }

%%

... grammar

%%      /* C code */
#include <stdio.h>
#include <string.h>

#include "eddl_data_type.h"

int main(void) {return yyparse();}

void yyerror (char *s) {fprintf (stderr, "%s\n", s);} 

in eddl_data_type.h

typedef struct eddl_variable eddl_variable_t;

struct eddl_variable{
    char*           __name;
    char*           __label;
    char*           __help;
    class_mask_t    __class;
    type_mask_t     __type;
    void*           __default_value;
    handling_mask_t __handling; 

    eddl_variable_t* next;
};

and i am using the following Makefile

scanner: lex.yy.c y.tab.c
        gcc -g lex.yy.c y.tab.c -o scanner 

lex.yy.c: y.tab.c eddl.lex
        lex eddl.lex

y.tab.c: eddl.y
       yacc -d eddl.y

clean:
        rm -f lex.yy.c y.tab.c y.tab.h scanner

My build output is

yacc -d eddl.y
lex eddl.lex
gcc -g lex.yy.c y.tab.c -o scanner
In file included from eddl.lex:2:0:
eddl.y:17:9: error: unknown type name 'eddl_variable_t'
eddl_variable_t* var;
^~~~~~~~~~~~~~~

Is it possible to have such a data type in the yacc union? If so I have been unable to find out how one does this.

Many thanks in advance.

I have found the issue. The union properties specified in the yacc file get compiled into y.tab.h . I didn't realize that my error was stemming from my lex file, as there I include the y.tab.h file but don't include the eddl_data_type.h file. Insuring that it is included before y.tab.h resolved this issue.

The top of my eddl.l file is as follows

%{
#include "eddl_data_type.h"
#include "y.tab.h"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM