野牛与非终端合作

时间:2012-11-25 01:09:22

标签: bison

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

(那些只是的例子。 不要只是复制它们。你需要根据你对值的处理来考虑这个。)

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