OSDN Git Service

Fix ancient declaration inconsistency in cube & seg ... came across a
[pg-rex/syncrep.git] / contrib / seg / segparse.y
1 %{
2 #define YYPARSE_PARAM result  /* need this to pass a pointer (void *) to yyparse */
3   
4 #include "postgres.h"
5
6 #include <math.h>
7
8 #include "segdata.h"
9
10 #undef yylex                  /* falure to redefine yylex will result in calling the */
11 #define yylex seg_yylex       /* wrong scanner when running inside postgres backend  */
12
13   extern int yylex(void);     /* defined as seg_yylex in segscan.l */
14   extern int significant_digits( char *str );    /* defined in seg.c */
15   
16   void seg_yyerror(const char *message);
17   int seg_yyparse( void *result );
18
19   float seg_atof( char *value );
20
21   long threshold;
22   char strbuf[25] = {
23     '0', '0', '0', '0', '0',
24     '0', '0', '0', '0', '0',
25     '0', '0', '0', '0', '0',
26     '0', '0', '0', '0', '0',
27     '0', '0', '0', '0', '\0'
28   };
29
30 %}
31
32 /* BISON Declarations */
33 %union {
34   struct BND {
35     float val;
36     char  ext;
37     char  sigd;
38   } bnd;
39   char * text;
40 }
41 %token <text> FLOAT
42 %token <text> RANGE
43 %token <text> PLUMIN
44 %token <text> EXTENSION
45 %type  <bnd>  boundary
46 %type  <bnd>  deviation
47 %start range
48
49 /* Grammar follows */
50 %%
51
52
53 range:
54           boundary PLUMIN deviation {
55             ((SEG *)result)->lower = $1.val - $3.val;
56             ((SEG *)result)->upper = $1.val + $3.val;
57             sprintf(strbuf, "%g", ((SEG *)result)->lower);
58             ((SEG *)result)->l_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
59             sprintf(strbuf, "%g", ((SEG *)result)->upper);
60             ((SEG *)result)->u_sigd = Max(Min(6, significant_digits(strbuf)), Max($1.sigd, $3.sigd));
61             ((SEG *)result)->l_ext = '\0';
62             ((SEG *)result)->u_ext = '\0';
63           }
64       |
65           boundary RANGE boundary {
66             ((SEG *)result)->lower = $1.val;
67             ((SEG *)result)->upper = $3.val;
68             if ( ((SEG *)result)->lower > ((SEG *)result)->upper ) {
69               ereport(ERROR,
70                                   (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
71                                    errmsg("swapped boundaries: %g is greater than %g",
72                                                   ((SEG *)result)->lower, ((SEG *)result)->upper)));
73
74               YYERROR;
75             }
76             ((SEG *)result)->l_sigd = $1.sigd;
77             ((SEG *)result)->u_sigd = $3.sigd;
78             ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
79             ((SEG *)result)->u_ext = ( $3.ext ? $3.ext : '\0' );
80           }
81       |
82           boundary RANGE {
83             ((SEG *)result)->lower = $1.val;
84             ((SEG *)result)->upper = HUGE_VAL;
85             ((SEG *)result)->l_sigd = $1.sigd;
86             ((SEG *)result)->u_sigd = 0;
87             ((SEG *)result)->l_ext = ( $1.ext ? $1.ext : '\0' );
88             ((SEG *)result)->u_ext = '-';
89           }
90       |
91           RANGE boundary {
92             ((SEG *)result)->lower = -HUGE_VAL;
93             ((SEG *)result)->upper = $2.val;
94             ((SEG *)result)->l_sigd = 0;
95             ((SEG *)result)->u_sigd = $2.sigd;
96             ((SEG *)result)->l_ext = '-';
97             ((SEG *)result)->u_ext = ( $2.ext ? $2.ext : '\0' );
98           }
99       |
100           boundary {
101             ((SEG *)result)->lower = ((SEG *)result)->upper = $1.val;
102             ((SEG *)result)->l_sigd = ((SEG *)result)->u_sigd = $1.sigd;
103             ((SEG *)result)->l_ext = ((SEG *)result)->u_ext = ( $1.ext ? $1.ext : '\0' );
104           }
105       ;
106
107 boundary:
108           FLOAT {
109              $$.ext = '\0';
110              $$.sigd = significant_digits($1);
111              $$.val = seg_atof($1);
112           }
113       | 
114           EXTENSION FLOAT {
115              $$.ext = $1[0];
116              $$.sigd = significant_digits($2);
117              $$.val = seg_atof($2);
118           }
119       ;
120
121 deviation:
122           FLOAT {
123              $$.ext = '\0';
124              $$.sigd = significant_digits($1);
125              $$.val = seg_atof($1);
126           }
127       ;
128
129 %%
130
131
132 float seg_atof ( char *value ) {
133   float result;
134   char *buf = (char *) palloc(256);
135
136   errno = 0;
137   sscanf(value, "%f", &result);
138
139   if ( errno ) {
140     snprintf(buf, 256, "numeric value %s unrepresentable", value);
141     ereport(ERROR,
142                     (errcode(ERRCODE_SYNTAX_ERROR),
143                      errmsg("syntax error"),
144                      errdetail("%s", buf)));
145   }
146
147   return result;
148 }
149
150
151 #include "segscan.c"