4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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
22 ** The following is the concatenation of all %include directives from the
23 ** input grammar file:
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
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
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
88 /************* Begin control #defines *****************************************/
90 /************* End control #defines *******************************************/
92 /* Define the yytestcase() macro to be a no-op if is not already defined
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
101 # define yytestcase(X)
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
110 ** Suppose the action integer is N. Then the action is determined as
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
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
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
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 */
206 yyStackEntry
*yytos
; /* Pointer to top element of the stack */
207 #ifdef YYTRACKMAXSTACKDEPTH
208 int yyhwm
; /* High-water mark of the stack */
210 #ifndef YYNOERRORRECOVERY
211 int yyerrcnt
; /* Shifts left before out of the error */
213 ParseARG_SDECL
/* A place to hold %extra_argument */
215 int yystksz
; /* Current side of the stack */
216 yyStackEntry
*yystack
; /* The parser's stack */
217 yyStackEntry yystk0
; /* First stack entry */
219 yyStackEntry yystack
[YYSTACKDEPTH
]; /* The parser's stack */
220 yyStackEntry
*yystackEnd
; /* Last entry in the stack */
223 typedef struct yyParser yyParser
;
227 static FILE *yyTraceFILE
= 0;
228 static char *yyTracePrompt
= 0;
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
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
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;
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) */
266 /* For tracing reduce actions, the names of all rules are required.
268 static const char *const yyRuleName
[] = {
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
){
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
;
290 pNew
= realloc(p
->yystack
, newSize
*sizeof(pNew
[0]));
294 p
->yytos
= &p
->yystack
[idx
];
297 fprintf(yyTraceFILE
,"%sStack grows from %d to %d entries.\n",
298 yyTracePrompt
, p
->yystksz
, newSize
);
301 p
->yystksz
= newSize
;
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
312 #ifndef YYMALLOCARGTYPE
313 # define YYMALLOCARGTYPE size_t
316 /* Initialize a new parser that has already been allocated.
318 void ParseInit(void *yypParser
){
319 yyParser
*pParser
= (yyParser
*)yypParser
;
320 #ifdef YYTRACKMAXSTACKDEPTH
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;
332 #ifndef YYNOERRORRECOVERY
333 pParser
->yyerrcnt
= -1;
335 pParser
->yytos
= pParser
->yystack
;
336 pParser
->yystack
[0].stateno
= 0;
337 pParser
->yystack
[0].major
= 0;
339 pParser
->yystackEnd
= &pParser
->yystack
[YYSTACKDEPTH
-1];
343 #ifndef Parse_ENGINEALWAYSONSTACK
345 ** This function allocates a new parser.
346 ** The only argument is a pointer to a function which works like
350 ** A pointer to the function used to allocate memory.
353 ** A pointer to a parser. This pointer is used in subsequent calls
354 ** to Parse and ParseFree.
356 void *ParseAlloc(void *(*mallocProc
)(YYMALLOCARGTYPE
)){
358 pParser
= (yyParser
*)(*mallocProc
)( (YYMALLOCARGTYPE
)sizeof(yyParser
) );
359 if( pParser
) ParseInit(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 */
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
){
404 assert( pParser
->yytos
!=0 );
405 assert( pParser
->yytos
> pParser
->yystack
);
406 yytos
= pParser
->yytos
--;
409 fprintf(yyTraceFILE
,"%sPopping %s\n",
411 yyTokenName
[yytos
->major
]);
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
);
424 if( pParser
->yystack
!=&pParser
->yystk0
) free(pParser
->yystack
);
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.
438 void *p
, /* The parser to be deleted */
439 void (*freeProc
)(void*) /* Function used to reclaim memory */
441 #ifndef YYPARSEFREENEVERNULL
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
;
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
];
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
;
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
++;
486 fprintf(out
,"State %d lookahead %s %s\n", stateno
,
487 yyTokenName
[iLookAhead
],
488 yycoverage
[stateno
][iLookAhead
] ? "ok" : "missed");
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 */
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;
513 i
= yy_shift_ofst
[stateno
];
515 assert( i
+YYNTOKEN
<=(int)sizeof(yy_lookahead
)/sizeof(yy_lookahead
[0]) );
516 assert( iLookAhead
!=YYNOCODE
);
517 assert( iLookAhead
< YYNTOKEN
);
519 if( yy_lookahead
[i
]!=iLookAhead
){
521 YYCODETYPE iFallback
; /* Fallback token */
522 if( iLookAhead
<sizeof(yyFallback
)/sizeof(yyFallback
[0])
523 && (iFallback
= yyFallback
[iLookAhead
])!=0 ){
526 fprintf(yyTraceFILE
, "%sFALLBACK %s => %s\n",
527 yyTracePrompt
, yyTokenName
[iLookAhead
], yyTokenName
[iFallback
]);
530 assert( yyFallback
[iFallback
]==0 ); /* Fallback loop must terminate */
531 iLookAhead
= iFallback
;
537 int j
= i
- iLookAhead
+ YYWILDCARD
;
539 #if YY_SHIFT_MIN+YYWILDCARD<0
542 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
545 yy_lookahead
[j
]==YYWILDCARD
&& iLookAhead
>0
549 fprintf(yyTraceFILE
, "%sWILDCARD %s => %s\n",
550 yyTracePrompt
, yyTokenName
[iLookAhead
],
551 yyTokenName
[YYWILDCARD
]);
557 #endif /* YYWILDCARD */
558 return yy_default
[stateno
];
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 */
575 if( stateno
>YY_REDUCE_COUNT
){
576 return yy_default
[stateno
];
579 assert( stateno
<=YY_REDUCE_COUNT
);
581 i
= yy_reduce_ofst
[stateno
];
582 assert( iLookAhead
!=YYNOCODE
);
585 if( i
<0 || i
>=YY_ACTTAB_COUNT
|| yy_lookahead
[i
]!=iLookAhead
){
586 return yy_default
[stateno
];
589 assert( i
>=0 && i
<YY_ACTTAB_COUNT
);
590 assert( yy_lookahead
[i
]==iLookAhead
);
596 ** The following routine is called if the stack overflows.
598 static void yyStackOverflow(yyParser
*yypParser
){
602 fprintf(yyTraceFILE
,"%sStack Overflow!\n",yyTracePrompt
);
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
618 static void yyTraceShift(yyParser
*yypParser
, int yyNewState
, const char *zTag
){
620 if( yyNewState
<YYNSTATE
){
621 fprintf(yyTraceFILE
,"%s%s '%s', go to state %d\n",
622 yyTracePrompt
, zTag
, yyTokenName
[yypParser
->yytos
->major
],
625 fprintf(yyTraceFILE
,"%s%s '%s', pending reduce %d\n",
626 yyTracePrompt
, zTag
, yyTokenName
[yypParser
->yytos
->major
],
627 yyNewState
- YY_MIN_REDUCE
);
632 # define yyTraceShift(X,Y,Z)
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 */
646 #ifdef YYTRACKMAXSTACKDEPTH
647 if( (int)(yypParser
->yytos
- yypParser
->yystack
)>yypParser
->yyhwm
){
649 assert( yypParser
->yyhwm
== (int)(yypParser
->yytos
- yypParser
->yystack
) );
653 if( yypParser
->yytos
>yypParser
->yystackEnd
){
655 yyStackOverflow(yypParser
);
659 if( yypParser
->yytos
>=&yypParser
->yystack
[yypParser
->yystksz
] ){
660 if( yyGrowStack(yypParser
) ){
662 yyStackOverflow(yypParser
);
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 */
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 */
711 (void)yyLookaheadToken
;
712 yymsp
= yypParser
->yytos
;
714 if( yyTraceFILE
&& yyruleno
<(int)(sizeof(yyRuleName
)/sizeof(yyRuleName
[0])) ){
715 yysize
= yyRuleInfo
[yyruleno
].nrhs
;
717 fprintf(yyTraceFILE
, "%sReduce %d [%s], go to state %d.\n",
719 yyruleno
, yyRuleName
[yyruleno
], yymsp
[yysize
].stateno
);
721 fprintf(yyTraceFILE
, "%sReduce %d [%s].\n",
722 yyTracePrompt
, yyruleno
, yyRuleName
[yyruleno
]);
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
){
734 assert( yypParser
->yyhwm
== (int)(yypParser
->yytos
- yypParser
->yystack
));
738 if( yypParser
->yytos
>=yypParser
->yystackEnd
){
739 yyStackOverflow(yypParser
);
743 if( yypParser
->yytos
>=&yypParser
->yystack
[yypParser
->yystksz
-1] ){
744 if( yyGrowStack(yypParser
) ){
745 yyStackOverflow(yypParser
);
748 yymsp
= yypParser
->yytos
;
754 /* Beginning here are the reduction cases. A typical example
757 ** #line <lineno> <grammarfile>
758 ** { ... } // User supplied code
759 ** #line <lineno> <thisfile>
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
);
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 */
795 fprintf(yyTraceFILE
,"%sFail!\n",yyTracePrompt
);
798 while( yypParser
->yytos
>yypParser
->yystack
) yy_pop_parser_stack(yypParser
);
799 /* Here code is inserted which will be executed whenever the
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 */
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 */
833 fprintf(yyTraceFILE
,"%sAccept!\n",yyTracePrompt
);
836 #ifndef YYNOERRORRECOVERY
837 yypParser
->yyerrcnt
= -1;
839 assert( yypParser
->yytos
==yypParser
->yystack
);
840 /* Here code is inserted which will be executed whenever the
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.
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.
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 */
879 int yyerrorhit
= 0; /* True if yymajor has invoked an error */
881 yyParser
*yypParser
; /* The parser */
883 yypParser
= (yyParser
*)yyp
;
884 assert( yypParser
->yytos
!=0 );
885 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
886 yyendofinput
= (yymajor
==0);
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
);
897 fprintf(yyTraceFILE
,"%sInput '%s' with pending reduce %d\n",
898 yyTracePrompt
,yyTokenName
[yymajor
],stateno
-YY_MIN_REDUCE
);
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
--;
913 }else if( yyact
==YY_ACCEPT_ACTION
){
915 yy_accept(yypParser
);
918 assert( yyact
== YY_ERROR_ACTION
);
919 yyminorunion
.yy0
= yyminor
;
925 fprintf(yyTraceFILE
,"%sSyntax Error!\n",yyTracePrompt
);
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
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
){
955 fprintf(yyTraceFILE
,"%sDiscard input token %s\n",
956 yyTracePrompt
,yyTokenName
[yymajor
]);
959 yy_destructor(yypParser
, (YYCODETYPE
)yymajor
, &yyminorunion
);
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;
977 }else if( yymx
!=YYERRORSYMBOL
){
978 yy_shift(yypParser
,yyact
,YYERRORSYMBOL
,yyminor
);
981 yypParser
->yyerrcnt
= 3;
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
);
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
);
1011 yy_parse_failed(yypParser
);
1012 #ifndef YYNOERRORRECOVERY
1013 yypParser
->yyerrcnt
= -1;
1019 }while( yymajor
!=YYNOCODE
&& yypParser
->yytos
>yypParser
->yystack
);
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
]);
1029 fprintf(yyTraceFILE
,"]\n");