Xiangyu Zhang
Xiangyu Zhang

Reputation: 61

YACC: token data type can not be changed

In my YACC file, I need to print the token value for GATENAME and NUMBER, which are supposed to be int and string. However, I get these error messages:

lab1.y:44:65: error: member reference base type 'char *' is not a structure or union 
...printf("gateindex is: %d\n", (yyvsp[(1) - (6)].number));

lab1.y:45:65: error: member reference base type 'char *' is not a structure or union
...printf("gatename is : %s\n", (yyvsp[(2) - (6)].string));

The following is my .l file:

%{
#include <stdlib.h>
void yyerror(char *);
#include "y.tab.h"
%}
%%
[ \t\n]+                      /*eat white space*/
([0-9])+              {
                            yylval.number = atoi(yytext);
                            return NUMBER;
                        }
([0-9])+gat           {
                            yylval.string = strdup(yytext);
                            return GATENAME;
                        }
([0-9])+fan           {
                            yylval.string = strdup(yytext);
                            return FANNAME;
                        }
from                   {
                            yylval.string = strdup(yytext);
                            return FROM;
                        }
([a-zA-Z])*            {
                            yylval.string = strdup(yytext);
                            return TYPE;
                        }
\>sa[0-9]              {
                            yylval.string = strdup(yytext);
                            return SA;
                        }

.                     printf("ERROR    : %c\n",yytext[0]);
%%
int yywrap(void) {
    return 1;
}

The following is my .y file:

%{
    #include <stdio.h>
    #include <stdlib.h> 
    #include <stdarg.h>
    #include <string.h>

%}

%union{
    char *string;
    int number;
}
%token <number> NUMBER 
%token <string> GATENAME 
%token <string> FANNAME 
%token <string> FROM 
%token <string> TYPE 
%token <string> SA

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

%%
program:
        program clause                                              {
                                                                        printf("enter program!!\n");
                                                                        exit(0);
                                                                        }
        |
        ;
clause:
        input                                                       {
                                                                        printf("clause entered!!\n");

                                                                        }
        ;   

input:
        NUMBER GATENAME TYPE NUMBER NUMBER SA                       {
                                                                        printf("input entered!!\n");
                                                                        //$$ = build_input(NUMBER, GATENAME, TYPE, NUMBER, NUMBER, SA);
                                                                        printf("gateindex is: %d\n", $1);
                                                                        printf("gatename is : %s\n", $2);
                                                                        }
        ;

%%

int build_input(int number, char* gatename, int number_output, int number_input, int sa)
{
    //printf("%d ", number);
    //printf("%s ", gatename);  
    // printf("d ;", find(number));
    return 1;
}


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

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

Upvotes: 1

Views: 345

Answers (1)

rici
rici

Reputation: 241861

Your files compile perfectly for me.

I suspect you were compiling (with gcc) an old version of the generated y.tab.c file. You should delete all of your generated files (y.tab.h, y.tab.c, lex.yy.c and do the entire build again. (Better yet, use a Makefile.)

Upvotes: 1

Related Questions