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:
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
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
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
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
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 */
214 ParseCTX_SDECL
/* A place to hold %extra_context */
216 int yystksz
; /* Current side of the stack */
217 yyStackEntry
*yystack
; /* The parser's stack */
218 yyStackEntry yystk0
; /* First stack entry */
220 yyStackEntry yystack
[YYSTACKDEPTH
]; /* The parser's stack */
221 yyStackEntry
*yystackEnd
; /* Last entry in the stack */
224 typedef struct yyParser yyParser
;
229 static FILE *yyTraceFILE
= 0;
230 static char *yyTracePrompt
= 0;
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
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
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;
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) */
268 /* For tracing reduce actions, the names of all rules are required.
270 static const char *const yyRuleName
[] = {
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
){
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
;
292 pNew
= realloc(p
->yystack
, newSize
*sizeof(pNew
[0]));
296 p
->yytos
= &p
->yystack
[idx
];
299 fprintf(yyTraceFILE
,"%sStack grows from %d to %d entries.\n",
300 yyTracePrompt
, p
->yystksz
, newSize
);
303 p
->yystksz
= newSize
;
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
314 #ifndef YYMALLOCARGTYPE
315 # define YYMALLOCARGTYPE size_t
318 /* Initialize a new parser that has already been allocated.
320 void ParseInit(void *yypRawParser ParseCTX_PDECL
){
321 yyParser
*yypParser
= (yyParser
*)yypRawParser
;
323 #ifdef YYTRACKMAXSTACKDEPTH
324 yypParser
->yyhwm
= 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;
335 #ifndef YYNOERRORRECOVERY
336 yypParser
->yyerrcnt
= -1;
338 yypParser
->yytos
= yypParser
->yystack
;
339 yypParser
->yystack
[0].stateno
= 0;
340 yypParser
->yystack
[0].major
= 0;
342 yypParser
->yystackEnd
= &yypParser
->yystack
[YYSTACKDEPTH
-1];
346 #ifndef Parse_ENGINEALWAYSONSTACK
348 ** This function allocates a new parser.
349 ** The only argument is a pointer to a function which works like
353 ** A pointer to the function used to allocate memory.
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
){
361 yypParser
= (yyParser
*)(*mallocProc
)( (YYMALLOCARGTYPE
)sizeof(yyParser
) );
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 */
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
){
411 assert( pParser
->yytos
!=0 );
412 assert( pParser
->yytos
> pParser
->yystack
);
413 yytos
= pParser
->yytos
--;
416 fprintf(yyTraceFILE
,"%sPopping %s\n",
418 yyTokenName
[yytos
->major
]);
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
);
431 if( pParser
->yystack
!=&pParser
->yystk0
) free(pParser
->yystack
);
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.
445 void *p
, /* The parser to be deleted */
446 void (*freeProc
)(void*) /* Function used to reclaim memory */
448 #ifndef YYPARSEFREENEVERNULL
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
;
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
];
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
;
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
++;
493 fprintf(out
,"State %d lookahead %s %s\n", stateno
,
494 yyTokenName
[iLookAhead
],
495 yycoverage
[stateno
][iLookAhead
] ? "ok" : "missed");
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 */
513 if( stateno
>YY_MAX_SHIFT
) return stateno
;
514 assert( stateno
<= YY_SHIFT_COUNT
);
515 #if defined(YYCOVERAGE)
516 yycoverage
[stateno
][iLookAhead
] = 1;
519 i
= yy_shift_ofst
[stateno
];
521 assert( i
<=YY_ACTTAB_COUNT
);
522 assert( i
+YYNTOKEN
<=(int)YY_NLOOKAHEAD
);
523 assert( iLookAhead
!=YYNOCODE
);
524 assert( iLookAhead
< YYNTOKEN
);
526 assert( i
<(int)YY_NLOOKAHEAD
);
527 if( yy_lookahead
[i
]!=iLookAhead
){
529 YYCODETYPE iFallback
; /* Fallback token */
530 assert( iLookAhead
<sizeof(yyFallback
)/sizeof(yyFallback
[0]) );
531 iFallback
= yyFallback
[iLookAhead
];
535 fprintf(yyTraceFILE
, "%sFALLBACK %s => %s\n",
536 yyTracePrompt
, yyTokenName
[iLookAhead
], yyTokenName
[iFallback
]);
539 assert( yyFallback
[iFallback
]==0 ); /* Fallback loop must terminate */
540 iLookAhead
= iFallback
;
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 ){
551 fprintf(yyTraceFILE
, "%sWILDCARD %s => %s\n",
552 yyTracePrompt
, yyTokenName
[iLookAhead
],
553 yyTokenName
[YYWILDCARD
]);
559 #endif /* YYWILDCARD */
560 return yy_default
[stateno
];
562 assert( i
>=0 && i
<(int)(sizeof(yy_action
)/sizeof(yy_action
[0])) );
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 */
578 if( stateno
>YY_REDUCE_COUNT
){
579 return yy_default
[stateno
];
582 assert( stateno
<=YY_REDUCE_COUNT
);
584 i
= yy_reduce_ofst
[stateno
];
585 assert( iLookAhead
!=YYNOCODE
);
588 if( i
<0 || i
>=YY_ACTTAB_COUNT
|| yy_lookahead
[i
]!=iLookAhead
){
589 return yy_default
[stateno
];
592 assert( i
>=0 && i
<YY_ACTTAB_COUNT
);
593 assert( yy_lookahead
[i
]==iLookAhead
);
599 ** The following routine is called if the stack overflows.
601 static void yyStackOverflow(yyParser
*yypParser
){
606 fprintf(yyTraceFILE
,"%sStack Overflow!\n",yyTracePrompt
);
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 */
620 ** Print tracing information for a SHIFT action
623 static void yyTraceShift(yyParser
*yypParser
, int yyNewState
, const char *zTag
){
625 if( yyNewState
<YYNSTATE
){
626 fprintf(yyTraceFILE
,"%s%s '%s', go to state %d\n",
627 yyTracePrompt
, zTag
, yyTokenName
[yypParser
->yytos
->major
],
630 fprintf(yyTraceFILE
,"%s%s '%s', pending reduce %d\n",
631 yyTracePrompt
, zTag
, yyTokenName
[yypParser
->yytos
->major
],
632 yyNewState
- YY_MIN_REDUCE
);
637 # define yyTraceShift(X,Y,Z)
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 */
651 #ifdef YYTRACKMAXSTACKDEPTH
652 if( (int)(yypParser
->yytos
- yypParser
->yystack
)>yypParser
->yyhwm
){
654 assert( yypParser
->yyhwm
== (int)(yypParser
->yytos
- yypParser
->yystack
) );
658 if( yypParser
->yytos
>yypParser
->yystackEnd
){
660 yyStackOverflow(yypParser
);
664 if( yypParser
->yytos
>=&yypParser
->yystack
[yypParser
->yystksz
] ){
665 if( yyGrowStack(yypParser
) ){
667 yyStackOverflow(yypParser
);
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
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 */
719 (void)yyLookaheadToken
;
720 yymsp
= yypParser
->yytos
;
723 /* Beginning here are the reduction cases. A typical example
726 ** #line <lineno> <grammarfile>
727 ** { ... } // User supplied code
728 ** #line <lineno> <thisfile>
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
);
748 yypParser
->yytos
= yymsp
;
749 yymsp
->stateno
= (YYACTIONTYPE
)yyact
;
750 yymsp
->major
= (YYCODETYPE
)yygoto
;
751 yyTraceShift(yypParser
, yyact
, "... then shift");
756 ** The following code executes when the parse fails
758 #ifndef YYNOERRORRECOVERY
759 static void yy_parse_failed(
760 yyParser
*yypParser
/* The parser */
766 fprintf(yyTraceFILE
,"%sFail!\n",yyTracePrompt
);
769 while( yypParser
->yytos
>yypParser
->yystack
) yy_pop_parser_stack(yypParser
);
770 /* Here code is inserted which will be executed whenever the
772 /************ Begin %parse_failure code ***************************************/
774 /************ End %parse_failure code *****************************************/
775 ParseARG_STORE
/* Suppress warning about unused %extra_argument variable */
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 */
790 #define TOKEN yyminor
791 /************ Begin %syntax_error code ****************************************/
793 /************ End %syntax_error code ******************************************/
794 ParseARG_STORE
/* Suppress warning about unused %extra_argument variable */
799 ** The following is executed when the parser accepts
801 static void yy_accept(
802 yyParser
*yypParser
/* The parser */
808 fprintf(yyTraceFILE
,"%sAccept!\n",yyTracePrompt
);
811 #ifndef YYNOERRORRECOVERY
812 yypParser
->yyerrcnt
= -1;
814 assert( yypParser
->yytos
==yypParser
->yystack
);
815 /* Here code is inserted which will be executed whenever the
817 /*********** Begin %parse_accept code *****************************************/
819 /*********** End %parse_accept code *******************************************/
820 ParseARG_STORE
/* Suppress warning about unused %extra_argument variable */
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.
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.
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 */
855 int yyerrorhit
= 0; /* True if yymajor has invoked an error */
857 yyParser
*yypParser
= (yyParser
*)yyp
; /* The parser */
861 assert( yypParser
->yytos
!=0 );
862 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
863 yyendofinput
= (yymajor
==0);
866 yyact
= yypParser
->yytos
->stateno
;
869 if( yyact
< YY_MIN_REDUCE
){
870 fprintf(yyTraceFILE
,"%sInput '%s' in state %d\n",
871 yyTracePrompt
,yyTokenName
[yymajor
],yyact
);
873 fprintf(yyTraceFILE
,"%sInput '%s' with pending reduce %d\n",
874 yyTracePrompt
,yyTokenName
[yymajor
],yyact
-YY_MIN_REDUCE
);
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 */
886 assert( yyruleno
<(int)(sizeof(yyRuleName
)/sizeof(yyRuleName
[0])) );
888 int yysize
= yyRuleInfoNRhs
[yyruleno
];
890 fprintf(yyTraceFILE
, "%sReduce %d [%s]%s, pop back to state %d.\n",
892 yyruleno
, yyRuleName
[yyruleno
],
893 yyruleno
<YYNRULE_WITH_ACTION
? "" : " without external action",
894 yypParser
->yytos
[yysize
].stateno
);
896 fprintf(yyTraceFILE
, "%sReduce %d [%s]%s.\n",
897 yyTracePrompt
, yyruleno
, yyRuleName
[yyruleno
],
898 yyruleno
<YYNRULE_WITH_ACTION
? "" : " without external action");
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
){
910 assert( yypParser
->yyhwm
==
911 (int)(yypParser
->yytos
- yypParser
->yystack
));
915 if( yypParser
->yytos
>=yypParser
->yystackEnd
){
916 yyStackOverflow(yypParser
);
920 if( yypParser
->yytos
>=&yypParser
->yystack
[yypParser
->yystksz
-1] ){
921 if( yyGrowStack(yypParser
) ){
922 yyStackOverflow(yypParser
);
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
--;
935 }else if( yyact
==YY_ACCEPT_ACTION
){
937 yy_accept(yypParser
);
940 assert( yyact
== YY_ERROR_ACTION
);
941 yyminorunion
.yy0
= yyminor
;
947 fprintf(yyTraceFILE
,"%sSyntax Error!\n",yyTracePrompt
);
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
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
){
977 fprintf(yyTraceFILE
,"%sDiscard input token %s\n",
978 yyTracePrompt
,yyTokenName
[yymajor
]);
981 yy_destructor(yypParser
, (YYCODETYPE
)yymajor
, &yyminorunion
);
984 while( yypParser
->yytos
> yypParser
->yystack
){
985 yyact
= yy_find_reduce_action(yypParser
->yytos
->stateno
,
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;
997 }else if( yymx
!=YYERRORSYMBOL
){
998 yy_shift(yypParser
,yyact
,YYERRORSYMBOL
,yyminor
);
1001 yypParser
->yyerrcnt
= 3;
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
);
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
);
1032 yy_parse_failed(yypParser
);
1033 #ifndef YYNOERRORRECOVERY
1034 yypParser
->yyerrcnt
= -1;
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
]);
1050 fprintf(yyTraceFILE
,"]\n");
1057 ** Return the fallback token corresponding to canonical token iToken, or
1058 ** 0 if iToken has no fallback.
1060 int ParseFallback(int iToken
){
1062 assert( iToken
<(int)(sizeof(yyFallback
)/sizeof(yyFallback
[0])) );
1063 return yyFallback
[iToken
];