野牛语法错误
Bison syntax error
我最近开始学习野牛,但我已经碰壁了。手册部分有点含糊不清,所以我想会出现错误。下面的代码是来自官方手册的第一个教程 - The Reverse Polish Notation Calculator,保存在一个文件中 - rpcalc.y.
/* 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", );}
;
exp:
NUM {$$ = ; }
| exp exp '+' {$$ = + ; }
| exp exp '-' {$$ = - ; }
| exp exp '*' {$$ = * ; }
| exp exp '/' {$$ = / ; }
| exp exp '^' {$$ = pow (, ); }
| exp 'n' {$$ = -; }
;
%%
/* 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);
}
在cmd中执行bison rpcalc.y returns出现如下错误:
rpcalc.y:11.24-31: syntax error, unexpected {...}
好像是什么问题?
该故障是由于您使用的是bison 3.0版本的新功能,而您安装的是旧版本的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", );}
;
exp:
NUM {$$ = ; }
| exp exp '+' {$$ = + ; }
| exp exp '-' {$$ = - ; }
| exp exp '*' {$$ = * ; }
| exp exp '/' {$$ = / ; }
| exp exp '^' {$$ = pow (, ); }
| exp 'n' {$$ = -; }
;
%%
/* 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);
}
这在更广泛的野牛版本中运行。
我最近开始学习野牛,但我已经碰壁了。手册部分有点含糊不清,所以我想会出现错误。下面的代码是来自官方手册的第一个教程 - The Reverse Polish Notation Calculator,保存在一个文件中 - rpcalc.y.
/* 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", );}
;
exp:
NUM {$$ = ; }
| exp exp '+' {$$ = + ; }
| exp exp '-' {$$ = - ; }
| exp exp '*' {$$ = * ; }
| exp exp '/' {$$ = / ; }
| exp exp '^' {$$ = pow (, ); }
| exp 'n' {$$ = -; }
;
%%
/* 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);
}
在cmd中执行bison rpcalc.y returns出现如下错误:
rpcalc.y:11.24-31: syntax error, unexpected {...}
好像是什么问题?
该故障是由于您使用的是bison 3.0版本的新功能,而您安装的是旧版本的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", );}
;
exp:
NUM {$$ = ; }
| exp exp '+' {$$ = + ; }
| exp exp '-' {$$ = - ; }
| exp exp '*' {$$ = * ; }
| exp exp '/' {$$ = / ; }
| exp exp '^' {$$ = pow (, ); }
| exp 'n' {$$ = -; }
;
%%
/* 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);
}
这在更广泛的野牛版本中运行。