Added quantiling UDAFs
[com/gs-lite.git] / src / ftacmp / fta.l
1 /* ------------------------------------------------\r
2 Copyright 2014 AT&T Intellectual Property\r
3    Licensed under the Apache License, Version 2.0 (the "License");\r
4    you may not use this file except in compliance with the License.\r
5    You may obtain a copy of the License at\r
6 \r
7      http://www.apache.org/licenses/LICENSE-2.0\r
8 \r
9    Unless required by applicable law or agreed to in writing, software\r
10    distributed under the License is distributed on an "AS IS" BASIS,\r
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12    See the License for the specific language governing permissions and\r
13    limitations under the License.\r
14  ------------------------------------------- */\r
15 \r
16 \r
17 /*\r
18         MUST COMPILE WITH\r
19                 flex -PFtaParser -oftalexer.cc fta.l\r
20         (or equivalent).\r
21 */      \r
22 \r
23 %{\r
24 /*\r
25  * AT&T lex can't handle this lexer due to lex bugs.  It works with flex\r
26  * 2.3.7, pclex 2.0.5, and MKS lex 3.1a.\r
27  */\r
28 \r
29  #include "parse_fta.h"\r
30  #include "parse_schema.h"\r
31  #include <string.h>\r
32 \r
33 \r
34 \r
35 #include "fta.tab.cc.h"\r
36 \r
37 /*\r
38         Some includes that flex doesn't include as standard,\r
39         but which are needed.\r
40 */\r
41 \r
42 #include <stdlib.h>\r
43 #include <string.h>\r
44 \r
45 \r
46 //              Prevent flex from defining yywrap as extern "C" \r
47 \r
48 #define YY_SKIP_YYWRAP\r
49 \r
50 /*              No lex lib, supply the yywrap fcn. that normally resides there\r
51 */\r
52 \r
53 int FtaParserwrap(){return(1);}\r
54 \r
55 extern int FtaParserdebug;\r
56 \r
57 \r
58 /*\r
59                 These variables are used for error reporting:\r
60                 flex_fta_lineno : the line currently being parsed when the error occurs.\r
61                 flex_fta_ch : the character on the line where the error occurs\r
62                 flex_fta_linebuf : store the line for reporting.\r
63 \r
64                 NOTE : 1) the fixed size flex_fta_linebuf buffer is dangerous.\r
65                            2) You might get pointed to a place shortly after\r
66                                   where the syntax error occurs.  It is close enough\r
67                                   for now.\r
68 */\r
69 \r
70 int flex_fta_lineno = 1;\r
71 int flex_fta_ch = 0;\r
72 char flex_fta_linebuf[200000];\r
73 \r
74 char *flex_fta_stringinput = NULL;\r
75 int flex_fta_stringinput_ptr = 0;\r
76 FILE *flex_fta_fileinput = NULL;\r
77 int my_FtaParser_yyinput(char *buf, int max_size);\r
78 \r
79 \r
80 \r
81 void FtaParsererror(char *s){\r
82         int i;\r
83         fprintf(stderr,"On line %d, char %d: %s (token %s):\n%s\n",\r
84                                 flex_fta_lineno, flex_fta_ch, s, FtaParsertext, flex_fta_linebuf );\r
85     for(i=0;i<flex_fta_ch;i++){\r
86                 if(flex_fta_linebuf[i] == '\t'){\r
87                         fprintf(stderr,"\t");\r
88                 }else{\r
89                         fprintf(stderr," ");\r
90                 }\r
91         }\r
92         fprintf(stderr,"^\n");\r
93         //      fprintf(stderr,"%*s\n",1+flex_fta_ch,"^");\r
94 }\r
95 \r
96 #undef YY_INPUT\r
97 #define YY_INPUT(b, r, ms) (r = my_FtaParser_yyinput(b,ms))\r
98 \r
99 %}\r
100         /* MKS needs the next line to increase the NFA table */\r
101 %e 1200\r
102 %option noyywrap\r
103 \r
104 %%\r
105 \r
106         /* literal keyword tokens */\r
107 \r
108  /*\r
109                         The actions associated with each text token are to\r
110                         keep track of the current location (for syntax error reporting)\r
111                         and to report any necessary info to the emf.y parse tree builder\r
112 \r
113                         Its likely that there are a number of omissions, inconsistencies\r
114                         (some keywords do not need to be in caps), and relics\r
115                         (keywords such as BETWEEN, INDICATOR, etc., are not used\r
116                          in emf.y)\r
117                         This parser is somewhat of a work in progress.\r
118  */\r
119 \r
120  /*             Query keywords          */\r
121 \r
122 AND|And|and             { flex_fta_ch+=FtaParserleng; return AND; }\r
123 AND_AGGR|And_Aggr|and_aggr              { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("AND_AGGR"); return AGGR; }\r
124 OR_AGGR|Or_Aggr|or_aggr         { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("OR_AGGR"); return AGGR; }\r
125 XOR_AGGR|Xor_Aggr|xor_aggr              { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("XOR_AGGR"); return AGGR; }\r
126 AVG|Avg|avg             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("AVG"); return AGGR; }\r
127 MIN|Min|min             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("MIN"); return AGGR; }\r
128 MAX|Max|max             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("MAX"); return AGGR; }\r
129 SUM|Sum|sum             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("SUM"); return AGGR; }\r
130 COUNT|Count|count       { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("COUNT"); return AGGR; }\r
131 BY|By|by                { flex_fta_ch+=FtaParserleng; return BY; }\r
132 FROM|From|from          { flex_fta_ch+=FtaParserleng; return FROM; }\r
133 INNER_JOIN|Inner_Join|inner_join        { flex_fta_ch+=FtaParserleng; return INNER_JOIN; }\r
134 OUTER_JOIN|Outer_Join|outer_join        { flex_fta_ch+=FtaParserleng; return OUTER_JOIN; }\r
135 RIGHT_OUTER_JOIN|Right_Outer_Join|right_outer_join      { flex_fta_ch+=FtaParserleng; return RIGHT_OUTER_JOIN; }\r
136 LEFT_OUTER_JOIN|Left_Outer_Join|left_outer_join { flex_fta_ch+=FtaParserleng; return LEFT_OUTER_JOIN; }\r
137 FILTER_JOIN|Filter_Join|filter_join     { flex_fta_ch+=FtaParserleng; return FILTER_JOIN; }\r
138 GROUP|Group|group       { flex_fta_ch+=FtaParserleng; return GROUP; }\r
139 ROLLUP|Rollup|rollup    { flex_fta_ch+=FtaParserleng; return ROLLUP; }\r
140 CUBE|Cube|cube  { flex_fta_ch+=FtaParserleng; return CUBE; }\r
141 GROUPING_SETS|Grouping_Sets|grouping_sets       { flex_fta_ch+=FtaParserleng; return GROUPING_SETS; }\r
142 SUPERGROUP|Supergroup|supergroup        { flex_fta_ch+=FtaParserleng; return SUPERGROUP; }\r
143 CLEANING_WHEN|Cleaning_When|cleaning_when       { flex_fta_ch+=FtaParserleng; return CLEANING_WHEN; }\r
144 CLEANING_BY|Cleaning_By|cleaning_by     { flex_fta_ch+=FtaParserleng; return CLEANING_BY; }\r
145 CLOSING_WHEN|Closing_When|closing_when  { flex_fta_ch+=FtaParserleng; return CLOSING_WHEN; }\r
146 HAVING|Having|having    { flex_fta_ch+=FtaParserleng; return HAVING; }\r
147 AS|As|as                { flex_fta_ch+=FtaParserleng; return AS; }\r
148 IN|In|in                { flex_fta_ch+=FtaParserleng; return IN; }\r
149 NOT|Not|not             { flex_fta_ch+=FtaParserleng; return NOT; }\r
150 OR|Or|or        { flex_fta_ch+=FtaParserleng; return OR; }\r
151 \r
152 SELECT|Select|select            { flex_fta_ch+=FtaParserleng; return SELECT; }\r
153 WHERE|Where|where               { flex_fta_ch+=FtaParserleng; return WHERE; }\r
154 SUCH|Such|such  { flex_fta_ch+=FtaParserleng; return SUCH;}\r
155 THAT|That|that  { flex_fta_ch+=FtaParserleng; return THAT;}\r
156 MERGE|Merge|merge        {flex_fta_ch+=FtaParserleng; return MERGE;}\r
157 SLACK    {flex_fta_ch+=FtaParserleng; return SLACK;}\r
158 \r
159 TRUE    { flex_fta_ch+=FtaParserleng; return TRUE_V;}\r
160 FALSE   { flex_fta_ch+=FtaParserleng; return FALSE_V;}\r
161 TIMEVAL { flex_fta_ch+=FtaParserleng; return TIMEVAL_L;}\r
162 HEX     { flex_fta_ch+=FtaParserleng; return HEX_L;}\r
163 LHEX    { flex_fta_ch+=FtaParserleng; return LHEX_L;}\r
164 IP_VAL  { flex_fta_ch+=FtaParserleng; return IP_L;}\r
165 IPV6_VAL|IPv6_VAL       { flex_fta_ch+=FtaParserleng; return IPV6_L;}\r
166 \r
167 DEFINE  { flex_fta_ch+=FtaParserleng; return DEFINE_SEC;}\r
168 PARAM   { flex_fta_ch+=FtaParserleng; return PARAM_SEC;}\r
169 \r
170 [\{]    {flex_fta_ch+=FtaParserleng; return LEFTBRACE;}\r
171 [\}]    {flex_fta_ch+=FtaParserleng; return RIGHTBRACE;}\r
172 \r
173  /*\r
174                 Table definition keywords\r
175  */             \r
176 TABLE           { flex_fta_ch+=FtaParserleng; return TABLE; }\r
177 PROTOCOL                { flex_fta_ch+=FtaParserleng; return PROTOCOL; }\r
178 STREAM          { flex_fta_ch+=FtaParserleng; return STREAM; }\r
179 FTA                     { flex_fta_ch+=FtaParserleng; return FTA; }\r
180 UNPACK_FCNS                     { flex_fta_ch+=FtaParserleng; return UNPACK_FCNS; }\r
181 \r
182 OPERATOR                        { flex_fta_ch+=FtaParserleng; return OPERATOR; }\r
183 OPERATOR_VIEW                   { flex_fta_ch+=FtaParserleng; return OPERATOR_VIEW; }\r
184 FIELDS                  { flex_fta_ch+=FtaParserleng; return FIELDS; }\r
185 SUBQUERIES                      { flex_fta_ch+=FtaParserleng; return SUBQUERIES; }\r
186 SELECTION_PUSHDOWN                      { flex_fta_ch+=FtaParserleng; return SELECTION_PUSHDOWN; }\r
187 ;       {flex_fta_ch+=FtaParserleng; return SEMICOLON;}\r
188 \r
189 \r
190         /* punctuation */\r
191 "<<"    |\r
192 ">>"                    { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return SHIFT_OP; }\r
193 \r
194 "="     |\r
195 "<>"    |\r
196 "<"     |\r
197 ">"     |\r
198 "<="    |\r
199 ">="            { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return COMPARISON; }\r
200 \r
201 [%&|!+*/:(),.\[\]$@#]   { flex_fta_ch+=FtaParserleng; return yytext[0]; }\r
202 "-"     { flex_fta_ch+=FtaParserleng; return yytext[0]; }\r
203 "~"     { flex_fta_ch+=FtaParserleng; return yytext[0]; }\r
204 \r
205         /* names */\r
206 \r
207 [A-Za-z_][A-Za-z0-9_]*  { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return NAME; }\r
208 \r
209         /* numbers */\r
210 \r
211 [0-9]+  |\r
212 [0-9]+UL        { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext);  return INTNUM; }\r
213 \r
214 [0-9]+ULL       { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext);  return LONGINTNUM; }\r
215 \r
216 [0-9]+"."[0-9]* |\r
217 "."[0-9]*       |\r
218 [0-9]+[eE][+-]?[0-9]+   |\r
219 [0-9]+"."[0-9]*[eE][+-]?[0-9]+ |\r
220 "."[0-9]*[eE][+-]?[0-9]+        { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return APPROXNUM; }\r
221 \r
222         /* strings */\r
223 \r
224 '[^'\n]*'       {\r
225                 \r
226                 int c;\r
227                 \r
228                 FtaParserlval.strval = strdup(FtaParsertext+1); \r
229 \r
230                 c = yyinput();\r
231 \r
232                 unput(c);       /* just peeking */\r
233                 if(c != '\'') {\r
234                         flex_fta_ch+=FtaParserleng; \r
235                         FtaParserlval.strval[FtaParserleng-2] = '\0';\r
236                         return STRING_TOKEN;\r
237                 } else\r
238                         yymore();\r
239         }\r
240                 \r
241 '[^'\n]*$       { flex_fta_ch+=FtaParserleng; FtaParsererror("Unterminated string"); }\r
242 \r
243  /*                                                                                                                     */\r
244  /*             Newline : advance the error reporting line number       */\r
245  /*             and grab the next line into flex_fta_linebuf                    */\r
246  /*                                                                                                                     */\r
247 \r
248 \n.*            {flex_fta_ch=0; flex_fta_lineno++;\r
249                            strcpy(flex_fta_linebuf,FtaParsertext+1);\r
250                            yyless(1);\r
251                            }\r
252 \r
253 [ \t\r]+        {flex_fta_ch+=FtaParserleng; }  /* white space */\r
254 \r
255 "--".*$         {flex_fta_ch+=FtaParserleng; }; /* comment */\r
256 "//".*$         {flex_fta_ch+=FtaParserleng; }; /* comment */\r
257 \r
258 .|\n            {flex_fta_ch+=FtaParserleng; fprintf(stderr,"Warning: unknown token (ignored)\n");  FtaParsererror(yytext);}\r
259 \r
260 %%\r
261 \r
262 int my_FtaParser_yyinput(char *buf, int max_size){\r
263         int c = 0;\r
264         int inchar = 0;\r
265         \r
266         if(flex_fta_stringinput != NULL){\r
267                 while(c<max_size){\r
268                         if(flex_fta_stringinput[flex_fta_stringinput_ptr] != '\0'){\r
269                                 buf[c++] = flex_fta_stringinput[flex_fta_stringinput_ptr++];\r
270                         }else{\r
271                                 break;\r
272                         }\r
273                 }\r
274                 return(c);\r
275         }\r
276         \r
277         if(flex_fta_fileinput != NULL){\r
278                 while(c < max_size){\r
279                         inchar = getc(flex_fta_fileinput);\r
280                         if(inchar != EOF){\r
281                                 buf[c++] = inchar;\r
282                         }else{\r
283                                 break;\r
284                         }\r
285                 }\r
286                 return(c);\r
287         }\r
288         \r
289         return(0);\r
290 }\r
291 \r
292 void FtaParser_setfileinput(FILE *f){\r
293         FtaParserrestart(NULL);\r
294 \r
295         flex_fta_fileinput = f;\r
296         flex_fta_stringinput = NULL;\r
297         flex_fta_lineno = 1;\r
298         flex_fta_ch = 0;\r
299 }\r
300 \r
301 void FtaParser_setstringinput(char *s){\r
302         FtaParserrestart(NULL);\r
303 \r
304         flex_fta_fileinput = NULL;\r
305         flex_fta_stringinput = s;\r
306         flex_fta_stringinput_ptr = 0;\r
307         flex_fta_lineno = 1;\r
308         flex_fta_ch = 0;\r
309 }\r
310         \r
311                 \r
312 \r
313 \r