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