我最近开始学习野牛,我已经打了一堵墙。手册部分有点模棱两可,所以我猜想一个错误是可以预料的。下面的代码是官方手册中的第一个教程 - 逆波兰标记计算器,保存在单个文件中 - 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 {...}
出了什么问题?
根据错误信息第11行提供了一个问题。这是{float}的大括号。如果你删除大括号会发生什么? – PapaAtHome
@PapaAtHome \t 我试过了。 cmd返回:rpcalc.y:11.24-29:语法错误,意外标识符 – HDFighter
'bison 3.0'引入了'%define api.value.type'功能。输入'bison --version'来检查你的版本号。你得到的错误是早于3.0的版本将返回这个语句, –