我最近開始學習野牛,我已經打了一堵牆。手冊部分有點模棱兩可,所以我猜想一個錯誤是可以預料的。下面的代碼是官方手冊中的第一個教程 - 逆波蘭標記計算器,保存在單個文件中 - 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的版本將返回這個語句, –