0

在我添加“浮动”规则之前,以下代码正在正确编译,然后它给了我在下面列出的错误,任何帮助将不胜感激。

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

以下是错误:

stojk_3_4.y:40.63-64: $2 of `lines' has no declared type
stojk_3_4.y:45.49-50: $$ of `expr' has no declared type
stojk_3_4.y:45.56-57: $1 of `expr' has no declared type
stojk_3_4.y:45.63-64: $3 of `expr' has no declared type
stojk_3_4.y:46.61-62: $$ of `expr' has no declared type
stojk_3_4.y:46.68-69: $1 of `expr' has no declared type
stojk_3_4.y:46.75-76: $3 of `expr' has no declared type
stojk_3_4.y:47.60-61: $$ of `expr' has no declared type
stojk_3_4.y:47.67-68: $1 of `expr' has no declared type
stojk_3_4.y:47.74-75: $3 of `expr' has no declared type
stojk_3_4.y:48.63-64: $$ of `expr' has no declared type
stojk_3_4.y:48.70-71: $1 of `expr' has no declared type
stojk_3_4.y:48.77-78: $3 of `expr' has no declared type
stojk_3_4.y:49.62-63: $$ of `expr' has no declared type
stojk_3_4.y:49.68-69: $2 of `expr' has no declared type
stojk_3_4.y:53.60-61: $$ of `float' has no declared type
stojk_3_4.y:53.67-68: $1 of `float' has no declared type
stojk_3_4.y:53.74-75: $3 of `float' has no declared type
stojk_3_4.y:54.82-83: $$ of `float' has no declared type
stojk_3_4.y:54.89-90: $1 of `float' has no declared type
stojk_3_4.y:54.96-97: $3 of `float' has no declared type
stojk_3_4.y:55.81-82: $$ of `float' has no declared type
stojk_3_4.y:55.88-89: $1 of `float' has no declared type
stojk_3_4.y:55.95-96: $3 of `float' has no declared type
stojk_3_4.y:56.83-84: $$ of `float' has no declared type
stojk_3_4.y:56.90-91: $1 of `float' has no declared type
stojk_3_4.y:56.97-98: $3 of `float' has no declared type
stojk_3_4.y:57.73-74: $$ of `float' has no declared type
stojk_3_4.y:57.79-80: $2 of `float' has no declared type
4

1 回答 1

1

您不应同时使用YYSTYPE%union。我很确定这样做会导致解析器无法编译。(但是,bison可能没有检测到这一点。)

如果您指定%union,那么您必须告知bison哪些union成员(按标记名)适用于每个终端和非终端。您可以使用%type非终端和%token终端的声明来执行此操作,如下所示:

%type <int_val> expr
%token <op_val> PLUS MINUS

(这些只是示例。 不要只是复制它们。您需要根据您对这些值所做的事情来考虑这一点。)

如果您指定%union 每个终端和非终端 - 或者至少,使用其值的那些 - 必须具有类型规范;否则,bison将产生您看到的错误消息。如果不指定%union,则不需要声明类型,因为每个终端和非终端都具有相同的类型,无论YYSTYPE是什么类型。

于 2012-11-25T02:18:12.227 回答