⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bis.y

📁 一个flex和bison用法入门的很好的例子。 按照词法分析,语法分析,语义分析的顺序来熟悉flex和bison这两个有力的工具。最后给出了一个用flex做词法分析用bison做语法分析设计的科学计算
💻 Y
字号:
/* Reverse polish notation calculator. */
%{
#define YYSTYPE double
#include <math.h>
#include <ctype.h>

%}
%token NUM
%% /* Grammar rules and actions follow */
input:    /* empty */
        | input line
;
line:    '\n'
        | exp '\n'  { printf ("\t%.10g\n", $1); }
	| error '\n' { yyerrok;                  }

;
exp:      NUM                { $$ = $1;         }
        | exp '+' exp        { $$ = $1 + $3;    }
        | exp '-' exp        { $$ = $1 - $3;    }
        | exp '*' exp        { $$ = $1 * $3;    }
        | exp '/' exp        { $$ = $1 / $3;    }
        | 'n' exp            { $$ = -$2;        }
        | exp '^' exp        { $$ = pow ($1, $3); }
        | '(' exp ')'        { $$ = $2;         }
;
%%

/*This function is writed by ourselve not by flex this function is needed by bison */
 yylex ()
{
  int c;
  /* skip white space  */
  while ((c = getchar ()) == ' ' || c == '\t');
  /* process numbers   */
  if (c == '.' || isdigit (c))                
    {
      ungetc (c, stdin);
      scanf ("%lf", &yylval);
      return NUM;
    }
  /* return end-of-file  */
  if (c == EOF)                            
    return 0;
  /* return single chars */
  return c;                                
}

yyerror (char *s)  /* Called by yyparse on error */
{
  printf ("%s\n", s);
}
main ()
{
  yyparse ();
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -