2016-03-08 58 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