2 static const char yysccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93";
10 #define yyclearin (yychar = YYEMPTY)
11 #define yyerrok (yyerrflag = 0)
12 #define YYRECOVERING() (yyerrflag != 0)
16 #define yyparse calc1_parse
20 #define yylex calc1_lex
24 #define yyerror calc1_error
28 #define yychar calc1_char
32 #define yyval calc1_val
36 #define yylval calc1_lval
40 #define yydebug calc1_debug
44 #define yynerrs calc1_nerrs
48 #define yyerrflag calc1_errflag
49 #endif /* yyerrflag */
52 #define yylhs calc1_lhs
56 #define yylen calc1_len
60 #define yydefred calc1_defred
64 #define yydgoto calc1_dgoto
68 #define yysindex calc1_sindex
72 #define yyrindex calc1_rindex
76 #define yygindex calc1_gindex
80 #define yytable calc1_table
84 #define yycheck calc1_check
88 #define yyname calc1_name
92 #define yyrule calc1_rule
94 #define YYPREFIX "calc1_"
100 /* http://dinosaur.compilertools.net/yacc/index.html */
107 typedef struct interval
113 INTERVAL vmul(double, double, INTERVAL);
114 INTERVAL vdiv(double, double, INTERVAL);
116 int dcheck(INTERVAL);
123 #undef YYSTYPE_IS_DECLARED
124 #define YYSTYPE_IS_DECLARED 1
126 #ifndef YYSTYPE_IS_DECLARED
127 #define YYSTYPE_IS_DECLARED 1
134 #endif /* !YYSTYPE_IS_DECLARED */
135 #line 136 "calc1.tab.c"
136 /* compatibility with bison */
138 /* compatibility with FreeBSD */
139 # ifdef YYPARSE_PARAM_TYPE
140 # define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)
142 # define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)
145 # define YYPARSE_DECL() yyparse(void)
148 /* Parameters sent to lex. */
150 # define YYLEX_DECL() yylex(void *YYLEX_PARAM)
151 # define YYLEX yylex(YYLEX_PARAM)
153 # define YYLEX_DECL() yylex(void)
154 # define YYLEX yylex()
157 /* Parameters sent to yyerror. */
158 #define YYERROR_DECL() yyerror(const char *s)
159 #define YYERROR_CALL(msg) yyerror(msg)
161 extern int YYPARSE_DECL();
167 #define YYERRCODE 256
168 static const short calc1_lhs[] = { -1,
169 3, 3, 0, 0, 0, 0, 0, 1, 1, 1,
170 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
171 2, 2, 2, 2, 2, 2, 2, 2,
173 static const short calc1_len[] = { 2,
174 0, 2, 2, 2, 4, 4, 2, 1, 1, 3,
175 3, 3, 3, 2, 3, 1, 5, 1, 3, 3,
176 3, 3, 3, 3, 3, 3, 2, 3,
178 static const short calc1_defred[] = { 0,
179 0, 0, 0, 8, 0, 0, 0, 0, 0, 7,
180 0, 0, 9, 18, 14, 27, 0, 0, 0, 0,
181 0, 0, 3, 0, 0, 0, 0, 4, 0, 0,
182 0, 0, 0, 15, 0, 28, 0, 0, 0, 0,
183 12, 24, 13, 26, 0, 0, 23, 25, 14, 0,
184 0, 0, 0, 0, 5, 6, 0, 0, 0, 12,
187 static const short calc1_dgoto[] = { 7,
190 static const short calc1_sindex[] = { -40,
191 -8, -48, -47, 0, -37, -37, 0, 2, 17, 0,
192 -34, -37, 0, 0, 0, 0, -25, 90, -37, -37,
193 -37, -37, 0, -37, -37, -37, -37, 0, -34, -34,
194 25, 125, 31, 0, -34, 0, -11, 37, -11, 37,
195 0, 0, 0, 0, 37, 37, 0, 0, 0, 111,
196 -34, -34, -34, -34, 0, 0, 118, 69, 69, 0,
199 static const short calc1_rindex[] = { 0,
200 0, 38, 44, 0, 0, 0, 0, 0, 0, 0,
201 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203 0, -9, 0, 0, 0, 0, 51, -3, 56, 61,
204 0, 0, 0, 0, 67, 72, 0, 0, 0, 0,
205 0, 0, 0, 0, 0, 0, 0, 78, 83, 0,
208 static const short calc1_gindex[] = { 0,
211 #define YYTABLESIZE 225
212 static const short calc1_table[] = { 6,
213 16, 10, 6, 8, 5, 30, 20, 5, 15, 17,
214 29, 23, 11, 12, 31, 34, 21, 19, 35, 20,
215 0, 22, 37, 39, 41, 43, 28, 0, 0, 0,
216 21, 16, 49, 50, 55, 22, 0, 20, 57, 20,
217 56, 20, 0, 21, 19, 0, 20, 9, 22, 0,
218 0, 0, 0, 18, 58, 59, 60, 61, 26, 24,
219 10, 25, 0, 27, 0, 11, 53, 51, 0, 52,
220 22, 54, 26, 24, 0, 25, 19, 27, 26, 9,
221 9, 21, 9, 27, 9, 18, 18, 10, 18, 0,
222 18, 10, 11, 10, 10, 10, 11, 0, 11, 11,
223 11, 22, 0, 22, 0, 22, 0, 19, 0, 19,
224 53, 19, 21, 0, 21, 54, 21, 0, 10, 0,
225 10, 0, 10, 11, 0, 11, 0, 11, 16, 18,
226 36, 26, 24, 0, 25, 33, 27, 0, 0, 0,
227 0, 0, 38, 40, 42, 44, 0, 45, 46, 47,
228 48, 34, 53, 51, 0, 52, 0, 54, 62, 53,
229 51, 0, 52, 0, 54, 0, 21, 19, 0, 20,
230 0, 22, 0, 0, 0, 0, 0, 0, 0, 0,
231 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
233 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
234 0, 0, 0, 0, 0, 1, 2, 3, 4, 13,
237 static const short calc1_check[] = { 40,
238 10, 10, 40, 0, 45, 40, 10, 45, 5, 6,
239 45, 10, 61, 61, 11, 41, 42, 43, 44, 45,
240 -1, 47, 19, 20, 21, 22, 10, -1, -1, -1,
241 42, 41, 29, 30, 10, 47, -1, 41, 35, 43,
242 10, 45, -1, 42, 43, -1, 45, 10, 47, -1,
243 -1, -1, -1, 10, 51, 52, 53, 54, 42, 43,
244 10, 45, -1, 47, -1, 10, 42, 43, -1, 45,
245 10, 47, 42, 43, -1, 45, 10, 47, 42, 42,
246 43, 10, 45, 47, 47, 42, 43, 10, 45, -1,
247 47, 41, 10, 43, 44, 45, 41, -1, 43, 44,
248 45, 41, -1, 43, -1, 45, -1, 41, -1, 43,
249 42, 45, 41, -1, 43, 47, 45, -1, 41, -1,
250 43, -1, 45, 41, -1, 43, -1, 45, 5, 6,
251 41, 42, 43, -1, 45, 12, 47, -1, -1, -1,
252 -1, -1, 19, 20, 21, 22, -1, 24, 25, 26,
253 27, 41, 42, 43, -1, 45, -1, 47, 41, 42,
254 43, -1, 45, -1, 47, -1, 42, 43, -1, 45,
255 -1, 47, -1, -1, -1, -1, -1, -1, -1, -1,
256 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
257 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
258 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
259 -1, -1, -1, -1, -1, 256, 257, 258, 259, 257,
260 258, 259, 257, -1, 259,
266 #define YYMAXTOKEN 260
268 static const char *yyname[] = {
270 "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,
271 0,0,0,0,0,0,0,0,0,"'('","')'","'*'","'+'","','","'-'",0,"'/'",0,0,0,0,0,0,0,0,0,
272 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,
273 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,
274 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,
275 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,
276 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,
277 0,0,"DREG","VREG","CONST","UMINUS",
279 static const char *yyrule[] = {
282 "lines : lines line",
285 "line : DREG '=' dexp '\\n'",
286 "line : VREG '=' vexp '\\n'",
287 "line : error '\\n'",
290 "dexp : dexp '+' dexp",
291 "dexp : dexp '-' dexp",
292 "dexp : dexp '*' dexp",
293 "dexp : dexp '/' dexp",
295 "dexp : '(' dexp ')'",
297 "vexp : '(' dexp ',' dexp ')'",
299 "vexp : vexp '+' vexp",
300 "vexp : dexp '+' vexp",
301 "vexp : vexp '-' vexp",
302 "vexp : dexp '-' vexp",
303 "vexp : vexp '*' vexp",
304 "vexp : dexp '*' vexp",
305 "vexp : vexp '/' vexp",
306 "vexp : dexp '/' vexp",
308 "vexp : '(' vexp ')'",
312 /* define the initial stack-sizes */
315 #define YYMAXDEPTH YYSTACKSIZE
318 #define YYSTACKSIZE YYMAXDEPTH
320 #define YYSTACKSIZE 500
321 #define YYMAXDEPTH 500
325 #define YYINITSTACKSIZE 500
343 /* variables for the parser stack */
344 static YYSTACKDATA yystack;
346 /* beginning of subroutines section */
349 extern int YYLEX_DECL();
350 static void YYERROR_DECL();
353 #define BSZ 50 /* buffer size for floating point numbers */
355 /* lexical analysis */
358 yyerror(const char *s)
360 fprintf(stderr, "%s\n", s);
368 while ((c = getchar()) == ' ')
369 { /* skip over blanks */
374 yylval.ival = c - 'A';
379 yylval.ival = c - 'a';
383 if (isdigit(c) || c == '.')
385 /* gobble up digits, points, exponents */
386 char buf[BSZ + 1], *cp = buf;
387 int dot = 0, expr = 0;
389 for (; (cp - buf) < BSZ; ++cp, c = getchar())
398 return ('.'); /* will cause syntax error */
405 return ('e'); /* will cause syntax error */
414 if ((cp - buf) >= BSZ)
415 printf("constant too long: truncated\n");
417 ungetc(c, stdin); /* push back last char read */
418 yylval.dval = atof(buf);
425 hilo(double a, double b, double c, double d)
427 /* returns the smallest interval containing a, b, c, and d */
428 /* used by *, / routines */
460 vmul(double a, double b, INTERVAL v)
462 return (hilo(a * v.hi, a * v.lo, b * v.hi, b * v.lo));
468 if (v.hi >= 0. && v.lo <= 0.)
470 printf("divisor interval contains 0.\n");
477 vdiv(double a, double b, INTERVAL v)
479 return (hilo(a / v.hi, a / v.lo, b / v.hi, b / v.lo));
481 #line 482 "calc1.tab.c"
484 #include <stdio.h> /* needed for printf */
487 #include <stdlib.h> /* needed for malloc, etc */
488 #include <string.h> /* needed for memset */
490 /* allocate initial stack or double stack size, up to YYMAXDEPTH */
491 static int yygrowstack(YYSTACKDATA *data)
498 if ((newsize = data->stacksize) == 0)
499 newsize = YYINITSTACKSIZE;
500 else if (newsize >= YYMAXDEPTH)
502 else if ((newsize *= 2) > YYMAXDEPTH)
503 newsize = YYMAXDEPTH;
505 i = data->s_mark - data->s_base;
506 newss = (short *)realloc(data->s_base, newsize * sizeof(*newss));
510 data->s_base = newss;
511 data->s_mark = newss + i;
513 newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs));
517 data->l_base = newvs;
518 data->l_mark = newvs + i;
520 data->stacksize = newsize;
521 data->s_last = data->s_base + newsize - 1;
525 #if YYPURE || defined(YY_NO_LEAKS)
526 static void yyfreestack(YYSTACKDATA *data)
530 memset(data, 0, sizeof(*data));
533 #define yyfreestack(data) /* nothing */
536 #define YYABORT goto yyabort
537 #define YYREJECT goto yyabort
538 #define YYACCEPT goto yyaccept
539 #define YYERROR goto yyerrlab
544 int yym, yyn, yystate;
548 if ((yys = getenv("YYDEBUG")) != 0)
551 if (yyn >= '0' && yyn <= '9')
562 memset(&yystack, 0, sizeof(yystack));
565 if (yystack.s_base == NULL && yygrowstack(&yystack)) goto yyoverflow;
566 yystack.s_mark = yystack.s_base;
567 yystack.l_mark = yystack.l_base;
572 if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
575 if ((yychar = YYLEX) < 0) yychar = 0;
580 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
581 if (!yys) yys = "illegal-symbol";
582 printf("%sdebug: state %d, reading %d (%s)\n",
583 YYPREFIX, yystate, yychar, yys);
587 if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
588 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
592 printf("%sdebug: state %d, shifting to state %d\n",
593 YYPREFIX, yystate, yytable[yyn]);
595 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
599 yystate = yytable[yyn];
600 *++yystack.s_mark = yytable[yyn];
601 *++yystack.l_mark = yylval;
603 if (yyerrflag > 0) --yyerrflag;
606 if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
607 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
612 if (yyerrflag) goto yyinrecovery;
614 yyerror("syntax error");
627 if ((yyn = yysindex[*yystack.s_mark]) && (yyn += YYERRCODE) >= 0 &&
628 yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
632 printf("%sdebug: state %d, error recovery shifting\
633 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]);
635 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
639 yystate = yytable[yyn];
640 *++yystack.s_mark = yytable[yyn];
641 *++yystack.l_mark = yylval;
648 printf("%sdebug: error recovery discarding state %d\n",
649 YYPREFIX, *yystack.s_mark);
651 if (yystack.s_mark <= yystack.s_base) goto yyabort;
659 if (yychar == 0) goto yyabort;
664 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
665 if (!yys) yys = "illegal-symbol";
666 printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
667 YYPREFIX, yystate, yychar, yys);
677 printf("%sdebug: state %d, reducing by rule %d (%s)\n",
678 YYPREFIX, yystate, yyn, yyrule[yyn]);
682 yyval = yystack.l_mark[1-yym];
684 memset(&yyval, 0, sizeof yyval);
690 (void) printf("%15.8f\n", yystack.l_mark[-1].dval);
696 (void) printf("(%15.8f, %15.8f)\n", yystack.l_mark[-1].vval.lo, yystack.l_mark[-1].vval.hi);
702 dreg[yystack.l_mark[-3].ival] = yystack.l_mark[-1].dval;
708 vreg[yystack.l_mark[-3].ival] = yystack.l_mark[-1].vval;
720 yyval.dval = dreg[yystack.l_mark[0].ival];
726 yyval.dval = yystack.l_mark[-2].dval + yystack.l_mark[0].dval;
732 yyval.dval = yystack.l_mark[-2].dval - yystack.l_mark[0].dval;
738 yyval.dval = yystack.l_mark[-2].dval * yystack.l_mark[0].dval;
744 yyval.dval = yystack.l_mark[-2].dval / yystack.l_mark[0].dval;
750 yyval.dval = -yystack.l_mark[0].dval;
756 yyval.dval = yystack.l_mark[-1].dval;
762 yyval.vval.hi = yyval.vval.lo = yystack.l_mark[0].dval;
768 yyval.vval.lo = yystack.l_mark[-3].dval;
769 yyval.vval.hi = yystack.l_mark[-1].dval;
770 if ( yyval.vval.lo > yyval.vval.hi )
772 (void) printf("interval out of order\n");
780 yyval.vval = vreg[yystack.l_mark[0].ival];
786 yyval.vval.hi = yystack.l_mark[-2].vval.hi + yystack.l_mark[0].vval.hi;
787 yyval.vval.lo = yystack.l_mark[-2].vval.lo + yystack.l_mark[0].vval.lo;
793 yyval.vval.hi = yystack.l_mark[-2].dval + yystack.l_mark[0].vval.hi;
794 yyval.vval.lo = yystack.l_mark[-2].dval + yystack.l_mark[0].vval.lo;
800 yyval.vval.hi = yystack.l_mark[-2].vval.hi - yystack.l_mark[0].vval.lo;
801 yyval.vval.lo = yystack.l_mark[-2].vval.lo - yystack.l_mark[0].vval.hi;
807 yyval.vval.hi = yystack.l_mark[-2].dval - yystack.l_mark[0].vval.lo;
808 yyval.vval.lo = yystack.l_mark[-2].dval - yystack.l_mark[0].vval.hi;
814 yyval.vval = vmul( yystack.l_mark[-2].vval.lo, yystack.l_mark[-2].vval.hi, yystack.l_mark[0].vval );
820 yyval.vval = vmul (yystack.l_mark[-2].dval, yystack.l_mark[-2].dval, yystack.l_mark[0].vval );
826 if (dcheck(yystack.l_mark[0].vval)) YYERROR;
827 yyval.vval = vdiv ( yystack.l_mark[-2].vval.lo, yystack.l_mark[-2].vval.hi, yystack.l_mark[0].vval );
833 if (dcheck ( yystack.l_mark[0].vval )) YYERROR;
834 yyval.vval = vdiv (yystack.l_mark[-2].dval, yystack.l_mark[-2].dval, yystack.l_mark[0].vval );
840 yyval.vval.hi = -yystack.l_mark[0].vval.lo;
841 yyval.vval.lo = -yystack.l_mark[0].vval.hi;
847 yyval.vval = yystack.l_mark[-1].vval;
850 #line 851 "calc1.tab.c"
852 yystack.s_mark -= yym;
853 yystate = *yystack.s_mark;
854 yystack.l_mark -= yym;
856 if (yystate == 0 && yym == 0)
860 printf("%sdebug: after reduction, shifting from state 0 to\
861 state %d\n", YYPREFIX, YYFINAL);
864 *++yystack.s_mark = YYFINAL;
865 *++yystack.l_mark = yyval;
868 if ((yychar = YYLEX) < 0) yychar = 0;
873 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
874 if (!yys) yys = "illegal-symbol";
875 printf("%sdebug: state %d, reading %d (%s)\n",
876 YYPREFIX, YYFINAL, yychar, yys);
880 if (yychar == 0) goto yyaccept;
883 if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
884 yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
885 yystate = yytable[yyn];
887 yystate = yydgoto[yym];
890 printf("%sdebug: after reduction, shifting from state %d \
891 to state %d\n", YYPREFIX, *yystack.s_mark, yystate);
893 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
897 *++yystack.s_mark = (short) yystate;
898 *++yystack.l_mark = yyval;
902 yyerror("yacc stack overflow");
905 yyfreestack(&yystack);
909 yyfreestack(&yystack);