1
votes

we have a homework assignment in which we need to write a compiler in stages, first stage is to use flex and bison. We learned that we can override the YYSTYPE with struct as we did:

typedef struct data {
    char * type;
    char * value;
      }DATA;
#define YYSTYPE DATA

this is writen inside an H file and we are including it from the flex file.

in the flex file we have a function that is using the yylval variable:

void allocate(char* input_name, char* input_value)
{
    yylval.type = (char*) malloc(sizeof(char)*(strlen(input_name)+1));
    strcpy( yylval.type, input_name);
    yylval.value = (char*) malloc(sizeof(char)*(strlen(input_value)+1));
    strcpy( yylval.value, input_value);
}

this function is in the last part of the flex file.

in the midlle part we have the call to the function:

"real"                                              {allocate("real",yytext); return real;} 

the Bison file have all the tokens declartion.

when we try to compile, using this commands:

bison -d source.ypp
flex part1.lex
g++ source.tab.cpp lex.yy.c

we get many errors, every time we called the allocate function, they all look the same:

part1.lex:90: error: request for member type' inyylval', which is of non-class type `YYSTYPE'

what are we doing wrong?

1

1 Answers

1
votes

You need to include the header which defines YYSTYPE in both the flex and the bison input files.