xref: /sqlite-3.40.0/tool/lempar.c (revision fd39c587)
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_PARAM     Code to pass %extra_argument as a subroutine parameter
70 **    ParseARG_STORE     Code to store %extra_argument into yypParser
71 **    ParseARG_FETCH     Code to extract %extra_argument from yypParser
72 **    ParseCTX_*         As ParseARG_ except for %extra_context
73 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
74 **                       defined, then do no error processing.
75 **    YYNSTATE           the combined number of states.
76 **    YYNRULE            the number of rules in the grammar
77 **    YYNTOKEN           Number of terminal symbols
78 **    YY_MAX_SHIFT       Maximum value for shift actions
79 **    YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
80 **    YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
81 **    YY_ERROR_ACTION    The yy_action[] code for syntax error
82 **    YY_ACCEPT_ACTION   The yy_action[] code for accept
83 **    YY_NO_ACTION       The yy_action[] code for no-op
84 **    YY_MIN_REDUCE      Minimum value for reduce actions
85 **    YY_MAX_REDUCE      Maximum value for reduce actions
86 */
87 #ifndef INTERFACE
88 # define INTERFACE 1
89 #endif
90 /************* Begin control #defines *****************************************/
91 %%
92 /************* End control #defines *******************************************/
93 
94 /* Define the yytestcase() macro to be a no-op if is not already defined
95 ** otherwise.
96 **
97 ** Applications can choose to define yytestcase() in the %include section
98 ** to a macro that can assist in verifying code coverage.  For production
99 ** code the yytestcase() macro should be turned off.  But it is useful
100 ** for testing.
101 */
102 #ifndef yytestcase
103 # define yytestcase(X)
104 #endif
105 
106 
107 /* Next are the tables used to determine what action to take based on the
108 ** current state and lookahead token.  These tables are used to implement
109 ** functions that take a state number and lookahead value and return an
110 ** action integer.
111 **
112 ** Suppose the action integer is N.  Then the action is determined as
113 ** follows
114 **
115 **   0 <= N <= YY_MAX_SHIFT             Shift N.  That is, push the lookahead
116 **                                      token onto the stack and goto state N.
117 **
118 **   N between YY_MIN_SHIFTREDUCE       Shift to an arbitrary state then
119 **     and YY_MAX_SHIFTREDUCE           reduce by rule N-YY_MIN_SHIFTREDUCE.
120 **
121 **   N == YY_ERROR_ACTION               A syntax error has occurred.
122 **
123 **   N == YY_ACCEPT_ACTION              The parser accepts its input.
124 **
125 **   N == YY_NO_ACTION                  No such action.  Denotes unused
126 **                                      slots in the yy_action[] table.
127 **
128 **   N between YY_MIN_REDUCE            Reduce by rule N-YY_MIN_REDUCE
129 **     and YY_MAX_REDUCE
130 **
131 ** The action table is constructed as a single large table named yy_action[].
132 ** Given state S and lookahead X, the action is computed as either:
133 **
134 **    (A)   N = yy_action[ yy_shift_ofst[S] + X ]
135 **    (B)   N = yy_default[S]
136 **
137 ** The (A) formula is preferred.  The B formula is used instead if
138 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
139 **
140 ** The formulas above are for computing the action when the lookahead is
141 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
142 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
143 ** the yy_shift_ofst[] array.
144 **
145 ** The following are the tables generated in this section:
146 **
147 **  yy_action[]        A single table containing all actions.
148 **  yy_lookahead[]     A table containing the lookahead for each entry in
149 **                     yy_action.  Used to detect hash collisions.
150 **  yy_shift_ofst[]    For each state, the offset into yy_action for
151 **                     shifting terminals.
152 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
153 **                     shifting non-terminals after a reduce.
154 **  yy_default[]       Default action for each state.
155 **
156 *********** Begin parsing tables **********************************************/
157 %%
158 /********** End of lemon-generated parsing tables *****************************/
159 
160 /* The next table maps tokens (terminal symbols) into fallback tokens.
161 ** If a construct like the following:
162 **
163 **      %fallback ID X Y Z.
164 **
165 ** appears in the grammar, then ID becomes a fallback token for X, Y,
166 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
167 ** but it does not parse, the type of the token is changed to ID and
168 ** the parse is retried before an error is thrown.
169 **
170 ** This feature can be used, for example, to cause some keywords in a language
171 ** to revert to identifiers if they keyword does not apply in the context where
172 ** it appears.
173 */
174 #ifdef YYFALLBACK
175 static const YYCODETYPE yyFallback[] = {
176 %%
177 };
178 #endif /* YYFALLBACK */
179 
180 /* The following structure represents a single element of the
181 ** parser's stack.  Information stored includes:
182 **
183 **   +  The state number for the parser at this level of the stack.
184 **
185 **   +  The value of the token stored at this level of the stack.
186 **      (In other words, the "major" token.)
187 **
188 **   +  The semantic value stored at this level of the stack.  This is
189 **      the information used by the action routines in the grammar.
190 **      It is sometimes called the "minor" token.
191 **
192 ** After the "shift" half of a SHIFTREDUCE action, the stateno field
193 ** actually contains the reduce action for the second half of the
194 ** SHIFTREDUCE.
195 */
196 struct yyStackEntry {
197   YYACTIONTYPE stateno;  /* The state-number, or reduce action in SHIFTREDUCE */
198   YYCODETYPE major;      /* The major token value.  This is the code
199                          ** number for the token at this stack level */
200   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
201                          ** is the value of the token  */
202 };
203 typedef struct yyStackEntry yyStackEntry;
204 
205 /* The state of the parser is completely contained in an instance of
206 ** the following structure */
207 struct yyParser {
208   yyStackEntry *yytos;          /* Pointer to top element of the stack */
209 #ifdef YYTRACKMAXSTACKDEPTH
210   int yyhwm;                    /* High-water mark of the stack */
211 #endif
212 #ifndef YYNOERRORRECOVERY
213   int yyerrcnt;                 /* Shifts left before out of the error */
214 #endif
215   ParseARG_SDECL                /* A place to hold %extra_argument */
216   ParseCTX_SDECL                /* A place to hold %extra_context */
217 #if YYSTACKDEPTH<=0
218   int yystksz;                  /* Current side of the stack */
219   yyStackEntry *yystack;        /* The parser's stack */
220   yyStackEntry yystk0;          /* First stack entry */
221 #else
222   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
223   yyStackEntry *yystackEnd;            /* Last entry in the stack */
224 #endif
225 };
226 typedef struct yyParser yyParser;
227 
228 #ifndef NDEBUG
229 #include <stdio.h>
230 static FILE *yyTraceFILE = 0;
231 static char *yyTracePrompt = 0;
232 #endif /* NDEBUG */
233 
234 #ifndef NDEBUG
235 /*
236 ** Turn parser tracing on by giving a stream to which to write the trace
237 ** and a prompt to preface each trace message.  Tracing is turned off
238 ** by making either argument NULL
239 **
240 ** Inputs:
241 ** <ul>
242 ** <li> A FILE* to which trace output should be written.
243 **      If NULL, then tracing is turned off.
244 ** <li> A prefix string written at the beginning of every
245 **      line of trace output.  If NULL, then tracing is
246 **      turned off.
247 ** </ul>
248 **
249 ** Outputs:
250 ** None.
251 */
252 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
253   yyTraceFILE = TraceFILE;
254   yyTracePrompt = zTracePrompt;
255   if( yyTraceFILE==0 ) yyTracePrompt = 0;
256   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
257 }
258 #endif /* NDEBUG */
259 
260 #if defined(YYCOVERAGE) || !defined(NDEBUG)
261 /* For tracing shifts, the names of all terminals and nonterminals
262 ** are required.  The following table supplies these names */
263 static const char *const yyTokenName[] = {
264 %%
265 };
266 #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
267 
268 #ifndef NDEBUG
269 /* For tracing reduce actions, the names of all rules are required.
270 */
271 static const char *const yyRuleName[] = {
272 %%
273 };
274 #endif /* NDEBUG */
275 
276 
277 #if YYSTACKDEPTH<=0
278 /*
279 ** Try to increase the size of the parser stack.  Return the number
280 ** of errors.  Return 0 on success.
281 */
282 static int yyGrowStack(yyParser *p){
283   int newSize;
284   int idx;
285   yyStackEntry *pNew;
286 
287   newSize = p->yystksz*2 + 100;
288   idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
289   if( p->yystack==&p->yystk0 ){
290     pNew = malloc(newSize*sizeof(pNew[0]));
291     if( pNew ) pNew[0] = p->yystk0;
292   }else{
293     pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
294   }
295   if( pNew ){
296     p->yystack = pNew;
297     p->yytos = &p->yystack[idx];
298 #ifndef NDEBUG
299     if( yyTraceFILE ){
300       fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
301               yyTracePrompt, p->yystksz, newSize);
302     }
303 #endif
304     p->yystksz = newSize;
305   }
306   return pNew==0;
307 }
308 #endif
309 
310 /* Datatype of the argument to the memory allocated passed as the
311 ** second argument to ParseAlloc() below.  This can be changed by
312 ** putting an appropriate #define in the %include section of the input
313 ** grammar.
314 */
315 #ifndef YYMALLOCARGTYPE
316 # define YYMALLOCARGTYPE size_t
317 #endif
318 
319 /* Initialize a new parser that has already been allocated.
320 */
321 void ParseInit(void *yypRawParser ParseCTX_PDECL){
322   yyParser *yypParser = (yyParser*)yypRawParser;
323   ParseCTX_STORE
324 #ifdef YYTRACKMAXSTACKDEPTH
325   yypParser->yyhwm = 0;
326 #endif
327 #if YYSTACKDEPTH<=0
328   yypParser->yytos = NULL;
329   yypParser->yystack = NULL;
330   yypParser->yystksz = 0;
331   if( yyGrowStack(yypParser) ){
332     yypParser->yystack = &yypParser->yystk0;
333     yypParser->yystksz = 1;
334   }
335 #endif
336 #ifndef YYNOERRORRECOVERY
337   yypParser->yyerrcnt = -1;
338 #endif
339   yypParser->yytos = yypParser->yystack;
340   yypParser->yystack[0].stateno = 0;
341   yypParser->yystack[0].major = 0;
342 #if YYSTACKDEPTH>0
343   yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
344 #endif
345 }
346 
347 #ifndef Parse_ENGINEALWAYSONSTACK
348 /*
349 ** This function allocates a new parser.
350 ** The only argument is a pointer to a function which works like
351 ** malloc.
352 **
353 ** Inputs:
354 ** A pointer to the function used to allocate memory.
355 **
356 ** Outputs:
357 ** A pointer to a parser.  This pointer is used in subsequent calls
358 ** to Parse and ParseFree.
359 */
360 void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){
361   yyParser *yypParser;
362   yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
363   if( yypParser ){
364     ParseCTX_STORE
365     ParseInit(yypParser ParseCTX_PARAM);
366   }
367   return (void*)yypParser;
368 }
369 #endif /* Parse_ENGINEALWAYSONSTACK */
370 
371 
372 /* The following function deletes the "minor type" or semantic value
373 ** associated with a symbol.  The symbol can be either a terminal
374 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
375 ** a pointer to the value to be deleted.  The code used to do the
376 ** deletions is derived from the %destructor and/or %token_destructor
377 ** directives of the input grammar.
378 */
379 static void yy_destructor(
380   yyParser *yypParser,    /* The parser */
381   YYCODETYPE yymajor,     /* Type code for object to destroy */
382   YYMINORTYPE *yypminor   /* The object to be destroyed */
383 ){
384   ParseARG_FETCH
385   ParseCTX_FETCH
386   switch( yymajor ){
387     /* Here is inserted the actions which take place when a
388     ** terminal or non-terminal is destroyed.  This can happen
389     ** when the symbol is popped from the stack during a
390     ** reduce or during error processing or when a parser is
391     ** being destroyed before it is finished parsing.
392     **
393     ** Note: during a reduce, the only symbols destroyed are those
394     ** which appear on the RHS of the rule, but which are *not* used
395     ** inside the C code.
396     */
397 /********* Begin destructor definitions ***************************************/
398 %%
399 /********* End destructor definitions *****************************************/
400     default:  break;   /* If no destructor action specified: do nothing */
401   }
402 }
403 
404 /*
405 ** Pop the parser's stack once.
406 **
407 ** If there is a destructor routine associated with the token which
408 ** is popped from the stack, then call it.
409 */
410 static void yy_pop_parser_stack(yyParser *pParser){
411   yyStackEntry *yytos;
412   assert( pParser->yytos!=0 );
413   assert( pParser->yytos > pParser->yystack );
414   yytos = pParser->yytos--;
415 #ifndef NDEBUG
416   if( yyTraceFILE ){
417     fprintf(yyTraceFILE,"%sPopping %s\n",
418       yyTracePrompt,
419       yyTokenName[yytos->major]);
420   }
421 #endif
422   yy_destructor(pParser, yytos->major, &yytos->minor);
423 }
424 
425 /*
426 ** Clear all secondary memory allocations from the parser
427 */
428 void ParseFinalize(void *p){
429   yyParser *pParser = (yyParser*)p;
430   while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
431 #if YYSTACKDEPTH<=0
432   if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
433 #endif
434 }
435 
436 #ifndef Parse_ENGINEALWAYSONSTACK
437 /*
438 ** Deallocate and destroy a parser.  Destructors are called for
439 ** all stack elements before shutting the parser down.
440 **
441 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
442 ** is defined in a %include section of the input grammar) then it is
443 ** assumed that the input pointer is never NULL.
444 */
445 void ParseFree(
446   void *p,                    /* The parser to be deleted */
447   void (*freeProc)(void*)     /* Function used to reclaim memory */
448 ){
449 #ifndef YYPARSEFREENEVERNULL
450   if( p==0 ) return;
451 #endif
452   ParseFinalize(p);
453   (*freeProc)(p);
454 }
455 #endif /* Parse_ENGINEALWAYSONSTACK */
456 
457 /*
458 ** Return the peak depth of the stack for a parser.
459 */
460 #ifdef YYTRACKMAXSTACKDEPTH
461 int ParseStackPeak(void *p){
462   yyParser *pParser = (yyParser*)p;
463   return pParser->yyhwm;
464 }
465 #endif
466 
467 /* This array of booleans keeps track of the parser statement
468 ** coverage.  The element yycoverage[X][Y] is set when the parser
469 ** is in state X and has a lookahead token Y.  In a well-tested
470 ** systems, every element of this matrix should end up being set.
471 */
472 #if defined(YYCOVERAGE)
473 static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
474 #endif
475 
476 /*
477 ** Write into out a description of every state/lookahead combination that
478 **
479 **   (1)  has not been used by the parser, and
480 **   (2)  is not a syntax error.
481 **
482 ** Return the number of missed state/lookahead combinations.
483 */
484 #if defined(YYCOVERAGE)
485 int ParseCoverage(FILE *out){
486   int stateno, iLookAhead, i;
487   int nMissed = 0;
488   for(stateno=0; stateno<YYNSTATE; stateno++){
489     i = yy_shift_ofst[stateno];
490     for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
491       if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
492       if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
493       if( out ){
494         fprintf(out,"State %d lookahead %s %s\n", stateno,
495                 yyTokenName[iLookAhead],
496                 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
497       }
498     }
499   }
500   return nMissed;
501 }
502 #endif
503 
504 /*
505 ** Find the appropriate action for a parser given the terminal
506 ** look-ahead token iLookAhead.
507 */
508 static YYACTIONTYPE yy_find_shift_action(
509   YYCODETYPE iLookAhead,    /* The look-ahead token */
510   YYACTIONTYPE stateno      /* Current state number */
511 ){
512   int i;
513 
514   if( stateno>YY_MAX_SHIFT ) return stateno;
515   assert( stateno <= YY_SHIFT_COUNT );
516 #if defined(YYCOVERAGE)
517   yycoverage[stateno][iLookAhead] = 1;
518 #endif
519   do{
520     i = yy_shift_ofst[stateno];
521     assert( i>=0 );
522     assert( i+YYNTOKEN<=(int)sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
523     assert( iLookAhead!=YYNOCODE );
524     assert( iLookAhead < YYNTOKEN );
525     i += iLookAhead;
526     if( yy_lookahead[i]!=iLookAhead ){
527 #ifdef YYFALLBACK
528       YYCODETYPE iFallback;            /* Fallback token */
529       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
530              && (iFallback = yyFallback[iLookAhead])!=0 ){
531 #ifndef NDEBUG
532         if( yyTraceFILE ){
533           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
534              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
535         }
536 #endif
537         assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
538         iLookAhead = iFallback;
539         continue;
540       }
541 #endif
542 #ifdef YYWILDCARD
543       {
544         int j = i - iLookAhead + YYWILDCARD;
545         if(
546 #if YY_SHIFT_MIN+YYWILDCARD<0
547           j>=0 &&
548 #endif
549 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
550           j<YY_ACTTAB_COUNT &&
551 #endif
552           yy_lookahead[j]==YYWILDCARD && iLookAhead>0
553         ){
554 #ifndef NDEBUG
555           if( yyTraceFILE ){
556             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
557                yyTracePrompt, yyTokenName[iLookAhead],
558                yyTokenName[YYWILDCARD]);
559           }
560 #endif /* NDEBUG */
561           return yy_action[j];
562         }
563       }
564 #endif /* YYWILDCARD */
565       return yy_default[stateno];
566     }else{
567       return yy_action[i];
568     }
569   }while(1);
570 }
571 
572 /*
573 ** Find the appropriate action for a parser given the non-terminal
574 ** look-ahead token iLookAhead.
575 */
576 static int yy_find_reduce_action(
577   YYACTIONTYPE stateno,     /* Current state number */
578   YYCODETYPE iLookAhead     /* The look-ahead token */
579 ){
580   int i;
581 #ifdef YYERRORSYMBOL
582   if( stateno>YY_REDUCE_COUNT ){
583     return yy_default[stateno];
584   }
585 #else
586   assert( stateno<=YY_REDUCE_COUNT );
587 #endif
588   i = yy_reduce_ofst[stateno];
589   assert( iLookAhead!=YYNOCODE );
590   i += iLookAhead;
591 #ifdef YYERRORSYMBOL
592   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
593     return yy_default[stateno];
594   }
595 #else
596   assert( i>=0 && i<YY_ACTTAB_COUNT );
597   assert( yy_lookahead[i]==iLookAhead );
598 #endif
599   return yy_action[i];
600 }
601 
602 /*
603 ** The following routine is called if the stack overflows.
604 */
605 static void yyStackOverflow(yyParser *yypParser){
606    ParseARG_FETCH
607    ParseCTX_FETCH
608 #ifndef NDEBUG
609    if( yyTraceFILE ){
610      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
611    }
612 #endif
613    while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
614    /* Here code is inserted which will execute if the parser
615    ** stack every overflows */
616 /******** Begin %stack_overflow code ******************************************/
617 %%
618 /******** End %stack_overflow code ********************************************/
619    ParseARG_STORE /* Suppress warning about unused %extra_argument var */
620    ParseCTX_STORE
621 }
622 
623 /*
624 ** Print tracing information for a SHIFT action
625 */
626 #ifndef NDEBUG
627 static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
628   if( yyTraceFILE ){
629     if( yyNewState<YYNSTATE ){
630       fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
631          yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
632          yyNewState);
633     }else{
634       fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
635          yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
636          yyNewState - YY_MIN_REDUCE);
637     }
638   }
639 }
640 #else
641 # define yyTraceShift(X,Y,Z)
642 #endif
643 
644 /*
645 ** Perform a shift action.
646 */
647 static void yy_shift(
648   yyParser *yypParser,          /* The parser to be shifted */
649   YYACTIONTYPE yyNewState,      /* The new state to shift in */
650   YYCODETYPE yyMajor,           /* The major token to shift in */
651   ParseTOKENTYPE yyMinor        /* The minor token to shift in */
652 ){
653   yyStackEntry *yytos;
654   yypParser->yytos++;
655 #ifdef YYTRACKMAXSTACKDEPTH
656   if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
657     yypParser->yyhwm++;
658     assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
659   }
660 #endif
661 #if YYSTACKDEPTH>0
662   if( yypParser->yytos>yypParser->yystackEnd ){
663     yypParser->yytos--;
664     yyStackOverflow(yypParser);
665     return;
666   }
667 #else
668   if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
669     if( yyGrowStack(yypParser) ){
670       yypParser->yytos--;
671       yyStackOverflow(yypParser);
672       return;
673     }
674   }
675 #endif
676   if( yyNewState > YY_MAX_SHIFT ){
677     yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
678   }
679   yytos = yypParser->yytos;
680   yytos->stateno = yyNewState;
681   yytos->major = yyMajor;
682   yytos->minor.yy0 = yyMinor;
683   yyTraceShift(yypParser, yyNewState, "Shift");
684 }
685 
686 /* The following table contains information about every rule that
687 ** is used during the reduce.
688 */
689 static const struct {
690   YYCODETYPE lhs;       /* Symbol on the left-hand side of the rule */
691   signed char nrhs;     /* Negative of the number of RHS symbols in the rule */
692 } yyRuleInfo[] = {
693 %%
694 };
695 
696 static void yy_accept(yyParser*);  /* Forward Declaration */
697 
698 /*
699 ** Perform a reduce action and the shift that must immediately
700 ** follow the reduce.
701 **
702 ** The yyLookahead and yyLookaheadToken parameters provide reduce actions
703 ** access to the lookahead token (if any).  The yyLookahead will be YYNOCODE
704 ** if the lookahead token has already been consumed.  As this procedure is
705 ** only called from one place, optimizing compilers will in-line it, which
706 ** means that the extra parameters have no performance impact.
707 */
708 static YYACTIONTYPE yy_reduce(
709   yyParser *yypParser,         /* The parser */
710   unsigned int yyruleno,       /* Number of the rule by which to reduce */
711   int yyLookahead,             /* Lookahead token, or YYNOCODE if none */
712   ParseTOKENTYPE yyLookaheadToken  /* Value of the lookahead token */
713   ParseCTX_PDECL                   /* %extra_context */
714 ){
715   int yygoto;                     /* The next state */
716   int yyact;                      /* The next action */
717   yyStackEntry *yymsp;            /* The top of the parser's stack */
718   int yysize;                     /* Amount to pop the stack */
719   ParseARG_FETCH
720   (void)yyLookahead;
721   (void)yyLookaheadToken;
722   yymsp = yypParser->yytos;
723 #ifndef NDEBUG
724   if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
725     yysize = yyRuleInfo[yyruleno].nrhs;
726     if( yysize ){
727       fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
728         yyTracePrompt,
729         yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno);
730     }else{
731       fprintf(yyTraceFILE, "%sReduce %d [%s].\n",
732         yyTracePrompt, yyruleno, yyRuleName[yyruleno]);
733     }
734   }
735 #endif /* NDEBUG */
736 
737   /* Check that the stack is large enough to grow by a single entry
738   ** if the RHS of the rule is empty.  This ensures that there is room
739   ** enough on the stack to push the LHS value */
740   if( yyRuleInfo[yyruleno].nrhs==0 ){
741 #ifdef YYTRACKMAXSTACKDEPTH
742     if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
743       yypParser->yyhwm++;
744       assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
745     }
746 #endif
747 #if YYSTACKDEPTH>0
748     if( yypParser->yytos>=yypParser->yystackEnd ){
749       yyStackOverflow(yypParser);
750       return YY_ACCEPT_ACTION;
751     }
752 #else
753     if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
754       if( yyGrowStack(yypParser) ){
755         yyStackOverflow(yypParser);
756         return YY_ACCEPT_ACTION;
757       }
758       yymsp = yypParser->yytos;
759     }
760 #endif
761   }
762 
763   switch( yyruleno ){
764   /* Beginning here are the reduction cases.  A typical example
765   ** follows:
766   **   case 0:
767   **  #line <lineno> <grammarfile>
768   **     { ... }           // User supplied code
769   **  #line <lineno> <thisfile>
770   **     break;
771   */
772 /********** Begin reduce actions **********************************************/
773 %%
774 /********** End reduce actions ************************************************/
775   };
776   assert( yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
777   yygoto = yyRuleInfo[yyruleno].lhs;
778   yysize = yyRuleInfo[yyruleno].nrhs;
779   yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
780 
781   /* There are no SHIFTREDUCE actions on nonterminals because the table
782   ** generator has simplified them to pure REDUCE actions. */
783   assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
784 
785   /* It is not possible for a REDUCE to be followed by an error */
786   assert( yyact!=YY_ERROR_ACTION );
787 
788   yymsp += yysize+1;
789   yypParser->yytos = yymsp;
790   yymsp->stateno = (YYACTIONTYPE)yyact;
791   yymsp->major = (YYCODETYPE)yygoto;
792   yyTraceShift(yypParser, yyact, "... then shift");
793   return yyact;
794 }
795 
796 /*
797 ** The following code executes when the parse fails
798 */
799 #ifndef YYNOERRORRECOVERY
800 static void yy_parse_failed(
801   yyParser *yypParser           /* The parser */
802 ){
803   ParseARG_FETCH
804   ParseCTX_FETCH
805 #ifndef NDEBUG
806   if( yyTraceFILE ){
807     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
808   }
809 #endif
810   while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
811   /* Here code is inserted which will be executed whenever the
812   ** parser fails */
813 /************ Begin %parse_failure code ***************************************/
814 %%
815 /************ End %parse_failure code *****************************************/
816   ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
817   ParseCTX_STORE
818 }
819 #endif /* YYNOERRORRECOVERY */
820 
821 /*
822 ** The following code executes when a syntax error first occurs.
823 */
824 static void yy_syntax_error(
825   yyParser *yypParser,           /* The parser */
826   int yymajor,                   /* The major type of the error token */
827   ParseTOKENTYPE yyminor         /* The minor type of the error token */
828 ){
829   ParseARG_FETCH
830   ParseCTX_FETCH
831 #define TOKEN yyminor
832 /************ Begin %syntax_error code ****************************************/
833 %%
834 /************ End %syntax_error code ******************************************/
835   ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
836   ParseCTX_STORE
837 }
838 
839 /*
840 ** The following is executed when the parser accepts
841 */
842 static void yy_accept(
843   yyParser *yypParser           /* The parser */
844 ){
845   ParseARG_FETCH
846   ParseCTX_FETCH
847 #ifndef NDEBUG
848   if( yyTraceFILE ){
849     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
850   }
851 #endif
852 #ifndef YYNOERRORRECOVERY
853   yypParser->yyerrcnt = -1;
854 #endif
855   assert( yypParser->yytos==yypParser->yystack );
856   /* Here code is inserted which will be executed whenever the
857   ** parser accepts */
858 /*********** Begin %parse_accept code *****************************************/
859 %%
860 /*********** End %parse_accept code *******************************************/
861   ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
862   ParseCTX_STORE
863 }
864 
865 /* The main parser program.
866 ** The first argument is a pointer to a structure obtained from
867 ** "ParseAlloc" which describes the current state of the parser.
868 ** The second argument is the major token number.  The third is
869 ** the minor token.  The fourth optional argument is whatever the
870 ** user wants (and specified in the grammar) and is available for
871 ** use by the action routines.
872 **
873 ** Inputs:
874 ** <ul>
875 ** <li> A pointer to the parser (an opaque structure.)
876 ** <li> The major token number.
877 ** <li> The minor token number.
878 ** <li> An option argument of a grammar-specified type.
879 ** </ul>
880 **
881 ** Outputs:
882 ** None.
883 */
884 void Parse(
885   void *yyp,                   /* The parser */
886   int yymajor,                 /* The major token code number */
887   ParseTOKENTYPE yyminor       /* The value for the token */
888   ParseARG_PDECL               /* Optional %extra_argument parameter */
889 ){
890   YYMINORTYPE yyminorunion;
891   YYACTIONTYPE yyact;   /* The parser action. */
892 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
893   int yyendofinput;     /* True if we are at the end of input */
894 #endif
895 #ifdef YYERRORSYMBOL
896   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
897 #endif
898   yyParser *yypParser = (yyParser*)yyp;  /* The parser */
899   ParseCTX_FETCH
900   ParseARG_STORE
901 
902   assert( yypParser->yytos!=0 );
903 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
904   yyendofinput = (yymajor==0);
905 #endif
906 
907   yyact = yypParser->yytos->stateno;
908 #ifndef NDEBUG
909   if( yyTraceFILE ){
910     if( yyact < YY_MIN_REDUCE ){
911       fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
912               yyTracePrompt,yyTokenName[yymajor],yyact);
913     }else{
914       fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
915               yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
916     }
917   }
918 #endif
919 
920   do{
921     assert( yyact==yypParser->yytos->stateno );
922     yyact = yy_find_shift_action(yymajor,yyact);
923     if( yyact >= YY_MIN_REDUCE ){
924       yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,
925                         yyminor ParseCTX_PARAM);
926     }else if( yyact <= YY_MAX_SHIFTREDUCE ){
927       yy_shift(yypParser,yyact,yymajor,yyminor);
928 #ifndef YYNOERRORRECOVERY
929       yypParser->yyerrcnt--;
930 #endif
931       break;
932     }else if( yyact==YY_ACCEPT_ACTION ){
933       /* YY_ACCEPT_ACTION also happens on a stack overflow.  We distingush
934       ** the two cases by observing that on a true accept, there should be
935       ** a single token left on the stack, whereas on a stack overflow,
936       ** the stack has been popped (by yyStackOverflow()) to be empty */
937       if( yypParser->yytos > yypParser->yystack ){
938         yypParser->yytos--;
939         yy_accept(yypParser);
940       }
941       return;
942     }else{
943       assert( yyact == YY_ERROR_ACTION );
944       yyminorunion.yy0 = yyminor;
945 #ifdef YYERRORSYMBOL
946       int yymx;
947 #endif
948 #ifndef NDEBUG
949       if( yyTraceFILE ){
950         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
951       }
952 #endif
953 #ifdef YYERRORSYMBOL
954       /* A syntax error has occurred.
955       ** The response to an error depends upon whether or not the
956       ** grammar defines an error token "ERROR".
957       **
958       ** This is what we do if the grammar does define ERROR:
959       **
960       **  * Call the %syntax_error function.
961       **
962       **  * Begin popping the stack until we enter a state where
963       **    it is legal to shift the error symbol, then shift
964       **    the error symbol.
965       **
966       **  * Set the error count to three.
967       **
968       **  * Begin accepting and shifting new tokens.  No new error
969       **    processing will occur until three tokens have been
970       **    shifted successfully.
971       **
972       */
973       if( yypParser->yyerrcnt<0 ){
974         yy_syntax_error(yypParser,yymajor,yyminor);
975       }
976       yymx = yypParser->yytos->major;
977       if( yymx==YYERRORSYMBOL || yyerrorhit ){
978 #ifndef NDEBUG
979         if( yyTraceFILE ){
980           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
981              yyTracePrompt,yyTokenName[yymajor]);
982         }
983 #endif
984         yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
985         yymajor = YYNOCODE;
986       }else{
987         while( yypParser->yytos >= yypParser->yystack
988             && yymx != YYERRORSYMBOL
989             && (yyact = yy_find_reduce_action(
990                         yypParser->yytos->stateno,
991                         YYERRORSYMBOL)) >= YY_MIN_REDUCE
992         ){
993           yy_pop_parser_stack(yypParser);
994         }
995         if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
996           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
997           yy_parse_failed(yypParser);
998 #ifndef YYNOERRORRECOVERY
999           yypParser->yyerrcnt = -1;
1000 #endif
1001           yymajor = YYNOCODE;
1002         }else if( yymx!=YYERRORSYMBOL ){
1003           yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
1004         }
1005       }
1006       yypParser->yyerrcnt = 3;
1007       yyerrorhit = 1;
1008       if( yymajor==YYNOCODE ) break;
1009       yyact = yypParser->yytos->stateno;
1010 #elif defined(YYNOERRORRECOVERY)
1011       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
1012       ** do any kind of error recovery.  Instead, simply invoke the syntax
1013       ** error routine and continue going as if nothing had happened.
1014       **
1015       ** Applications can set this macro (for example inside %include) if
1016       ** they intend to abandon the parse upon the first syntax error seen.
1017       */
1018       yy_syntax_error(yypParser,yymajor, yyminor);
1019       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1020       break;
1021 #else  /* YYERRORSYMBOL is not defined */
1022       /* This is what we do if the grammar does not define ERROR:
1023       **
1024       **  * Report an error message, and throw away the input token.
1025       **
1026       **  * If the input token is $, then fail the parse.
1027       **
1028       ** As before, subsequent error messages are suppressed until
1029       ** three input tokens have been successfully shifted.
1030       */
1031       if( yypParser->yyerrcnt<=0 ){
1032         yy_syntax_error(yypParser,yymajor, yyminor);
1033       }
1034       yypParser->yyerrcnt = 3;
1035       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1036       if( yyendofinput ){
1037         yy_parse_failed(yypParser);
1038 #ifndef YYNOERRORRECOVERY
1039         yypParser->yyerrcnt = -1;
1040 #endif
1041       }
1042       break;
1043 #endif
1044     }
1045   }while( yypParser->yytos>yypParser->yystack );
1046 #ifndef NDEBUG
1047   if( yyTraceFILE ){
1048     yyStackEntry *i;
1049     char cDiv = '[';
1050     fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
1051     for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
1052       fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
1053       cDiv = ' ';
1054     }
1055     fprintf(yyTraceFILE,"]\n");
1056   }
1057 #endif
1058   return;
1059 }
1060