Consider using "=" and IS operators with even low-quality indexes in cases where...
[sqlite.git] / tool / lempar.c
blob851a0e2e5433ec560df3256d4cd4ec3087a274b6
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 /************ Begin %include sections from the grammar ************************/
27 /**************** End of %include directives **********************************/
28 /* These constants specify the various numeric values for terminal symbols.
29 ***************** Begin token definitions *************************************/
31 /**************** End token definitions ***************************************/
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 ** YYREALLOC Name of the realloc() function to use
71 ** YYFREE Name of the free() function to use
72 ** YYDYNSTACK True if stack space should be extended on heap
73 ** YYERRORSYMBOL is the code number of the error symbol. If not
74 ** defined, then do no error processing.
75 ** YYNSTATE the combined number of states.
76 ** YYNRULE the number of rules in the grammar
77 ** YYNTOKEN Number of terminal symbols
78 ** YY_MAX_SHIFT Maximum value for shift actions
79 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
80 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
81 ** YY_ERROR_ACTION The yy_action[] code for syntax error
82 ** YY_ACCEPT_ACTION The yy_action[] code for accept
83 ** YY_NO_ACTION The yy_action[] code for no-op
84 ** YY_MIN_REDUCE Minimum value for reduce actions
85 ** YY_MAX_REDUCE Maximum value for reduce actions
86 ** YY_MIN_DSTRCTR Minimum symbol value that has a destructor
87 ** YY_MAX_DSTRCTR Maximum symbol value that has a destructor
89 #ifndef INTERFACE
90 # define INTERFACE 1
91 #endif
92 /************* Begin control #defines *****************************************/
94 /************* End control #defines *******************************************/
95 #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
97 /* Define the yytestcase() macro to be a no-op if is not already defined
98 ** otherwise.
100 ** Applications can choose to define yytestcase() in the %include section
101 ** to a macro that can assist in verifying code coverage. For production
102 ** code the yytestcase() macro should be turned off. But it is useful
103 ** for testing.
105 #ifndef yytestcase
106 # define yytestcase(X)
107 #endif
109 /* Macro to determine if stack space has the ability to grow using
110 ** heap memory.
112 #if YYSTACKDEPTH<=0 || YYDYNSTACK
113 # define YYGROWABLESTACK 1
114 #else
115 # define YYGROWABLESTACK 0
116 #endif
118 /* Guarantee a minimum number of initial stack slots.
120 #if YYSTACKDEPTH<=0
121 # undef YYSTACKDEPTH
122 # define YYSTACKDEPTH 2 /* Need a minimum stack size */
123 #endif
126 /* Next are the tables used to determine what action to take based on the
127 ** current state and lookahead token. These tables are used to implement
128 ** functions that take a state number and lookahead value and return an
129 ** action integer.
131 ** Suppose the action integer is N. Then the action is determined as
132 ** follows
134 ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
135 ** token onto the stack and goto state N.
137 ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
138 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
140 ** N == YY_ERROR_ACTION A syntax error has occurred.
142 ** N == YY_ACCEPT_ACTION The parser accepts its input.
144 ** N == YY_NO_ACTION No such action. Denotes unused
145 ** slots in the yy_action[] table.
147 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
148 ** and YY_MAX_REDUCE
150 ** The action table is constructed as a single large table named yy_action[].
151 ** Given state S and lookahead X, the action is computed as either:
153 ** (A) N = yy_action[ yy_shift_ofst[S] + X ]
154 ** (B) N = yy_default[S]
156 ** The (A) formula is preferred. The B formula is used instead if
157 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
159 ** The formulas above are for computing the action when the lookahead is
160 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
161 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
162 ** the yy_shift_ofst[] array.
164 ** The following are the tables generated in this section:
166 ** yy_action[] A single table containing all actions.
167 ** yy_lookahead[] A table containing the lookahead for each entry in
168 ** yy_action. Used to detect hash collisions.
169 ** yy_shift_ofst[] For each state, the offset into yy_action for
170 ** shifting terminals.
171 ** yy_reduce_ofst[] For each state, the offset into yy_action for
172 ** shifting non-terminals after a reduce.
173 ** yy_default[] Default action for each state.
175 *********** Begin parsing tables **********************************************/
177 /********** End of lemon-generated parsing tables *****************************/
179 /* The next table maps tokens (terminal symbols) into fallback tokens.
180 ** If a construct like the following:
182 ** %fallback ID X Y Z.
184 ** appears in the grammar, then ID becomes a fallback token for X, Y,
185 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
186 ** but it does not parse, the type of the token is changed to ID and
187 ** the parse is retried before an error is thrown.
189 ** This feature can be used, for example, to cause some keywords in a language
190 ** to revert to identifiers if they keyword does not apply in the context where
191 ** it appears.
193 #ifdef YYFALLBACK
194 static const YYCODETYPE yyFallback[] = {
197 #endif /* YYFALLBACK */
199 /* The following structure represents a single element of the
200 ** parser's stack. Information stored includes:
202 ** + The state number for the parser at this level of the stack.
204 ** + The value of the token stored at this level of the stack.
205 ** (In other words, the "major" token.)
207 ** + The semantic value stored at this level of the stack. This is
208 ** the information used by the action routines in the grammar.
209 ** It is sometimes called the "minor" token.
211 ** After the "shift" half of a SHIFTREDUCE action, the stateno field
212 ** actually contains the reduce action for the second half of the
213 ** SHIFTREDUCE.
215 struct yyStackEntry {
216 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
217 YYCODETYPE major; /* The major token value. This is the code
218 ** number for the token at this stack level */
219 YYMINORTYPE minor; /* The user-supplied minor token value. This
220 ** is the value of the token */
222 typedef struct yyStackEntry yyStackEntry;
224 /* The state of the parser is completely contained in an instance of
225 ** the following structure */
226 struct yyParser {
227 yyStackEntry *yytos; /* Pointer to top element of the stack */
228 #ifdef YYTRACKMAXSTACKDEPTH
229 int yyhwm; /* High-water mark of the stack */
230 #endif
231 #ifndef YYNOERRORRECOVERY
232 int yyerrcnt; /* Shifts left before out of the error */
233 #endif
234 ParseARG_SDECL /* A place to hold %extra_argument */
235 ParseCTX_SDECL /* A place to hold %extra_context */
236 yyStackEntry *yystackEnd; /* Last entry in the stack */
237 yyStackEntry *yystack; /* The parser stack */
238 yyStackEntry yystk0[YYSTACKDEPTH]; /* Initial stack space */
240 typedef struct yyParser yyParser;
242 #include <assert.h>
243 #ifndef NDEBUG
244 #include <stdio.h>
245 static FILE *yyTraceFILE = 0;
246 static char *yyTracePrompt = 0;
247 #endif /* NDEBUG */
249 #ifndef NDEBUG
251 ** Turn parser tracing on by giving a stream to which to write the trace
252 ** and a prompt to preface each trace message. Tracing is turned off
253 ** by making either argument NULL
255 ** Inputs:
256 ** <ul>
257 ** <li> A FILE* to which trace output should be written.
258 ** If NULL, then tracing is turned off.
259 ** <li> A prefix string written at the beginning of every
260 ** line of trace output. If NULL, then tracing is
261 ** turned off.
262 ** </ul>
264 ** Outputs:
265 ** None.
267 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
268 yyTraceFILE = TraceFILE;
269 yyTracePrompt = zTracePrompt;
270 if( yyTraceFILE==0 ) yyTracePrompt = 0;
271 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
273 #endif /* NDEBUG */
275 #if defined(YYCOVERAGE) || !defined(NDEBUG)
276 /* For tracing shifts, the names of all terminals and nonterminals
277 ** are required. The following table supplies these names */
278 static const char *const yyTokenName[] = {
281 #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
283 #ifndef NDEBUG
284 /* For tracing reduce actions, the names of all rules are required.
286 static const char *const yyRuleName[] = {
289 #endif /* NDEBUG */
292 #if YYGROWABLESTACK
294 ** Try to increase the size of the parser stack. Return the number
295 ** of errors. Return 0 on success.
297 static int yyGrowStack(yyParser *p){
298 int oldSize = 1 + (int)(p->yystackEnd - p->yystack);
299 int newSize;
300 int idx;
301 yyStackEntry *pNew;
303 newSize = oldSize*2 + 100;
304 idx = (int)(p->yytos - p->yystack);
305 if( p->yystack==p->yystk0 ){
306 pNew = YYREALLOC(0, newSize*sizeof(pNew[0]));
307 if( pNew==0 ) return 1;
308 memcpy(pNew, p->yystack, oldSize*sizeof(pNew[0]));
309 }else{
310 pNew = YYREALLOC(p->yystack, newSize*sizeof(pNew[0]));
311 if( pNew==0 ) return 1;
313 p->yystack = pNew;
314 p->yytos = &p->yystack[idx];
315 #ifndef NDEBUG
316 if( yyTraceFILE ){
317 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
318 yyTracePrompt, oldSize, newSize);
320 #endif
321 p->yystackEnd = &p->yystack[newSize-1];
322 return 0;
324 #endif /* YYGROWABLESTACK */
326 #if !YYGROWABLESTACK
327 /* For builds that do no have a growable stack, yyGrowStack always
328 ** returns an error.
330 # define yyGrowStack(X) 1
331 #endif
333 /* Datatype of the argument to the memory allocated passed as the
334 ** second argument to ParseAlloc() below. This can be changed by
335 ** putting an appropriate #define in the %include section of the input
336 ** grammar.
338 #ifndef YYMALLOCARGTYPE
339 # define YYMALLOCARGTYPE size_t
340 #endif
342 /* Initialize a new parser that has already been allocated.
344 void ParseInit(void *yypRawParser ParseCTX_PDECL){
345 yyParser *yypParser = (yyParser*)yypRawParser;
346 ParseCTX_STORE
347 #ifdef YYTRACKMAXSTACKDEPTH
348 yypParser->yyhwm = 0;
349 #endif
350 yypParser->yystack = yypParser->yystk0;
351 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
352 #ifndef YYNOERRORRECOVERY
353 yypParser->yyerrcnt = -1;
354 #endif
355 yypParser->yytos = yypParser->yystack;
356 yypParser->yystack[0].stateno = 0;
357 yypParser->yystack[0].major = 0;
360 #ifndef Parse_ENGINEALWAYSONSTACK
362 ** This function allocates a new parser.
363 ** The only argument is a pointer to a function which works like
364 ** malloc.
366 ** Inputs:
367 ** A pointer to the function used to allocate memory.
369 ** Outputs:
370 ** A pointer to a parser. This pointer is used in subsequent calls
371 ** to Parse and ParseFree.
373 void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){
374 yyParser *yypParser;
375 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
376 if( yypParser ){
377 ParseCTX_STORE
378 ParseInit(yypParser ParseCTX_PARAM);
380 return (void*)yypParser;
382 #endif /* Parse_ENGINEALWAYSONSTACK */
385 /* The following function deletes the "minor type" or semantic value
386 ** associated with a symbol. The symbol can be either a terminal
387 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
388 ** a pointer to the value to be deleted. The code used to do the
389 ** deletions is derived from the %destructor and/or %token_destructor
390 ** directives of the input grammar.
392 static void yy_destructor(
393 yyParser *yypParser, /* The parser */
394 YYCODETYPE yymajor, /* Type code for object to destroy */
395 YYMINORTYPE *yypminor /* The object to be destroyed */
397 ParseARG_FETCH
398 ParseCTX_FETCH
399 switch( yymajor ){
400 /* Here is inserted the actions which take place when a
401 ** terminal or non-terminal is destroyed. This can happen
402 ** when the symbol is popped from the stack during a
403 ** reduce or during error processing or when a parser is
404 ** being destroyed before it is finished parsing.
406 ** Note: during a reduce, the only symbols destroyed are those
407 ** which appear on the RHS of the rule, but which are *not* used
408 ** inside the C code.
410 /********* Begin destructor definitions ***************************************/
412 /********* End destructor definitions *****************************************/
413 default: break; /* If no destructor action specified: do nothing */
418 ** Pop the parser's stack once.
420 ** If there is a destructor routine associated with the token which
421 ** is popped from the stack, then call it.
423 static void yy_pop_parser_stack(yyParser *pParser){
424 yyStackEntry *yytos;
425 assert( pParser->yytos!=0 );
426 assert( pParser->yytos > pParser->yystack );
427 yytos = pParser->yytos--;
428 #ifndef NDEBUG
429 if( yyTraceFILE ){
430 fprintf(yyTraceFILE,"%sPopping %s\n",
431 yyTracePrompt,
432 yyTokenName[yytos->major]);
434 #endif
435 yy_destructor(pParser, yytos->major, &yytos->minor);
439 ** Clear all secondary memory allocations from the parser
441 void ParseFinalize(void *p){
442 yyParser *pParser = (yyParser*)p;
444 /* In-lined version of calling yy_pop_parser_stack() for each
445 ** element left in the stack */
446 yyStackEntry *yytos = pParser->yytos;
447 while( yytos>pParser->yystack ){
448 #ifndef NDEBUG
449 if( yyTraceFILE ){
450 fprintf(yyTraceFILE,"%sPopping %s\n",
451 yyTracePrompt,
452 yyTokenName[yytos->major]);
454 #endif
455 if( yytos->major>=YY_MIN_DSTRCTR ){
456 yy_destructor(pParser, yytos->major, &yytos->minor);
458 yytos--;
461 #if YYGROWABLESTACK
462 if( pParser->yystack!=pParser->yystk0 ) YYFREE(pParser->yystack);
463 #endif
466 #ifndef Parse_ENGINEALWAYSONSTACK
468 ** Deallocate and destroy a parser. Destructors are called for
469 ** all stack elements before shutting the parser down.
471 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
472 ** is defined in a %include section of the input grammar) then it is
473 ** assumed that the input pointer is never NULL.
475 void ParseFree(
476 void *p, /* The parser to be deleted */
477 void (*freeProc)(void*) /* Function used to reclaim memory */
479 #ifndef YYPARSEFREENEVERNULL
480 if( p==0 ) return;
481 #endif
482 ParseFinalize(p);
483 (*freeProc)(p);
485 #endif /* Parse_ENGINEALWAYSONSTACK */
488 ** Return the peak depth of the stack for a parser.
490 #ifdef YYTRACKMAXSTACKDEPTH
491 int ParseStackPeak(void *p){
492 yyParser *pParser = (yyParser*)p;
493 return pParser->yyhwm;
495 #endif
497 /* This array of booleans keeps track of the parser statement
498 ** coverage. The element yycoverage[X][Y] is set when the parser
499 ** is in state X and has a lookahead token Y. In a well-tested
500 ** systems, every element of this matrix should end up being set.
502 #if defined(YYCOVERAGE)
503 static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
504 #endif
507 ** Write into out a description of every state/lookahead combination that
509 ** (1) has not been used by the parser, and
510 ** (2) is not a syntax error.
512 ** Return the number of missed state/lookahead combinations.
514 #if defined(YYCOVERAGE)
515 int ParseCoverage(FILE *out){
516 int stateno, iLookAhead, i;
517 int nMissed = 0;
518 for(stateno=0; stateno<YYNSTATE; stateno++){
519 i = yy_shift_ofst[stateno];
520 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
521 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
522 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
523 if( out ){
524 fprintf(out,"State %d lookahead %s %s\n", stateno,
525 yyTokenName[iLookAhead],
526 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
530 return nMissed;
532 #endif
535 ** Find the appropriate action for a parser given the terminal
536 ** look-ahead token iLookAhead.
538 static YYACTIONTYPE yy_find_shift_action(
539 YYCODETYPE iLookAhead, /* The look-ahead token */
540 YYACTIONTYPE stateno /* Current state number */
542 int i;
544 if( stateno>YY_MAX_SHIFT ) return stateno;
545 assert( stateno <= YY_SHIFT_COUNT );
546 #if defined(YYCOVERAGE)
547 yycoverage[stateno][iLookAhead] = 1;
548 #endif
550 i = yy_shift_ofst[stateno];
551 assert( i>=0 );
552 assert( i<=YY_ACTTAB_COUNT );
553 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD );
554 assert( iLookAhead!=YYNOCODE );
555 assert( iLookAhead < YYNTOKEN );
556 i += iLookAhead;
557 assert( i<(int)YY_NLOOKAHEAD );
558 if( yy_lookahead[i]!=iLookAhead ){
559 #ifdef YYFALLBACK
560 YYCODETYPE iFallback; /* Fallback token */
561 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
562 iFallback = yyFallback[iLookAhead];
563 if( iFallback!=0 ){
564 #ifndef NDEBUG
565 if( yyTraceFILE ){
566 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
567 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
569 #endif
570 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
571 iLookAhead = iFallback;
572 continue;
574 #endif
575 #ifdef YYWILDCARD
577 int j = i - iLookAhead + YYWILDCARD;
578 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) );
579 if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){
580 #ifndef NDEBUG
581 if( yyTraceFILE ){
582 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
583 yyTracePrompt, yyTokenName[iLookAhead],
584 yyTokenName[YYWILDCARD]);
586 #endif /* NDEBUG */
587 return yy_action[j];
590 #endif /* YYWILDCARD */
591 return yy_default[stateno];
592 }else{
593 assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) );
594 return yy_action[i];
596 }while(1);
600 ** Find the appropriate action for a parser given the non-terminal
601 ** look-ahead token iLookAhead.
603 static YYACTIONTYPE yy_find_reduce_action(
604 YYACTIONTYPE stateno, /* Current state number */
605 YYCODETYPE iLookAhead /* The look-ahead token */
607 int i;
608 #ifdef YYERRORSYMBOL
609 if( stateno>YY_REDUCE_COUNT ){
610 return yy_default[stateno];
612 #else
613 assert( stateno<=YY_REDUCE_COUNT );
614 #endif
615 i = yy_reduce_ofst[stateno];
616 assert( iLookAhead!=YYNOCODE );
617 i += iLookAhead;
618 #ifdef YYERRORSYMBOL
619 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
620 return yy_default[stateno];
622 #else
623 assert( i>=0 && i<YY_ACTTAB_COUNT );
624 assert( yy_lookahead[i]==iLookAhead );
625 #endif
626 return yy_action[i];
630 ** The following routine is called if the stack overflows.
632 static void yyStackOverflow(yyParser *yypParser){
633 ParseARG_FETCH
634 ParseCTX_FETCH
635 #ifndef NDEBUG
636 if( yyTraceFILE ){
637 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
639 #endif
640 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
641 /* Here code is inserted which will execute if the parser
642 ** stack every overflows */
643 /******** Begin %stack_overflow code ******************************************/
645 /******** End %stack_overflow code ********************************************/
646 ParseARG_STORE /* Suppress warning about unused %extra_argument var */
647 ParseCTX_STORE
651 ** Print tracing information for a SHIFT action
653 #ifndef NDEBUG
654 static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
655 if( yyTraceFILE ){
656 if( yyNewState<YYNSTATE ){
657 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
658 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
659 yyNewState);
660 }else{
661 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
662 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
663 yyNewState - YY_MIN_REDUCE);
667 #else
668 # define yyTraceShift(X,Y,Z)
669 #endif
672 ** Perform a shift action.
674 static void yy_shift(
675 yyParser *yypParser, /* The parser to be shifted */
676 YYACTIONTYPE yyNewState, /* The new state to shift in */
677 YYCODETYPE yyMajor, /* The major token to shift in */
678 ParseTOKENTYPE yyMinor /* The minor token to shift in */
680 yyStackEntry *yytos;
681 yypParser->yytos++;
682 #ifdef YYTRACKMAXSTACKDEPTH
683 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
684 yypParser->yyhwm++;
685 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
687 #endif
688 yytos = yypParser->yytos;
689 if( yytos>yypParser->yystackEnd ){
690 if( yyGrowStack(yypParser) ){
691 yypParser->yytos--;
692 yyStackOverflow(yypParser);
693 return;
695 yytos = yypParser->yytos;
696 assert( yytos <= yypParser->yystackEnd );
698 if( yyNewState > YY_MAX_SHIFT ){
699 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
701 yytos->stateno = yyNewState;
702 yytos->major = yyMajor;
703 yytos->minor.yy0 = yyMinor;
704 yyTraceShift(yypParser, yyNewState, "Shift");
707 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
708 ** of that rule */
709 static const YYCODETYPE yyRuleInfoLhs[] = {
713 /* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
714 ** of symbols on the right-hand side of that rule. */
715 static const signed char yyRuleInfoNRhs[] = {
719 static void yy_accept(yyParser*); /* Forward Declaration */
722 ** Perform a reduce action and the shift that must immediately
723 ** follow the reduce.
725 ** The yyLookahead and yyLookaheadToken parameters provide reduce actions
726 ** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
727 ** if the lookahead token has already been consumed. As this procedure is
728 ** only called from one place, optimizing compilers will in-line it, which
729 ** means that the extra parameters have no performance impact.
731 static YYACTIONTYPE yy_reduce(
732 yyParser *yypParser, /* The parser */
733 unsigned int yyruleno, /* Number of the rule by which to reduce */
734 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
735 ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
736 ParseCTX_PDECL /* %extra_context */
738 int yygoto; /* The next state */
739 YYACTIONTYPE yyact; /* The next action */
740 yyStackEntry *yymsp; /* The top of the parser's stack */
741 int yysize; /* Amount to pop the stack */
742 ParseARG_FETCH
743 (void)yyLookahead;
744 (void)yyLookaheadToken;
745 yymsp = yypParser->yytos;
747 switch( yyruleno ){
748 /* Beginning here are the reduction cases. A typical example
749 ** follows:
750 ** case 0:
751 ** #line <lineno> <grammarfile>
752 ** { ... } // User supplied code
753 ** #line <lineno> <thisfile>
754 ** break;
756 /********** Begin reduce actions **********************************************/
758 /********** End reduce actions ************************************************/
760 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
761 yygoto = yyRuleInfoLhs[yyruleno];
762 yysize = yyRuleInfoNRhs[yyruleno];
763 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
765 /* There are no SHIFTREDUCE actions on nonterminals because the table
766 ** generator has simplified them to pure REDUCE actions. */
767 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
769 /* It is not possible for a REDUCE to be followed by an error */
770 assert( yyact!=YY_ERROR_ACTION );
772 yymsp += yysize+1;
773 yypParser->yytos = yymsp;
774 yymsp->stateno = (YYACTIONTYPE)yyact;
775 yymsp->major = (YYCODETYPE)yygoto;
776 yyTraceShift(yypParser, yyact, "... then shift");
777 return yyact;
781 ** The following code executes when the parse fails
783 #ifndef YYNOERRORRECOVERY
784 static void yy_parse_failed(
785 yyParser *yypParser /* The parser */
787 ParseARG_FETCH
788 ParseCTX_FETCH
789 #ifndef NDEBUG
790 if( yyTraceFILE ){
791 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
793 #endif
794 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
795 /* Here code is inserted which will be executed whenever the
796 ** parser fails */
797 /************ Begin %parse_failure code ***************************************/
799 /************ End %parse_failure code *****************************************/
800 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
801 ParseCTX_STORE
803 #endif /* YYNOERRORRECOVERY */
806 ** The following code executes when a syntax error first occurs.
808 static void yy_syntax_error(
809 yyParser *yypParser, /* The parser */
810 int yymajor, /* The major type of the error token */
811 ParseTOKENTYPE yyminor /* The minor type of the error token */
813 ParseARG_FETCH
814 ParseCTX_FETCH
815 #define TOKEN yyminor
816 /************ Begin %syntax_error code ****************************************/
818 /************ End %syntax_error code ******************************************/
819 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
820 ParseCTX_STORE
824 ** The following is executed when the parser accepts
826 static void yy_accept(
827 yyParser *yypParser /* The parser */
829 ParseARG_FETCH
830 ParseCTX_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 */
846 ParseCTX_STORE
849 /* The main parser program.
850 ** The first argument is a pointer to a structure obtained from
851 ** "ParseAlloc" which describes the current state of the parser.
852 ** The second argument is the major token number. The third is
853 ** the minor token. The fourth optional argument is whatever the
854 ** user wants (and specified in the grammar) and is available for
855 ** use by the action routines.
857 ** Inputs:
858 ** <ul>
859 ** <li> A pointer to the parser (an opaque structure.)
860 ** <li> The major token number.
861 ** <li> The minor token number.
862 ** <li> An option argument of a grammar-specified type.
863 ** </ul>
865 ** Outputs:
866 ** None.
868 void Parse(
869 void *yyp, /* The parser */
870 int yymajor, /* The major token code number */
871 ParseTOKENTYPE yyminor /* The value for the token */
872 ParseARG_PDECL /* Optional %extra_argument parameter */
874 YYMINORTYPE yyminorunion;
875 YYACTIONTYPE yyact; /* The parser action. */
876 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
877 int yyendofinput; /* True if we are at the end of input */
878 #endif
879 #ifdef YYERRORSYMBOL
880 int yyerrorhit = 0; /* True if yymajor has invoked an error */
881 #endif
882 yyParser *yypParser = (yyParser*)yyp; /* The parser */
883 ParseCTX_FETCH
884 ParseARG_STORE
886 assert( yypParser->yytos!=0 );
887 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
888 yyendofinput = (yymajor==0);
889 #endif
891 yyact = yypParser->yytos->stateno;
892 #ifndef NDEBUG
893 if( yyTraceFILE ){
894 if( yyact < YY_MIN_REDUCE ){
895 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
896 yyTracePrompt,yyTokenName[yymajor],yyact);
897 }else{
898 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
899 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
902 #endif
904 while(1){ /* Exit by "break" */
905 assert( yypParser->yytos>=yypParser->yystack );
906 assert( yyact==yypParser->yytos->stateno );
907 yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
908 if( yyact >= YY_MIN_REDUCE ){
909 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
910 #ifndef NDEBUG
911 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) );
912 if( yyTraceFILE ){
913 int yysize = yyRuleInfoNRhs[yyruleno];
914 if( yysize ){
915 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
916 yyTracePrompt,
917 yyruleno, yyRuleName[yyruleno],
918 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
919 yypParser->yytos[yysize].stateno);
920 }else{
921 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
922 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
923 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
926 #endif /* NDEBUG */
928 /* Check that the stack is large enough to grow by a single entry
929 ** if the RHS of the rule is empty. This ensures that there is room
930 ** enough on the stack to push the LHS value */
931 if( yyRuleInfoNRhs[yyruleno]==0 ){
932 #ifdef YYTRACKMAXSTACKDEPTH
933 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
934 yypParser->yyhwm++;
935 assert( yypParser->yyhwm ==
936 (int)(yypParser->yytos - yypParser->yystack));
938 #endif
939 if( yypParser->yytos>=yypParser->yystackEnd ){
940 if( yyGrowStack(yypParser) ){
941 yyStackOverflow(yypParser);
942 break;
946 yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor ParseCTX_PARAM);
947 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
948 yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
949 #ifndef YYNOERRORRECOVERY
950 yypParser->yyerrcnt--;
951 #endif
952 break;
953 }else if( yyact==YY_ACCEPT_ACTION ){
954 yypParser->yytos--;
955 yy_accept(yypParser);
956 return;
957 }else{
958 assert( yyact == YY_ERROR_ACTION );
959 yyminorunion.yy0 = yyminor;
960 #ifdef YYERRORSYMBOL
961 int yymx;
962 #endif
963 #ifndef NDEBUG
964 if( yyTraceFILE ){
965 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
967 #endif
968 #ifdef YYERRORSYMBOL
969 /* A syntax error has occurred.
970 ** The response to an error depends upon whether or not the
971 ** grammar defines an error token "ERROR".
973 ** This is what we do if the grammar does define ERROR:
975 ** * Call the %syntax_error function.
977 ** * Begin popping the stack until we enter a state where
978 ** it is legal to shift the error symbol, then shift
979 ** the error symbol.
981 ** * Set the error count to three.
983 ** * Begin accepting and shifting new tokens. No new error
984 ** processing will occur until three tokens have been
985 ** shifted successfully.
988 if( yypParser->yyerrcnt<0 ){
989 yy_syntax_error(yypParser,yymajor,yyminor);
991 yymx = yypParser->yytos->major;
992 if( yymx==YYERRORSYMBOL || yyerrorhit ){
993 #ifndef NDEBUG
994 if( yyTraceFILE ){
995 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
996 yyTracePrompt,yyTokenName[yymajor]);
998 #endif
999 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
1000 yymajor = YYNOCODE;
1001 }else{
1002 while( yypParser->yytos > yypParser->yystack ){
1003 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
1004 YYERRORSYMBOL);
1005 if( yyact<=YY_MAX_SHIFTREDUCE ) break;
1006 yy_pop_parser_stack(yypParser);
1008 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
1009 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1010 yy_parse_failed(yypParser);
1011 #ifndef YYNOERRORRECOVERY
1012 yypParser->yyerrcnt = -1;
1013 #endif
1014 yymajor = YYNOCODE;
1015 }else if( yymx!=YYERRORSYMBOL ){
1016 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
1019 yypParser->yyerrcnt = 3;
1020 yyerrorhit = 1;
1021 if( yymajor==YYNOCODE ) break;
1022 yyact = yypParser->yytos->stateno;
1023 #elif defined(YYNOERRORRECOVERY)
1024 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
1025 ** do any kind of error recovery. Instead, simply invoke the syntax
1026 ** error routine and continue going as if nothing had happened.
1028 ** Applications can set this macro (for example inside %include) if
1029 ** they intend to abandon the parse upon the first syntax error seen.
1031 yy_syntax_error(yypParser,yymajor, yyminor);
1032 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1033 break;
1034 #else /* YYERRORSYMBOL is not defined */
1035 /* This is what we do if the grammar does not define ERROR:
1037 ** * Report an error message, and throw away the input token.
1039 ** * If the input token is $, then fail the parse.
1041 ** As before, subsequent error messages are suppressed until
1042 ** three input tokens have been successfully shifted.
1044 if( yypParser->yyerrcnt<=0 ){
1045 yy_syntax_error(yypParser,yymajor, yyminor);
1047 yypParser->yyerrcnt = 3;
1048 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1049 if( yyendofinput ){
1050 yy_parse_failed(yypParser);
1051 #ifndef YYNOERRORRECOVERY
1052 yypParser->yyerrcnt = -1;
1053 #endif
1055 break;
1056 #endif
1059 #ifndef NDEBUG
1060 if( yyTraceFILE ){
1061 yyStackEntry *i;
1062 char cDiv = '[';
1063 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
1064 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
1065 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
1066 cDiv = ' ';
1068 fprintf(yyTraceFILE,"]\n");
1070 #endif
1071 return;
1075 ** Return the fallback token corresponding to canonical token iToken, or
1076 ** 0 if iToken has no fallback.
1078 int ParseFallback(int iToken){
1079 #ifdef YYFALLBACK
1080 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
1081 return yyFallback[iToken];
1082 #else
1083 (void)iToken;
1084 return 0;
1085 #endif