Enhance the command-line completion extension to return the names of
[sqlite.git] / tool / lempar.c
blob1ade6669166bf7a47fab844b052404f2faff4ca6
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.
11 *************************************************************************
12 ** Driver template for the LEMON parser generator.
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.
22 ** The following is the concatenation of all %include directives from the
23 ** input grammar file:
25 #include <stdio.h>
26 /************ Begin %include sections from the grammar ************************/
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 *************************/
34 /**************** End makeheaders token definitions ***************************/
36 /* The next sections is a series of control #defines.
37 ** various aspects of the generated parser.
38 ** YYCODETYPE is the data type used to store the integer codes
39 ** that represent terminal and non-terminal symbols.
40 ** "unsigned char" is used if there are fewer than
41 ** 256 symbols. Larger types otherwise.
42 ** YYNOCODE is a number of type YYCODETYPE that is not used for
43 ** any terminal or nonterminal symbol.
44 ** YYFALLBACK If defined, this indicates that one or more tokens
45 ** (also known as: "terminal symbols") have fall-back
46 ** values which should be used if the original symbol
47 ** would not parse. This permits keywords to sometimes
48 ** be used as identifiers, for example.
49 ** YYACTIONTYPE is the data type used for "action codes" - numbers
50 ** that indicate what to do in response to the next
51 ** token.
52 ** ParseTOKENTYPE is the data type used for minor type for terminal
53 ** symbols. Background: A "minor type" is a semantic
54 ** value associated with a terminal or non-terminal
55 ** symbols. For example, for an "ID" terminal symbol,
56 ** the minor type might be the name of the identifier.
57 ** Each non-terminal can have a different minor type.
58 ** Terminal symbols all have the same minor type, though.
59 ** This macros defines the minor type for terminal
60 ** symbols.
61 ** YYMINORTYPE is the data type used for all minor types.
62 ** This is typically a union of many types, one of
63 ** which is ParseTOKENTYPE. The entry in the union
64 ** for terminal symbols is called "yy0".
65 ** YYSTACKDEPTH is the maximum depth of the parser's stack. If
66 ** zero the stack is dynamically sized using realloc()
67 ** ParseARG_SDECL A static variable declaration for the %extra_argument
68 ** ParseARG_PDECL A parameter declaration for the %extra_argument
69 ** ParseARG_STORE Code to store %extra_argument into yypParser
70 ** ParseARG_FETCH Code to extract %extra_argument from yypParser
71 ** YYERRORSYMBOL is the code number of the error symbol. If not
72 ** defined, then do no error processing.
73 ** YYNSTATE the combined number of states.
74 ** YYNRULE the number of rules in the grammar
75 ** YYNTOKEN Number of terminal symbols
76 ** YY_MAX_SHIFT Maximum value for shift actions
77 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
78 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
79 ** YY_ERROR_ACTION The yy_action[] code for syntax error
80 ** YY_ACCEPT_ACTION The yy_action[] code for accept
81 ** YY_NO_ACTION The yy_action[] code for no-op
82 ** YY_MIN_REDUCE Minimum value for reduce actions
83 ** YY_MAX_REDUCE Maximum value for reduce actions
85 #ifndef INTERFACE
86 # define INTERFACE 1
87 #endif
88 /************* Begin control #defines *****************************************/
90 /************* End control #defines *******************************************/
92 /* Define the yytestcase() macro to be a no-op if is not already defined
93 ** otherwise.
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.
100 #ifndef yytestcase
101 # define yytestcase(X)
102 #endif
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.
110 ** Suppose the action integer is N. Then the action is determined as
111 ** follows
113 ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
114 ** token onto the stack and goto state N.
116 ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
117 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
119 ** N == YY_ERROR_ACTION A syntax error has occurred.
121 ** N == YY_ACCEPT_ACTION The parser accepts its input.
123 ** N == YY_NO_ACTION No such action. Denotes unused
124 ** slots in the yy_action[] table.
126 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
127 ** and YY_MAX_REDUCE
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:
132 ** (A) N = yy_action[ yy_shift_ofst[S] + X ]
133 ** (B) N = yy_default[S]
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.
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.
143 ** The following are the tables generated in this section:
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.
154 *********** Begin parsing tables **********************************************/
156 /********** End of lemon-generated parsing tables *****************************/
158 /* The next table maps tokens (terminal symbols) into fallback tokens.
159 ** If a construct like the following:
161 ** %fallback ID X Y Z.
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.
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.
172 #ifdef YYFALLBACK
173 static const YYCODETYPE yyFallback[] = {
176 #endif /* YYFALLBACK */
178 /* The following structure represents a single element of the
179 ** parser's stack. Information stored includes:
181 ** + The state number for the parser at this level of the stack.
183 ** + The value of the token stored at this level of the stack.
184 ** (In other words, the "major" token.)
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.
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.
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 */
201 typedef struct yyStackEntry yyStackEntry;
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 #if YYSTACKDEPTH<=0
215 int yystksz; /* Current side of the stack */
216 yyStackEntry *yystack; /* The parser's stack */
217 yyStackEntry yystk0; /* First stack entry */
218 #else
219 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
220 yyStackEntry *yystackEnd; /* Last entry in the stack */
221 #endif
223 typedef struct yyParser yyParser;
225 #ifndef NDEBUG
226 #include <stdio.h>
227 static FILE *yyTraceFILE = 0;
228 static char *yyTracePrompt = 0;
229 #endif /* NDEBUG */
231 #ifndef NDEBUG
233 ** Turn parser tracing on by giving a stream to which to write the trace
234 ** and a prompt to preface each trace message. Tracing is turned off
235 ** by making either argument NULL
237 ** Inputs:
238 ** <ul>
239 ** <li> A FILE* to which trace output should be written.
240 ** If NULL, then tracing is turned off.
241 ** <li> A prefix string written at the beginning of every
242 ** line of trace output. If NULL, then tracing is
243 ** turned off.
244 ** </ul>
246 ** Outputs:
247 ** None.
249 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
250 yyTraceFILE = TraceFILE;
251 yyTracePrompt = zTracePrompt;
252 if( yyTraceFILE==0 ) yyTracePrompt = 0;
253 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
255 #endif /* NDEBUG */
257 #if defined(YYCOVERAGE) || !defined(NDEBUG)
258 /* For tracing shifts, the names of all terminals and nonterminals
259 ** are required. The following table supplies these names */
260 static const char *const yyTokenName[] = {
263 #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
265 #ifndef NDEBUG
266 /* For tracing reduce actions, the names of all rules are required.
268 static const char *const yyRuleName[] = {
271 #endif /* NDEBUG */
274 #if YYSTACKDEPTH<=0
276 ** Try to increase the size of the parser stack. Return the number
277 ** of errors. Return 0 on success.
279 static int yyGrowStack(yyParser *p){
280 int newSize;
281 int idx;
282 yyStackEntry *pNew;
284 newSize = p->yystksz*2 + 100;
285 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
286 if( p->yystack==&p->yystk0 ){
287 pNew = malloc(newSize*sizeof(pNew[0]));
288 if( pNew ) pNew[0] = p->yystk0;
289 }else{
290 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
292 if( pNew ){
293 p->yystack = pNew;
294 p->yytos = &p->yystack[idx];
295 #ifndef NDEBUG
296 if( yyTraceFILE ){
297 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
298 yyTracePrompt, p->yystksz, newSize);
300 #endif
301 p->yystksz = newSize;
303 return pNew==0;
305 #endif
307 /* Datatype of the argument to the memory allocated passed as the
308 ** second argument to ParseAlloc() below. This can be changed by
309 ** putting an appropriate #define in the %include section of the input
310 ** grammar.
312 #ifndef YYMALLOCARGTYPE
313 # define YYMALLOCARGTYPE size_t
314 #endif
316 /* Initialize a new parser that has already been allocated.
318 void ParseInit(void *yypParser){
319 yyParser *pParser = (yyParser*)yypParser;
320 #ifdef YYTRACKMAXSTACKDEPTH
321 pParser->yyhwm = 0;
322 #endif
323 #if YYSTACKDEPTH<=0
324 pParser->yytos = NULL;
325 pParser->yystack = NULL;
326 pParser->yystksz = 0;
327 if( yyGrowStack(pParser) ){
328 pParser->yystack = &pParser->yystk0;
329 pParser->yystksz = 1;
331 #endif
332 #ifndef YYNOERRORRECOVERY
333 pParser->yyerrcnt = -1;
334 #endif
335 pParser->yytos = pParser->yystack;
336 pParser->yystack[0].stateno = 0;
337 pParser->yystack[0].major = 0;
338 #if YYSTACKDEPTH>0
339 pParser->yystackEnd = &pParser->yystack[YYSTACKDEPTH-1];
340 #endif
343 #ifndef Parse_ENGINEALWAYSONSTACK
345 ** This function allocates a new parser.
346 ** The only argument is a pointer to a function which works like
347 ** malloc.
349 ** Inputs:
350 ** A pointer to the function used to allocate memory.
352 ** Outputs:
353 ** A pointer to a parser. This pointer is used in subsequent calls
354 ** to Parse and ParseFree.
356 void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){
357 yyParser *pParser;
358 pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
359 if( pParser ) ParseInit(pParser);
360 return pParser;
362 #endif /* Parse_ENGINEALWAYSONSTACK */
365 /* The following function deletes the "minor type" or semantic value
366 ** associated with a symbol. The symbol can be either a terminal
367 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
368 ** a pointer to the value to be deleted. The code used to do the
369 ** deletions is derived from the %destructor and/or %token_destructor
370 ** directives of the input grammar.
372 static void yy_destructor(
373 yyParser *yypParser, /* The parser */
374 YYCODETYPE yymajor, /* Type code for object to destroy */
375 YYMINORTYPE *yypminor /* The object to be destroyed */
377 ParseARG_FETCH;
378 switch( yymajor ){
379 /* Here is inserted the actions which take place when a
380 ** terminal or non-terminal is destroyed. This can happen
381 ** when the symbol is popped from the stack during a
382 ** reduce or during error processing or when a parser is
383 ** being destroyed before it is finished parsing.
385 ** Note: during a reduce, the only symbols destroyed are those
386 ** which appear on the RHS of the rule, but which are *not* used
387 ** inside the C code.
389 /********* Begin destructor definitions ***************************************/
391 /********* End destructor definitions *****************************************/
392 default: break; /* If no destructor action specified: do nothing */
397 ** Pop the parser's stack once.
399 ** If there is a destructor routine associated with the token which
400 ** is popped from the stack, then call it.
402 static void yy_pop_parser_stack(yyParser *pParser){
403 yyStackEntry *yytos;
404 assert( pParser->yytos!=0 );
405 assert( pParser->yytos > pParser->yystack );
406 yytos = pParser->yytos--;
407 #ifndef NDEBUG
408 if( yyTraceFILE ){
409 fprintf(yyTraceFILE,"%sPopping %s\n",
410 yyTracePrompt,
411 yyTokenName[yytos->major]);
413 #endif
414 yy_destructor(pParser, yytos->major, &yytos->minor);
418 ** Clear all secondary memory allocations from the parser
420 void ParseFinalize(void *p){
421 yyParser *pParser = (yyParser*)p;
422 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
423 #if YYSTACKDEPTH<=0
424 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
425 #endif
428 #ifndef Parse_ENGINEALWAYSONSTACK
430 ** Deallocate and destroy a parser. Destructors are called for
431 ** all stack elements before shutting the parser down.
433 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
434 ** is defined in a %include section of the input grammar) then it is
435 ** assumed that the input pointer is never NULL.
437 void ParseFree(
438 void *p, /* The parser to be deleted */
439 void (*freeProc)(void*) /* Function used to reclaim memory */
441 #ifndef YYPARSEFREENEVERNULL
442 if( p==0 ) return;
443 #endif
444 ParseFinalize(p);
445 (*freeProc)(p);
447 #endif /* Parse_ENGINEALWAYSONSTACK */
450 ** Return the peak depth of the stack for a parser.
452 #ifdef YYTRACKMAXSTACKDEPTH
453 int ParseStackPeak(void *p){
454 yyParser *pParser = (yyParser*)p;
455 return pParser->yyhwm;
457 #endif
459 /* This array of booleans keeps track of the parser statement
460 ** coverage. The element yycoverage[X][Y] is set when the parser
461 ** is in state X and has a lookahead token Y. In a well-tested
462 ** systems, every element of this matrix should end up being set.
464 #if defined(YYCOVERAGE)
465 static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
466 #endif
469 ** Write into out a description of every state/lookahead combination that
471 ** (1) has not been used by the parser, and
472 ** (2) is not a syntax error.
474 ** Return the number of missed state/lookahead combinations.
476 #if defined(YYCOVERAGE)
477 int ParseCoverage(FILE *out){
478 int stateno, iLookAhead, i;
479 int nMissed = 0;
480 for(stateno=0; stateno<YYNSTATE; stateno++){
481 i = yy_shift_ofst[stateno];
482 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
483 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
484 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
485 if( out ){
486 fprintf(out,"State %d lookahead %s %s\n", stateno,
487 yyTokenName[iLookAhead],
488 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
492 return nMissed;
494 #endif
497 ** Find the appropriate action for a parser given the terminal
498 ** look-ahead token iLookAhead.
500 static unsigned int yy_find_shift_action(
501 yyParser *pParser, /* The parser */
502 YYCODETYPE iLookAhead /* The look-ahead token */
504 int i;
505 int stateno = pParser->yytos->stateno;
507 if( stateno>YY_MAX_SHIFT ) return stateno;
508 assert( stateno <= YY_SHIFT_COUNT );
509 #if defined(YYCOVERAGE)
510 yycoverage[stateno][iLookAhead] = 1;
511 #endif
513 i = yy_shift_ofst[stateno];
514 assert( i>=0 );
515 assert( i+YYNTOKEN<=(int)sizeof(yy_lookahead)/sizeof(yy_lookahead[0]) );
516 assert( iLookAhead!=YYNOCODE );
517 assert( iLookAhead < YYNTOKEN );
518 i += iLookAhead;
519 if( yy_lookahead[i]!=iLookAhead ){
520 #ifdef YYFALLBACK
521 YYCODETYPE iFallback; /* Fallback token */
522 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
523 && (iFallback = yyFallback[iLookAhead])!=0 ){
524 #ifndef NDEBUG
525 if( yyTraceFILE ){
526 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
527 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
529 #endif
530 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
531 iLookAhead = iFallback;
532 continue;
534 #endif
535 #ifdef YYWILDCARD
537 int j = i - iLookAhead + YYWILDCARD;
538 if(
539 #if YY_SHIFT_MIN+YYWILDCARD<0
540 j>=0 &&
541 #endif
542 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
543 j<YY_ACTTAB_COUNT &&
544 #endif
545 yy_lookahead[j]==YYWILDCARD && iLookAhead>0
547 #ifndef NDEBUG
548 if( yyTraceFILE ){
549 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
550 yyTracePrompt, yyTokenName[iLookAhead],
551 yyTokenName[YYWILDCARD]);
553 #endif /* NDEBUG */
554 return yy_action[j];
557 #endif /* YYWILDCARD */
558 return yy_default[stateno];
559 }else{
560 return yy_action[i];
562 }while(1);
566 ** Find the appropriate action for a parser given the non-terminal
567 ** look-ahead token iLookAhead.
569 static int yy_find_reduce_action(
570 int stateno, /* Current state number */
571 YYCODETYPE iLookAhead /* The look-ahead token */
573 int i;
574 #ifdef YYERRORSYMBOL
575 if( stateno>YY_REDUCE_COUNT ){
576 return yy_default[stateno];
578 #else
579 assert( stateno<=YY_REDUCE_COUNT );
580 #endif
581 i = yy_reduce_ofst[stateno];
582 assert( iLookAhead!=YYNOCODE );
583 i += iLookAhead;
584 #ifdef YYERRORSYMBOL
585 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
586 return yy_default[stateno];
588 #else
589 assert( i>=0 && i<YY_ACTTAB_COUNT );
590 assert( yy_lookahead[i]==iLookAhead );
591 #endif
592 return yy_action[i];
596 ** The following routine is called if the stack overflows.
598 static void yyStackOverflow(yyParser *yypParser){
599 ParseARG_FETCH;
600 #ifndef NDEBUG
601 if( yyTraceFILE ){
602 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
604 #endif
605 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
606 /* Here code is inserted which will execute if the parser
607 ** stack every overflows */
608 /******** Begin %stack_overflow code ******************************************/
610 /******** End %stack_overflow code ********************************************/
611 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
615 ** Print tracing information for a SHIFT action
617 #ifndef NDEBUG
618 static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
619 if( yyTraceFILE ){
620 if( yyNewState<YYNSTATE ){
621 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
622 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
623 yyNewState);
624 }else{
625 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
626 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
627 yyNewState - YY_MIN_REDUCE);
631 #else
632 # define yyTraceShift(X,Y,Z)
633 #endif
636 ** Perform a shift action.
638 static void yy_shift(
639 yyParser *yypParser, /* The parser to be shifted */
640 int yyNewState, /* The new state to shift in */
641 int yyMajor, /* The major token to shift in */
642 ParseTOKENTYPE yyMinor /* The minor token to shift in */
644 yyStackEntry *yytos;
645 yypParser->yytos++;
646 #ifdef YYTRACKMAXSTACKDEPTH
647 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
648 yypParser->yyhwm++;
649 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
651 #endif
652 #if YYSTACKDEPTH>0
653 if( yypParser->yytos>yypParser->yystackEnd ){
654 yypParser->yytos--;
655 yyStackOverflow(yypParser);
656 return;
658 #else
659 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
660 if( yyGrowStack(yypParser) ){
661 yypParser->yytos--;
662 yyStackOverflow(yypParser);
663 return;
666 #endif
667 if( yyNewState > YY_MAX_SHIFT ){
668 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
670 yytos = yypParser->yytos;
671 yytos->stateno = (YYACTIONTYPE)yyNewState;
672 yytos->major = (YYCODETYPE)yyMajor;
673 yytos->minor.yy0 = yyMinor;
674 yyTraceShift(yypParser, yyNewState, "Shift");
677 /* The following table contains information about every rule that
678 ** is used during the reduce.
680 static const struct {
681 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
682 signed char nrhs; /* Negative of the number of RHS symbols in the rule */
683 } yyRuleInfo[] = {
687 static void yy_accept(yyParser*); /* Forward Declaration */
690 ** Perform a reduce action and the shift that must immediately
691 ** follow the reduce.
693 ** The yyLookahead and yyLookaheadToken parameters provide reduce actions
694 ** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
695 ** if the lookahead token has already been consumed. As this procedure is
696 ** only called from one place, optimizing compilers will in-line it, which
697 ** means that the extra parameters have no performance impact.
699 static void yy_reduce(
700 yyParser *yypParser, /* The parser */
701 unsigned int yyruleno, /* Number of the rule by which to reduce */
702 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
703 ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
705 int yygoto; /* The next state */
706 int yyact; /* The next action */
707 yyStackEntry *yymsp; /* The top of the parser's stack */
708 int yysize; /* Amount to pop the stack */
709 ParseARG_FETCH;
710 (void)yyLookahead;
711 (void)yyLookaheadToken;
712 yymsp = yypParser->yytos;
713 #ifndef NDEBUG
714 if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
715 yysize = yyRuleInfo[yyruleno].nrhs;
716 if( yysize ){
717 fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
718 yyTracePrompt,
719 yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno);
720 }else{
721 fprintf(yyTraceFILE, "%sReduce %d [%s].\n",
722 yyTracePrompt, yyruleno, yyRuleName[yyruleno]);
725 #endif /* NDEBUG */
727 /* Check that the stack is large enough to grow by a single entry
728 ** if the RHS of the rule is empty. This ensures that there is room
729 ** enough on the stack to push the LHS value */
730 if( yyRuleInfo[yyruleno].nrhs==0 ){
731 #ifdef YYTRACKMAXSTACKDEPTH
732 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
733 yypParser->yyhwm++;
734 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack));
736 #endif
737 #if YYSTACKDEPTH>0
738 if( yypParser->yytos>=yypParser->yystackEnd ){
739 yyStackOverflow(yypParser);
740 return;
742 #else
743 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
744 if( yyGrowStack(yypParser) ){
745 yyStackOverflow(yypParser);
746 return;
748 yymsp = yypParser->yytos;
750 #endif
753 switch( yyruleno ){
754 /* Beginning here are the reduction cases. A typical example
755 ** follows:
756 ** case 0:
757 ** #line <lineno> <grammarfile>
758 ** { ... } // User supplied code
759 ** #line <lineno> <thisfile>
760 ** break;
762 /********** Begin reduce actions **********************************************/
764 /********** End reduce actions ************************************************/
766 assert( yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
767 yygoto = yyRuleInfo[yyruleno].lhs;
768 yysize = yyRuleInfo[yyruleno].nrhs;
769 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
771 /* There are no SHIFTREDUCE actions on nonterminals because the table
772 ** generator has simplified them to pure REDUCE actions. */
773 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
775 /* It is not possible for a REDUCE to be followed by an error */
776 assert( yyact!=YY_ERROR_ACTION );
778 yymsp += yysize+1;
779 yypParser->yytos = yymsp;
780 yymsp->stateno = (YYACTIONTYPE)yyact;
781 yymsp->major = (YYCODETYPE)yygoto;
782 yyTraceShift(yypParser, yyact, "... then shift");
786 ** The following code executes when the parse fails
788 #ifndef YYNOERRORRECOVERY
789 static void yy_parse_failed(
790 yyParser *yypParser /* The parser */
792 ParseARG_FETCH;
793 #ifndef NDEBUG
794 if( yyTraceFILE ){
795 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
797 #endif
798 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
799 /* Here code is inserted which will be executed whenever the
800 ** parser fails */
801 /************ Begin %parse_failure code ***************************************/
803 /************ End %parse_failure code *****************************************/
804 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
806 #endif /* YYNOERRORRECOVERY */
809 ** The following code executes when a syntax error first occurs.
811 static void yy_syntax_error(
812 yyParser *yypParser, /* The parser */
813 int yymajor, /* The major type of the error token */
814 ParseTOKENTYPE yyminor /* The minor type of the error token */
816 ParseARG_FETCH;
817 #define TOKEN yyminor
818 /************ Begin %syntax_error code ****************************************/
820 /************ End %syntax_error code ******************************************/
821 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
825 ** The following is executed when the parser accepts
827 static void yy_accept(
828 yyParser *yypParser /* The parser */
830 ParseARG_FETCH;
831 #ifndef NDEBUG
832 if( yyTraceFILE ){
833 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
835 #endif
836 #ifndef YYNOERRORRECOVERY
837 yypParser->yyerrcnt = -1;
838 #endif
839 assert( yypParser->yytos==yypParser->yystack );
840 /* Here code is inserted which will be executed whenever the
841 ** parser accepts */
842 /*********** Begin %parse_accept code *****************************************/
844 /*********** End %parse_accept code *******************************************/
845 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
848 /* The main parser program.
849 ** The first argument is a pointer to a structure obtained from
850 ** "ParseAlloc" which describes the current state of the parser.
851 ** The second argument is the major token number. The third is
852 ** the minor token. The fourth optional argument is whatever the
853 ** user wants (and specified in the grammar) and is available for
854 ** use by the action routines.
856 ** Inputs:
857 ** <ul>
858 ** <li> A pointer to the parser (an opaque structure.)
859 ** <li> The major token number.
860 ** <li> The minor token number.
861 ** <li> An option argument of a grammar-specified type.
862 ** </ul>
864 ** Outputs:
865 ** None.
867 void Parse(
868 void *yyp, /* The parser */
869 int yymajor, /* The major token code number */
870 ParseTOKENTYPE yyminor /* The value for the token */
871 ParseARG_PDECL /* Optional %extra_argument parameter */
873 YYMINORTYPE yyminorunion;
874 unsigned int yyact; /* The parser action. */
875 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
876 int yyendofinput; /* True if we are at the end of input */
877 #endif
878 #ifdef YYERRORSYMBOL
879 int yyerrorhit = 0; /* True if yymajor has invoked an error */
880 #endif
881 yyParser *yypParser; /* The parser */
883 yypParser = (yyParser*)yyp;
884 assert( yypParser->yytos!=0 );
885 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
886 yyendofinput = (yymajor==0);
887 #endif
888 ParseARG_STORE;
890 #ifndef NDEBUG
891 if( yyTraceFILE ){
892 int stateno = yypParser->yytos->stateno;
893 if( stateno < YY_MIN_REDUCE ){
894 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
895 yyTracePrompt,yyTokenName[yymajor],stateno);
896 }else{
897 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
898 yyTracePrompt,yyTokenName[yymajor],stateno-YY_MIN_REDUCE);
901 #endif
904 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
905 if( yyact >= YY_MIN_REDUCE ){
906 yy_reduce(yypParser,yyact-YY_MIN_REDUCE,yymajor,yyminor);
907 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
908 yy_shift(yypParser,yyact,yymajor,yyminor);
909 #ifndef YYNOERRORRECOVERY
910 yypParser->yyerrcnt--;
911 #endif
912 yymajor = YYNOCODE;
913 }else if( yyact==YY_ACCEPT_ACTION ){
914 yypParser->yytos--;
915 yy_accept(yypParser);
916 return;
917 }else{
918 assert( yyact == YY_ERROR_ACTION );
919 yyminorunion.yy0 = yyminor;
920 #ifdef YYERRORSYMBOL
921 int yymx;
922 #endif
923 #ifndef NDEBUG
924 if( yyTraceFILE ){
925 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
927 #endif
928 #ifdef YYERRORSYMBOL
929 /* A syntax error has occurred.
930 ** The response to an error depends upon whether or not the
931 ** grammar defines an error token "ERROR".
933 ** This is what we do if the grammar does define ERROR:
935 ** * Call the %syntax_error function.
937 ** * Begin popping the stack until we enter a state where
938 ** it is legal to shift the error symbol, then shift
939 ** the error symbol.
941 ** * Set the error count to three.
943 ** * Begin accepting and shifting new tokens. No new error
944 ** processing will occur until three tokens have been
945 ** shifted successfully.
948 if( yypParser->yyerrcnt<0 ){
949 yy_syntax_error(yypParser,yymajor,yyminor);
951 yymx = yypParser->yytos->major;
952 if( yymx==YYERRORSYMBOL || yyerrorhit ){
953 #ifndef NDEBUG
954 if( yyTraceFILE ){
955 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
956 yyTracePrompt,yyTokenName[yymajor]);
958 #endif
959 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
960 yymajor = YYNOCODE;
961 }else{
962 while( yypParser->yytos >= yypParser->yystack
963 && yymx != YYERRORSYMBOL
964 && (yyact = yy_find_reduce_action(
965 yypParser->yytos->stateno,
966 YYERRORSYMBOL)) >= YY_MIN_REDUCE
968 yy_pop_parser_stack(yypParser);
970 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
971 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
972 yy_parse_failed(yypParser);
973 #ifndef YYNOERRORRECOVERY
974 yypParser->yyerrcnt = -1;
975 #endif
976 yymajor = YYNOCODE;
977 }else if( yymx!=YYERRORSYMBOL ){
978 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
981 yypParser->yyerrcnt = 3;
982 yyerrorhit = 1;
983 #elif defined(YYNOERRORRECOVERY)
984 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
985 ** do any kind of error recovery. Instead, simply invoke the syntax
986 ** error routine and continue going as if nothing had happened.
988 ** Applications can set this macro (for example inside %include) if
989 ** they intend to abandon the parse upon the first syntax error seen.
991 yy_syntax_error(yypParser,yymajor, yyminor);
992 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
993 yymajor = YYNOCODE;
995 #else /* YYERRORSYMBOL is not defined */
996 /* This is what we do if the grammar does not define ERROR:
998 ** * Report an error message, and throw away the input token.
1000 ** * If the input token is $, then fail the parse.
1002 ** As before, subsequent error messages are suppressed until
1003 ** three input tokens have been successfully shifted.
1005 if( yypParser->yyerrcnt<=0 ){
1006 yy_syntax_error(yypParser,yymajor, yyminor);
1008 yypParser->yyerrcnt = 3;
1009 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1010 if( yyendofinput ){
1011 yy_parse_failed(yypParser);
1012 #ifndef YYNOERRORRECOVERY
1013 yypParser->yyerrcnt = -1;
1014 #endif
1016 yymajor = YYNOCODE;
1017 #endif
1019 }while( yymajor!=YYNOCODE && yypParser->yytos>yypParser->yystack );
1020 #ifndef NDEBUG
1021 if( yyTraceFILE ){
1022 yyStackEntry *i;
1023 char cDiv = '[';
1024 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
1025 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
1026 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
1027 cDiv = ' ';
1029 fprintf(yyTraceFILE,"]\n");
1031 #endif
1032 return;