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