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