2016-03-08 74 views
2

我最近開始學習野牛,我已經打了一堵牆。手冊部分有點模棱兩可,所以我猜想一個錯誤是可以預料的。下面的代碼是官方手冊中的第一個教程 - 逆波蘭標記計算器,保存在單個文件中 - rpcalc.y。Bison語法錯誤

/* Reverse polish notation calculator */ 

%{ 
    #include <stdio.h> 
    #include <math.h> 
    #include <ctype.h> 
    int yylex (void); 
    void yyerror (char const *); 
%} 

%define api.value.type {double} 
%token NUM 

%% /* Grammar rules and actions follow. */ 

input: 
    %empty 
| input line 
; 

line: 
    '\n' 
| exp '\n' {printf ("%.10g\n", $1);} 
; 

exp: 
    NUM   {$$ = $1;   } 
| exp exp '+' {$$ = $1 + $2;  } 
| exp exp '-' {$$ = $1 - $2;  } 
| exp exp '*' {$$ = $1 * $2;  } 
| exp exp '/' {$$ = $1/$2;  } 
| exp exp '^' {$$ = pow ($1, $2); } 
| exp 'n'  {$$ = -$1;   } 
; 
%% 

/* The lexical analyzer */ 

int yylex (void) 
{ 
    int c; 

    /* Skip white space */ 
    while((c = getchar()) == ' ' || c == '\t') 
     continue; 
    /* Process numbers */ 
    if(c == '.' || isdigit (c)) 
    { 
     ungetc (c, stdin); 
     scanf ("%lf", $yylval); 
     return NUM; 
    } 
    /* Return end-of-imput */ 
    if (c == EOF) 
     return 0; 
    /* Return a single char */ 
    return c; 
} 

int main (void) 
{ 
    return yyparse(); 
} 

void yyerror (char const *s) 
{ 
    fprintf (stderr, "%s\n", s); 
} 

執行野牛rpcalc.y在cmd中返回以下錯誤:

rpcalc.y:11.24-31: syntax error, unexpected {...} 

出了什麼問題?

+0

根據錯誤信息第11行提供了一個問題。這是{float}的大括號。如果你刪除大括號會發生什麼? – PapaAtHome

+0

@PapaAtHome \t 我試過了。 cmd返回:rpcalc.y:11.24-29:語法錯誤,意外標識符 – HDFighter

+4

'bison 3.0'引入了'%define api.value.type'功能。輸入'bison --version'來檢查你的版本號。你得到的錯誤是早於3.0的版本將返回這個語句, –

回答

1

錯誤是由於您使用的是3.0版本的bison的新功能而導致的,而您已安裝舊版本的bison。如果您無法升級到3.0版,則將語法轉換爲使用早期版本的bison的功能是一個簡單的改變。

可將%define api.value.type {double}更改爲%type命令,並將%empty命令刪除。將所得野牛程序將是:

/* Reverse polish notation calculator */ 

%{ 
    #include <stdio.h> 
    #include <math.h> 
    #include <ctype.h> 
    int yylex (void); 
    void yyerror (char const *); 
%} 

%type <double> exp 
%token <double> NUM 

%% /* Grammar rules and actions follow. */ 

input: 
| input line 
; 

line: 
    '\n' 
| exp '\n' {printf ("%.10g\n", $1);} 
; 

exp: 
    NUM   {$$ = $1;   } 
| exp exp '+' {$$ = $1 + $2;  } 
| exp exp '-' {$$ = $1 - $2;  } 
| exp exp '*' {$$ = $1 * $2;  } 
| exp exp '/' {$$ = $1/$2;  } 
| exp exp '^' {$$ = pow ($1, $2); } 
| exp 'n'  {$$ = -$1;   } 
; 
%% 

/* The lexical analyzer */ 

int yylex (void) 
{ 
    int c; 

    /* Skip white space */ 
    while((c = getchar()) == ' ' || c == '\t') 
     continue; 
    /* Process numbers */ 
    if(c == '.' || isdigit (c)) 
    { 
     ungetc (c, stdin); 
     scanf ("%lf", $yylval); 
     return NUM; 
    } 
    /* Return end-of-imput */ 
    if (c == EOF) 
     return 0; 
    /* Return a single char */ 
    return c; 
} 

int main (void) 
{ 
    return yyparse(); 
} 

void yyerror (char const *s) 
{ 
    fprintf (stderr, "%s\n", s); 
} 

這將運行在較寬的範圍野牛版本。

+0

再次感謝您!我發現了一個名爲win_bison.exe(V3.0)的特殊win版本(3.0)。我使用它與-y參數並使用Code :: Blocks編譯生成的文件。它像一個魅力! – HDFighter