1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // This source code is automatically generated by Lemon Parser.
31 // It is generated by following command:
32 //
33 // $ cd parser
34 // $ lemon -q parser.y
35 // $ mv parser.c ../
36 //
37 // lemon command generates "parser.c" and "parser.h". "parser.h" defines ID
38 // of each operator, and is manually inserted into this "parser.c".
39 //
40 // The reason why I placed parser.y in "parser" directory is descripted in
41 // parser/README.
42 //
43 // Auto-generated "parser.c" causes a compile error because of defining an
44 // unused variable. This is the first line of yy_destructor(), and is manually
45 // commented out in this file.
46
47
48 /* Driver template for the LEMON parser generator.
49 ** The author disclaims copyright to this source code.
50 */
51 /* First off, code is included that follows the "include" declaration
52 ** in the input grammar file. */
53 #include <stdio.h>
54 #line 7 "parser.y"
55
56 #include <assert.h>
57 #ifdef OS_WIN
58 #include <float.h>
59 #endif
60 #include <string.h>
61 #include <math.h>
62
63 // Contents of parser.h is manually inserted here.
64 #define PLUS 1
65 #define MINUS 2
66 #define DIVIDE 3
67 #define TIMES 4
68 #define MOD 5
69 #define POW 6
70 #define NOT 7
71 #define LP 8
72 #define RP 9
73 #define INTEGER 10
74 // End of parser.h.
75
76 struct Result {
77 enum ErrorType {
78 NOT_ACCEPTED,
79 SYNTAX_ERROR,
80 DIVIDE_BY_ZERO,
81 STACK_OVERFLOW,
82 RESULT_OVERFLOW,
83 PARSE_ERROR,
84 UNKNOWN_ERROR,
85 ACCEPTED
86 };
87
88 double value;
89 ErrorType error_type;
90
ResultResult91 Result() : value(0), error_type(NOT_ACCEPTED) {}
~ResultResult92 ~Result() {}
93
CheckValueResult94 void CheckValue(double val) {
95 if (!IsFinite(val)) {
96 error_type = RESULT_OVERFLOW;
97 }
98 }
99 };
100
101 #line 54 "parser.c"
102 /* Next is all token values, in a form suitable for use by makeheaders.
103 ** This section will be null unless lemon is run with the -m switch.
104 */
105 /*
106 ** These constants (all generated automatically by the parser generator)
107 ** specify the various kinds of tokens (terminals) that the parser
108 ** understands.
109 **
110 ** Each symbol here is a terminal symbol in the grammar.
111 */
112 /* Make sure the INTERFACE macro is defined.
113 */
114 #ifndef INTERFACE
115 # define INTERFACE 1
116 #endif
117 /* The next thing included is series of defines which control
118 ** various aspects of the generated parser.
119 ** YYCODETYPE is the data type used for storing terminal
120 ** and nonterminal numbers. "unsigned char" is
121 ** used if there are fewer than 250 terminals
122 ** and nonterminals. "int" is used otherwise.
123 ** YYNOCODE is a number of type YYCODETYPE which corresponds
124 ** to no legal terminal or nonterminal number. This
125 ** number is used to fill in empty slots of the hash
126 ** table.
127 ** YYFALLBACK If defined, this indicates that one or more tokens
128 ** have fall-back values which should be used if the
129 ** original value of the token will not parse.
130 ** YYACTIONTYPE is the data type used for storing terminal
131 ** and nonterminal numbers. "unsigned char" is
132 ** used if there are fewer than 250 rules and
133 ** states combined. "int" is used otherwise.
134 ** ParseTOKENTYPE is the data type used for minor tokens given
135 ** directly to the parser from the tokenizer.
136 ** YYMINORTYPE is the data type used for all minor tokens.
137 ** This is typically a union of many types, one of
138 ** which is ParseTOKENTYPE. The entry in the union
139 ** for base tokens is called "yy0".
140 ** YYSTACKDEPTH is the maximum depth of the parser's stack. If
141 ** zero the stack is dynamically sized using realloc()
142 ** ParseARG_SDECL A static variable declaration for the %extra_argument
143 ** ParseARG_PDECL A parameter declaration for the %extra_argument
144 ** ParseARG_STORE Code to store %extra_argument into yypParser
145 ** ParseARG_FETCH Code to extract %extra_argument from yypParser
146 ** YYNSTATE the combined number of states.
147 ** YYNRULE the number of rules in the grammar
148 ** YYERRORSYMBOL is the code number of the error symbol. If not
149 ** defined, then do no error processing.
150 */
151 #define YYCODETYPE unsigned char
152 #define YYNOCODE 15
153 #define YYACTIONTYPE unsigned char
154 #define ParseTOKENTYPE double
155 typedef union {
156 int yyinit;
157 ParseTOKENTYPE yy0;
158 } YYMINORTYPE;
159 #ifndef YYSTACKDEPTH
160 #define YYSTACKDEPTH 100
161 #endif
162 #define ParseARG_SDECL Result *result ;
163 #define ParseARG_PDECL , Result *result
164 #define ParseARG_FETCH Result *result = yypParser->result
165 #define ParseARG_STORE yypParser->result = result
166 #define YYNSTATE 22
167 #define YYNRULE 11
168 #define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
169 #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
170 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
171
172 /* The yyzerominor constant is used to initialize instances of
173 ** YYMINORTYPE objects to zero. */
174 static const YYMINORTYPE yyzerominor = { 0 };
175
176 /* Define the yytestcase() macro to be a no-op if is not already defined
177 ** otherwise.
178 **
179 ** Applications can choose to define yytestcase() in the %include section
180 ** to a macro that can assist in verifying code coverage. For production
181 ** code the yytestcase() macro should be turned off. But it is useful
182 ** for testing.
183 */
184 #ifndef yytestcase
185 # define yytestcase(X)
186 #endif
187
188
189 /* Next are the tables used to determine what action to take based on the
190 ** current state and lookahead token. These tables are used to implement
191 ** functions that take a state number and lookahead value and return an
192 ** action integer.
193 **
194 ** Suppose the action integer is N. Then the action is determined as
195 ** follows
196 **
197 ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
198 ** token onto the stack and goto state N.
199 **
200 ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
201 **
202 ** N == YYNSTATE+YYNRULE A syntax error has occurred.
203 **
204 ** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
205 **
206 ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
207 ** slots in the yy_action[] table.
208 **
209 ** The action table is constructed as a single large table named yy_action[].
210 ** Given state S and lookahead X, the action is computed as
211 **
212 ** yy_action[ yy_shift_ofst[S] + X ]
213 **
214 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
215 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
216 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
217 ** and that yy_default[S] should be used instead.
218 **
219 ** The formula above is for computing the action when the lookahead is
220 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
221 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
222 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
223 ** YY_SHIFT_USE_DFLT.
224 **
225 ** The following are the tables generated in this section:
226 **
227 ** yy_action[] A single table containing all actions.
228 ** yy_lookahead[] A table containing the lookahead for each entry in
229 ** yy_action. Used to detect hash collisions.
230 ** yy_shift_ofst[] For each state, the offset into yy_action for
231 ** shifting terminals.
232 ** yy_reduce_ofst[] For each state, the offset into yy_action for
233 ** shifting non-terminals after a reduce.
234 ** yy_default[] Default action for each state.
235 */
236 #define YY_ACTTAB_COUNT (34)
237 static const YYACTIONTYPE yy_action[] = {
238 /* 0 */ 22, 8, 9, 5, 7, 4, 6, 8, 9, 5,
239 /* 10 */ 7, 4, 6, 2, 3, 21, 5, 7, 4, 6,
240 /* 20 */ 1, 6, 20, 34, 11, 13, 12, 19, 18, 17,
241 /* 30 */ 16, 15, 14, 10,
242 };
243 static const YYCODETYPE yy_lookahead[] = {
244 /* 0 */ 0, 1, 2, 3, 4, 5, 6, 1, 2, 3,
245 /* 10 */ 4, 5, 6, 1, 2, 9, 3, 4, 5, 6,
246 /* 20 */ 8, 6, 10, 12, 13, 13, 13, 13, 13, 13,
247 /* 30 */ 13, 13, 13, 13,
248 };
249 #define YY_SHIFT_USE_DFLT (-1)
250 #define YY_SHIFT_COUNT (19)
251 #define YY_SHIFT_MIN (0)
252 #define YY_SHIFT_MAX (15)
253 static const signed char yy_shift_ofst[] = {
254 /* 0 */ 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
255 /* 10 */ 6, 0, 13, 13, 15, 15, 15, 15, 15, 15,
256 };
257 #define YY_REDUCE_USE_DFLT (-1)
258 #define YY_REDUCE_COUNT (9)
259 #define YY_REDUCE_MIN (0)
260 #define YY_REDUCE_MAX (20)
261 static const signed char yy_reduce_ofst[] = {
262 /* 0 */ 11, 20, 19, 18, 17, 16, 15, 14, 13, 12,
263 };
264 static const YYACTIONTYPE yy_default[] = {
265 /* 0 */ 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
266 /* 10 */ 33, 33, 24, 23, 27, 26, 31, 30, 28, 25,
267 /* 20 */ 32, 29,
268 };
269
270 /* The next table maps tokens into fallback tokens. If a construct
271 ** like the following:
272 **
273 ** %fallback ID X Y Z.
274 **
275 ** appears in the grammar, then ID becomes a fallback token for X, Y,
276 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
277 ** but it does not parse, the type of the token is changed to ID and
278 ** the parse is retried before an error is thrown.
279 */
280 #ifdef YYFALLBACK
281 static const YYCODETYPE yyFallback[] = {
282 };
283 #endif /* YYFALLBACK */
284
285 /* The following structure represents a single element of the
286 ** parser's stack. Information stored includes:
287 **
288 ** + The state number for the parser at this level of the stack.
289 **
290 ** + The value of the token stored at this level of the stack.
291 ** (In other words, the "major" token.)
292 **
293 ** + The semantic value stored at this level of the stack. This is
294 ** the information used by the action routines in the grammar.
295 ** It is sometimes called the "minor" token.
296 */
297 struct yyStackEntry {
298 YYACTIONTYPE stateno; /* The state-number */
299 YYCODETYPE major; /* The major token value. This is the code
300 ** number for the token at this stack level */
301 YYMINORTYPE minor; /* The user-supplied minor token value. This
302 ** is the value of the token */
303 };
304 typedef struct yyStackEntry yyStackEntry;
305
306 /* The state of the parser is completely contained in an instance of
307 ** the following structure */
308 struct yyParser {
309 int yyidx; /* Index of top element in stack */
310 #ifdef YYTRACKMAXSTACKDEPTH
311 int yyidxMax; /* Maximum value of yyidx */
312 #endif
313 int yyerrcnt; /* Shifts left before out of the error */
314 ParseARG_SDECL /* A place to hold %extra_argument */
315 #if YYSTACKDEPTH<=0
316 int yystksz; /* Current side of the stack */
317 yyStackEntry *yystack; /* The parser's stack */
318 #else
319 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
320 #endif
321 };
322 typedef struct yyParser yyParser;
323
324 #ifndef NDEBUG
325 #include <stdio.h>
326 static FILE *yyTraceFILE = 0;
327 static char *yyTracePrompt = 0;
328 #endif /* NDEBUG */
329
330 #ifndef NDEBUG
331 /*
332 ** Turn parser tracing on by giving a stream to which to write the trace
333 ** and a prompt to preface each trace message. Tracing is turned off
334 ** by making either argument NULL
335 **
336 ** Inputs:
337 ** <ul>
338 ** <li> A FILE* to which trace output should be written.
339 ** If NULL, then tracing is turned off.
340 ** <li> A prefix string written at the beginning of every
341 ** line of trace output. If NULL, then tracing is
342 ** turned off.
343 ** </ul>
344 **
345 ** Outputs:
346 ** None.
347 */
348 /* This function is not used anywhere.
349 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
350 yyTraceFILE = TraceFILE;
351 yyTracePrompt = zTracePrompt;
352 if( yyTraceFILE==0 ) yyTracePrompt = 0;
353 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
354 }
355 */
356 #endif /* NDEBUG */
357
358 #ifndef NDEBUG
359 /* For tracing shifts, the names of all terminals and nonterminals
360 ** are required. The following table supplies these names */
361 static const char *const yyTokenName[] = {
362 "$", "PLUS", "MINUS", "DIVIDE",
363 "TIMES", "MOD", "POW", "NOT",
364 "LP", "RP", "INTEGER", "error",
365 "program", "expr",
366 };
367 #endif /* NDEBUG */
368
369 #ifndef NDEBUG
370 /* For tracing reduce actions, the names of all rules are required.
371 */
372 static const char *const yyRuleName[] = {
373 /* 0 */ "program ::= expr",
374 /* 1 */ "expr ::= expr MINUS expr",
375 /* 2 */ "expr ::= expr PLUS expr",
376 /* 3 */ "expr ::= expr TIMES expr",
377 /* 4 */ "expr ::= MINUS expr",
378 /* 5 */ "expr ::= PLUS expr",
379 /* 6 */ "expr ::= expr POW expr",
380 /* 7 */ "expr ::= LP expr RP",
381 /* 8 */ "expr ::= expr DIVIDE expr",
382 /* 9 */ "expr ::= expr MOD expr",
383 /* 10 */ "expr ::= INTEGER",
384 };
385 #endif /* NDEBUG */
386
387
388 #if YYSTACKDEPTH<=0
389 /*
390 ** Try to increase the size of the parser stack.
391 */
yyGrowStack(yyParser * p)392 static void yyGrowStack(yyParser *p){
393 int newSize;
394 yyStackEntry *pNew;
395
396 newSize = p->yystksz*2 + 100;
397 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
398 if( pNew ){
399 p->yystack = pNew;
400 p->yystksz = newSize;
401 #ifndef NDEBUG
402 if( yyTraceFILE ){
403 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
404 yyTracePrompt, p->yystksz);
405 }
406 #endif
407 }
408 }
409 #endif
410
411 /*
412 ** This function allocates a new parser.
413 ** The only argument is a pointer to a function which works like
414 ** malloc.
415 **
416 ** Inputs:
417 ** A pointer to the function used to allocate memory.
418 **
419 ** Outputs:
420 ** A pointer to a parser. This pointer is used in subsequent calls
421 ** to Parse and ParseFree.
422 */
ParseAlloc(void * (* mallocProc)(size_t))423 void *ParseAlloc(void *(*mallocProc)(size_t)){
424 yyParser *pParser;
425 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
426 if( pParser ){
427 pParser->yyidx = -1;
428 #ifdef YYTRACKMAXSTACKDEPTH
429 pParser->yyidxMax = 0;
430 #endif
431 #if YYSTACKDEPTH<=0
432 pParser->yystack = NULL;
433 pParser->yystksz = 0;
434 yyGrowStack(pParser);
435 #endif
436 }
437 return pParser;
438 }
439
440 /* The following function deletes the value associated with a
441 ** symbol. The symbol can be either a terminal or nonterminal.
442 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
443 ** the value.
444 */
yy_destructor(yyParser * yypParser,YYCODETYPE yymajor,YYMINORTYPE * yypminor)445 static void yy_destructor(
446 yyParser *yypParser, /* The parser */
447 YYCODETYPE yymajor, /* Type code for object to destroy */
448 YYMINORTYPE *yypminor /* The object to be destroyed */
449 ){
450 // This ParseARG_FETCH causes compile error, because it defines a variable
451 // named 'result', which is not used in this function.
452 // ParseARG_FETCH;
453 switch( yymajor ){
454 /* Here is inserted the actions which take place when a
455 ** terminal or non-terminal is destroyed. This can happen
456 ** when the symbol is popped from the stack during a
457 ** reduce or during error processing or when a parser is
458 ** being destroyed before it is finished parsing.
459 **
460 ** Note: during a reduce, the only symbols destroyed are those
461 ** which appear on the RHS of the rule, but which are not used
462 ** inside the C code.
463 */
464 default: break; /* If no destructor action specified: do nothing */
465 }
466 }
467
468 /*
469 ** Pop the parser's stack once.
470 **
471 ** If there is a destructor routine associated with the token which
472 ** is popped from the stack, then call it.
473 **
474 ** Return the major token number for the symbol popped.
475 */
yy_pop_parser_stack(yyParser * pParser)476 static int yy_pop_parser_stack(yyParser *pParser){
477 YYCODETYPE yymajor;
478 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
479
480 if( pParser->yyidx<0 ) return 0;
481 #ifndef NDEBUG
482 if( yyTraceFILE && pParser->yyidx>=0 ){
483 fprintf(yyTraceFILE,"%sPopping %s\n",
484 yyTracePrompt,
485 yyTokenName[yytos->major]);
486 }
487 #endif
488 yymajor = yytos->major;
489 yy_destructor(pParser, yymajor, &yytos->minor);
490 pParser->yyidx--;
491 return yymajor;
492 }
493
494 /*
495 ** Deallocate and destroy a parser. Destructors are all called for
496 ** all stack elements before shutting the parser down.
497 **
498 ** Inputs:
499 ** <ul>
500 ** <li> A pointer to the parser. This should be a pointer
501 ** obtained from ParseAlloc.
502 ** <li> A pointer to a function used to reclaim memory obtained
503 ** from malloc.
504 ** </ul>
505 */
ParseFree(void * p,void (* freeProc)(void *))506 void ParseFree(
507 void *p, /* The parser to be deleted */
508 void (*freeProc)(void*) /* Function used to reclaim memory */
509 ){
510 yyParser *pParser = (yyParser*)p;
511 if( pParser==0 ) return;
512 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
513 #if YYSTACKDEPTH<=0
514 free(pParser->yystack);
515 #endif
516 (*freeProc)((void*)pParser);
517 }
518
519 /*
520 ** Return the peak depth of the stack for a parser.
521 */
522 #ifdef YYTRACKMAXSTACKDEPTH
ParseStackPeak(void * p)523 int ParseStackPeak(void *p){
524 yyParser *pParser = (yyParser*)p;
525 return pParser->yyidxMax;
526 }
527 #endif
528
529 /*
530 ** Find the appropriate action for a parser given the terminal
531 ** look-ahead token iLookAhead.
532 **
533 ** If the look-ahead token is YYNOCODE, then check to see if the action is
534 ** independent of the look-ahead. If it is, return the action, otherwise
535 ** return YY_NO_ACTION.
536 */
yy_find_shift_action(yyParser * pParser,YYCODETYPE iLookAhead)537 static int yy_find_shift_action(
538 yyParser *pParser, /* The parser */
539 YYCODETYPE iLookAhead /* The look-ahead token */
540 ){
541 int i;
542 int stateno = pParser->yystack[pParser->yyidx].stateno;
543
544 if( stateno>YY_SHIFT_COUNT
545 || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
546 return yy_default[stateno];
547 }
548 assert( iLookAhead!=YYNOCODE );
549 i += iLookAhead;
550 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
551 if( iLookAhead>0 ){
552 #ifdef YYFALLBACK
553 YYCODETYPE iFallback; /* Fallback token */
554 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
555 && (iFallback = yyFallback[iLookAhead])!=0 ){
556 #ifndef NDEBUG
557 if( yyTraceFILE ){
558 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
559 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
560 }
561 #endif
562 return yy_find_shift_action(pParser, iFallback);
563 }
564 #endif
565 #ifdef YYWILDCARD
566 {
567 int j = i - iLookAhead + YYWILDCARD;
568 if(
569 #if YY_SHIFT_MIN+YYWILDCARD<0
570 j>=0 &&
571 #endif
572 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
573 j<YY_ACTTAB_COUNT &&
574 #endif
575 yy_lookahead[j]==YYWILDCARD
576 ){
577 #ifndef NDEBUG
578 if( yyTraceFILE ){
579 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
580 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
581 }
582 #endif /* NDEBUG */
583 return yy_action[j];
584 }
585 }
586 #endif /* YYWILDCARD */
587 }
588 return yy_default[stateno];
589 }else{
590 return yy_action[i];
591 }
592 }
593
594 /*
595 ** Find the appropriate action for a parser given the non-terminal
596 ** look-ahead token iLookAhead.
597 **
598 ** If the look-ahead token is YYNOCODE, then check to see if the action is
599 ** independent of the look-ahead. If it is, return the action, otherwise
600 ** return YY_NO_ACTION.
601 */
yy_find_reduce_action(int stateno,YYCODETYPE iLookAhead)602 static int yy_find_reduce_action(
603 int stateno, /* Current state number */
604 YYCODETYPE iLookAhead /* The look-ahead token */
605 ){
606 int i;
607 #ifdef YYERRORSYMBOL
608 if( stateno>YY_REDUCE_COUNT ){
609 return yy_default[stateno];
610 }
611 #else
612 assert( stateno<=YY_REDUCE_COUNT );
613 #endif
614 i = yy_reduce_ofst[stateno];
615 assert( i!=YY_REDUCE_USE_DFLT );
616 assert( iLookAhead!=YYNOCODE );
617 i += iLookAhead;
618 #ifdef YYERRORSYMBOL
619 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
620 return yy_default[stateno];
621 }
622 #else
623 assert( i>=0 && i<YY_ACTTAB_COUNT );
624 assert( yy_lookahead[i]==iLookAhead );
625 #endif
626 return yy_action[i];
627 }
628
629 /*
630 ** The following routine is called if the stack overflows.
631 */
yyStackOverflow(yyParser * yypParser,YYMINORTYPE * yypMinor)632 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
633 ParseARG_FETCH;
634 yypParser->yyidx--;
635 #ifndef NDEBUG
636 if( yyTraceFILE ){
637 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
638 }
639 #endif
640 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
641 /* Here code is inserted which will execute if the parser
642 ** stack every overflows */
643 #line 60 "parser.y"
644
645 result->error_type = Result::STACK_OVERFLOW;
646 #line 595 "parser.c"
647 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
648 }
649
650 /*
651 ** Perform a shift action.
652 */
yy_shift(yyParser * yypParser,int yyNewState,int yyMajor,YYMINORTYPE * yypMinor)653 static void yy_shift(
654 yyParser *yypParser, /* The parser to be shifted */
655 int yyNewState, /* The new state to shift in */
656 int yyMajor, /* The major token to shift in */
657 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
658 ){
659 yyStackEntry *yytos;
660 yypParser->yyidx++;
661 #ifdef YYTRACKMAXSTACKDEPTH
662 if( yypParser->yyidx>yypParser->yyidxMax ){
663 yypParser->yyidxMax = yypParser->yyidx;
664 }
665 #endif
666 #if YYSTACKDEPTH>0
667 if( yypParser->yyidx>=YYSTACKDEPTH ){
668 yyStackOverflow(yypParser, yypMinor);
669 return;
670 }
671 #else
672 if( yypParser->yyidx>=yypParser->yystksz ){
673 yyGrowStack(yypParser);
674 if( yypParser->yyidx>=yypParser->yystksz ){
675 yyStackOverflow(yypParser, yypMinor);
676 return;
677 }
678 }
679 #endif
680 yytos = &yypParser->yystack[yypParser->yyidx];
681 yytos->stateno = (YYACTIONTYPE)yyNewState;
682 yytos->major = (YYCODETYPE)yyMajor;
683 yytos->minor = *yypMinor;
684 #ifndef NDEBUG
685 if( yyTraceFILE && yypParser->yyidx>0 ){
686 int i;
687 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
688 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
689 for(i=1; i<=yypParser->yyidx; i++)
690 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
691 fprintf(yyTraceFILE,"\n");
692 }
693 #endif
694 }
695
696 /* The following table contains information about every rule that
697 ** is used during the reduce.
698 */
699 static const struct {
700 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
701 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
702 } yyRuleInfo[] = {
703 { 12, 1 },
704 { 13, 3 },
705 { 13, 3 },
706 { 13, 3 },
707 { 13, 2 },
708 { 13, 2 },
709 { 13, 3 },
710 { 13, 3 },
711 { 13, 3 },
712 { 13, 3 },
713 { 13, 1 },
714 };
715
716 static void yy_accept(yyParser*); /* Forward Declaration */
717
718 /*
719 ** Perform a reduce action and the shift that must immediately
720 ** follow the reduce.
721 */
yy_reduce(yyParser * yypParser,int yyruleno)722 static void yy_reduce(
723 yyParser *yypParser, /* The parser */
724 int yyruleno /* Number of the rule by which to reduce */
725 ){
726 int yygoto; /* The next state */
727 int yyact; /* The next action */
728 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
729 yyStackEntry *yymsp; /* The top of the parser's stack */
730 int yysize; /* Amount to pop the stack */
731 ParseARG_FETCH;
732 yymsp = &yypParser->yystack[yypParser->yyidx];
733 #ifndef NDEBUG
734 if( yyTraceFILE && yyruleno>=0
735 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
736 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
737 yyRuleName[yyruleno]);
738 }
739 #endif /* NDEBUG */
740
741 /* Silence complaints from purify about yygotominor being uninitialized
742 ** in some cases when it is copied into the stack after the following
743 ** switch. yygotominor is uninitialized when a rule reduces that does
744 ** not set the value of its left-hand side nonterminal. Leaving the
745 ** value of the nonterminal uninitialized is utterly harmless as long
746 ** as the value is never used. So really the only thing this code
747 ** accomplishes is to quieten purify.
748 **
749 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
750 ** without this code, their parser segfaults. I'm not sure what there
751 ** parser is doing to make this happen. This is the second bug report
752 ** from wireshark this week. Clearly they are stressing Lemon in ways
753 ** that it has not been previously stressed... (SQLite ticket #2172)
754 */
755 /*memset(&yygotominor, 0, sizeof(yygotominor));*/
756 yygotominor = yyzerominor;
757
758
759 switch( yyruleno ){
760 /* Beginning here are the reduction cases. A typical example
761 ** follows:
762 ** case 0:
763 ** #line <lineno> <grammarfile>
764 ** { ... } // User supplied code
765 ** #line <lineno> <thisfile>
766 ** break;
767 */
768 case 0: /* program ::= expr */
769 #line 74 "parser.y"
770 {
771 if (result->error_type == Result::NOT_ACCEPTED &&
772 IsFinite(yymsp[0].minor.yy0)){
773 result->value = yymsp[0].minor.yy0;
774 } else {
775 result->error_type = Result::RESULT_OVERFLOW;
776 }
777 }
778 #line 727 "parser.c"
779 break;
780 case 1: /* expr ::= expr MINUS expr */
781 #line 83 "parser.y"
782 {
783 yygotominor.yy0 = yymsp[-2].minor.yy0 - yymsp[0].minor.yy0;
784 result->CheckValue(yygotominor.yy0);
785 }
786 #line 735 "parser.c"
787 break;
788 case 2: /* expr ::= expr PLUS expr */
789 #line 87 "parser.y"
790 {
791 yygotominor.yy0 = yymsp[-2].minor.yy0 + yymsp[0].minor.yy0;
792 result->CheckValue(yygotominor.yy0);
793 }
794 #line 743 "parser.c"
795 break;
796 case 3: /* expr ::= expr TIMES expr */
797 #line 91 "parser.y"
798 {
799 yygotominor.yy0 = yymsp[-2].minor.yy0 * yymsp[0].minor.yy0;
800 result->CheckValue(yygotominor.yy0);
801 }
802 #line 751 "parser.c"
803 break;
804 case 4: /* expr ::= MINUS expr */
805 #line 95 "parser.y"
806 { yygotominor.yy0 = - yymsp[0].minor.yy0; }
807 #line 756 "parser.c"
808 break;
809 case 5: /* expr ::= PLUS expr */
810 case 10: /* expr ::= INTEGER */ yytestcase(yyruleno==10);
811 #line 96 "parser.y"
812 { yygotominor.yy0 = yymsp[0].minor.yy0; }
813 #line 762 "parser.c"
814 break;
815 case 6: /* expr ::= expr POW expr */
816 #line 97 "parser.y"
817 {
818 yygotominor.yy0 = pow(yymsp[-2].minor.yy0, yymsp[0].minor.yy0);
819 result->CheckValue(yygotominor.yy0);
820 }
821 #line 770 "parser.c"
822 break;
823 case 7: /* expr ::= LP expr RP */
824 #line 101 "parser.y"
825 { yygotominor.yy0 = yymsp[-1].minor.yy0; }
826 #line 775 "parser.c"
827 break;
828 case 8: /* expr ::= expr DIVIDE expr */
829 #line 102 "parser.y"
830 {
831 if (yymsp[0].minor.yy0 != 0.0) {
832 yygotominor.yy0 = yymsp[-2].minor.yy0 / yymsp[0].minor.yy0;
833 result->CheckValue(yygotominor.yy0);
834 } else {
835 result->error_type = Result::DIVIDE_BY_ZERO;
836 }
837 }
838 #line 787 "parser.c"
839 break;
840 case 9: /* expr ::= expr MOD expr */
841 #line 110 "parser.y"
842 {
843 if (yymsp[0].minor.yy0 != 0.0) {
844 yygotominor.yy0 = fmod(yymsp[-2].minor.yy0, yymsp[0].minor.yy0);
845 result->CheckValue(yygotominor.yy0);
846 } else {
847 result->error_type = Result::DIVIDE_BY_ZERO;
848 }
849 }
850 #line 799 "parser.c"
851 break;
852 default:
853 break;
854 };
855 yygoto = yyRuleInfo[yyruleno].lhs;
856 yysize = yyRuleInfo[yyruleno].nrhs;
857 yypParser->yyidx -= yysize;
858 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
859 if( yyact < YYNSTATE ){
860 #ifdef NDEBUG
861 /* If we are not debugging and the reduce action popped at least
862 ** one element off the stack, then we can push the new element back
863 ** onto the stack here, and skip the stack overflow test in yy_shift().
864 ** That gives a significant speed improvement. */
865 if( yysize ){
866 yypParser->yyidx++;
867 yymsp -= yysize-1;
868 yymsp->stateno = (YYACTIONTYPE)yyact;
869 yymsp->major = (YYCODETYPE)yygoto;
870 yymsp->minor = yygotominor;
871 }else
872 #endif
873 {
874 yy_shift(yypParser,yyact,yygoto,&yygotominor);
875 }
876 }else{
877 assert( yyact == YYNSTATE + YYNRULE + 1 );
878 yy_accept(yypParser);
879 }
880 }
881
882 /*
883 ** The following code executes when the parse fails
884 */
885 #ifndef YYNOERRORRECOVERY
yy_parse_failed(yyParser * yypParser)886 static void yy_parse_failed(
887 yyParser *yypParser /* The parser */
888 ){
889 ParseARG_FETCH;
890 #ifndef NDEBUG
891 if( yyTraceFILE ){
892 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
893 }
894 #endif
895 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
896 /* Here code is inserted which will be executed whenever the
897 ** parser fails */
898 #line 56 "parser.y"
899
900 result->error_type = Result::PARSE_ERROR;
901 #line 850 "parser.c"
902 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
903 }
904 #endif /* YYNOERRORRECOVERY */
905
906 /*
907 ** The following code executes when a syntax error first occurs.
908 */
yy_syntax_error(yyParser * yypParser,int yymajor,YYMINORTYPE yyminor)909 static void yy_syntax_error(
910 yyParser *yypParser, /* The parser */
911 int yymajor, /* The major type of the error token */
912 YYMINORTYPE yyminor /* The minor type of the error token */
913 ){
914 ParseARG_FETCH;
915 #define TOKEN (yyminor.yy0)
916 #line 64 "parser.y"
917
918 result->error_type = Result::SYNTAX_ERROR;
919 #line 868 "parser.c"
920 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
921 }
922
923 /*
924 ** The following is executed when the parser accepts
925 */
yy_accept(yyParser * yypParser)926 static void yy_accept(
927 yyParser *yypParser /* The parser */
928 ){
929 ParseARG_FETCH;
930 #ifndef NDEBUG
931 if( yyTraceFILE ){
932 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
933 }
934 #endif
935 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
936 /* Here code is inserted which will be executed whenever the
937 ** parser accepts */
938 #line 68 "parser.y"
939
940 if (result->error_type == Result::NOT_ACCEPTED) {
941 result->error_type = Result::ACCEPTED;
942 }
943 #line 892 "parser.c"
944 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
945 }
946
947 /* The main parser program.
948 ** The first argument is a pointer to a structure obtained from
949 ** "ParseAlloc" which describes the current state of the parser.
950 ** The second argument is the major token number. The third is
951 ** the minor token. The fourth optional argument is whatever the
952 ** user wants (and specified in the grammar) and is available for
953 ** use by the action routines.
954 **
955 ** Inputs:
956 ** <ul>
957 ** <li> A pointer to the parser (an opaque structure.)
958 ** <li> The major token number.
959 ** <li> The minor token number.
960 ** <li> An option argument of a grammar-specified type.
961 ** </ul>
962 **
963 ** Outputs:
964 ** None.
965 */
Parse(void * yyp,int yymajor,ParseTOKENTYPE yyminor ParseARG_PDECL)966 void Parse(
967 void *yyp, /* The parser */
968 int yymajor, /* The major token code number */
969 ParseTOKENTYPE yyminor /* The value for the token */
970 ParseARG_PDECL /* Optional %extra_argument parameter */
971 ){
972 YYMINORTYPE yyminorunion;
973 int yyact; /* The parser action. */
974 int yyendofinput; /* True if we are at the end of input */
975 #ifdef YYERRORSYMBOL
976 int yyerrorhit = 0; /* True if yymajor has invoked an error */
977 #endif
978 yyParser *yypParser; /* The parser */
979
980 /* (re)initialize the parser, if necessary */
981 yypParser = (yyParser*)yyp;
982 if( yypParser->yyidx<0 ){
983 #if YYSTACKDEPTH<=0
984 if( yypParser->yystksz <=0 ){
985 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
986 yyminorunion = yyzerominor;
987 yyStackOverflow(yypParser, &yyminorunion);
988 return;
989 }
990 #endif
991 yypParser->yyidx = 0;
992 yypParser->yyerrcnt = -1;
993 yypParser->yystack[0].stateno = 0;
994 yypParser->yystack[0].major = 0;
995 }
996 yyminorunion.yy0 = yyminor;
997 yyendofinput = (yymajor==0);
998 ParseARG_STORE;
999
1000 #ifndef NDEBUG
1001 if( yyTraceFILE ){
1002 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
1003 }
1004 #endif
1005
1006 do{
1007 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
1008 if( yyact<YYNSTATE ){
1009 assert( !yyendofinput ); /* Impossible to shift the $ token */
1010 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
1011 yypParser->yyerrcnt--;
1012 yymajor = YYNOCODE;
1013 }else if( yyact < YYNSTATE + YYNRULE ){
1014 yy_reduce(yypParser,yyact-YYNSTATE);
1015 }else{
1016 assert( yyact == YY_ERROR_ACTION );
1017 #ifdef YYERRORSYMBOL
1018 int yymx;
1019 #endif
1020 #ifndef NDEBUG
1021 if( yyTraceFILE ){
1022 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
1023 }
1024 #endif
1025 #ifdef YYERRORSYMBOL
1026 /* A syntax error has occurred.
1027 ** The response to an error depends upon whether or not the
1028 ** grammar defines an error token "ERROR".
1029 **
1030 ** This is what we do if the grammar does define ERROR:
1031 **
1032 ** * Call the %syntax_error function.
1033 **
1034 ** * Begin popping the stack until we enter a state where
1035 ** it is legal to shift the error symbol, then shift
1036 ** the error symbol.
1037 **
1038 ** * Set the error count to three.
1039 **
1040 ** * Begin accepting and shifting new tokens. No new error
1041 ** processing will occur until three tokens have been
1042 ** shifted successfully.
1043 **
1044 */
1045 if( yypParser->yyerrcnt<0 ){
1046 yy_syntax_error(yypParser,yymajor,yyminorunion);
1047 }
1048 yymx = yypParser->yystack[yypParser->yyidx].major;
1049 if( yymx==YYERRORSYMBOL || yyerrorhit ){
1050 #ifndef NDEBUG
1051 if( yyTraceFILE ){
1052 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
1053 yyTracePrompt,yyTokenName[yymajor]);
1054 }
1055 #endif
1056 yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
1057 yymajor = YYNOCODE;
1058 }else{
1059 while(
1060 yypParser->yyidx >= 0 &&
1061 yymx != YYERRORSYMBOL &&
1062 (yyact = yy_find_reduce_action(
1063 yypParser->yystack[yypParser->yyidx].stateno,
1064 YYERRORSYMBOL)) >= YYNSTATE
1065 ){
1066 yy_pop_parser_stack(yypParser);
1067 }
1068 if( yypParser->yyidx < 0 || yymajor==0 ){
1069 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1070 yy_parse_failed(yypParser);
1071 yymajor = YYNOCODE;
1072 }else if( yymx!=YYERRORSYMBOL ){
1073 YYMINORTYPE u2;
1074 u2.YYERRSYMDT = 0;
1075 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
1076 }
1077 }
1078 yypParser->yyerrcnt = 3;
1079 yyerrorhit = 1;
1080 #elif defined(YYNOERRORRECOVERY)
1081 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
1082 ** do any kind of error recovery. Instead, simply invoke the syntax
1083 ** error routine and continue going as if nothing had happened.
1084 **
1085 ** Applications can set this macro (for example inside %include) if
1086 ** they intend to abandon the parse upon the first syntax error seen.
1087 */
1088 yy_syntax_error(yypParser,yymajor,yyminorunion);
1089 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1090 yymajor = YYNOCODE;
1091
1092 #else /* YYERRORSYMBOL is not defined */
1093 /* This is what we do if the grammar does not define ERROR:
1094 **
1095 ** * Report an error message, and throw away the input token.
1096 **
1097 ** * If the input token is $, then fail the parse.
1098 **
1099 ** As before, subsequent error messages are suppressed until
1100 ** three input tokens have been successfully shifted.
1101 */
1102 if( yypParser->yyerrcnt<=0 ){
1103 yy_syntax_error(yypParser,yymajor,yyminorunion);
1104 }
1105 yypParser->yyerrcnt = 3;
1106 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1107 if( yyendofinput ){
1108 yy_parse_failed(yypParser);
1109 }
1110 yymajor = YYNOCODE;
1111 #endif
1112 }
1113 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
1114 return;
1115 }
1116