YACC:令牌数据类型无法更改

时间:2015-09-28 20:06:07

标签: c++ c bison yacc lex

在我的YACC文件中,我需要打印GATENAME和NUMBER的标记值,它们应该是int和string。但是,我收到以下错误消息:

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));

以下是我的.l文件:

%{
#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;
}

以下是我的.y文件:

%{
    #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;
}

1 个答案:

答案 0 :(得分:1)

你的文件编译得很完美。

我怀疑您正在编译(使用gcc)生成的y.tab.c文件的旧版本。您应该删除所有生成的文件(y.tab.hy.tab.clex.yy.c并再次执行整个构建。(更好的是,使用Makefile。)