Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / byacc / dist / test / calc.tab.c
blobd6b1c416acbdc19cbd52889c49323691d6c2d4c0
1 /* $NetBSD$ */
3 #ifndef lint
4 static const char yysccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93";
5 #endif
7 #include <stdlib.h>
8 #include <string.h>
10 #define YYBYACC 1
11 #define YYMAJOR 1
12 #define YYMINOR 9
14 #define YYEMPTY (-1)
15 #define yyclearin (yychar = YYEMPTY)
16 #define yyerrok (yyerrflag = 0)
17 #define YYRECOVERING() (yyerrflag != 0)
19 /* compatibility with bison */
20 #ifdef YYPARSE_PARAM
21 /* compatibility with FreeBSD */
22 #ifdef YYPARSE_PARAM_TYPE
23 #define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)
24 #else
25 #define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)
26 #endif
27 #else
28 #define YYPARSE_DECL() yyparse(void)
29 #endif /* YYPARSE_PARAM */
31 extern int YYPARSE_DECL();
33 static int yygrowstack(void);
34 #define yyparse calc_parse
35 #define yylex calc_lex
36 #define yyerror calc_error
37 #define yychar calc_char
38 #define yyval calc_val
39 #define yylval calc_lval
40 #define yydebug calc_debug
41 #define yynerrs calc_nerrs
42 #define yyerrflag calc_errflag
43 #define yyss calc_ss
44 #define yyssp calc_ssp
45 #define yyvs calc_vs
46 #define yyvsp calc_vsp
47 #define yylhs calc_lhs
48 #define yylen calc_len
49 #define yydefred calc_defred
50 #define yydgoto calc_dgoto
51 #define yysindex calc_sindex
52 #define yyrindex calc_rindex
53 #define yygindex calc_gindex
54 #define yytable calc_table
55 #define yycheck calc_check
56 #define yyname calc_name
57 #define yyrule calc_rule
58 #define YYPREFIX "calc_"
59 #line 2 "calc.y"
60 # include <stdio.h>
61 # include <ctype.h>
63 int regs[26];
64 int base;
66 #line 65 "calc.tab.c"
67 #define DIGIT 257
68 #define LETTER 258
69 #define UMINUS 259
70 #define YYERRCODE 256
71 static const short calc_lhs[] = { -1,
72 0, 0, 0, 1, 1, 2, 2, 2, 2, 2,
73 2, 2, 2, 2, 2, 2, 3, 3,
75 static const short calc_len[] = { 2,
76 0, 3, 3, 1, 3, 3, 3, 3, 3, 3,
77 3, 3, 3, 2, 1, 1, 1, 2,
79 static const short calc_defred[] = { 1,
80 0, 0, 17, 0, 0, 0, 0, 0, 0, 3,
81 0, 15, 14, 0, 2, 0, 0, 0, 0, 0,
82 0, 0, 18, 0, 6, 0, 0, 0, 0, 9,
83 10, 11,
85 static const short calc_dgoto[] = { 1,
86 7, 8, 9,
88 static const short calc_sindex[] = { 0,
89 -40, -7, 0, -55, -38, -38, 1, -29, -247, 0,
90 -38, 0, 0, 22, 0, -38, -38, -38, -38, -38,
91 -38, -38, 0, -29, 0, 51, 60, -20, -20, 0,
92 0, 0,
94 static const short calc_rindex[] = { 0,
95 0, 0, 0, 2, 0, 0, 0, 9, -9, 0,
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 10, 0, -6, 14, 5, 13, 0,
98 0, 0,
100 static const short calc_gindex[] = { 0,
101 0, 65, 0,
103 #define YYTABLESIZE 220
104 static const short calc_table[] = { 6,
105 16, 6, 10, 13, 5, 11, 5, 22, 17, 23,
106 15, 15, 20, 18, 7, 19, 22, 21, 4, 5,
107 0, 20, 8, 12, 0, 0, 21, 16, 16, 0,
108 0, 16, 16, 16, 13, 16, 0, 16, 15, 15,
109 0, 0, 7, 15, 15, 7, 15, 7, 15, 7,
110 8, 12, 0, 8, 12, 8, 0, 8, 22, 17,
111 0, 0, 25, 20, 18, 0, 19, 0, 21, 13,
112 14, 0, 0, 0, 0, 24, 0, 0, 0, 0,
113 26, 27, 28, 29, 30, 31, 32, 22, 17, 0,
114 0, 0, 20, 18, 16, 19, 22, 21, 0, 0,
115 0, 20, 18, 0, 19, 0, 21, 0, 0, 0,
116 0, 0, 0, 0, 16, 0, 0, 13, 0, 0,
117 0, 0, 0, 0, 0, 15, 0, 0, 7, 0,
118 0, 0, 0, 0, 0, 0, 8, 12, 0, 0,
119 0, 0, 0, 0, 0, 16, 0, 0, 0, 0,
120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126 0, 0, 0, 0, 0, 2, 3, 4, 3, 12,
128 static const short calc_check[] = { 40,
129 10, 40, 10, 10, 45, 61, 45, 37, 38, 257,
130 10, 10, 42, 43, 10, 45, 37, 47, 10, 10,
131 -1, 42, 10, 10, -1, -1, 47, 37, 38, -1,
132 -1, 41, 42, 43, 41, 45, -1, 47, 37, 38,
133 -1, -1, 38, 42, 43, 41, 45, 43, 47, 45,
134 38, 38, -1, 41, 41, 43, -1, 45, 37, 38,
135 -1, -1, 41, 42, 43, -1, 45, -1, 47, 5,
136 6, -1, -1, -1, -1, 11, -1, -1, -1, -1,
137 16, 17, 18, 19, 20, 21, 22, 37, 38, -1,
138 -1, -1, 42, 43, 124, 45, 37, 47, -1, -1,
139 -1, 42, 43, -1, 45, -1, 47, -1, -1, -1,
140 -1, -1, -1, -1, 124, -1, -1, 124, -1, -1,
141 -1, -1, -1, -1, -1, 124, -1, -1, 124, -1,
142 -1, -1, -1, -1, -1, -1, 124, 124, -1, -1,
143 -1, -1, -1, -1, -1, 124, -1, -1, -1, -1,
144 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
145 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
146 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
147 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
148 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150 -1, -1, -1, -1, -1, 256, 257, 258, 257, 258,
152 #define YYFINAL 1
153 #ifndef YYDEBUG
154 #define YYDEBUG 0
155 #endif
156 #define YYMAXTOKEN 259
157 #if YYDEBUG
158 static const char *calc_name[] = {
160 "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,
161 0,0,0,0,0,0,"'%'","'&'",0,"'('","')'","'*'","'+'",0,"'-'",0,"'/'",0,0,0,0,0,0,0,
162 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,
163 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,
164 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,
165 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,
166 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,
167 0,0,0,0,0,0,"DIGIT","LETTER","UMINUS",
169 static const char *calc_rule[] = {
170 "$accept : list",
171 "list :",
172 "list : list stat '\\n'",
173 "list : list error '\\n'",
174 "stat : expr",
175 "stat : LETTER '=' expr",
176 "expr : '(' expr ')'",
177 "expr : expr '+' expr",
178 "expr : expr '-' expr",
179 "expr : expr '*' expr",
180 "expr : expr '/' expr",
181 "expr : expr '%' expr",
182 "expr : expr '&' expr",
183 "expr : expr '|' expr",
184 "expr : '-' expr",
185 "expr : LETTER",
186 "expr : number",
187 "number : DIGIT",
188 "number : number DIGIT",
191 #endif
192 #ifndef YYSTYPE
193 typedef int YYSTYPE;
194 #endif
195 #if YYDEBUG
196 #include <stdio.h>
197 #endif
199 /* define the initial stack-sizes */
200 #ifdef YYSTACKSIZE
201 #undef YYMAXDEPTH
202 #define YYMAXDEPTH YYSTACKSIZE
203 #else
204 #ifdef YYMAXDEPTH
205 #define YYSTACKSIZE YYMAXDEPTH
206 #else
207 #define YYSTACKSIZE 500
208 #define YYMAXDEPTH 500
209 #endif
210 #endif
212 #define YYINITSTACKSIZE 500
214 int yydebug;
215 int yynerrs;
216 int yyerrflag;
217 int yychar;
218 short *yyssp;
219 YYSTYPE *yyvsp;
220 YYSTYPE yyval;
221 YYSTYPE yylval;
223 /* variables for the parser stack */
224 static short *yyss;
225 static short *yysslim;
226 static YYSTYPE *yyvs;
227 static unsigned yystacksize;
228 #line 63 "calc.y"
229 /* start of programs */
231 main ()
234 while(!feof(stdin)) {
235 yyparse();
239 yyerror(char *s)
241 fprintf(stderr, "%s\n", s);
245 yylex() { /* lexical analysis routine */
246 /* returns LETTER for a lower case letter, yylval = 0 through 25 */
247 /* return DIGIT for a digit, yylval = 0 through 9 */
248 /* all other characters are returned immediately */
250 int c;
252 while( (c=getchar()) == ' ' ) { /* skip blanks */ }
254 /* c is now nonblank */
256 if( islower( c )) {
257 yylval = c - 'a';
258 return ( LETTER );
260 if( isdigit( c )) {
261 yylval = c - '0';
262 return ( DIGIT );
264 return( c );
266 #line 265 "calc.tab.c"
267 /* allocate initial stack or double stack size, up to YYMAXDEPTH */
268 static int yygrowstack(void)
270 int i;
271 unsigned newsize;
272 short *newss;
273 YYSTYPE *newvs;
275 if ((newsize = yystacksize) == 0)
276 newsize = YYINITSTACKSIZE;
277 else if (newsize >= YYMAXDEPTH)
278 return -1;
279 else if ((newsize *= 2) > YYMAXDEPTH)
280 newsize = YYMAXDEPTH;
282 i = yyssp - yyss;
283 newss = (yyss != 0)
284 ? (short *)realloc(yyss, newsize * sizeof(*newss))
285 : (short *)malloc(newsize * sizeof(*newss));
286 if (newss == 0)
287 return -1;
289 yyss = newss;
290 yyssp = newss + i;
291 newvs = (yyvs != 0)
292 ? (YYSTYPE *)realloc(yyvs, newsize * sizeof(*newvs))
293 : (YYSTYPE *)malloc(newsize * sizeof(*newvs));
294 if (newvs == 0)
295 return -1;
297 yyvs = newvs;
298 yyvsp = newvs + i;
299 yystacksize = newsize;
300 yysslim = yyss + newsize - 1;
301 return 0;
304 #define YYABORT goto yyabort
305 #define YYREJECT goto yyabort
306 #define YYACCEPT goto yyaccept
307 #define YYERROR goto yyerrlab
310 YYPARSE_DECL()
312 int yym, yyn, yystate;
313 #if YYDEBUG
314 const char *yys;
316 if ((yys = getenv("YYDEBUG")) != 0)
318 yyn = *yys;
319 if (yyn >= '0' && yyn <= '9')
320 yydebug = yyn - '0';
322 #endif
324 yynerrs = 0;
325 yyerrflag = 0;
326 yychar = YYEMPTY;
327 yystate = 0;
329 if (yyss == NULL && yygrowstack()) goto yyoverflow;
330 yyssp = yyss;
331 yyvsp = yyvs;
332 yystate = 0;
333 *yyssp = 0;
335 yyloop:
336 if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
337 if (yychar < 0)
339 if ((yychar = yylex()) < 0) yychar = 0;
340 #if YYDEBUG
341 if (yydebug)
343 yys = 0;
344 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
345 if (!yys) yys = "illegal-symbol";
346 printf("%sdebug: state %d, reading %d (%s)\n",
347 YYPREFIX, yystate, yychar, yys);
349 #endif
351 if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
352 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
354 #if YYDEBUG
355 if (yydebug)
356 printf("%sdebug: state %d, shifting to state %d\n",
357 YYPREFIX, yystate, yytable[yyn]);
358 #endif
359 if (yyssp >= yysslim && yygrowstack())
361 goto yyoverflow;
363 yystate = yytable[yyn];
364 *++yyssp = yytable[yyn];
365 *++yyvsp = yylval;
366 yychar = YYEMPTY;
367 if (yyerrflag > 0) --yyerrflag;
368 goto yyloop;
370 if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
371 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
373 yyn = yytable[yyn];
374 goto yyreduce;
376 if (yyerrflag) goto yyinrecovery;
378 yyerror("syntax error");
380 goto yyerrlab;
382 yyerrlab:
383 ++yynerrs;
385 yyinrecovery:
386 if (yyerrflag < 3)
388 yyerrflag = 3;
389 for (;;)
391 if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
392 yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
394 #if YYDEBUG
395 if (yydebug)
396 printf("%sdebug: state %d, error recovery shifting\
397 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
398 #endif
399 if (yyssp >= yysslim && yygrowstack())
401 goto yyoverflow;
403 yystate = yytable[yyn];
404 *++yyssp = yytable[yyn];
405 *++yyvsp = yylval;
406 goto yyloop;
408 else
410 #if YYDEBUG
411 if (yydebug)
412 printf("%sdebug: error recovery discarding state %d\n",
413 YYPREFIX, *yyssp);
414 #endif
415 if (yyssp <= yyss) goto yyabort;
416 --yyssp;
417 --yyvsp;
421 else
423 if (yychar == 0) goto yyabort;
424 #if YYDEBUG
425 if (yydebug)
427 yys = 0;
428 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
429 if (!yys) yys = "illegal-symbol";
430 printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
431 YYPREFIX, yystate, yychar, yys);
433 #endif
434 yychar = YYEMPTY;
435 goto yyloop;
438 yyreduce:
439 #if YYDEBUG
440 if (yydebug)
441 printf("%sdebug: state %d, reducing by rule %d (%s)\n",
442 YYPREFIX, yystate, yyn, yyrule[yyn]);
443 #endif
444 yym = yylen[yyn];
445 if (yym)
446 yyval = yyvsp[1-yym];
447 else
448 memset(&yyval, 0, sizeof yyval);
449 switch (yyn)
451 case 3:
452 #line 25 "calc.y"
453 { yyerrok ; }
454 break;
455 case 4:
456 #line 29 "calc.y"
457 { printf("%d\n",yyvsp[0]);}
458 break;
459 case 5:
460 #line 31 "calc.y"
461 { regs[yyvsp[-2]] = yyvsp[0]; }
462 break;
463 case 6:
464 #line 35 "calc.y"
465 { yyval = yyvsp[-1]; }
466 break;
467 case 7:
468 #line 37 "calc.y"
469 { yyval = yyvsp[-2] + yyvsp[0]; }
470 break;
471 case 8:
472 #line 39 "calc.y"
473 { yyval = yyvsp[-2] - yyvsp[0]; }
474 break;
475 case 9:
476 #line 41 "calc.y"
477 { yyval = yyvsp[-2] * yyvsp[0]; }
478 break;
479 case 10:
480 #line 43 "calc.y"
481 { yyval = yyvsp[-2] / yyvsp[0]; }
482 break;
483 case 11:
484 #line 45 "calc.y"
485 { yyval = yyvsp[-2] % yyvsp[0]; }
486 break;
487 case 12:
488 #line 47 "calc.y"
489 { yyval = yyvsp[-2] & yyvsp[0]; }
490 break;
491 case 13:
492 #line 49 "calc.y"
493 { yyval = yyvsp[-2] | yyvsp[0]; }
494 break;
495 case 14:
496 #line 51 "calc.y"
497 { yyval = - yyvsp[0]; }
498 break;
499 case 15:
500 #line 53 "calc.y"
501 { yyval = regs[yyvsp[0]]; }
502 break;
503 case 17:
504 #line 58 "calc.y"
505 { yyval = yyvsp[0]; base = (yyvsp[0]==0) ? 8 : 10; }
506 break;
507 case 18:
508 #line 60 "calc.y"
509 { yyval = base * yyvsp[-1] + yyvsp[0]; }
510 break;
511 #line 510 "calc.tab.c"
513 yyssp -= yym;
514 yystate = *yyssp;
515 yyvsp -= yym;
516 yym = yylhs[yyn];
517 if (yystate == 0 && yym == 0)
519 #if YYDEBUG
520 if (yydebug)
521 printf("%sdebug: after reduction, shifting from state 0 to\
522 state %d\n", YYPREFIX, YYFINAL);
523 #endif
524 yystate = YYFINAL;
525 *++yyssp = YYFINAL;
526 *++yyvsp = yyval;
527 if (yychar < 0)
529 if ((yychar = yylex()) < 0) yychar = 0;
530 #if YYDEBUG
531 if (yydebug)
533 yys = 0;
534 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
535 if (!yys) yys = "illegal-symbol";
536 printf("%sdebug: state %d, reading %d (%s)\n",
537 YYPREFIX, YYFINAL, yychar, yys);
539 #endif
541 if (yychar == 0) goto yyaccept;
542 goto yyloop;
544 if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
545 yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
546 yystate = yytable[yyn];
547 else
548 yystate = yydgoto[yym];
549 #if YYDEBUG
550 if (yydebug)
551 printf("%sdebug: after reduction, shifting from state %d \
552 to state %d\n", YYPREFIX, *yyssp, yystate);
553 #endif
554 if (yyssp >= yysslim && yygrowstack())
556 goto yyoverflow;
558 *++yyssp = (short) yystate;
559 *++yyvsp = yyval;
560 goto yyloop;
562 yyoverflow:
563 yyerror("yacc stack overflow");
565 yyabort:
566 return (1);
568 yyaccept:
569 return (0);