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