2012-11-25 19 views
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 

回答

0

你不應該同時使用YYSTYPE%union。我很確定這樣做會導致解析器無法編譯。 (然而,bison可能不檢測。)

如果指定%union,那麼你必須告訴bison這(通過標記名)的union成員適用於每個終端和非終端。你這樣做與%type聲明非終端,%token爲終端,具體如下:

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

(這些都是只是一個例子不要只是複製它們您需要通過視認爲這。 )

如果您指定%union終端和非終端 - 或者至少,使用其值的 - 必須具有類型說明;否則,bison將產生您看到的錯誤消息。如果您未指定%union,則不需要聲明類型,因爲每個終端和非終端具有相同的類型,這與YYSTYPE是什麼相關。