Bison 野牛在非终点站工作
在我添加“float”规则之前,下面的代码是正确编译的,然后它给了我下面列出的错误。如果您能提供帮助,我们将不胜感激Bison 野牛在非终点站工作,bison,Bison,在我添加“float”规则之前,下面的代码是正确编译的,然后它给了我下面列出的错误。如果您能提供帮助,我们将不胜感激 %{ #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define YYSTYPE double int yylex(void); static void yyerror(char *s) { printf("yyerror: %s\n", s); } %} %u
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define YYSTYPE double
int yylex(void);
static
void yyerror(char *s)
{
printf("yyerror: %s\n", s);
}
%}
%union{
int int_val;
string* op_val;
}
%token PLUS
%token MINUS
%token MULT
%token DIVIDE
%token LPAREN
%token RPAREN
%token Unsigned_float
%token UNSIGNEDINTEGER
%left PLUS MINUS
%left MULT DIVIDE
%%
lines : lines expr '\n' {printf("%g\n", $2);}
| lines '\n'
| /*empty*/
;
expr : expr PLUS expr {$$ = $1 + $3;}
| expr MINUS expr {$$ = $1 - $3;}
| expr MULT expr {$$ = $1 * $3;}
| expr DIVIDE expr {$$ = $1 / $3;}
| LPAREN expr RPAREN {$$ = $2;}
| UNSIGNEDINTEGER
;
float : Unsigned_float PLUS Unsigned_float {$$ = $1 + $3;}
| Unsigned_float MINUS Unsigned_float {$$ = $1 - $3;}
| Unsigned_float MULT Unsigned_float {$$ = $1 * $3;}
| Unsigned_float DIVIDE Unsigned_float {$$ = $1 / $3;}
| LPAREN Unsigned_float RPAREN {$$ = $2;}
;
%%
#include "lex.yy.c"
int yylex(void);
int yyparse(void);
int main(void)
{
return yyparse();
}
您不应同时使用
YYSTYPE
和%union
。我很确定这样做会导致解析器无法编译。(然而,bison
可能没有检测到。)
如果指定%union
,则必须告诉bison
哪个union
成员(按标记名)适用于每个终端和非终端。您可以使用非终端的%type
声明和终端的%token
执行此操作,如下所示:
%type <int_val> expr
%token <op_val> PLUS MINUS
%type expr
%代币加减
(这些只是示例。不要只是复制它们。您需要根据您对这些值所做的操作来仔细考虑。)
如果指定%union
,则每个终端和非终端(或至少使用其值的终端)必须具有类型规范;否则,bison
将产生您看到的错误消息。如果不指定%union
,则不需要声明类型,因为每个终端和非终端都具有相同的类型,即无论yysype
是什么类型
%type <int_val> expr
%token <op_val> PLUS MINUS