Work toward enhanced functionality for json_valid() with deep checking
[sqlite.git] / tool / lempar.c
blob8cc57897db13a3b4d83c669aa58f720547c11def
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 ** YYERRORSYMBOL is the code number of the error symbol. If not
71 ** defined, then do no error processing.
72 ** YYNSTATE the combined number of states.
73 ** YYNRULE the number of rules in the grammar
74 ** YYNTOKEN Number of terminal symbols
75 ** YY_MAX_SHIFT Maximum value for shift actions
76 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
77 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
78 ** YY_ERROR_ACTION The yy_action[] code for syntax error
79 ** YY_ACCEPT_ACTION The yy_action[] code for accept
80 ** YY_NO_ACTION The yy_action[] code for no-op
81 ** YY_MIN_REDUCE Minimum value for reduce actions
82 ** YY_MAX_REDUCE Maximum value for reduce actions
84 #ifndef INTERFACE
85 # define INTERFACE 1
86 #endif
87 /************* Begin control #defines *****************************************/
89 /************* End control #defines *******************************************/
90 #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
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 ParseCTX_SDECL /* A place to hold %extra_context */
215 #if YYSTACKDEPTH<=0
216 int yystksz; /* Current side of the stack */
217 yyStackEntry *yystack; /* The parser's stack */
218 yyStackEntry yystk0; /* First stack entry */
219 #else
220 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
221 yyStackEntry *yystackEnd; /* Last entry in the stack */
222 #endif
224 typedef struct yyParser yyParser;
226 #include <assert.h>
227 #ifndef NDEBUG
228 #include <stdio.h>
229 static FILE *yyTraceFILE = 0;
230 static char *yyTracePrompt = 0;
231 #endif /* NDEBUG */
233 #ifndef NDEBUG
235 ** Turn parser tracing on by giving a stream to which to write the trace
236 ** and a prompt to preface each trace message. Tracing is turned off
237 ** by making either argument NULL
239 ** Inputs:
240 ** <ul>
241 ** <li> A FILE* to which trace output should be written.
242 ** If NULL, then tracing is turned off.
243 ** <li> A prefix string written at the beginning of every
244 ** line of trace output. If NULL, then tracing is
245 ** turned off.
246 ** </ul>
248 ** Outputs:
249 ** None.
251 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
252 yyTraceFILE = TraceFILE;
253 yyTracePrompt = zTracePrompt;
254 if( yyTraceFILE==0 ) yyTracePrompt = 0;
255 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
257 #endif /* NDEBUG */
259 #if defined(YYCOVERAGE) || !defined(NDEBUG)
260 /* For tracing shifts, the names of all terminals and nonterminals
261 ** are required. The following table supplies these names */
262 static const char *const yyTokenName[] = {
265 #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
267 #ifndef NDEBUG
268 /* For tracing reduce actions, the names of all rules are required.
270 static const char *const yyRuleName[] = {
273 #endif /* NDEBUG */
276 #if YYSTACKDEPTH<=0
278 ** Try to increase the size of the parser stack. Return the number
279 ** of errors. Return 0 on success.
281 static int yyGrowStack(yyParser *p){
282 int newSize;
283 int idx;
284 yyStackEntry *pNew;
286 newSize = p->yystksz*2 + 100;
287 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
288 if( p->yystack==&p->yystk0 ){
289 pNew = malloc(newSize*sizeof(pNew[0]));
290 if( pNew ) pNew[0] = p->yystk0;
291 }else{
292 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
294 if( pNew ){
295 p->yystack = pNew;
296 p->yytos = &p->yystack[idx];
297 #ifndef NDEBUG
298 if( yyTraceFILE ){
299 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
300 yyTracePrompt, p->yystksz, newSize);
302 #endif
303 p->yystksz = newSize;
305 return pNew==0;
307 #endif
309 /* Datatype of the argument to the memory allocated passed as the
310 ** second argument to ParseAlloc() below. This can be changed by
311 ** putting an appropriate #define in the %include section of the input
312 ** grammar.
314 #ifndef YYMALLOCARGTYPE
315 # define YYMALLOCARGTYPE size_t
316 #endif
318 /* Initialize a new parser that has already been allocated.
320 void ParseInit(void *yypRawParser ParseCTX_PDECL){
321 yyParser *yypParser = (yyParser*)yypRawParser;
322 ParseCTX_STORE
323 #ifdef YYTRACKMAXSTACKDEPTH
324 yypParser->yyhwm = 0;
325 #endif
326 #if YYSTACKDEPTH<=0
327 yypParser->yytos = NULL;
328 yypParser->yystack = NULL;
329 yypParser->yystksz = 0;
330 if( yyGrowStack(yypParser) ){
331 yypParser->yystack = &yypParser->yystk0;
332 yypParser->yystksz = 1;
334 #endif
335 #ifndef YYNOERRORRECOVERY
336 yypParser->yyerrcnt = -1;
337 #endif
338 yypParser->yytos = yypParser->yystack;
339 yypParser->yystack[0].stateno = 0;
340 yypParser->yystack[0].major = 0;
341 #if YYSTACKDEPTH>0
342 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
343 #endif
346 #ifndef Parse_ENGINEALWAYSONSTACK
348 ** This function allocates a new parser.
349 ** The only argument is a pointer to a function which works like
350 ** malloc.
352 ** Inputs:
353 ** A pointer to the function used to allocate memory.
355 ** Outputs:
356 ** A pointer to a parser. This pointer is used in subsequent calls
357 ** to Parse and ParseFree.
359 void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE) ParseCTX_PDECL){
360 yyParser *yypParser;
361 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
362 if( yypParser ){
363 ParseCTX_STORE
364 ParseInit(yypParser ParseCTX_PARAM);
366 return (void*)yypParser;
368 #endif /* Parse_ENGINEALWAYSONSTACK */
371 /* The following function deletes the "minor type" or semantic value
372 ** associated with a symbol. The symbol can be either a terminal
373 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
374 ** a pointer to the value to be deleted. The code used to do the
375 ** deletions is derived from the %destructor and/or %token_destructor
376 ** directives of the input grammar.
378 static void yy_destructor(
379 yyParser *yypParser, /* The parser */
380 YYCODETYPE yymajor, /* Type code for object to destroy */
381 YYMINORTYPE *yypminor /* The object to be destroyed */
383 ParseARG_FETCH
384 ParseCTX_FETCH
385 switch( yymajor ){
386 /* Here is inserted the actions which take place when a
387 ** terminal or non-terminal is destroyed. This can happen
388 ** when the symbol is popped from the stack during a
389 ** reduce or during error processing or when a parser is
390 ** being destroyed before it is finished parsing.
392 ** Note: during a reduce, the only symbols destroyed are those
393 ** which appear on the RHS of the rule, but which are *not* used
394 ** inside the C code.
396 /********* Begin destructor definitions ***************************************/
398 /********* End destructor definitions *****************************************/
399 default: break; /* If no destructor action specified: do nothing */
404 ** Pop the parser's stack once.
406 ** If there is a destructor routine associated with the token which
407 ** is popped from the stack, then call it.
409 static void yy_pop_parser_stack(yyParser *pParser){
410 yyStackEntry *yytos;
411 assert( pParser->yytos!=0 );
412 assert( pParser->yytos > pParser->yystack );
413 yytos = pParser->yytos--;
414 #ifndef NDEBUG
415 if( yyTraceFILE ){
416 fprintf(yyTraceFILE,"%sPopping %s\n",
417 yyTracePrompt,
418 yyTokenName[yytos->major]);
420 #endif
421 yy_destructor(pParser, yytos->major, &yytos->minor);
425 ** Clear all secondary memory allocations from the parser
427 void ParseFinalize(void *p){
428 yyParser *pParser = (yyParser*)p;
429 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
430 #if YYSTACKDEPTH<=0
431 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
432 #endif
435 #ifndef Parse_ENGINEALWAYSONSTACK
437 ** Deallocate and destroy a parser. Destructors are called for
438 ** all stack elements before shutting the parser down.
440 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
441 ** is defined in a %include section of the input grammar) then it is
442 ** assumed that the input pointer is never NULL.
444 void ParseFree(
445 void *p, /* The parser to be deleted */
446 void (*freeProc)(void*) /* Function used to reclaim memory */
448 #ifndef YYPARSEFREENEVERNULL
449 if( p==0 ) return;
450 #endif
451 ParseFinalize(p);
452 (*freeProc)(p);
454 #endif /* Parse_ENGINEALWAYSONSTACK */
457 ** Return the peak depth of the stack for a parser.
459 #ifdef YYTRACKMAXSTACKDEPTH
460 int ParseStackPeak(void *p){
461 yyParser *pParser = (yyParser*)p;
462 return pParser->yyhwm;
464 #endif
466 /* This array of booleans keeps track of the parser statement
467 ** coverage. The element yycoverage[X][Y] is set when the parser
468 ** is in state X and has a lookahead token Y. In a well-tested
469 ** systems, every element of this matrix should end up being set.
471 #if defined(YYCOVERAGE)
472 static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
473 #endif
476 ** Write into out a description of every state/lookahead combination that
478 ** (1) has not been used by the parser, and
479 ** (2) is not a syntax error.
481 ** Return the number of missed state/lookahead combinations.
483 #if defined(YYCOVERAGE)
484 int ParseCoverage(FILE *out){
485 int stateno, iLookAhead, i;
486 int nMissed = 0;
487 for(stateno=0; stateno<YYNSTATE; stateno++){
488 i = yy_shift_ofst[stateno];
489 for(iLookAhead=0; iLookAhead<YYNTOKEN; iLookAhead++){
490 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
491 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
492 if( out ){
493 fprintf(out,"State %d lookahead %s %s\n", stateno,
494 yyTokenName[iLookAhead],
495 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
499 return nMissed;
501 #endif
504 ** Find the appropriate action for a parser given the terminal
505 ** look-ahead token iLookAhead.
507 static YYACTIONTYPE yy_find_shift_action(
508 YYCODETYPE iLookAhead, /* The look-ahead token */
509 YYACTIONTYPE stateno /* Current state number */
511 int i;
513 if( stateno>YY_MAX_SHIFT ) return stateno;
514 assert( stateno <= YY_SHIFT_COUNT );
515 #if defined(YYCOVERAGE)
516 yycoverage[stateno][iLookAhead] = 1;
517 #endif
519 i = yy_shift_ofst[stateno];
520 assert( i>=0 );
521 assert( i<=YY_ACTTAB_COUNT );
522 assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD );
523 assert( iLookAhead!=YYNOCODE );
524 assert( iLookAhead < YYNTOKEN );
525 i += iLookAhead;
526 assert( i<(int)YY_NLOOKAHEAD );
527 if( yy_lookahead[i]!=iLookAhead ){
528 #ifdef YYFALLBACK
529 YYCODETYPE iFallback; /* Fallback token */
530 assert( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) );
531 iFallback = yyFallback[iLookAhead];
532 if( iFallback!=0 ){
533 #ifndef NDEBUG
534 if( yyTraceFILE ){
535 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
536 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
538 #endif
539 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
540 iLookAhead = iFallback;
541 continue;
543 #endif
544 #ifdef YYWILDCARD
546 int j = i - iLookAhead + YYWILDCARD;
547 assert( j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) );
548 if( yy_lookahead[j]==YYWILDCARD && iLookAhead>0 ){
549 #ifndef NDEBUG
550 if( yyTraceFILE ){
551 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
552 yyTracePrompt, yyTokenName[iLookAhead],
553 yyTokenName[YYWILDCARD]);
555 #endif /* NDEBUG */
556 return yy_action[j];
559 #endif /* YYWILDCARD */
560 return yy_default[stateno];
561 }else{
562 assert( i>=0 && i<(int)(sizeof(yy_action)/sizeof(yy_action[0])) );
563 return yy_action[i];
565 }while(1);
569 ** Find the appropriate action for a parser given the non-terminal
570 ** look-ahead token iLookAhead.
572 static YYACTIONTYPE yy_find_reduce_action(
573 YYACTIONTYPE stateno, /* Current state number */
574 YYCODETYPE iLookAhead /* The look-ahead token */
576 int i;
577 #ifdef YYERRORSYMBOL
578 if( stateno>YY_REDUCE_COUNT ){
579 return yy_default[stateno];
581 #else
582 assert( stateno<=YY_REDUCE_COUNT );
583 #endif
584 i = yy_reduce_ofst[stateno];
585 assert( iLookAhead!=YYNOCODE );
586 i += iLookAhead;
587 #ifdef YYERRORSYMBOL
588 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
589 return yy_default[stateno];
591 #else
592 assert( i>=0 && i<YY_ACTTAB_COUNT );
593 assert( yy_lookahead[i]==iLookAhead );
594 #endif
595 return yy_action[i];
599 ** The following routine is called if the stack overflows.
601 static void yyStackOverflow(yyParser *yypParser){
602 ParseARG_FETCH
603 ParseCTX_FETCH
604 #ifndef NDEBUG
605 if( yyTraceFILE ){
606 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
608 #endif
609 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
610 /* Here code is inserted which will execute if the parser
611 ** stack every overflows */
612 /******** Begin %stack_overflow code ******************************************/
614 /******** End %stack_overflow code ********************************************/
615 ParseARG_STORE /* Suppress warning about unused %extra_argument var */
616 ParseCTX_STORE
620 ** Print tracing information for a SHIFT action
622 #ifndef NDEBUG
623 static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
624 if( yyTraceFILE ){
625 if( yyNewState<YYNSTATE ){
626 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
627 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
628 yyNewState);
629 }else{
630 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
631 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
632 yyNewState - YY_MIN_REDUCE);
636 #else
637 # define yyTraceShift(X,Y,Z)
638 #endif
641 ** Perform a shift action.
643 static void yy_shift(
644 yyParser *yypParser, /* The parser to be shifted */
645 YYACTIONTYPE yyNewState, /* The new state to shift in */
646 YYCODETYPE yyMajor, /* The major token to shift in */
647 ParseTOKENTYPE yyMinor /* The minor token to shift in */
649 yyStackEntry *yytos;
650 yypParser->yytos++;
651 #ifdef YYTRACKMAXSTACKDEPTH
652 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
653 yypParser->yyhwm++;
654 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
656 #endif
657 #if YYSTACKDEPTH>0
658 if( yypParser->yytos>yypParser->yystackEnd ){
659 yypParser->yytos--;
660 yyStackOverflow(yypParser);
661 return;
663 #else
664 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
665 if( yyGrowStack(yypParser) ){
666 yypParser->yytos--;
667 yyStackOverflow(yypParser);
668 return;
671 #endif
672 if( yyNewState > YY_MAX_SHIFT ){
673 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
675 yytos = yypParser->yytos;
676 yytos->stateno = yyNewState;
677 yytos->major = yyMajor;
678 yytos->minor.yy0 = yyMinor;
679 yyTraceShift(yypParser, yyNewState, "Shift");
682 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
683 ** of that rule */
684 static const YYCODETYPE yyRuleInfoLhs[] = {
688 /* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
689 ** of symbols on the right-hand side of that rule. */
690 static const signed char yyRuleInfoNRhs[] = {
694 static void yy_accept(yyParser*); /* Forward Declaration */
697 ** Perform a reduce action and the shift that must immediately
698 ** follow the reduce.
700 ** The yyLookahead and yyLookaheadToken parameters provide reduce actions
701 ** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
702 ** if the lookahead token has already been consumed. As this procedure is
703 ** only called from one place, optimizing compilers will in-line it, which
704 ** means that the extra parameters have no performance impact.
706 static YYACTIONTYPE yy_reduce(
707 yyParser *yypParser, /* The parser */
708 unsigned int yyruleno, /* Number of the rule by which to reduce */
709 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
710 ParseTOKENTYPE yyLookaheadToken /* Value of the lookahead token */
711 ParseCTX_PDECL /* %extra_context */
713 int yygoto; /* The next state */
714 YYACTIONTYPE yyact; /* The next action */
715 yyStackEntry *yymsp; /* The top of the parser's stack */
716 int yysize; /* Amount to pop the stack */
717 ParseARG_FETCH
718 (void)yyLookahead;
719 (void)yyLookaheadToken;
720 yymsp = yypParser->yytos;
722 switch( yyruleno ){
723 /* Beginning here are the reduction cases. A typical example
724 ** follows:
725 ** case 0:
726 ** #line <lineno> <grammarfile>
727 ** { ... } // User supplied code
728 ** #line <lineno> <thisfile>
729 ** break;
731 /********** Begin reduce actions **********************************************/
733 /********** End reduce actions ************************************************/
735 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) );
736 yygoto = yyRuleInfoLhs[yyruleno];
737 yysize = yyRuleInfoNRhs[yyruleno];
738 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPE)yygoto);
740 /* There are no SHIFTREDUCE actions on nonterminals because the table
741 ** generator has simplified them to pure REDUCE actions. */
742 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) );
744 /* It is not possible for a REDUCE to be followed by an error */
745 assert( yyact!=YY_ERROR_ACTION );
747 yymsp += yysize+1;
748 yypParser->yytos = yymsp;
749 yymsp->stateno = (YYACTIONTYPE)yyact;
750 yymsp->major = (YYCODETYPE)yygoto;
751 yyTraceShift(yypParser, yyact, "... then shift");
752 return yyact;
756 ** The following code executes when the parse fails
758 #ifndef YYNOERRORRECOVERY
759 static void yy_parse_failed(
760 yyParser *yypParser /* The parser */
762 ParseARG_FETCH
763 ParseCTX_FETCH
764 #ifndef NDEBUG
765 if( yyTraceFILE ){
766 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
768 #endif
769 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
770 /* Here code is inserted which will be executed whenever the
771 ** parser fails */
772 /************ Begin %parse_failure code ***************************************/
774 /************ End %parse_failure code *****************************************/
775 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
776 ParseCTX_STORE
778 #endif /* YYNOERRORRECOVERY */
781 ** The following code executes when a syntax error first occurs.
783 static void yy_syntax_error(
784 yyParser *yypParser, /* The parser */
785 int yymajor, /* The major type of the error token */
786 ParseTOKENTYPE yyminor /* The minor type of the error token */
788 ParseARG_FETCH
789 ParseCTX_FETCH
790 #define TOKEN yyminor
791 /************ Begin %syntax_error code ****************************************/
793 /************ End %syntax_error code ******************************************/
794 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
795 ParseCTX_STORE
799 ** The following is executed when the parser accepts
801 static void yy_accept(
802 yyParser *yypParser /* The parser */
804 ParseARG_FETCH
805 ParseCTX_FETCH
806 #ifndef NDEBUG
807 if( yyTraceFILE ){
808 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
810 #endif
811 #ifndef YYNOERRORRECOVERY
812 yypParser->yyerrcnt = -1;
813 #endif
814 assert( yypParser->yytos==yypParser->yystack );
815 /* Here code is inserted which will be executed whenever the
816 ** parser accepts */
817 /*********** Begin %parse_accept code *****************************************/
819 /*********** End %parse_accept code *******************************************/
820 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
821 ParseCTX_STORE
824 /* The main parser program.
825 ** The first argument is a pointer to a structure obtained from
826 ** "ParseAlloc" which describes the current state of the parser.
827 ** The second argument is the major token number. The third is
828 ** the minor token. The fourth optional argument is whatever the
829 ** user wants (and specified in the grammar) and is available for
830 ** use by the action routines.
832 ** Inputs:
833 ** <ul>
834 ** <li> A pointer to the parser (an opaque structure.)
835 ** <li> The major token number.
836 ** <li> The minor token number.
837 ** <li> An option argument of a grammar-specified type.
838 ** </ul>
840 ** Outputs:
841 ** None.
843 void Parse(
844 void *yyp, /* The parser */
845 int yymajor, /* The major token code number */
846 ParseTOKENTYPE yyminor /* The value for the token */
847 ParseARG_PDECL /* Optional %extra_argument parameter */
849 YYMINORTYPE yyminorunion;
850 YYACTIONTYPE yyact; /* The parser action. */
851 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
852 int yyendofinput; /* True if we are at the end of input */
853 #endif
854 #ifdef YYERRORSYMBOL
855 int yyerrorhit = 0; /* True if yymajor has invoked an error */
856 #endif
857 yyParser *yypParser = (yyParser*)yyp; /* The parser */
858 ParseCTX_FETCH
859 ParseARG_STORE
861 assert( yypParser->yytos!=0 );
862 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
863 yyendofinput = (yymajor==0);
864 #endif
866 yyact = yypParser->yytos->stateno;
867 #ifndef NDEBUG
868 if( yyTraceFILE ){
869 if( yyact < YY_MIN_REDUCE ){
870 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
871 yyTracePrompt,yyTokenName[yymajor],yyact);
872 }else{
873 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
874 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
877 #endif
879 while(1){ /* Exit by "break" */
880 assert( yypParser->yytos>=yypParser->yystack );
881 assert( yyact==yypParser->yytos->stateno );
882 yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
883 if( yyact >= YY_MIN_REDUCE ){
884 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
885 #ifndef NDEBUG
886 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) );
887 if( yyTraceFILE ){
888 int yysize = yyRuleInfoNRhs[yyruleno];
889 if( yysize ){
890 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
891 yyTracePrompt,
892 yyruleno, yyRuleName[yyruleno],
893 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
894 yypParser->yytos[yysize].stateno);
895 }else{
896 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
897 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
898 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
901 #endif /* NDEBUG */
903 /* Check that the stack is large enough to grow by a single entry
904 ** if the RHS of the rule is empty. This ensures that there is room
905 ** enough on the stack to push the LHS value */
906 if( yyRuleInfoNRhs[yyruleno]==0 ){
907 #ifdef YYTRACKMAXSTACKDEPTH
908 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
909 yypParser->yyhwm++;
910 assert( yypParser->yyhwm ==
911 (int)(yypParser->yytos - yypParser->yystack));
913 #endif
914 #if YYSTACKDEPTH>0
915 if( yypParser->yytos>=yypParser->yystackEnd ){
916 yyStackOverflow(yypParser);
917 break;
919 #else
920 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
921 if( yyGrowStack(yypParser) ){
922 yyStackOverflow(yypParser);
923 break;
926 #endif
928 yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor ParseCTX_PARAM);
929 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
930 yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
931 #ifndef YYNOERRORRECOVERY
932 yypParser->yyerrcnt--;
933 #endif
934 break;
935 }else if( yyact==YY_ACCEPT_ACTION ){
936 yypParser->yytos--;
937 yy_accept(yypParser);
938 return;
939 }else{
940 assert( yyact == YY_ERROR_ACTION );
941 yyminorunion.yy0 = yyminor;
942 #ifdef YYERRORSYMBOL
943 int yymx;
944 #endif
945 #ifndef NDEBUG
946 if( yyTraceFILE ){
947 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
949 #endif
950 #ifdef YYERRORSYMBOL
951 /* A syntax error has occurred.
952 ** The response to an error depends upon whether or not the
953 ** grammar defines an error token "ERROR".
955 ** This is what we do if the grammar does define ERROR:
957 ** * Call the %syntax_error function.
959 ** * Begin popping the stack until we enter a state where
960 ** it is legal to shift the error symbol, then shift
961 ** the error symbol.
963 ** * Set the error count to three.
965 ** * Begin accepting and shifting new tokens. No new error
966 ** processing will occur until three tokens have been
967 ** shifted successfully.
970 if( yypParser->yyerrcnt<0 ){
971 yy_syntax_error(yypParser,yymajor,yyminor);
973 yymx = yypParser->yytos->major;
974 if( yymx==YYERRORSYMBOL || yyerrorhit ){
975 #ifndef NDEBUG
976 if( yyTraceFILE ){
977 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
978 yyTracePrompt,yyTokenName[yymajor]);
980 #endif
981 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
982 yymajor = YYNOCODE;
983 }else{
984 while( yypParser->yytos > yypParser->yystack ){
985 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
986 YYERRORSYMBOL);
987 if( yyact<=YY_MAX_SHIFTREDUCE ) break;
988 yy_pop_parser_stack(yypParser);
990 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
991 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
992 yy_parse_failed(yypParser);
993 #ifndef YYNOERRORRECOVERY
994 yypParser->yyerrcnt = -1;
995 #endif
996 yymajor = YYNOCODE;
997 }else if( yymx!=YYERRORSYMBOL ){
998 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
1001 yypParser->yyerrcnt = 3;
1002 yyerrorhit = 1;
1003 if( yymajor==YYNOCODE ) break;
1004 yyact = yypParser->yytos->stateno;
1005 #elif defined(YYNOERRORRECOVERY)
1006 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
1007 ** do any kind of error recovery. Instead, simply invoke the syntax
1008 ** error routine and continue going as if nothing had happened.
1010 ** Applications can set this macro (for example inside %include) if
1011 ** they intend to abandon the parse upon the first syntax error seen.
1013 yy_syntax_error(yypParser,yymajor, yyminor);
1014 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1015 break;
1016 #else /* YYERRORSYMBOL is not defined */
1017 /* This is what we do if the grammar does not define ERROR:
1019 ** * Report an error message, and throw away the input token.
1021 ** * If the input token is $, then fail the parse.
1023 ** As before, subsequent error messages are suppressed until
1024 ** three input tokens have been successfully shifted.
1026 if( yypParser->yyerrcnt<=0 ){
1027 yy_syntax_error(yypParser,yymajor, yyminor);
1029 yypParser->yyerrcnt = 3;
1030 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1031 if( yyendofinput ){
1032 yy_parse_failed(yypParser);
1033 #ifndef YYNOERRORRECOVERY
1034 yypParser->yyerrcnt = -1;
1035 #endif
1037 break;
1038 #endif
1041 #ifndef NDEBUG
1042 if( yyTraceFILE ){
1043 yyStackEntry *i;
1044 char cDiv = '[';
1045 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
1046 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
1047 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
1048 cDiv = ' ';
1050 fprintf(yyTraceFILE,"]\n");
1052 #endif
1053 return;
1057 ** Return the fallback token corresponding to canonical token iToken, or
1058 ** 0 if iToken has no fallback.
1060 int ParseFallback(int iToken){
1061 #ifdef YYFALLBACK
1062 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
1063 return yyFallback[iToken];
1064 #else
1065 (void)iToken;
1066 return 0;
1067 #endif