0
在我最後的項目中給我留下了幾個代碼,其中一個是flex & bison。 問題是GCC返回我的消息「爲成員的分貝「要求的東西不是一個結構或聯合」野牛文件 ... 我不知道如何解決這個問題,我找到解決辦法的例子但沒有一個適合我。 我希望我可以幫忙,在此先感謝。請求會員「」的東西不是結構或聯盟?在Flex&Bison
Flex的文件:
%{
#include <stdlib.h>
#include "y.tab.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
%}
%option noyywrap
%option yylineno
digit [0-9]
blank [\t]
sign [+-/*]
other .
%%
{digit}+ { sscanf(yytext, "%lf", &yylval.db); return NUMBER;}
{digit}+\.{digit}* { sscanf(yytext, "%lf", &yylval.db); return NUMBER;}
\.{digit}+ { sscanf(yytext, "%lf", &yylval.db); return NUMBER;}
sign return *yytext;
{blank}+ ;
{other} return yytext[0];
%%
int main()
{
if (yyparse()==0){
printf("\n NO ERROR");}
return 0;
}
int yyerror(char * mensaje)
{
printf("\n AND STOP");
printf("\n ERROR: %s",mensaje);
printf("\n ERROR LINE: %d",yylineno);
return 0;
}
野牛文件:
%{
#include <stdio.h>
#include <stdlib.h>
char result[100];
%}
%union { double db; int i; }
%token NUMBER
%left '-' '+'
%left '*' '/'
%left '(' ')'
%nonassoc UMINUS
%type<db> list NUMBER
%type<i> expression
%start list
%%
list : expression { printf("\nResultado: %5g\n",$$.db);}
;
expression : expression '+' expression { $$.db = $1.db + $3.db; }
| expression '-' expression { $$.db = $1.db - $3.db; }
| expression '*' expression { $$.db = $1.db * $3.db; }
| expression '/' expression { if ($3.db==(double)0) yyerror("Division por cero\n");
else $$.db = $1.db/$3.db; }
| '-' expression %prec UMINUS { $$.db = -$2.db; }
| '(' expression ')' { $$.db = $2.db; }
| NUMBER { $$.db = $1.db; }
;