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