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 int yydebug; 184 int yynerrs; 185 186 int yyerrflag; 187 int yychar; 188 YYSTYPE yyval; 189 YYSTYPE yylval; 190 191 /* define the initial stack-sizes */ 192 #ifdef YYSTACKSIZE 193 #undef YYMAXDEPTH 194 #define YYMAXDEPTH YYSTACKSIZE 195 #else 196 #ifdef YYMAXDEPTH 197 #define YYSTACKSIZE YYMAXDEPTH 198 #else 199 #define YYSTACKSIZE 10000 200 #define YYMAXDEPTH 10000 201 #endif 202 #endif 203 204 #define YYINITSTACKSIZE 200 205 206 typedef struct { 207 unsigned stacksize; 208 YYINT *s_base; 209 YYINT *s_mark; 210 YYINT *s_last; 211 YYSTYPE *l_base; 212 YYSTYPE *l_mark; 213 } YYSTACKDATA; 214 /* variables for the parser stack */ 215 static YYSTACKDATA yystack; 216 #line 70 "code_calc.y" 217 /* start of programs */ 218 219 #ifdef YYBYACC 220 extern int YYLEX_DECL(); 221 #endif 222 223 int 224 main (void) 225 { 226 while(!feof(stdin)) { 227 yyparse(); 228 } 229 return 0; 230 } 231 232 static void 233 yyerror(const char *s) 234 { 235 fprintf(stderr, "%s\n", s); 236 } 237 238 int 239 yylex(void) 240 { 241 /* lexical analysis routine */ 242 /* returns LETTER for a lower case letter, yylval = 0 through 25 */ 243 /* return DIGIT for a digit, yylval = 0 through 9 */ 244 /* all other characters are returned immediately */ 245 246 int c; 247 248 while( (c=getchar()) == ' ' ) { /* skip blanks */ } 249 250 /* c is now nonblank */ 251 252 if( islower( c )) { 253 yylval = c - 'a'; 254 return ( LETTER ); 255 } 256 if( isdigit( c )) { 257 yylval = c - '0'; 258 return ( DIGIT ); 259 } 260 return( c ); 261 } 262 #line 263 "code_calc.code.c" 263 264 #if YYDEBUG 265 #include <stdio.h> /* needed for printf */ 266 #endif 267 268 #include <stdlib.h> /* needed for malloc, etc */ 269 #include <string.h> /* needed for memset */ 270 271 /* allocate initial stack or double stack size, up to YYMAXDEPTH */ 272 static int yygrowstack(YYSTACKDATA *data) 273 { 274 int i; 275 unsigned newsize; 276 YYINT *newss; 277 YYSTYPE *newvs; 278 279 if ((newsize = data->stacksize) == 0) 280 newsize = YYINITSTACKSIZE; 281 else if (newsize >= YYMAXDEPTH) 282 return YYENOMEM; 283 else if ((newsize *= 2) > YYMAXDEPTH) 284 newsize = YYMAXDEPTH; 285 286 i = (int) (data->s_mark - data->s_base); 287 newss = (YYINT *)realloc(data->s_base, newsize * sizeof(*newss)); 288 if (newss == 0) 289 return YYENOMEM; 290 291 data->s_base = newss; 292 data->s_mark = newss + i; 293 294 newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs)); 295 if (newvs == 0) 296 return YYENOMEM; 297 298 data->l_base = newvs; 299 data->l_mark = newvs + i; 300 301 data->stacksize = newsize; 302 data->s_last = data->s_base + newsize - 1; 303 return 0; 304 } 305 306 #if YYPURE || defined(YY_NO_LEAKS) 307 static void yyfreestack(YYSTACKDATA *data) 308 { 309 free(data->s_base); 310 free(data->l_base); 311 memset(data, 0, sizeof(*data)); 312 } 313 #else 314 #define yyfreestack(data) /* nothing */ 315 #endif 316 317 #define YYABORT goto yyabort 318 #define YYREJECT goto yyabort 319 #define YYACCEPT goto yyaccept 320 #define YYERROR goto yyerrlab 321 322 int 323 YYPARSE_DECL() 324 { 325 int yym, yyn, yystate; 326 #if YYDEBUG 327 const char *yys; 328 329 if ((yys = getenv("YYDEBUG")) != 0) 330 { 331 yyn = *yys; 332 if (yyn >= '0' && yyn <= '9') 333 yydebug = yyn - '0'; 334 } 335 #endif 336 337 yym = 0; 338 yyn = 0; 339 yynerrs = 0; 340 yyerrflag = 0; 341 yychar = YYEMPTY; 342 yystate = 0; 343 344 #if YYPURE 345 memset(&yystack, 0, sizeof(yystack)); 346 #endif 347 348 if (yystack.s_base == NULL && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 349 yystack.s_mark = yystack.s_base; 350 yystack.l_mark = yystack.l_base; 351 yystate = 0; 352 *yystack.s_mark = 0; 353 354 yyloop: 355 if ((yyn = yydefred[yystate]) != 0) goto yyreduce; 356 if (yychar < 0) 357 { 358 yychar = YYLEX; 359 if (yychar < 0) yychar = YYEOF; 360 #if YYDEBUG 361 if (yydebug) 362 { 363 if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN]; 364 printf("%sdebug: state %d, reading %d (%s)\n", 365 YYPREFIX, yystate, yychar, yys); 366 } 367 #endif 368 } 369 if (((yyn = yysindex[yystate]) != 0) && (yyn += yychar) >= 0 && 370 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar) 371 { 372 #if YYDEBUG 373 if (yydebug) 374 printf("%sdebug: state %d, shifting to state %d\n", 375 YYPREFIX, yystate, yytable[yyn]); 376 #endif 377 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 378 yystate = yytable[yyn]; 379 *++yystack.s_mark = yytable[yyn]; 380 *++yystack.l_mark = yylval; 381 yychar = YYEMPTY; 382 if (yyerrflag > 0) --yyerrflag; 383 goto yyloop; 384 } 385 if (((yyn = yyrindex[yystate]) != 0) && (yyn += yychar) >= 0 && 386 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yychar) 387 { 388 yyn = yytable[yyn]; 389 goto yyreduce; 390 } 391 if (yyerrflag != 0) goto yyinrecovery; 392 393 YYERROR_CALL("syntax error"); 394 395 goto yyerrlab; /* redundant goto avoids 'unused label' warning */ 396 yyerrlab: 397 ++yynerrs; 398 399 yyinrecovery: 400 if (yyerrflag < 3) 401 { 402 yyerrflag = 3; 403 for (;;) 404 { 405 if (((yyn = yysindex[*yystack.s_mark]) != 0) && (yyn += YYERRCODE) >= 0 && 406 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) YYERRCODE) 407 { 408 #if YYDEBUG 409 if (yydebug) 410 printf("%sdebug: state %d, error recovery shifting\ 411 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]); 412 #endif 413 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 414 yystate = yytable[yyn]; 415 *++yystack.s_mark = yytable[yyn]; 416 *++yystack.l_mark = yylval; 417 goto yyloop; 418 } 419 else 420 { 421 #if YYDEBUG 422 if (yydebug) 423 printf("%sdebug: error recovery discarding state %d\n", 424 YYPREFIX, *yystack.s_mark); 425 #endif 426 if (yystack.s_mark <= yystack.s_base) goto yyabort; 427 --yystack.s_mark; 428 --yystack.l_mark; 429 } 430 } 431 } 432 else 433 { 434 if (yychar == YYEOF) goto yyabort; 435 #if YYDEBUG 436 if (yydebug) 437 { 438 if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN]; 439 printf("%sdebug: state %d, error recovery discards token %d (%s)\n", 440 YYPREFIX, yystate, yychar, yys); 441 } 442 #endif 443 yychar = YYEMPTY; 444 goto yyloop; 445 } 446 447 yyreduce: 448 #if YYDEBUG 449 if (yydebug) 450 printf("%sdebug: state %d, reducing by rule %d (%s)\n", 451 YYPREFIX, yystate, yyn, yyrule[yyn]); 452 #endif 453 yym = yylen[yyn]; 454 if (yym > 0) 455 yyval = yystack.l_mark[1-yym]; 456 else 457 memset(&yyval, 0, sizeof yyval); 458 459 switch (yyn) 460 { 461 case 3: 462 #line 32 "code_calc.y" 463 { yyerrok ; } 464 break; 465 case 4: 466 #line 36 "code_calc.y" 467 { printf("%d\n",yystack.l_mark[0]);} 468 break; 469 case 5: 470 #line 38 "code_calc.y" 471 { regs[yystack.l_mark[-2]] = yystack.l_mark[0]; } 472 break; 473 case 6: 474 #line 42 "code_calc.y" 475 { yyval = yystack.l_mark[-1]; } 476 break; 477 case 7: 478 #line 44 "code_calc.y" 479 { yyval = yystack.l_mark[-2] + yystack.l_mark[0]; } 480 break; 481 case 8: 482 #line 46 "code_calc.y" 483 { yyval = yystack.l_mark[-2] - yystack.l_mark[0]; } 484 break; 485 case 9: 486 #line 48 "code_calc.y" 487 { yyval = yystack.l_mark[-2] * yystack.l_mark[0]; } 488 break; 489 case 10: 490 #line 50 "code_calc.y" 491 { yyval = yystack.l_mark[-2] / yystack.l_mark[0]; } 492 break; 493 case 11: 494 #line 52 "code_calc.y" 495 { yyval = yystack.l_mark[-2] % yystack.l_mark[0]; } 496 break; 497 case 12: 498 #line 54 "code_calc.y" 499 { yyval = yystack.l_mark[-2] & yystack.l_mark[0]; } 500 break; 501 case 13: 502 #line 56 "code_calc.y" 503 { yyval = yystack.l_mark[-2] | yystack.l_mark[0]; } 504 break; 505 case 14: 506 #line 58 "code_calc.y" 507 { yyval = - yystack.l_mark[0]; } 508 break; 509 case 15: 510 #line 60 "code_calc.y" 511 { yyval = regs[yystack.l_mark[0]]; } 512 break; 513 case 17: 514 #line 65 "code_calc.y" 515 { yyval = yystack.l_mark[0]; base = (yystack.l_mark[0]==0) ? 8 : 10; } 516 break; 517 case 18: 518 #line 67 "code_calc.y" 519 { yyval = base * yystack.l_mark[-1] + yystack.l_mark[0]; } 520 break; 521 #line 522 "code_calc.code.c" 522 } 523 yystack.s_mark -= yym; 524 yystate = *yystack.s_mark; 525 yystack.l_mark -= yym; 526 yym = yylhs[yyn]; 527 if (yystate == 0 && yym == 0) 528 { 529 #if YYDEBUG 530 if (yydebug) 531 printf("%sdebug: after reduction, shifting from state 0 to\ 532 state %d\n", YYPREFIX, YYFINAL); 533 #endif 534 yystate = YYFINAL; 535 *++yystack.s_mark = YYFINAL; 536 *++yystack.l_mark = yyval; 537 if (yychar < 0) 538 { 539 yychar = YYLEX; 540 if (yychar < 0) yychar = YYEOF; 541 #if YYDEBUG 542 if (yydebug) 543 { 544 if ((yys = yyname[YYTRANSLATE(yychar)]) == NULL) yys = yyname[YYUNDFTOKEN]; 545 printf("%sdebug: state %d, reading %d (%s)\n", 546 YYPREFIX, YYFINAL, yychar, yys); 547 } 548 #endif 549 } 550 if (yychar == YYEOF) goto yyaccept; 551 goto yyloop; 552 } 553 if (((yyn = yygindex[yym]) != 0) && (yyn += yystate) >= 0 && 554 yyn <= YYTABLESIZE && yycheck[yyn] == (YYINT) yystate) 555 yystate = yytable[yyn]; 556 else 557 yystate = yydgoto[yym]; 558 #if YYDEBUG 559 if (yydebug) 560 printf("%sdebug: after reduction, shifting from state %d \ 561 to state %d\n", YYPREFIX, *yystack.s_mark, yystate); 562 #endif 563 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack) == YYENOMEM) goto yyoverflow; 564 *++yystack.s_mark = (YYINT) yystate; 565 *++yystack.l_mark = yyval; 566 goto yyloop; 567 568 yyoverflow: 569 YYERROR_CALL("yacc stack overflow"); 570 571 yyabort: 572 yyfreestack(&yystack); 573 return (1); 574 575 yyaccept: 576 yyfreestack(&yystack); 577 return (0); 578 } 579