1 /* original parser id follows */ 2 /* yysccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93" */ 3 /* (use YYMAJOR/YYMINOR for ifdefs dependent on parser version) */ 4 5 #define YYBYACC 1 6 #define YYMAJOR 1 7 #define YYMINOR 9 8 #define YYCHECK "yyyymmdd" 9 10 #define YYEMPTY (-1) 11 #define yyclearin (yychar = YYEMPTY) 12 #define yyerrok (yyerrflag = 0) 13 #define YYRECOVERING() (yyerrflag != 0) 14 #define YYENOMEM (-2) 15 #define YYEOF 0 16 17 #ifndef yyparse 18 #define yyparse calc_parse 19 #endif /* yyparse */ 20 21 #ifndef yylex 22 #define yylex calc_lex 23 #endif /* yylex */ 24 25 #ifndef yyerror 26 #define yyerror calc_error 27 #endif /* yyerror */ 28 29 #ifndef yychar 30 #define yychar calc_char 31 #endif /* yychar */ 32 33 #ifndef yyval 34 #define yyval calc_val 35 #endif /* yyval */ 36 37 #ifndef yylval 38 #define yylval calc_lval 39 #endif /* yylval */ 40 41 #ifndef yydebug 42 #define yydebug calc_debug 43 #endif /* yydebug */ 44 45 #ifndef yynerrs 46 #define yynerrs calc_nerrs 47 #endif /* yynerrs */ 48 49 #ifndef yyerrflag 50 #define yyerrflag calc_errflag 51 #endif /* yyerrflag */ 52 53 #ifndef yylhs 54 #define yylhs calc_lhs 55 #endif /* yylhs */ 56 57 #ifndef yylen 58 #define yylen calc_len 59 #endif /* yylen */ 60 61 #ifndef yydefred 62 #define yydefred calc_defred 63 #endif /* yydefred */ 64 65 #ifndef yydgoto 66 #define yydgoto calc_dgoto 67 #endif /* yydgoto */ 68 69 #ifndef yysindex 70 #define yysindex calc_sindex 71 #endif /* yysindex */ 72 73 #ifndef yyrindex 74 #define yyrindex calc_rindex 75 #endif /* yyrindex */ 76 77 #ifndef yygindex 78 #define yygindex calc_gindex 79 #endif /* yygindex */ 80 81 #ifndef yytable 82 #define yytable calc_table 83 #endif /* yytable */ 84 85 #ifndef yycheck 86 #define yycheck calc_check 87 #endif /* yycheck */ 88 89 #ifndef yyname 90 #define yyname calc_name 91 #endif /* yyname */ 92 93 #ifndef yyrule 94 #define yyrule calc_rule 95 #endif /* yyrule */ 96 #define YYPREFIX "calc_" 97 98 #define YYPURE 0 99 100 #line 4 "code_calc.y" 101 # include <stdio.h> 102 # include <ctype.h> 103 104 int regs[26]; 105 int base; 106 107 #ifdef YYBISON 108 int yylex(void); 109 static void yyerror(const char *s); 110 #endif 111 112 #line 113 "code_calc.code.c" 113 114 #if ! defined(YYSTYPE) && ! defined(YYSTYPE_IS_DECLARED) 115 /* Default: YYSTYPE is the semantic value type. */ 116 typedef int YYSTYPE; 117 # define YYSTYPE_IS_DECLARED 1 118 #endif 119 120 /* compatibility with bison */ 121 #ifdef YYPARSE_PARAM 122 /* compatibility with FreeBSD */ 123 # ifdef YYPARSE_PARAM_TYPE 124 # define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM) 125 # else 126 # define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM) 127 # endif 128 #else 129 # define YYPARSE_DECL() yyparse(void) 130 #endif 131 132 /* Parameters sent to lex. */ 133 #ifdef YYLEX_PARAM 134 # define YYLEX_DECL() yylex(void *YYLEX_PARAM) 135 # define YYLEX yylex(YYLEX_PARAM) 136 #else 137 # define YYLEX_DECL() yylex(void) 138 # define YYLEX yylex() 139 #endif 140 141 /* Parameters sent to yyerror. */ 142 #ifndef YYERROR_DECL 143 #define YYERROR_DECL() yyerror(const char *s) 144 #endif 145 #ifndef YYERROR_CALL 146 #define YYERROR_CALL(msg) yyerror(msg) 147 #endif 148 149 #define DIGIT 257 150 #define LETTER 258 151 #define UMINUS 259 152 #define YYERRCODE 256 153 #undef yytname 154 #define yytname yyname 155 #define YYTABLESIZE 220 156 #define YYFINAL 1 157 #ifndef YYDEBUG 158 #define YYDEBUG 0 159 #endif 160 #define YYMAXTOKEN 259 161 #define YYUNDFTOKEN 265 162 #define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? YYUNDFTOKEN : (a)) 163 164 extern int YYPARSE_DECL(); 165 typedef int YYINT; 166 extern YYINT yylhs[]; 167 extern YYINT yylen[]; 168 extern YYINT yydefred[]; 169 extern YYINT yydgoto[]; 170 extern YYINT yysindex[]; 171 extern YYINT yyrindex[]; 172 extern YYINT yygindex[]; 173 extern YYINT yytable[]; 174 extern YYINT yycheck[]; 175 176 #if YYDEBUG || defined(yytname) 177 extern char *yyname[]; 178 #endif 179 #if YYDEBUG 180 extern char *yyrule[]; 181 #endif 182 183 #if YYDEBUG 184 int yydebug; 185 #endif 186 187 int yyerrflag; 188 int yychar; 189 YYSTYPE yyval; 190 YYSTYPE yylval; 191 int yynerrs; 192 193 /* define the initial stack-sizes */ 194 #ifdef YYSTACKSIZE 195 #undef YYMAXDEPTH 196 #define YYMAXDEPTH YYSTACKSIZE 197 #else 198 #ifdef YYMAXDEPTH 199 #define YYSTACKSIZE YYMAXDEPTH 200 #else 201 #define YYSTACKSIZE 10000 202 #define YYMAXDEPTH 10000 203 #endif 204 #endif 205 206 #define YYINITSTACKSIZE 200 207 208 typedef struct { 209 unsigned stacksize; 210 YYINT *s_base; 211 YYINT *s_mark; 212 YYINT *s_last; 213 YYSTYPE *l_base; 214 YYSTYPE *l_mark; 215 } YYSTACKDATA; 216 /* variables for the parser stack */ 217 static YYSTACKDATA yystack; 218 #line 70 "code_calc.y" 219 /* start of programs */ 220 221 #ifdef YYBYACC 222 extern int YYLEX_DECL(); 223 #endif 224 225 int 226 main (void) 227 { 228 while(!feof(stdin)) { 229 yyparse(); 230 } 231 return 0; 232 } 233 234 static void 235 yyerror(const char *s) 236 { 237 fprintf(stderr, "%s\n", s); 238 } 239 240 int 241 yylex(void) 242 { 243 /* lexical analysis routine */ 244 /* returns LETTER for a lower case letter, yylval = 0 through 25 */ 245 /* return DIGIT for a digit, yylval = 0 through 9 */ 246 /* all other characters are returned immediately */ 247 248 int c; 249 250 while( (c=getchar()) == ' ' ) { /* skip blanks */ } 251 252 /* c is now nonblank */ 253 254 if( islower( c )) { 255 yylval = c - 'a'; 256 return ( LETTER ); 257 } 258 if( isdigit( c )) { 259 yylval = c - '0'; 260 return ( DIGIT ); 261 } 262 return( c ); 263 } 264 #line 265 "code_calc.code.c" 265 266 #if YYDEBUG 267 #include <stdio.h> /* needed for printf */ 268 #endif 269 270 #include <stdlib.h> /* needed for malloc, etc */ 271 #include <string.h> /* needed for memset */ 272 273 /* allocate initial stack or double stack size, up to YYMAXDEPTH */ 274 static int yygrowstack(YYSTACKDATA *data) 275 { 276 int i; 277 unsigned newsize; 278 YYINT *newss; 279 YYSTYPE *newvs; 280 281 if ((newsize = data->stacksize) == 0) 282 newsize = YYINITSTACKSIZE; 283 else if (newsize >= YYMAXDEPTH) 284 return YYENOMEM; 285 else if ((newsize *= 2) > YYMAXDEPTH) 286 newsize = YYMAXDEPTH; 287 288 i = (int) (data->s_mark - data->s_base); 289 newss = (YYINT *)realloc(data->s_base, newsize * sizeof(*newss)); 290 if (newss == NULL) 291 return YYENOMEM; 292 293 data->s_base = newss; 294 data->s_mark = newss + i; 295 296 newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs)); 297 if (newvs == NULL) 298 return YYENOMEM; 299 300 data->l_base = newvs; 301 data->l_mark = newvs + i; 302 303 data->stacksize = newsize; 304 data->s_last = data->s_base + newsize - 1; 305 return 0; 306 } 307 308 #if YYPURE || defined(YY_NO_LEAKS) 309 static void yyfreestack(YYSTACKDATA *data) 310 { 311 free(data->s_base); 312 free(data->l_base); 313 memset(data, 0, sizeof(*data)); 314 } 315 #else 316 #define yyfreestack(data) /* nothing */ 317 #endif 318 319 #define YYABORT goto yyabort 320 #define YYREJECT goto yyabort 321 #define YYACCEPT goto yyaccept 322 #define YYERROR goto yyerrlab 323 324 int 325 YYPARSE_DECL() 326 { 327 int yym, yyn, yystate; 328 #if YYDEBUG 329 const char *yys; 330 331 if ((yys = getenv("YYDEBUG")) != NULL) 332 { 333 yyn = *yys; 334 if (yyn >= '0' && yyn <= '9') 335 yydebug = yyn - '0'; 336 } 337 #endif 338 339 yym = 0; 340 yyn = 0; 341 yynerrs = 0; 342 yyerrflag = 0; 343 yychar = YYEMPTY; 344 yystate = 0; 345 346 #if YYPURE 347 memset(&yystack, 0, sizeof(yystack)); 348 #endif 349 350 if (yystack.s_base == NULL && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 351 yystack.s_mark = yystack.s_base; 352 yystack.l_mark = yystack.l_base; 353 yystate = 0; 354 *yystack.s_mark = 0; 355 356 yyloop: 357 if ((yyn = yydefred[yystate]) != 0) goto yyreduce; 358 if (yychar < 0) 359 { 360 yychar = YYLEX; 361 if (yychar < 0) yychar = YYEOF; 362 #if YYDEBUG 363 if (yydebug) 364 { 365 if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN]; 366 printf("%sdebug: state %d, reading %d (%s)\n", 367 YYPREFIX, yystate, yychar, yys); 368 } 369 #endif 370 } 371 if (((yyn = yysindex[yystate]) != 0) && (yyn += yychar) >= 0 && 372 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar) 373 { 374 #if YYDEBUG 375 if (yydebug) 376 printf("%sdebug: state %d, shifting to state %d\n", 377 YYPREFIX, yystate, yytable[yyn]); 378 #endif 379 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 380 yystate = yytable[yyn]; 381 *++yystack.s_mark = yytable[yyn]; 382 *++yystack.l_mark = yylval; 383 yychar = YYEMPTY; 384 if (yyerrflag > 0) --yyerrflag; 385 goto yyloop; 386 } 387 if (((yyn = yyrindex[yystate]) != 0) && (yyn += yychar) >= 0 && 388 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar) 389 { 390 yyn = yytable[yyn]; 391 goto yyreduce; 392 } 393 if (yyerrflag != 0) goto yyinrecovery; 394 395 YYERROR_CALL("syntax error"); 396 397 goto yyerrlab; /* redundant goto avoids 'unused label' warning */ 398 yyerrlab: 399 ++yynerrs; 400 401 yyinrecovery: 402 if (yyerrflag < 3) 403 { 404 yyerrflag = 3; 405 for (;;) 406 { 407 if (((yyn = yysindex[*yystack.s_mark]) != 0) && (yyn += YYERRCODE) >= 0 && 408 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) YYERRCODE) 409 { 410 #if YYDEBUG 411 if (yydebug) 412 printf("%sdebug: state %d, error recovery shifting\ 413 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]); 414 #endif 415 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 416 yystate = yytable[yyn]; 417 *++yystack.s_mark = yytable[yyn]; 418 *++yystack.l_mark = yylval; 419 goto yyloop; 420 } 421 else 422 { 423 #if YYDEBUG 424 if (yydebug) 425 printf("%sdebug: error recovery discarding state %d\n", 426 YYPREFIX, *yystack.s_mark); 427 #endif 428 if (yystack.s_mark <= yystack.s_base) goto yyabort; 429 --yystack.s_mark; 430 --yystack.l_mark; 431 } 432 } 433 } 434 else 435 { 436 if (yychar == YYEOF) goto yyabort; 437 #if YYDEBUG 438 if (yydebug) 439 { 440 if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN]; 441 printf("%sdebug: state %d, error recovery discards token %d (%s)\n", 442 YYPREFIX, yystate, yychar, yys); 443 } 444 #endif 445 yychar = YYEMPTY; 446 goto yyloop; 447 } 448 449 yyreduce: 450 #if YYDEBUG 451 if (yydebug) 452 printf("%sdebug: state %d, reducing by rule %d (%s)\n", 453 YYPREFIX, yystate, yyn, yyrule[yyn]); 454 #endif 455 yym = yylen[yyn]; 456 if (yym > 0) 457 yyval = yystack.l_mark[1-yym]; 458 else 459 memset(&yyval, 0, sizeof yyval); 460 461 switch (yyn) 462 { 463 case 3: 464 #line 32 "code_calc.y" 465 { yyerrok ; } 466 break; 467 case 4: 468 #line 36 "code_calc.y" 469 { printf("%d\n",yystack.l_mark[0]);} 470 break; 471 case 5: 472 #line 38 "code_calc.y" 473 { regs[yystack.l_mark[-2]] = yystack.l_mark[0]; } 474 break; 475 case 6: 476 #line 42 "code_calc.y" 477 { yyval = yystack.l_mark[-1]; } 478 break; 479 case 7: 480 #line 44 "code_calc.y" 481 { yyval = yystack.l_mark[-2] + yystack.l_mark[0]; } 482 break; 483 case 8: 484 #line 46 "code_calc.y" 485 { yyval = yystack.l_mark[-2] - yystack.l_mark[0]; } 486 break; 487 case 9: 488 #line 48 "code_calc.y" 489 { yyval = yystack.l_mark[-2] * yystack.l_mark[0]; } 490 break; 491 case 10: 492 #line 50 "code_calc.y" 493 { yyval = yystack.l_mark[-2] / yystack.l_mark[0]; } 494 break; 495 case 11: 496 #line 52 "code_calc.y" 497 { yyval = yystack.l_mark[-2] % yystack.l_mark[0]; } 498 break; 499 case 12: 500 #line 54 "code_calc.y" 501 { yyval = yystack.l_mark[-2] & yystack.l_mark[0]; } 502 break; 503 case 13: 504 #line 56 "code_calc.y" 505 { yyval = yystack.l_mark[-2] | yystack.l_mark[0]; } 506 break; 507 case 14: 508 #line 58 "code_calc.y" 509 { yyval = - yystack.l_mark[0]; } 510 break; 511 case 15: 512 #line 60 "code_calc.y" 513 { yyval = regs[yystack.l_mark[0]]; } 514 break; 515 case 17: 516 #line 65 "code_calc.y" 517 { yyval = yystack.l_mark[0]; base = (yystack.l_mark[0]==0) ? 8 : 10; } 518 break; 519 case 18: 520 #line 67 "code_calc.y" 521 { yyval = base * yystack.l_mark[-1] + yystack.l_mark[0]; } 522 break; 523 #line 524 "code_calc.code.c" 524 } 525 yystack.s_mark -= yym; 526 yystate = *yystack.s_mark; 527 yystack.l_mark -= yym; 528 yym = yylhs[yyn]; 529 if (yystate == 0 && yym == 0) 530 { 531 #if YYDEBUG 532 if (yydebug) 533 printf("%sdebug: after reduction, shifting from state 0 to\ 534 state %d\n", YYPREFIX, YYFINAL); 535 #endif 536 yystate = YYFINAL; 537 *++yystack.s_mark = YYFINAL; 538 *++yystack.l_mark = yyval; 539 if (yychar < 0) 540 { 541 yychar = YYLEX; 542 if (yychar < 0) yychar = YYEOF; 543 #if YYDEBUG 544 if (yydebug) 545 { 546 if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN]; 547 printf("%sdebug: state %d, reading %d (%s)\n", 548 YYPREFIX, YYFINAL, yychar, yys); 549 } 550 #endif 551 } 552 if (yychar == YYEOF) goto yyaccept; 553 goto yyloop; 554 } 555 if (((yyn = yygindex[yym]) != 0) && (yyn += yystate) >= 0 && 556 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yystate) 557 yystate = yytable[yyn]; 558 else 559 yystate = yydgoto[yym]; 560 #if YYDEBUG 561 if (yydebug) 562 printf("%sdebug: after reduction, shifting from state %d \ 563 to state %d\n", YYPREFIX, *yystack.s_mark, yystate); 564 #endif 565 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 566 *++yystack.s_mark = (YYINT) yystate; 567 *++yystack.l_mark = yyval; 568 goto yyloop; 569 570 yyoverflow: 571 YYERROR_CALL("yacc stack overflow"); 572 573 yyabort: 574 yyfreestack(&yystack); 575 return (1); 576 577 yyaccept: 578 yyfreestack(&yystack); 579 return (0); 580 } 581