1 /* Driver template for the LEMON parser generator.
2 ** The author disclaims copyright to this source code.
3 **
4 ** This version of "lempar.c" is modified, slightly, for use by SQLite.
5 ** The only modifications are the addition of a couple of NEVER()
6 ** macros to disable tests that are needed in the case of a general
7 ** LALR(1) grammar but which are always false in the
8 ** specific grammar used by SQLite.
9 */
10 /* First off, code is included that follows the "include" declaration
11 ** in the input grammar file. */
12 #include <stdio.h>
13 %%
14 /* Next is all token values, in a form suitable for use by makeheaders.
15 ** This section will be null unless lemon is run with the -m switch.
16 */
17 /*
18 ** These constants (all generated automatically by the parser generator)
19 ** specify the various kinds of tokens (terminals) that the parser
20 ** understands.
21 **
22 ** Each symbol here is a terminal symbol in the grammar.
23 */
24 %%
25 /* Make sure the INTERFACE macro is defined.
26 */
27 #ifndef INTERFACE
28 # define INTERFACE 1
29 #endif
30 /* The next thing included is series of defines which control
31 ** various aspects of the generated parser.
32 **    YYCODETYPE         is the data type used for storing terminal
33 **                       and nonterminal numbers.  "unsigned char" is
34 **                       used if there are fewer than 250 terminals
35 **                       and nonterminals.  "int" is used otherwise.
36 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
37 **                       to no legal terminal or nonterminal number.  This
38 **                       number is used to fill in empty slots of the hash
39 **                       table.
40 **    YYFALLBACK         If defined, this indicates that one or more tokens
41 **                       have fall-back values which should be used if the
42 **                       original value of the token will not parse.
43 **    YYACTIONTYPE       is the data type used for storing terminal
44 **                       and nonterminal numbers.  "unsigned char" is
45 **                       used if there are fewer than 250 rules and
46 **                       states combined.  "int" is used otherwise.
47 **    ParseTOKENTYPE     is the data type used for minor tokens given
48 **                       directly to the parser from the tokenizer.
49 **    YYMINORTYPE        is the data type used for all minor tokens.
50 **                       This is typically a union of many types, one of
51 **                       which is ParseTOKENTYPE.  The entry in the union
52 **                       for base tokens is called "yy0".
53 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
54 **                       zero the stack is dynamically sized using realloc()
55 **    ParseARG_SDECL     A static variable declaration for the %extra_argument
56 **    ParseARG_PDECL     A parameter declaration for the %extra_argument
57 **    ParseARG_STORE     Code to store %extra_argument into yypParser
58 **    ParseARG_FETCH     Code to extract %extra_argument from yypParser
59 **    YYNSTATE           the combined number of states.
60 **    YYNRULE            the number of rules in the grammar
61 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
62 **                       defined, then do no error processing.
63 */
64 %%
65 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
66 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
67 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
68 
69 /* The yyzerominor constant is used to initialize instances of
70 ** YYMINORTYPE objects to zero. */
71 static const YYMINORTYPE yyzerominor = { 0 };
72 
73 /* Define the yytestcase() macro to be a no-op if is not already defined
74 ** otherwise.
75 **
76 ** Applications can choose to define yytestcase() in the %include section
77 ** to a macro that can assist in verifying code coverage.  For production
78 ** code the yytestcase() macro should be turned off.  But it is useful
79 ** for testing.
80 */
81 #ifndef yytestcase
82 # define yytestcase(X)
83 #endif
84 
85 
86 /* Next are the tables used to determine what action to take based on the
87 ** current state and lookahead token.  These tables are used to implement
88 ** functions that take a state number and lookahead value and return an
89 ** action integer.
90 **
91 ** Suppose the action integer is N.  Then the action is determined as
92 ** follows
93 **
94 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
95 **                                      token onto the stack and goto state N.
96 **
97 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
98 **
99 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
100 **
101 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
102 **
103 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
104 **                                      slots in the yy_action[] table.
105 **
106 ** The action table is constructed as a single large table named yy_action[].
107 ** Given state S and lookahead X, the action is computed as
108 **
109 **      yy_action[ yy_shift_ofst[S] + X ]
110 **
111 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
112 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
113 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
114 ** and that yy_default[S] should be used instead.
115 **
116 ** The formula above is for computing the action when the lookahead is
117 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
118 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
119 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
120 ** YY_SHIFT_USE_DFLT.
121 **
122 ** The following are the tables generated in this section:
123 **
124 **  yy_action[]        A single table containing all actions.
125 **  yy_lookahead[]     A table containing the lookahead for each entry in
126 **                     yy_action.  Used to detect hash collisions.
127 **  yy_shift_ofst[]    For each state, the offset into yy_action for
128 **                     shifting terminals.
129 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
130 **                     shifting non-terminals after a reduce.
131 **  yy_default[]       Default action for each state.
132 */
133 %%
134 
135 /* The next table maps tokens into fallback tokens.  If a construct
136 ** like the following:
137 **
138 **      %fallback ID X Y Z.
139 **
140 ** appears in the grammar, then ID becomes a fallback token for X, Y,
141 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
142 ** but it does not parse, the type of the token is changed to ID and
143 ** the parse is retried before an error is thrown.
144 */
145 #ifdef YYFALLBACK
146 static const YYCODETYPE yyFallback[] = {
147 %%
148 };
149 #endif /* YYFALLBACK */
150 
151 /* The following structure represents a single element of the
152 ** parser's stack.  Information stored includes:
153 **
154 **   +  The state number for the parser at this level of the stack.
155 **
156 **   +  The value of the token stored at this level of the stack.
157 **      (In other words, the "major" token.)
158 **
159 **   +  The semantic value stored at this level of the stack.  This is
160 **      the information used by the action routines in the grammar.
161 **      It is sometimes called the "minor" token.
162 */
163 struct yyStackEntry {
164   YYACTIONTYPE stateno;  /* The state-number */
165   YYCODETYPE major;      /* The major token value.  This is the code
166                          ** number for the token at this stack level */
167   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
168                          ** is the value of the token  */
169 };
170 typedef struct yyStackEntry yyStackEntry;
171 
172 /* The state of the parser is completely contained in an instance of
173 ** the following structure */
174 struct yyParser {
175   int yyidx;                    /* Index of top element in stack */
176 #ifdef YYTRACKMAXSTACKDEPTH
177   int yyidxMax;                 /* Maximum value of yyidx */
178 #endif
179   int yyerrcnt;                 /* Shifts left before out of the error */
180   ParseARG_SDECL                /* A place to hold %extra_argument */
181 #if YYSTACKDEPTH<=0
182   int yystksz;                  /* Current side of the stack */
183   yyStackEntry *yystack;        /* The parser's stack */
184 #else
185   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
186 #endif
187 };
188 typedef struct yyParser yyParser;
189 
190 #ifndef NDEBUG
191 #include <stdio.h>
192 static FILE *yyTraceFILE = 0;
193 static char *yyTracePrompt = 0;
194 #endif /* NDEBUG */
195 
196 #ifndef NDEBUG
197 /*
198 ** Turn parser tracing on by giving a stream to which to write the trace
199 ** and a prompt to preface each trace message.  Tracing is turned off
200 ** by making either argument NULL
201 **
202 ** Inputs:
203 ** <ul>
204 ** <li> A FILE* to which trace output should be written.
205 **      If NULL, then tracing is turned off.
206 ** <li> A prefix string written at the beginning of every
207 **      line of trace output.  If NULL, then tracing is
208 **      turned off.
209 ** </ul>
210 **
211 ** Outputs:
212 ** None.
213 */
ParseTrace(FILE * TraceFILE,char * zTracePrompt)214 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
215   yyTraceFILE = TraceFILE;
216   yyTracePrompt = zTracePrompt;
217   if( yyTraceFILE==0 ) yyTracePrompt = 0;
218   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
219 }
220 #endif /* NDEBUG */
221 
222 #ifndef NDEBUG
223 /* For tracing shifts, the names of all terminals and nonterminals
224 ** are required.  The following table supplies these names */
225 static const char *const yyTokenName[] = {
226 %%
227 };
228 #endif /* NDEBUG */
229 
230 #ifndef NDEBUG
231 /* For tracing reduce actions, the names of all rules are required.
232 */
233 static const char *const yyRuleName[] = {
234 %%
235 };
236 #endif /* NDEBUG */
237 
238 
239 #if YYSTACKDEPTH<=0
240 /*
241 ** Try to increase the size of the parser stack.
242 */
yyGrowStack(yyParser * p)243 static void yyGrowStack(yyParser *p){
244   int newSize;
245   yyStackEntry *pNew;
246 
247   newSize = p->yystksz*2 + 100;
248   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
249   if( pNew ){
250     p->yystack = pNew;
251     p->yystksz = newSize;
252 #ifndef NDEBUG
253     if( yyTraceFILE ){
254       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
255               yyTracePrompt, p->yystksz);
256     }
257 #endif
258   }
259 }
260 #endif
261 
262 /*
263 ** This function allocates a new parser.
264 ** The only argument is a pointer to a function which works like
265 ** malloc.
266 **
267 ** Inputs:
268 ** A pointer to the function used to allocate memory.
269 **
270 ** Outputs:
271 ** A pointer to a parser.  This pointer is used in subsequent calls
272 ** to Parse and ParseFree.
273 */
ParseAlloc(void * (* mallocProc)(size_t))274 void *ParseAlloc(void *(*mallocProc)(size_t)){
275   yyParser *pParser;
276   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
277   if( pParser ){
278     pParser->yyidx = -1;
279 #ifdef YYTRACKMAXSTACKDEPTH
280     pParser->yyidxMax = 0;
281 #endif
282 #if YYSTACKDEPTH<=0
283     pParser->yystack = NULL;
284     pParser->yystksz = 0;
285     yyGrowStack(pParser);
286 #endif
287   }
288   return pParser;
289 }
290 
291 /* The following function deletes the value associated with a
292 ** symbol.  The symbol can be either a terminal or nonterminal.
293 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
294 ** the value.
295 */
yy_destructor(yyParser * yypParser,YYCODETYPE yymajor,YYMINORTYPE * yypminor)296 static void yy_destructor(
297   yyParser *yypParser,    /* The parser */
298   YYCODETYPE yymajor,     /* Type code for object to destroy */
299   YYMINORTYPE *yypminor   /* The object to be destroyed */
300 ){
301   ParseARG_FETCH;
302   switch( yymajor ){
303     /* Here is inserted the actions which take place when a
304     ** terminal or non-terminal is destroyed.  This can happen
305     ** when the symbol is popped from the stack during a
306     ** reduce or during error processing or when a parser is
307     ** being destroyed before it is finished parsing.
308     **
309     ** Note: during a reduce, the only symbols destroyed are those
310     ** which appear on the RHS of the rule, but which are not used
311     ** inside the C code.
312     */
313 %%
314     default:  break;   /* If no destructor action specified: do nothing */
315   }
316 }
317 
318 /*
319 ** Pop the parser's stack once.
320 **
321 ** If there is a destructor routine associated with the token which
322 ** is popped from the stack, then call it.
323 **
324 ** Return the major token number for the symbol popped.
325 */
yy_pop_parser_stack(yyParser * pParser)326 static int yy_pop_parser_stack(yyParser *pParser){
327   YYCODETYPE yymajor;
328   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
329 
330   /* There is no mechanism by which the parser stack can be popped below
331   ** empty in SQLite.  */
332   if( NEVER(pParser->yyidx<0) ) return 0;
333 #ifndef NDEBUG
334   if( yyTraceFILE && pParser->yyidx>=0 ){
335     fprintf(yyTraceFILE,"%sPopping %s\n",
336       yyTracePrompt,
337       yyTokenName[yytos->major]);
338   }
339 #endif
340   yymajor = yytos->major;
341   yy_destructor(pParser, yymajor, &yytos->minor);
342   pParser->yyidx--;
343   return yymajor;
344 }
345 
346 /*
347 ** Deallocate and destroy a parser.  Destructors are all called for
348 ** all stack elements before shutting the parser down.
349 **
350 ** Inputs:
351 ** <ul>
352 ** <li>  A pointer to the parser.  This should be a pointer
353 **       obtained from ParseAlloc.
354 ** <li>  A pointer to a function used to reclaim memory obtained
355 **       from malloc.
356 ** </ul>
357 */
ParseFree(void * p,void (* freeProc)(void *))358 void ParseFree(
359   void *p,                    /* The parser to be deleted */
360   void (*freeProc)(void*)     /* Function used to reclaim memory */
361 ){
362   yyParser *pParser = (yyParser*)p;
363   /* In SQLite, we never try to destroy a parser that was not successfully
364   ** created in the first place. */
365   if( NEVER(pParser==0) ) return;
366   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
367 #if YYSTACKDEPTH<=0
368   free(pParser->yystack);
369 #endif
370   (*freeProc)((void*)pParser);
371 }
372 
373 /*
374 ** Return the peak depth of the stack for a parser.
375 */
376 #ifdef YYTRACKMAXSTACKDEPTH
ParseStackPeak(void * p)377 int ParseStackPeak(void *p){
378   yyParser *pParser = (yyParser*)p;
379   return pParser->yyidxMax;
380 }
381 #endif
382 
383 /*
384 ** Find the appropriate action for a parser given the terminal
385 ** look-ahead token iLookAhead.
386 **
387 ** If the look-ahead token is YYNOCODE, then check to see if the action is
388 ** independent of the look-ahead.  If it is, return the action, otherwise
389 ** return YY_NO_ACTION.
390 */
yy_find_shift_action(yyParser * pParser,YYCODETYPE iLookAhead)391 static int yy_find_shift_action(
392   yyParser *pParser,        /* The parser */
393   YYCODETYPE iLookAhead     /* The look-ahead token */
394 ){
395   int i;
396   int stateno = pParser->yystack[pParser->yyidx].stateno;
397 
398   if( stateno>YY_SHIFT_COUNT
399    || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
400     return yy_default[stateno];
401   }
402   assert( iLookAhead!=YYNOCODE );
403   i += iLookAhead;
404   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
405     if( iLookAhead>0 ){
406 #ifdef YYFALLBACK
407       YYCODETYPE iFallback;            /* Fallback token */
408       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
409              && (iFallback = yyFallback[iLookAhead])!=0 ){
410 #ifndef NDEBUG
411         if( yyTraceFILE ){
412           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
413              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
414         }
415 #endif
416         return yy_find_shift_action(pParser, iFallback);
417       }
418 #endif
419 #ifdef YYWILDCARD
420       {
421         int j = i - iLookAhead + YYWILDCARD;
422         if(
423 #if YY_SHIFT_MIN+YYWILDCARD<0
424           j>=0 &&
425 #endif
426 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
427           j<YY_ACTTAB_COUNT &&
428 #endif
429           yy_lookahead[j]==YYWILDCARD
430         ){
431 #ifndef NDEBUG
432           if( yyTraceFILE ){
433             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
434                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
435           }
436 #endif /* NDEBUG */
437           return yy_action[j];
438         }
439       }
440 #endif /* YYWILDCARD */
441     }
442     return yy_default[stateno];
443   }else{
444     return yy_action[i];
445   }
446 }
447 
448 /*
449 ** Find the appropriate action for a parser given the non-terminal
450 ** look-ahead token iLookAhead.
451 **
452 ** If the look-ahead token is YYNOCODE, then check to see if the action is
453 ** independent of the look-ahead.  If it is, return the action, otherwise
454 ** return YY_NO_ACTION.
455 */
yy_find_reduce_action(int stateno,YYCODETYPE iLookAhead)456 static int yy_find_reduce_action(
457   int stateno,              /* Current state number */
458   YYCODETYPE iLookAhead     /* The look-ahead token */
459 ){
460   int i;
461 #ifdef YYERRORSYMBOL
462   if( stateno>YY_REDUCE_COUNT ){
463     return yy_default[stateno];
464   }
465 #else
466   assert( stateno<=YY_REDUCE_COUNT );
467 #endif
468   i = yy_reduce_ofst[stateno];
469   assert( i!=YY_REDUCE_USE_DFLT );
470   assert( iLookAhead!=YYNOCODE );
471   i += iLookAhead;
472 #ifdef YYERRORSYMBOL
473   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
474     return yy_default[stateno];
475   }
476 #else
477   assert( i>=0 && i<YY_ACTTAB_COUNT );
478   assert( yy_lookahead[i]==iLookAhead );
479 #endif
480   return yy_action[i];
481 }
482 
483 /*
484 ** The following routine is called if the stack overflows.
485 */
yyStackOverflow(yyParser * yypParser,YYMINORTYPE * yypMinor)486 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
487    ParseARG_FETCH;
488    yypParser->yyidx--;
489 #ifndef NDEBUG
490    if( yyTraceFILE ){
491      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
492    }
493 #endif
494    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
495    /* Here code is inserted which will execute if the parser
496    ** stack every overflows */
497 %%
498    ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
499 }
500 
501 /*
502 ** Perform a shift action.
503 */
yy_shift(yyParser * yypParser,int yyNewState,int yyMajor,YYMINORTYPE * yypMinor)504 static void yy_shift(
505   yyParser *yypParser,          /* The parser to be shifted */
506   int yyNewState,               /* The new state to shift in */
507   int yyMajor,                  /* The major token to shift in */
508   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
509 ){
510   yyStackEntry *yytos;
511   yypParser->yyidx++;
512 #ifdef YYTRACKMAXSTACKDEPTH
513   if( yypParser->yyidx>yypParser->yyidxMax ){
514     yypParser->yyidxMax = yypParser->yyidx;
515   }
516 #endif
517 #if YYSTACKDEPTH>0
518   if( yypParser->yyidx>=YYSTACKDEPTH ){
519     yyStackOverflow(yypParser, yypMinor);
520     return;
521   }
522 #else
523   if( yypParser->yyidx>=yypParser->yystksz ){
524     yyGrowStack(yypParser);
525     if( yypParser->yyidx>=yypParser->yystksz ){
526       yyStackOverflow(yypParser, yypMinor);
527       return;
528     }
529   }
530 #endif
531   yytos = &yypParser->yystack[yypParser->yyidx];
532   yytos->stateno = (YYACTIONTYPE)yyNewState;
533   yytos->major = (YYCODETYPE)yyMajor;
534   yytos->minor = *yypMinor;
535 #ifndef NDEBUG
536   if( yyTraceFILE && yypParser->yyidx>0 ){
537     int i;
538     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
539     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
540     for(i=1; i<=yypParser->yyidx; i++)
541       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
542     fprintf(yyTraceFILE,"\n");
543   }
544 #endif
545 }
546 
547 /* The following table contains information about every rule that
548 ** is used during the reduce.
549 */
550 static const struct {
551   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
552   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
553 } yyRuleInfo[] = {
554 %%
555 };
556 
557 static void yy_accept(yyParser*);  /* Forward Declaration */
558 
559 /*
560 ** Perform a reduce action and the shift that must immediately
561 ** follow the reduce.
562 */
yy_reduce(yyParser * yypParser,int yyruleno)563 static void yy_reduce(
564   yyParser *yypParser,         /* The parser */
565   int yyruleno                 /* Number of the rule by which to reduce */
566 ){
567   int yygoto;                     /* The next state */
568   int yyact;                      /* The next action */
569   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
570   yyStackEntry *yymsp;            /* The top of the parser's stack */
571   int yysize;                     /* Amount to pop the stack */
572   ParseARG_FETCH;
573   yymsp = &yypParser->yystack[yypParser->yyidx];
574 #ifndef NDEBUG
575   if( yyTraceFILE && yyruleno>=0
576         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
577     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
578       yyRuleName[yyruleno]);
579   }
580 #endif /* NDEBUG */
581 
582   /* Silence complaints from purify about yygotominor being uninitialized
583   ** in some cases when it is copied into the stack after the following
584   ** switch.  yygotominor is uninitialized when a rule reduces that does
585   ** not set the value of its left-hand side nonterminal.  Leaving the
586   ** value of the nonterminal uninitialized is utterly harmless as long
587   ** as the value is never used.  So really the only thing this code
588   ** accomplishes is to quieten purify.
589   **
590   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
591   ** without this code, their parser segfaults.  I'm not sure what there
592   ** parser is doing to make this happen.  This is the second bug report
593   ** from wireshark this week.  Clearly they are stressing Lemon in ways
594   ** that it has not been previously stressed...  (SQLite ticket #2172)
595   */
596   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
597   yygotominor = yyzerominor;
598 
599 
600   switch( yyruleno ){
601   /* Beginning here are the reduction cases.  A typical example
602   ** follows:
603   **   case 0:
604   **  #line <lineno> <grammarfile>
605   **     { ... }           // User supplied code
606   **  #line <lineno> <thisfile>
607   **     break;
608   */
609 %%
610   };
611   yygoto = yyRuleInfo[yyruleno].lhs;
612   yysize = yyRuleInfo[yyruleno].nrhs;
613   yypParser->yyidx -= yysize;
614   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
615   if( yyact < YYNSTATE ){
616 #ifdef NDEBUG
617     /* If we are not debugging and the reduce action popped at least
618     ** one element off the stack, then we can push the new element back
619     ** onto the stack here, and skip the stack overflow test in yy_shift().
620     ** That gives a significant speed improvement. */
621     if( yysize ){
622       yypParser->yyidx++;
623       yymsp -= yysize-1;
624       yymsp->stateno = (YYACTIONTYPE)yyact;
625       yymsp->major = (YYCODETYPE)yygoto;
626       yymsp->minor = yygotominor;
627     }else
628 #endif
629     {
630       yy_shift(yypParser,yyact,yygoto,&yygotominor);
631     }
632   }else{
633     assert( yyact == YYNSTATE + YYNRULE + 1 );
634     yy_accept(yypParser);
635   }
636 }
637 
638 /*
639 ** The following code executes when the parse fails
640 */
641 #ifndef YYNOERRORRECOVERY
yy_parse_failed(yyParser * yypParser)642 static void yy_parse_failed(
643   yyParser *yypParser           /* The parser */
644 ){
645   ParseARG_FETCH;
646 #ifndef NDEBUG
647   if( yyTraceFILE ){
648     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
649   }
650 #endif
651   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
652   /* Here code is inserted which will be executed whenever the
653   ** parser fails */
654 %%
655   ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
656 }
657 #endif /* YYNOERRORRECOVERY */
658 
659 /*
660 ** The following code executes when a syntax error first occurs.
661 */
yy_syntax_error(yyParser * yypParser,int yymajor,YYMINORTYPE yyminor)662 static void yy_syntax_error(
663   yyParser *yypParser,           /* The parser */
664   int yymajor,                   /* The major type of the error token */
665   YYMINORTYPE yyminor            /* The minor type of the error token */
666 ){
667   ParseARG_FETCH;
668 #define TOKEN (yyminor.yy0)
669 %%
670   ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
671 }
672 
673 /*
674 ** The following is executed when the parser accepts
675 */
yy_accept(yyParser * yypParser)676 static void yy_accept(
677   yyParser *yypParser           /* The parser */
678 ){
679   ParseARG_FETCH;
680 #ifndef NDEBUG
681   if( yyTraceFILE ){
682     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
683   }
684 #endif
685   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
686   /* Here code is inserted which will be executed whenever the
687   ** parser accepts */
688 %%
689   ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
690 }
691 
692 /* The main parser program.
693 ** The first argument is a pointer to a structure obtained from
694 ** "ParseAlloc" which describes the current state of the parser.
695 ** The second argument is the major token number.  The third is
696 ** the minor token.  The fourth optional argument is whatever the
697 ** user wants (and specified in the grammar) and is available for
698 ** use by the action routines.
699 **
700 ** Inputs:
701 ** <ul>
702 ** <li> A pointer to the parser (an opaque structure.)
703 ** <li> The major token number.
704 ** <li> The minor token number.
705 ** <li> An option argument of a grammar-specified type.
706 ** </ul>
707 **
708 ** Outputs:
709 ** None.
710 */
Parse(void * yyp,int yymajor,ParseTOKENTYPE yyminor ParseARG_PDECL)711 void Parse(
712   void *yyp,                   /* The parser */
713   int yymajor,                 /* The major token code number */
714   ParseTOKENTYPE yyminor       /* The value for the token */
715   ParseARG_PDECL               /* Optional %extra_argument parameter */
716 ){
717   YYMINORTYPE yyminorunion;
718   int yyact;            /* The parser action. */
719   int yyendofinput;     /* True if we are at the end of input */
720 #ifdef YYERRORSYMBOL
721   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
722 #endif
723   yyParser *yypParser;  /* The parser */
724 
725   /* (re)initialize the parser, if necessary */
726   yypParser = (yyParser*)yyp;
727   if( yypParser->yyidx<0 ){
728 #if YYSTACKDEPTH<=0
729     if( yypParser->yystksz <=0 ){
730       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
731       yyminorunion = yyzerominor;
732       yyStackOverflow(yypParser, &yyminorunion);
733       return;
734     }
735 #endif
736     yypParser->yyidx = 0;
737     yypParser->yyerrcnt = -1;
738     yypParser->yystack[0].stateno = 0;
739     yypParser->yystack[0].major = 0;
740   }
741   yyminorunion.yy0 = yyminor;
742   yyendofinput = (yymajor==0);
743   ParseARG_STORE;
744 
745 #ifndef NDEBUG
746   if( yyTraceFILE ){
747     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
748   }
749 #endif
750 
751   do{
752     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
753     if( yyact<YYNSTATE ){
754       assert( !yyendofinput );  /* Impossible to shift the $ token */
755       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
756       yypParser->yyerrcnt--;
757       yymajor = YYNOCODE;
758     }else if( yyact < YYNSTATE + YYNRULE ){
759       yy_reduce(yypParser,yyact-YYNSTATE);
760     }else{
761       assert( yyact == YY_ERROR_ACTION );
762 #ifdef YYERRORSYMBOL
763       int yymx;
764 #endif
765 #ifndef NDEBUG
766       if( yyTraceFILE ){
767         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
768       }
769 #endif
770 #ifdef YYERRORSYMBOL
771       /* A syntax error has occurred.
772       ** The response to an error depends upon whether or not the
773       ** grammar defines an error token "ERROR".
774       **
775       ** This is what we do if the grammar does define ERROR:
776       **
777       **  * Call the %syntax_error function.
778       **
779       **  * Begin popping the stack until we enter a state where
780       **    it is legal to shift the error symbol, then shift
781       **    the error symbol.
782       **
783       **  * Set the error count to three.
784       **
785       **  * Begin accepting and shifting new tokens.  No new error
786       **    processing will occur until three tokens have been
787       **    shifted successfully.
788       **
789       */
790       if( yypParser->yyerrcnt<0 ){
791         yy_syntax_error(yypParser,yymajor,yyminorunion);
792       }
793       yymx = yypParser->yystack[yypParser->yyidx].major;
794       if( yymx==YYERRORSYMBOL || yyerrorhit ){
795 #ifndef NDEBUG
796         if( yyTraceFILE ){
797           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
798              yyTracePrompt,yyTokenName[yymajor]);
799         }
800 #endif
801         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
802         yymajor = YYNOCODE;
803       }else{
804          while(
805           yypParser->yyidx >= 0 &&
806           yymx != YYERRORSYMBOL &&
807           (yyact = yy_find_reduce_action(
808                         yypParser->yystack[yypParser->yyidx].stateno,
809                         YYERRORSYMBOL)) >= YYNSTATE
810         ){
811           yy_pop_parser_stack(yypParser);
812         }
813         if( yypParser->yyidx < 0 || yymajor==0 ){
814           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
815           yy_parse_failed(yypParser);
816           yymajor = YYNOCODE;
817         }else if( yymx!=YYERRORSYMBOL ){
818           YYMINORTYPE u2;
819           u2.YYERRSYMDT = 0;
820           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
821         }
822       }
823       yypParser->yyerrcnt = 3;
824       yyerrorhit = 1;
825 #elif defined(YYNOERRORRECOVERY)
826       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
827       ** do any kind of error recovery.  Instead, simply invoke the syntax
828       ** error routine and continue going as if nothing had happened.
829       **
830       ** Applications can set this macro (for example inside %include) if
831       ** they intend to abandon the parse upon the first syntax error seen.
832       */
833       yy_syntax_error(yypParser,yymajor,yyminorunion);
834       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
835       yymajor = YYNOCODE;
836 
837 #else  /* YYERRORSYMBOL is not defined */
838       /* This is what we do if the grammar does not define ERROR:
839       **
840       **  * Report an error message, and throw away the input token.
841       **
842       **  * If the input token is $, then fail the parse.
843       **
844       ** As before, subsequent error messages are suppressed until
845       ** three input tokens have been successfully shifted.
846       */
847       if( yypParser->yyerrcnt<=0 ){
848         yy_syntax_error(yypParser,yymajor,yyminorunion);
849       }
850       yypParser->yyerrcnt = 3;
851       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
852       if( yyendofinput ){
853         yy_parse_failed(yypParser);
854       }
855       yymajor = YYNOCODE;
856 #endif
857     }
858   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
859   return;
860 }
861