calc.y

来自「和YACC一样」· Y 代码 · 共 87 行

Y
87
字号
/* Infix notation calculator--calc */%{#include <math.h>#include <stdio.h>#define YYSTYPE doubleint yylex(void);void yyerror(const char *errMsg);%}/* zyacc declarations */%token NUM_TOK%left '-' '+'%left '*' '/'%left NEG	/* negation--unary minus */%right '^'	/* exponentiation   *//* Grammar follows */%%input  : /* empty */  | input line  ;line  : '\n'  | exp '\n'		{ printf ("\t%.10g\n", $1); }  ;exp  : NUM_TOK  | exp '+' exp   	{ $$= $1 + $3; }  | exp '-' exp   	{ $$= $1 - $3; }  | exp '*' exp   	{ $$= $1 * $3; }  | exp '/' exp   	{ $$= $1 / $3; }  | '-' exp  %prec NEG 	{ $$= -$2; }  | exp '^' exp   	{ $$= pow ($1, $3); }  | '(' exp ')'   	{ $$= $2; }  ;%%/* !done! *//* Lexical analyzer returns a double floating point * number in yylval and the token NUM_TOK, or the ASCII * character read if not a number.  Skips all blanks * and tabs, returns 0 for EOF.  */#include <ctype.h>intyylex(void){  int c;  /* skip white space  */  while ((c = getchar ()) == ' ' || c == '\t')    ;  /* process numbers   */  if (c == '.' || isdigit (c)) {     ungetc(c, stdin);     scanf("%lf", &yylval);     return NUM_TOK;  }  /* return end-of-file  */  if (c == EOF)    return 0;  /* return single chars */  return c;}#include <stdio.h>/* Called by yyparse on error */voidyyerror(const char *s)  {  printf("%s\n", s);}int main(){  return yyparse();}

⌨️ 快捷键说明

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