Remove building with NOCRYPTO option
[minix3.git] / external / bsd / byacc / dist / test / varsyntax_calc1.y
blob455438cb8767f7a18ccfa47bcb554eb2f435f1f1
1 /* $NetBSD: varsyntax_calc1.y,v 1.1.1.1 2015/01/03 22:58:23 christos Exp $ */
3 %IDENT "check variant syntax features"
4 %{
6 // http://dinosaur.compilertools.net/yacc/index.html */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <ctype.h>
11 #include <math.h>
13 typedef struct interval
15 double lo, hi;
17 INTERVAL;
19 INTERVAL vmul(double, double, INTERVAL);
20 INTERVAL vdiv(double, double, INTERVAL);
22 extern int yylex(void);
23 static void yyerror(const char *s);
25 int dcheck(INTERVAL);
27 double dreg[26];
28 INTERVAL vreg[26];
31 %expect 18
33 %start line
34 %union
36 int ival; // dreg & vreg array index values
37 double dval; // floating point values
38 INTERVAL vval; // interval values
41 %token <ival> DREG VREG // indices into dreg, vreg arrays */
42 %token <dval> CONST // floating point constant */
44 %type <dval> dexp // expression */
45 %type <vval> vexp // interval expression */
47 // precedence information about the operators */
49 %< '+' '-' // %< is an obsolete synonym for %left
50 %< '*' '/'
51 %> UMINUS // precedence for unary minus;
52 // %> is an obsolete synonym for %right
54 \\ // beginning of rules section; \\ is an obsolete synonym for %%
56 lines : // empty */
57 | lines line
60 line : dexp '\n'
62 (void) printf("%15.8f\n", $1);
64 | vexp '\n'
66 (void) printf("(%15.8f, %15.8f)\n", $1.lo, $1.hi);
68 | DREG '=' dexp '\n'
70 dreg[$1] = $3;
72 | VREG '=' vexp '\n'
74 vreg[$1] = $3;
76 | error '\n'
78 yyerrok;
82 dexp : CONST
83 | DREG
85 $<dval>$ = dreg[$<ival>1]; // $$ & $1 are sufficient here
87 | dexp '+' dexp
89 $$ = $1 + $3;
91 | dexp '-' dexp
93 $$ = $1 - $3;
95 | dexp '*' dexp
97 $$ = $1 * $3;
99 | dexp '/' dexp
101 $$ = $1 / $3;
103 | '-' dexp %prec UMINUS
105 $$ = -$2;
107 | '(' dexp ')'
109 $$ = $2;
113 vexp : dexp
115 $$.hi = $$.lo = $1;
117 | '(' dexp ',' dexp ')'
119 $$.lo = $2;
120 $$.hi = $4;
121 if ( $$.lo > $$.hi )
123 (void) printf("interval out of order\n");
124 YYERROR;
127 | VREG
129 $$ = vreg[$1];
131 | vexp '+' vexp
133 $$.hi = $1.hi + $3.hi;
134 $$.lo = $1.lo + $3.lo;
136 | dexp '+' vexp
138 $$.hi = $1 + $3.hi;
139 $$.lo = $1 + $3.lo;
141 | vexp '-' vexp
143 $$.hi = $1.hi - $3.lo;
144 $$.lo = $1.lo - $3.hi;
146 | dexp '-' vexp
148 $$.hi = $1 - $3.lo;
149 $$.lo = $1 - $3.hi;
151 | vexp '*' vexp
153 $$ = vmul( $1.lo, $1.hi, $3 );
155 | dexp '*' vexp
157 $$ = vmul ($1, $1, $3 );
159 | vexp '/' vexp
161 if (dcheck($3)) YYERROR;
162 $$ = vdiv ( $1.lo, $1.hi, $3 );
164 | dexp '/' vexp
166 if (dcheck ( $3 )) YYERROR;
167 $$ = vdiv ($1, $1, $3 );
169 | '-' vexp %prec UMINUS
171 $$.hi = -$2.lo;
172 $$.lo = -$2.hi;
174 | '(' vexp ')'
176 $$ = $2;
180 \\ /* beginning of subroutines section */
182 #define BSZ 50 /* buffer size for floating point numbers */
184 /* lexical analysis */
186 static void
187 yyerror(const char *s)
189 fprintf(stderr, "%s\n", s);
193 yylex(void)
195 int c;
197 while ((c = getchar()) == ' ')
198 { /* skip over blanks */
201 if (isupper(c))
203 yylval.ival = c - 'A';
204 return (VREG);
206 if (islower(c))
208 yylval.ival = c - 'a';
209 return (DREG);
212 if (isdigit(c) || c == '.')
214 /* gobble up digits, points, exponents */
215 char buf[BSZ + 1], *cp = buf;
216 int dot = 0, expr = 0;
218 for (; (cp - buf) < BSZ; ++cp, c = getchar())
221 *cp = (char) c;
222 if (isdigit(c))
223 continue;
224 if (c == '.')
226 if (dot++ || expr)
227 return ('.'); /* will cause syntax error */
228 continue;
231 if (c == 'e')
233 if (expr++)
234 return ('e'); /* will cause syntax error */
235 continue;
238 /* end of number */
239 break;
241 *cp = '\0';
243 if ((cp - buf) >= BSZ)
244 printf("constant too long: truncated\n");
245 else
246 ungetc(c, stdin); /* push back last char read */
247 yylval.dval = atof(buf);
248 return (CONST);
250 return (c);
253 static INTERVAL
254 hilo(double a, double b, double c, double d)
256 /* returns the smallest interval containing a, b, c, and d */
257 /* used by *, / routines */
258 INTERVAL v;
260 if (a > b)
262 v.hi = a;
263 v.lo = b;
265 else
267 v.hi = b;
268 v.lo = a;
271 if (c > d)
273 if (c > v.hi)
274 v.hi = c;
275 if (d < v.lo)
276 v.lo = d;
278 else
280 if (d > v.hi)
281 v.hi = d;
282 if (c < v.lo)
283 v.lo = c;
285 return (v);
288 INTERVAL
289 vmul(double a, double b, INTERVAL v)
291 return (hilo(a * v.hi, a * v.lo, b * v.hi, b * v.lo));
295 dcheck(INTERVAL v)
297 if (v.hi >= 0. && v.lo <= 0.)
299 printf("divisor interval contains 0.\n");
300 return (1);
302 return (0);
305 INTERVAL
306 vdiv(double a, double b, INTERVAL v)
308 return (hilo(a / v.hi, a / v.lo, b / v.hi, b / v.lo));