1 /* $NetBSD: varsyntax_calc1.y,v 1.1.1.1 2015/01/03 22:58:23 christos Exp $ */
3 %IDENT
"check variant syntax features"
6 // http://dinosaur.compilertools.net/yacc/index.html */
13 typedef
struct 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
);
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
51 %
> UMINUS
// precedence for unary minus;
52 // %> is an obsolete synonym for %right
54 \\ // beginning of rules section; \\ is an obsolete synonym for %%
62 (void) printf
("%15.8f\n", $1);
66 (void) printf
("(%15.8f, %15.8f)\n", $1.lo
, $1.hi
);
85 $
<dval
>$
= dreg
[$
<ival
>1]; // $$ & $1 are sufficient here
103 |
'-' dexp %prec UMINUS
117 |
'(' dexp
',' dexp
')'
123 (void) printf
("interval out of order\n");
133 $$.hi
= $1.hi
+ $3.hi
;
134 $$.lo
= $1.lo
+ $3.lo
;
143 $$.hi
= $1.hi
- $3.lo
;
144 $$.lo
= $1.lo
- $3.hi
;
153 $$
= vmul
( $1.lo
, $1.hi
, $3 );
157 $$
= vmul
($1, $1, $3 );
161 if
(dcheck
($3)) YYERROR;
162 $$
= vdiv
( $1.lo
, $1.hi
, $3 );
166 if
(dcheck
( $3 )) YYERROR;
167 $$
= vdiv
($1, $1, $3 );
169 |
'-' vexp %prec UMINUS
180 \\ /* beginning of subroutines section */
182 #define BSZ 50 /* buffer size for floating point numbers */
184 /* lexical analysis */
187 yyerror(const char *s
)
189 fprintf
(stderr
, "%s\n", s
);
197 while
((c
= getchar
()) == ' ')
198 { /* skip over blanks */
203 yylval.ival
= c
- 'A';
208 yylval.ival
= c
- 'a';
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
())
227 return
('.'); /* will cause syntax error */
234 return
('e'); /* will cause syntax error */
243 if
((cp
- buf
) >= BSZ
)
244 printf
("constant too long: truncated\n");
246 ungetc
(c
, stdin
); /* push back last char read */
247 yylval.dval
= atof
(buf
);
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 */
289 vmul
(double a
, double b
, INTERVAL v
)
291 return
(hilo
(a
* v.hi
, a
* v.lo
, b
* v.hi
, b
* v.lo
));
297 if
(v.hi
>= 0.
&& v.lo
<= 0.
)
299 printf
("divisor interval contains 0.\n");
306 vdiv
(double a
, double b
, INTERVAL v
)
308 return
(hilo
(a
/ v.hi
, a
/ v.lo
, b
/ v.hi
, b
/ v.lo
));