刘义情 2017-10-10 17:36
浏览 1344

我在运行bison的第一个实例程序(计算后缀表达式时),永远都是报syntax error?

/* Reverse polish notation calculator. */

/* === C prolog === */

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

/* === Bison declarations === */
%define api.value.type {double}
%token NUM

/* === Bison Grammar === */

 %%      /* rules and actions follow.  */

 input:  %empty
         | input line
 ;

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

 exp:      NUM           { $$ = $1;           }
         | exp exp '+'   { $$ = $1 + $2;      }
         | exp exp '-'   { $$ = $1 - $2;      }
         | exp exp '*'   { $$ = $1 * $2;      }
         | exp exp '/'   { $$ = $1 / $2;      }

          /* Exponentiation */
         | exp exp '^'   { $$ = pow ($1, $2); }

          /* Unary minus    */
         | exp 'n'       { $$ = -$1;          }
 ;

 %%

/* === C epilog === */

 #include <stdio.h>

 /* Called by yyparse on error.  */
 void yyerror (char const *s){
   fprintf(stderr, "%s\n", s);
 }


 /* The lexical analyzer returns a double floating point number
    on the stack and the token NUM, or the numeric code of the
    character read if not a number.  It skips all blanks and tabs,
    and returns 0 for end-of-input.

    This next yylex function would usually be generated by Flex .
 */

 #include <ctype.h>

 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-input.  */
   if (c == EOF)
     return 0;
   /* Return a single char.  */
   return c;
 }

 int main (void){
   return yyparse ();
 }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算