003 File Manager
Current Path:
/usr/src/contrib/byacc/test
usr
/
src
/
contrib
/
byacc
/
test
/
📁
..
📄
README
(142 B)
📁
btyacc
📄
btyacc_calc1.y
(4.42 KB)
📄
btyacc_demo.y
(6.55 KB)
📄
btyacc_destroy1.y
(1.56 KB)
📄
btyacc_destroy2.y
(1.56 KB)
📄
btyacc_destroy3.y
(1.56 KB)
📄
calc.y
(1.84 KB)
📄
calc1.y
(4.22 KB)
📄
calc2.y
(2.31 KB)
📄
calc3.y
(2.37 KB)
📄
calc_code_all.y
(2.07 KB)
📄
calc_code_default.y
(1.92 KB)
📄
calc_code_imports.y
(1.92 KB)
📄
calc_code_provides.y
(1.92 KB)
📄
calc_code_requires.y
(1.92 KB)
📄
calc_code_top.y
(1.91 KB)
📄
code_calc.y
(1.92 KB)
📄
code_debug.y
(341 B)
📄
code_error.y
(341 B)
📄
empty.y
(290 B)
📄
err_inherit1.y
(1.09 KB)
📄
err_inherit2.y
(1.39 KB)
📄
err_inherit3.y
(1.38 KB)
📄
err_inherit4.y
(1.41 KB)
📄
err_inherit5.y
(1.37 KB)
📄
err_syntax1.y
(272 B)
📄
err_syntax10.y
(323 B)
📄
err_syntax11.y
(292 B)
📄
err_syntax12.y
(303 B)
📄
err_syntax13.y
(299 B)
📄
err_syntax14.y
(296 B)
📄
err_syntax15.y
(286 B)
📄
err_syntax16.y
(330 B)
📄
err_syntax17.y
(273 B)
📄
err_syntax18.y
(319 B)
📄
err_syntax19.y
(324 B)
📄
err_syntax2.y
(272 B)
📄
err_syntax20.y
(347 B)
📄
err_syntax21.y
(355 B)
📄
err_syntax22.y
(410 B)
📄
err_syntax23.y
(418 B)
📄
err_syntax24.y
(415 B)
📄
err_syntax25.y
(454 B)
📄
err_syntax26.y
(71 B)
📄
err_syntax27.y
(2.36 KB)
📄
err_syntax3.y
(296 B)
📄
err_syntax4.y
(268 B)
📄
err_syntax5.y
(301 B)
📄
err_syntax6.y
(296 B)
📄
err_syntax7.y
(292 B)
📄
err_syntax7a.y
(293 B)
📄
err_syntax7b.y
(291 B)
📄
err_syntax8.y
(287 B)
📄
err_syntax8a.y
(291 B)
📄
err_syntax9.y
(307 B)
📄
error.y
(269 B)
📄
expr.oxout.y
(29.75 KB)
📄
grammar.y
(26.98 KB)
📄
inherit0.y
(797 B)
📄
inherit1.y
(1.38 KB)
📄
inherit2.y
(1.37 KB)
📄
ok_syntax1.y
(2.92 KB)
📄
pure_calc.y
(2.03 KB)
📄
pure_error.y
(496 B)
📄
quote_calc.y
(1.97 KB)
📄
quote_calc2.y
(1.97 KB)
📄
quote_calc3.y
(2.03 KB)
📄
quote_calc4.y
(2.08 KB)
📄
run_lint.sh
(509 B)
📄
run_make.sh
(3.58 KB)
📄
run_test.sh
(5.9 KB)
📄
varsyntax_calc1.y
(4.48 KB)
📁
yacc
Editing: calc1.y
%{ /* http://dinosaur.compilertools.net/yacc/index.html */ #include <stdlib.h> #include <stdio.h> #include <ctype.h> #include <math.h> typedef struct interval { double lo, hi; } INTERVAL; INTERVAL vmul(double, double, INTERVAL); INTERVAL vdiv(double, double, INTERVAL); extern int yylex(void); static void yyerror(const char *s); int dcheck(INTERVAL); double dreg[26]; INTERVAL vreg[26]; %} %expect 18 %start line %union { int ival; double dval; INTERVAL vval; } %token <ival> DREG VREG /* indices into dreg, vreg arrays */ %token <dval> CONST /* floating point constant */ %type <dval> dexp /* expression */ %type <vval> vexp /* interval expression */ /* precedence information about the operators */ %left '+' '-' %left '*' '/' %left UMINUS /* precedence for unary minus */ %% /* beginning of rules section */ lines : /* empty */ | lines line ; line : dexp '\n' { (void) printf("%15.8f\n", $1); } | vexp '\n' { (void) printf("(%15.8f, %15.8f)\n", $1.lo, $1.hi); } | DREG '=' dexp '\n' { dreg[$1] = $3; } | VREG '=' vexp '\n' { vreg[$1] = $3; } | error '\n' { yyerrok; } ; dexp : CONST | DREG { $$ = dreg[$1]; } | dexp '+' dexp { $$ = $1 + $3; } | dexp '-' dexp { $$ = $1 - $3; } | dexp '*' dexp { $$ = $1 * $3; } | dexp '/' dexp { $$ = $1 / $3; } | '-' dexp %prec UMINUS { $$ = -$2; } | '(' dexp ')' { $$ = $2; } ; vexp : dexp { $$.hi = $$.lo = $1; } | '(' dexp ',' dexp ')' { $$.lo = $2; $$.hi = $4; if ( $$.lo > $$.hi ) { (void) printf("interval out of order\n"); YYERROR; } } | VREG { $$ = vreg[$1]; } | vexp '+' vexp { $$.hi = $1.hi + $3.hi; $$.lo = $1.lo + $3.lo; } | dexp '+' vexp { $$.hi = $1 + $3.hi; $$.lo = $1 + $3.lo; } | vexp '-' vexp { $$.hi = $1.hi - $3.lo; $$.lo = $1.lo - $3.hi; } | dexp '-' vexp { $$.hi = $1 - $3.lo; $$.lo = $1 - $3.hi; } | vexp '*' vexp { $$ = vmul( $1.lo, $1.hi, $3 ); } | dexp '*' vexp { $$ = vmul ($1, $1, $3 ); } | vexp '/' vexp { if (dcheck($3)) YYERROR; $$ = vdiv ( $1.lo, $1.hi, $3 ); } | dexp '/' vexp { if (dcheck ( $3 )) YYERROR; $$ = vdiv ($1, $1, $3 ); } | '-' vexp %prec UMINUS { $$.hi = -$2.lo; $$.lo = -$2.hi; } | '(' vexp ')' { $$ = $2; } ; %% /* beginning of subroutines section */ #define BSZ 50 /* buffer size for floating point numbers */ /* lexical analysis */ static void yyerror(const char *s) { fprintf(stderr, "%s\n", s); } int yylex(void) { int c; while ((c = getchar()) == ' ') { /* skip over blanks */ } if (isupper(c)) { yylval.ival = c - 'A'; return (VREG); } if (islower(c)) { yylval.ival = c - 'a'; return (DREG); } if (isdigit(c) || c == '.') { /* gobble up digits, points, exponents */ char buf[BSZ + 1], *cp = buf; int dot = 0, expr = 0; for (; (cp - buf) < BSZ; ++cp, c = getchar()) { *cp = (char) c; if (isdigit(c)) continue; if (c == '.') { if (dot++ || expr) return ('.'); /* will cause syntax error */ continue; } if (c == 'e') { if (expr++) return ('e'); /* will cause syntax error */ continue; } /* end of number */ break; } *cp = '\0'; if ((cp - buf) >= BSZ) printf("constant too long: truncated\n"); else ungetc(c, stdin); /* push back last char read */ yylval.dval = atof(buf); return (CONST); } return (c); } static INTERVAL hilo(double a, double b, double c, double d) { /* returns the smallest interval containing a, b, c, and d */ /* used by *, / routines */ INTERVAL v; if (a > b) { v.hi = a; v.lo = b; } else { v.hi = b; v.lo = a; } if (c > d) { if (c > v.hi) v.hi = c; if (d < v.lo) v.lo = d; } else { if (d > v.hi) v.hi = d; if (c < v.lo) v.lo = c; } return (v); } INTERVAL vmul(double a, double b, INTERVAL v) { return (hilo(a * v.hi, a * v.lo, b * v.hi, b * v.lo)); } int dcheck(INTERVAL v) { if (v.hi >= 0. && v.lo <= 0.) { printf("divisor interval contains 0.\n"); return (1); } return (0); } INTERVAL vdiv(double a, double b, INTERVAL v) { return (hilo(a / v.hi, a / v.lo, b / v.hi, b / v.lo)); }
Upload File
Create Folder