2
votes

I've been trying for a while, to implement a parser for a grammar by using bison and lex.

I have a problem with the type redeclaration of yylval, I explain myself.

I have 4 files: lexico.l, parser.y, funcionesTabla.c, funcionesTabla.h

The first, contains the specification for lex The second, specification for bison/yacc The last two, are a bunch of methods for dealing with a symbol table.

I have in funcionesTabla.h:

typedef enum {
entero,
real,
caracter,
arrayEntero,
arrayReal,
arrayCaracter,
matrizEntero,
matrizReal,
matrizCaracter,
desconocido,
no_asignado
} dtipo ;

typedef struct{
int    atrib ;
char  *lexema ;
dtipo  tipo ;
} atributos;

#define YYSTYPE atributos

I've tried the next:

From parser.y, within a rule, tried to access to yylval.tipo, no problem.

From lexico.l, within a token rule, tried to access to yylval.lexema (or whichever attribute), and gcc says me:

 lexico.l: In function ‘yylex’:
 lexico.l:93: error: request for member ‘lexema’ in something not a structure or union
 make: *** [lex.yy.o] Error 1

Any suggestion?

Thanks a lot in advance, and sorry for my english.

2

2 Answers

1
votes

David is right, by default yacc gives you a %union directive, but seeing as this gets translated to plain C, you could just nest your structs in there:

%union {
    struct {
        int atrib;
        char *lexema;
        dtipo tipo;
    };
}

which will simply work as you expect in C dialects that support anonymous structs inside unions (C99 for instance).

-2
votes

I don't fully understand how you are using the struct atributos, but I will take a guess.

Look at your generated y.tab.h file: I think you will see that the generated code for yylval is incompatible with the way you want to use it.

Usually, I see YYSTYPE defined as a union, not a struct. Take a look at the documentation for bison's %union directive to define the data types for your semantic values. I think you want something like this:

%union {
    int    atrib;
    char  *lexema;
    dtipo  tipo;
}