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);
115 int dcheck(INTERVAL);
122 #undef YYSTYPE_IS_DECLARED
123 #define YYSTYPE_IS_DECLARED 1
125 #ifndef YYSTYPE_IS_DECLARED
126 #define YYSTYPE_IS_DECLARED 1
133 #endif /* !YYSTYPE_IS_DECLARED */
134 #line 135 "calc1.tab.c"
135 /* compatibility with bison */
137 /* compatibility with FreeBSD */
138 # ifdef YYPARSE_PARAM_TYPE
139 # define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)
141 # define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)
144 # define YYPARSE_DECL() yyparse(void)
147 /* Parameters sent to lex. */
149 # define YYLEX_DECL() yylex(void *YYLEX_PARAM)
150 # define YYLEX yylex(YYLEX_PARAM)
152 # define YYLEX_DECL() yylex(void)
153 # define YYLEX yylex()
156 /* Parameters sent to yyerror. */
157 #define YYERROR_DECL() yyerror(const char *s)
158 #define YYERROR_CALL(msg) yyerror(msg)
160 extern int YYPARSE_DECL();
161 extern int YYLEX_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 */
348 #define BSZ 50 /* buffer size for floating point numbers */
350 /* lexical analysis */
353 yyerror(const char *s)
355 fprintf(stderr, "%s\n", s);
363 while ((c = getchar()) == ' ')
364 { /* skip over blanks */
369 yylval.ival = c - 'A';
374 yylval.ival = c - 'a';
378 if (isdigit(c) || c == '.')
380 /* gobble up digits, points, exponents */
381 char buf[BSZ + 1], *cp = buf;
382 int dot = 0, expr = 0;
384 for (; (cp - buf) < BSZ; ++cp, c = getchar())
393 return ('.'); /* will cause syntax error */
400 return ('e'); /* will cause syntax error */
409 if ((cp - buf) >= BSZ)
410 printf("constant too long: truncated\n");
412 ungetc(c, stdin); /* push back last char read */
413 yylval.dval = atof(buf);
420 hilo(double a, double b, double c, double d)
422 /* returns the smallest interval containing a, b, c, and d */
423 /* used by *, / routines */
455 vmul(double a, double b, INTERVAL v)
457 return (hilo(a * v.hi, a * v.lo, b * v.hi, b * v.lo));
463 if (v.hi >= 0. && v.lo <= 0.)
465 printf("divisor interval contains 0.\n");
472 vdiv(double a, double b, INTERVAL v)
474 return (hilo(a / v.hi, a / v.lo, b / v.hi, b / v.lo));
476 #line 477 "calc1.tab.c"
479 #include <stdio.h> /* needed for printf */
482 #include <stdlib.h> /* needed for malloc, etc */
483 #include <string.h> /* needed for memset */
485 /* allocate initial stack or double stack size, up to YYMAXDEPTH */
486 static int yygrowstack(YYSTACKDATA *data)
493 if ((newsize = data->stacksize) == 0)
494 newsize = YYINITSTACKSIZE;
495 else if (newsize >= YYMAXDEPTH)
497 else if ((newsize *= 2) > YYMAXDEPTH)
498 newsize = YYMAXDEPTH;
500 i = data->s_mark - data->s_base;
501 newss = (data->s_base != 0)
502 ? (short *)realloc(data->s_base, newsize * sizeof(*newss))
503 : (short *)malloc(newsize * sizeof(*newss));
507 data->s_base = newss;
508 data->s_mark = newss + i;
510 newvs = (data->l_base != 0)
511 ? (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs))
512 : (YYSTYPE *)malloc(newsize * sizeof(*newvs));
516 data->l_base = newvs;
517 data->l_mark = newvs + i;
519 data->stacksize = newsize;
520 data->s_last = data->s_base + newsize - 1;
524 #if YYPURE || defined(YY_NO_LEAKS)
525 static void yyfreestack(YYSTACKDATA *data)
529 memset(data, 0, sizeof(*data));
532 #define yyfreestack(data) /* nothing */
535 #define YYABORT goto yyabort
536 #define YYREJECT goto yyabort
537 #define YYACCEPT goto yyaccept
538 #define YYERROR goto yyerrlab
543 int yym, yyn, yystate;
547 if ((yys = getenv("YYDEBUG")) != 0)
550 if (yyn >= '0' && yyn <= '9')
561 memset(&yystack, 0, sizeof(yystack));
564 if (yystack.s_base == NULL && yygrowstack(&yystack)) goto yyoverflow;
565 yystack.s_mark = yystack.s_base;
566 yystack.l_mark = yystack.l_base;
571 if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
574 if ((yychar = YYLEX) < 0) yychar = 0;
579 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
580 if (!yys) yys = "illegal-symbol";
581 printf("%sdebug: state %d, reading %d (%s)\n",
582 YYPREFIX, yystate, yychar, yys);
586 if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
587 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
591 printf("%sdebug: state %d, shifting to state %d\n",
592 YYPREFIX, yystate, yytable[yyn]);
594 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
598 yystate = yytable[yyn];
599 *++yystack.s_mark = yytable[yyn];
600 *++yystack.l_mark = yylval;
602 if (yyerrflag > 0) --yyerrflag;
605 if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
606 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
611 if (yyerrflag) goto yyinrecovery;
613 yyerror("syntax error");
626 if ((yyn = yysindex[*yystack.s_mark]) && (yyn += YYERRCODE) >= 0 &&
627 yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
631 printf("%sdebug: state %d, error recovery shifting\
632 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]);
634 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
638 yystate = yytable[yyn];
639 *++yystack.s_mark = yytable[yyn];
640 *++yystack.l_mark = yylval;
647 printf("%sdebug: error recovery discarding state %d\n",
648 YYPREFIX, *yystack.s_mark);
650 if (yystack.s_mark <= yystack.s_base) goto yyabort;
658 if (yychar == 0) goto yyabort;
663 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
664 if (!yys) yys = "illegal-symbol";
665 printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
666 YYPREFIX, yystate, yychar, yys);
676 printf("%sdebug: state %d, reducing by rule %d (%s)\n",
677 YYPREFIX, yystate, yyn, yyrule[yyn]);
681 yyval = yystack.l_mark[1-yym];
683 memset(&yyval, 0, sizeof yyval);
689 (void) printf("%15.8f\n", yystack.l_mark[-1].dval);
695 (void) printf("(%15.8f, %15.8f)\n", yystack.l_mark[-1].vval.lo, yystack.l_mark[-1].vval.hi);
701 dreg[yystack.l_mark[-3].ival] = yystack.l_mark[-1].dval;
707 vreg[yystack.l_mark[-3].ival] = yystack.l_mark[-1].vval;
719 yyval.dval = dreg[yystack.l_mark[0].ival];
725 yyval.dval = yystack.l_mark[-2].dval + yystack.l_mark[0].dval;
731 yyval.dval = yystack.l_mark[-2].dval - yystack.l_mark[0].dval;
737 yyval.dval = yystack.l_mark[-2].dval * yystack.l_mark[0].dval;
743 yyval.dval = yystack.l_mark[-2].dval / yystack.l_mark[0].dval;
749 yyval.dval = -yystack.l_mark[0].dval;
755 yyval.dval = yystack.l_mark[-1].dval;
761 yyval.vval.hi = yyval.vval.lo = yystack.l_mark[0].dval;
767 yyval.vval.lo = yystack.l_mark[-3].dval;
768 yyval.vval.hi = yystack.l_mark[-1].dval;
769 if ( yyval.vval.lo > yyval.vval.hi )
771 (void) printf("interval out of order\n");
779 yyval.vval = vreg[yystack.l_mark[0].ival];
785 yyval.vval.hi = yystack.l_mark[-2].vval.hi + yystack.l_mark[0].vval.hi;
786 yyval.vval.lo = yystack.l_mark[-2].vval.lo + yystack.l_mark[0].vval.lo;
792 yyval.vval.hi = yystack.l_mark[-2].dval + yystack.l_mark[0].vval.hi;
793 yyval.vval.lo = yystack.l_mark[-2].dval + yystack.l_mark[0].vval.lo;
799 yyval.vval.hi = yystack.l_mark[-2].vval.hi - yystack.l_mark[0].vval.lo;
800 yyval.vval.lo = yystack.l_mark[-2].vval.lo - yystack.l_mark[0].vval.hi;
806 yyval.vval.hi = yystack.l_mark[-2].dval - yystack.l_mark[0].vval.lo;
807 yyval.vval.lo = yystack.l_mark[-2].dval - yystack.l_mark[0].vval.hi;
813 yyval.vval = vmul( yystack.l_mark[-2].vval.lo, yystack.l_mark[-2].vval.hi, yystack.l_mark[0].vval );
819 yyval.vval = vmul (yystack.l_mark[-2].dval, yystack.l_mark[-2].dval, yystack.l_mark[0].vval );
825 if (dcheck(yystack.l_mark[0].vval)) YYERROR;
826 yyval.vval = vdiv ( yystack.l_mark[-2].vval.lo, yystack.l_mark[-2].vval.hi, yystack.l_mark[0].vval );
832 if (dcheck ( yystack.l_mark[0].vval )) YYERROR;
833 yyval.vval = vdiv (yystack.l_mark[-2].dval, yystack.l_mark[-2].dval, yystack.l_mark[0].vval );
839 yyval.vval.hi = -yystack.l_mark[0].vval.lo;
840 yyval.vval.lo = -yystack.l_mark[0].vval.hi;
846 yyval.vval = yystack.l_mark[-1].vval;
849 #line 850 "calc1.tab.c"
851 yystack.s_mark -= yym;
852 yystate = *yystack.s_mark;
853 yystack.l_mark -= yym;
855 if (yystate == 0 && yym == 0)
859 printf("%sdebug: after reduction, shifting from state 0 to\
860 state %d\n", YYPREFIX, YYFINAL);
863 *++yystack.s_mark = YYFINAL;
864 *++yystack.l_mark = yyval;
867 if ((yychar = YYLEX) < 0) yychar = 0;
872 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
873 if (!yys) yys = "illegal-symbol";
874 printf("%sdebug: state %d, reading %d (%s)\n",
875 YYPREFIX, YYFINAL, yychar, yys);
879 if (yychar == 0) goto yyaccept;
882 if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
883 yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
884 yystate = yytable[yyn];
886 yystate = yydgoto[yym];
889 printf("%sdebug: after reduction, shifting from state %d \
890 to state %d\n", YYPREFIX, *yystack.s_mark, yystate);
892 if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
896 *++yystack.s_mark = (short) yystate;
897 *++yystack.l_mark = yyval;
901 yyerror("yacc stack overflow");
904 yyfreestack(&yystack);
908 yyfreestack(&yystack);