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 ** 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_MIN_REDUCE Minimum value for reduce actions
79 ** YY_MAX_REDUCE Maximum value for reduce actions
80 ** YY_ERROR_ACTION The yy_action[] code for syntax error
81 ** YY_ACCEPT_ACTION The yy_action[] code for accept
82 ** YY_NO_ACTION The yy_action[] code for no-op
87 /************* Begin control #defines *****************************************/
89 /************* End control #defines *******************************************/
91 /* Define the yytestcase() macro to be a no-op if is not already defined
94 ** Applications can choose to define yytestcase() in the %include section
95 ** to a macro that can assist in verifying code coverage. For production
96 ** code the yytestcase() macro should be turned off. But it is useful
100 # define yytestcase(X)
104 /* Next are the tables used to determine what action to take based on the
105 ** current state and lookahead token. These tables are used to implement
106 ** functions that take a state number and lookahead value and return an
109 ** Suppose the action integer is N. Then the action is determined as
112 ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
113 ** token onto the stack and goto state N.
115 ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
116 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
118 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
121 ** N == YY_ERROR_ACTION A syntax error has occurred.
123 ** N == YY_ACCEPT_ACTION The parser accepts its input.
125 ** N == YY_NO_ACTION No such action. Denotes unused
126 ** slots in the yy_action[] table.
128 ** The action table is constructed as a single large table named yy_action[].
129 ** Given state S and lookahead X, the action is computed as either:
131 ** (A) N = yy_action[ yy_shift_ofst[S] + X ]
132 ** (B) N = yy_default[S]
134 ** The (A) formula is preferred. The B formula is used instead if:
135 ** (1) The yy_shift_ofst[S]+X value is out of range, or
136 ** (2) yy_lookahead[yy_shift_ofst[S]+X] is not equal to X, or
137 ** (3) yy_shift_ofst[S] equal YY_SHIFT_USE_DFLT.
138 ** (Implementation note: YY_SHIFT_USE_DFLT is chosen so that
139 ** YY_SHIFT_USE_DFLT+X will be out of range for all possible lookaheads X.
140 ** Hence only tests (1) and (2) need to be evaluated.)
142 ** The formulas above are for computing the action when the lookahead is
143 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
144 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
145 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
146 ** YY_SHIFT_USE_DFLT.
148 ** The following are the tables generated in this section:
150 ** yy_action[] A single table containing all actions.
151 ** yy_lookahead[] A table containing the lookahead for each entry in
152 ** yy_action. Used to detect hash collisions.
153 ** yy_shift_ofst[] For each state, the offset into yy_action for
154 ** shifting terminals.
155 ** yy_reduce_ofst[] For each state, the offset into yy_action for
156 ** shifting non-terminals after a reduce.
157 ** yy_default[] Default action for each state.
159 *********** Begin parsing tables **********************************************/
161 /********** End of lemon-generated parsing tables *****************************/
163 /* The next table maps tokens (terminal symbols) into fallback tokens.
164 ** If a construct like the following:
166 ** %fallback ID X Y Z.
168 ** appears in the grammar, then ID becomes a fallback token for X, Y,
169 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
170 ** but it does not parse, the type of the token is changed to ID and
171 ** the parse is retried before an error is thrown.
173 ** This feature can be used, for example, to cause some keywords in a language
174 ** to revert to identifiers if they keyword does not apply in the context where
178 static const YYCODETYPE yyFallback
[] = {
181 #endif /* YYFALLBACK */
183 /* The following structure represents a single element of the
184 ** parser's stack. Information stored includes:
186 ** + The state number for the parser at this level of the stack.
188 ** + The value of the token stored at this level of the stack.
189 ** (In other words, the "major" token.)
191 ** + The semantic value stored at this level of the stack. This is
192 ** the information used by the action routines in the grammar.
193 ** It is sometimes called the "minor" token.
195 ** After the "shift" half of a SHIFTREDUCE action, the stateno field
196 ** actually contains the reduce action for the second half of the
199 struct yyStackEntry
{
200 YYACTIONTYPE stateno
; /* The state-number, or reduce action in SHIFTREDUCE */
201 YYCODETYPE major
; /* The major token value. This is the code
202 ** number for the token at this stack level */
203 YYMINORTYPE minor
; /* The user-supplied minor token value. This
204 ** is the value of the token */
206 typedef struct yyStackEntry yyStackEntry
;
208 /* The state of the parser is completely contained in an instance of
209 ** the following structure */
211 yyStackEntry
*yytos
; /* Pointer to top element of the stack */
212 #ifdef YYTRACKMAXSTACKDEPTH
213 int yyhwm
; /* High-water mark of the stack */
215 #ifndef YYNOERRORRECOVERY
216 int yyerrcnt
; /* Shifts left before out of the error */
218 ParseARG_SDECL
/* A place to hold %extra_argument */
220 int yystksz
; /* Current side of the stack */
221 yyStackEntry
*yystack
; /* The parser's stack */
222 yyStackEntry yystk0
; /* First stack entry */
224 yyStackEntry yystack
[YYSTACKDEPTH
]; /* The parser's stack */
225 yyStackEntry
*yystackEnd
; /* Last entry in the stack */
228 typedef struct yyParser yyParser
;
232 static FILE *yyTraceFILE
= 0;
233 static char *yyTracePrompt
= 0;
238 ** Turn parser tracing on by giving a stream to which to write the trace
239 ** and a prompt to preface each trace message. Tracing is turned off
240 ** by making either argument NULL
244 ** <li> A FILE* to which trace output should be written.
245 ** If NULL, then tracing is turned off.
246 ** <li> A prefix string written at the beginning of every
247 ** line of trace output. If NULL, then tracing is
254 void ParseTrace(FILE *TraceFILE
, char *zTracePrompt
){
255 yyTraceFILE
= TraceFILE
;
256 yyTracePrompt
= zTracePrompt
;
257 if( yyTraceFILE
==0 ) yyTracePrompt
= 0;
258 else if( yyTracePrompt
==0 ) yyTraceFILE
= 0;
263 /* For tracing shifts, the names of all terminals and nonterminals
264 ** are required. The following table supplies these names */
265 static const char *const yyTokenName
[] = {
271 /* For tracing reduce actions, the names of all rules are required.
273 static const char *const yyRuleName
[] = {
281 ** Try to increase the size of the parser stack. Return the number
282 ** of errors. Return 0 on success.
284 static int yyGrowStack(yyParser
*p
){
289 newSize
= p
->yystksz
*2 + 100;
290 idx
= p
->yytos
? (int)(p
->yytos
- p
->yystack
) : 0;
291 if( p
->yystack
==&p
->yystk0
){
292 pNew
= malloc(newSize
*sizeof(pNew
[0]));
293 if( pNew
) pNew
[0] = p
->yystk0
;
295 pNew
= realloc(p
->yystack
, newSize
*sizeof(pNew
[0]));
299 p
->yytos
= &p
->yystack
[idx
];
302 fprintf(yyTraceFILE
,"%sStack grows from %d to %d entries.\n",
303 yyTracePrompt
, p
->yystksz
, newSize
);
306 p
->yystksz
= newSize
;
312 /* Datatype of the argument to the memory allocated passed as the
313 ** second argument to ParseAlloc() below. This can be changed by
314 ** putting an appropriate #define in the %include section of the input
317 #ifndef YYMALLOCARGTYPE
318 # define YYMALLOCARGTYPE size_t
321 /* Initialize a new parser that has already been allocated.
323 void ParseInit(void *yypParser
){
324 yyParser
*pParser
= (yyParser
*)yypParser
;
325 #ifdef YYTRACKMAXSTACKDEPTH
329 pParser
->yytos
= NULL
;
330 pParser
->yystack
= NULL
;
331 pParser
->yystksz
= 0;
332 if( yyGrowStack(pParser
) ){
333 pParser
->yystack
= &pParser
->yystk0
;
334 pParser
->yystksz
= 1;
337 #ifndef YYNOERRORRECOVERY
338 pParser
->yyerrcnt
= -1;
340 pParser
->yytos
= pParser
->yystack
;
341 pParser
->yystack
[0].stateno
= 0;
342 pParser
->yystack
[0].major
= 0;
344 pParser
->yystackEnd
= &pParser
->yystack
[YYSTACKDEPTH
-1];
348 #ifndef Parse_ENGINEALWAYSONSTACK
350 ** This function allocates a new parser.
351 ** The only argument is a pointer to a function which works like
355 ** A pointer to the function used to allocate memory.
358 ** A pointer to a parser. This pointer is used in subsequent calls
359 ** to Parse and ParseFree.
361 void *ParseAlloc(void *(*mallocProc
)(YYMALLOCARGTYPE
)){
363 pParser
= (yyParser
*)(*mallocProc
)( (YYMALLOCARGTYPE
)sizeof(yyParser
) );
364 if( pParser
) ParseInit(pParser
);
367 #endif /* Parse_ENGINEALWAYSONSTACK */
370 /* The following function deletes the "minor type" or semantic value
371 ** associated with a symbol. The symbol can be either a terminal
372 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
373 ** a pointer to the value to be deleted. The code used to do the
374 ** deletions is derived from the %destructor and/or %token_destructor
375 ** directives of the input grammar.
377 static void yy_destructor(
378 yyParser
*yypParser
, /* The parser */
379 YYCODETYPE yymajor
, /* Type code for object to destroy */
380 YYMINORTYPE
*yypminor
/* The object to be destroyed */
384 /* Here is inserted the actions which take place when a
385 ** terminal or non-terminal is destroyed. This can happen
386 ** when the symbol is popped from the stack during a
387 ** reduce or during error processing or when a parser is
388 ** being destroyed before it is finished parsing.
390 ** Note: during a reduce, the only symbols destroyed are those
391 ** which appear on the RHS of the rule, but which are *not* used
392 ** inside the C code.
394 /********* Begin destructor definitions ***************************************/
396 /********* End destructor definitions *****************************************/
397 default: break; /* If no destructor action specified: do nothing */
402 ** Pop the parser's stack once.
404 ** If there is a destructor routine associated with the token which
405 ** is popped from the stack, then call it.
407 static void yy_pop_parser_stack(yyParser
*pParser
){
409 assert( pParser
->yytos
!=0 );
410 assert( pParser
->yytos
> pParser
->yystack
);
411 yytos
= pParser
->yytos
--;
414 fprintf(yyTraceFILE
,"%sPopping %s\n",
416 yyTokenName
[yytos
->major
]);
419 yy_destructor(pParser
, yytos
->major
, &yytos
->minor
);
423 ** Clear all secondary memory allocations from the parser
425 void ParseFinalize(void *p
){
426 yyParser
*pParser
= (yyParser
*)p
;
427 while( pParser
->yytos
>pParser
->yystack
) yy_pop_parser_stack(pParser
);
429 if( pParser
->yystack
!=&pParser
->yystk0
) free(pParser
->yystack
);
433 #ifndef Parse_ENGINEALWAYSONSTACK
435 ** Deallocate and destroy a parser. Destructors are called for
436 ** all stack elements before shutting the parser down.
438 ** If the YYPARSEFREENEVERNULL macro exists (for example because it
439 ** is defined in a %include section of the input grammar) then it is
440 ** assumed that the input pointer is never NULL.
443 void *p
, /* The parser to be deleted */
444 void (*freeProc
)(void*) /* Function used to reclaim memory */
446 #ifndef YYPARSEFREENEVERNULL
452 #endif /* Parse_ENGINEALWAYSONSTACK */
455 ** Return the peak depth of the stack for a parser.
457 #ifdef YYTRACKMAXSTACKDEPTH
458 int ParseStackPeak(void *p
){
459 yyParser
*pParser
= (yyParser
*)p
;
460 return pParser
->yyhwm
;
465 ** Find the appropriate action for a parser given the terminal
466 ** look-ahead token iLookAhead.
468 static unsigned int yy_find_shift_action(
469 yyParser
*pParser
, /* The parser */
470 YYCODETYPE iLookAhead
/* The look-ahead token */
473 int stateno
= pParser
->yytos
->stateno
;
475 if( stateno
>=YY_MIN_REDUCE
) return stateno
;
476 assert( stateno
<= YY_SHIFT_COUNT
);
478 i
= yy_shift_ofst
[stateno
];
479 assert( iLookAhead
!=YYNOCODE
);
481 if( i
<0 || i
>=YY_ACTTAB_COUNT
|| yy_lookahead
[i
]!=iLookAhead
){
483 YYCODETYPE iFallback
; /* Fallback token */
484 if( iLookAhead
<sizeof(yyFallback
)/sizeof(yyFallback
[0])
485 && (iFallback
= yyFallback
[iLookAhead
])!=0 ){
488 fprintf(yyTraceFILE
, "%sFALLBACK %s => %s\n",
489 yyTracePrompt
, yyTokenName
[iLookAhead
], yyTokenName
[iFallback
]);
492 assert( yyFallback
[iFallback
]==0 ); /* Fallback loop must terminate */
493 iLookAhead
= iFallback
;
499 int j
= i
- iLookAhead
+ YYWILDCARD
;
501 #if YY_SHIFT_MIN+YYWILDCARD<0
504 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
507 yy_lookahead
[j
]==YYWILDCARD
&& iLookAhead
>0
511 fprintf(yyTraceFILE
, "%sWILDCARD %s => %s\n",
512 yyTracePrompt
, yyTokenName
[iLookAhead
],
513 yyTokenName
[YYWILDCARD
]);
519 #endif /* YYWILDCARD */
520 return yy_default
[stateno
];
528 ** Find the appropriate action for a parser given the non-terminal
529 ** look-ahead token iLookAhead.
531 static int yy_find_reduce_action(
532 int stateno
, /* Current state number */
533 YYCODETYPE iLookAhead
/* The look-ahead token */
537 if( stateno
>YY_REDUCE_COUNT
){
538 return yy_default
[stateno
];
541 assert( stateno
<=YY_REDUCE_COUNT
);
543 i
= yy_reduce_ofst
[stateno
];
544 assert( i
!=YY_REDUCE_USE_DFLT
);
545 assert( iLookAhead
!=YYNOCODE
);
548 if( i
<0 || i
>=YY_ACTTAB_COUNT
|| yy_lookahead
[i
]!=iLookAhead
){
549 return yy_default
[stateno
];
552 assert( i
>=0 && i
<YY_ACTTAB_COUNT
);
553 assert( yy_lookahead
[i
]==iLookAhead
);
559 ** The following routine is called if the stack overflows.
561 static void yyStackOverflow(yyParser
*yypParser
){
565 fprintf(yyTraceFILE
,"%sStack Overflow!\n",yyTracePrompt
);
568 while( yypParser
->yytos
>yypParser
->yystack
) yy_pop_parser_stack(yypParser
);
569 /* Here code is inserted which will execute if the parser
570 ** stack every overflows */
571 /******** Begin %stack_overflow code ******************************************/
573 /******** End %stack_overflow code ********************************************/
574 ParseARG_STORE
; /* Suppress warning about unused %extra_argument var */
578 ** Print tracing information for a SHIFT action
581 static void yyTraceShift(yyParser
*yypParser
, int yyNewState
){
583 if( yyNewState
<YYNSTATE
){
584 fprintf(yyTraceFILE
,"%sShift '%s', go to state %d\n",
585 yyTracePrompt
,yyTokenName
[yypParser
->yytos
->major
],
588 fprintf(yyTraceFILE
,"%sShift '%s'\n",
589 yyTracePrompt
,yyTokenName
[yypParser
->yytos
->major
]);
594 # define yyTraceShift(X,Y)
598 ** Perform a shift action.
600 static void yy_shift(
601 yyParser
*yypParser
, /* The parser to be shifted */
602 int yyNewState
, /* The new state to shift in */
603 int yyMajor
, /* The major token to shift in */
604 ParseTOKENTYPE yyMinor
/* The minor token to shift in */
608 #ifdef YYTRACKMAXSTACKDEPTH
609 if( (int)(yypParser
->yytos
- yypParser
->yystack
)>yypParser
->yyhwm
){
611 assert( yypParser
->yyhwm
== (int)(yypParser
->yytos
- yypParser
->yystack
) );
615 if( yypParser
->yytos
>yypParser
->yystackEnd
){
617 yyStackOverflow(yypParser
);
621 if( yypParser
->yytos
>=&yypParser
->yystack
[yypParser
->yystksz
] ){
622 if( yyGrowStack(yypParser
) ){
624 yyStackOverflow(yypParser
);
629 if( yyNewState
> YY_MAX_SHIFT
){
630 yyNewState
+= YY_MIN_REDUCE
- YY_MIN_SHIFTREDUCE
;
632 yytos
= yypParser
->yytos
;
633 yytos
->stateno
= (YYACTIONTYPE
)yyNewState
;
634 yytos
->major
= (YYCODETYPE
)yyMajor
;
635 yytos
->minor
.yy0
= yyMinor
;
636 yyTraceShift(yypParser
, yyNewState
);
639 /* The following table contains information about every rule that
640 ** is used during the reduce.
642 static const struct {
643 YYCODETYPE lhs
; /* Symbol on the left-hand side of the rule */
644 signed char nrhs
; /* Negative of the number of RHS symbols in the rule */
649 static void yy_accept(yyParser
*); /* Forward Declaration */
652 ** Perform a reduce action and the shift that must immediately
653 ** follow the reduce.
655 static void yy_reduce(
656 yyParser
*yypParser
, /* The parser */
657 unsigned int yyruleno
/* Number of the rule by which to reduce */
659 int yygoto
; /* The next state */
660 int yyact
; /* The next action */
661 yyStackEntry
*yymsp
; /* The top of the parser's stack */
662 int yysize
; /* Amount to pop the stack */
664 yymsp
= yypParser
->yytos
;
666 if( yyTraceFILE
&& yyruleno
<(int)(sizeof(yyRuleName
)/sizeof(yyRuleName
[0])) ){
667 yysize
= yyRuleInfo
[yyruleno
].nrhs
;
668 fprintf(yyTraceFILE
, "%sReduce [%s], go to state %d.\n", yyTracePrompt
,
669 yyRuleName
[yyruleno
], yymsp
[yysize
].stateno
);
673 /* Check that the stack is large enough to grow by a single entry
674 ** if the RHS of the rule is empty. This ensures that there is room
675 ** enough on the stack to push the LHS value */
676 if( yyRuleInfo
[yyruleno
].nrhs
==0 ){
677 #ifdef YYTRACKMAXSTACKDEPTH
678 if( (int)(yypParser
->yytos
- yypParser
->yystack
)>yypParser
->yyhwm
){
680 assert( yypParser
->yyhwm
== (int)(yypParser
->yytos
- yypParser
->yystack
));
684 if( yypParser
->yytos
>=yypParser
->yystackEnd
){
685 yyStackOverflow(yypParser
);
689 if( yypParser
->yytos
>=&yypParser
->yystack
[yypParser
->yystksz
-1] ){
690 if( yyGrowStack(yypParser
) ){
691 yyStackOverflow(yypParser
);
694 yymsp
= yypParser
->yytos
;
700 /* Beginning here are the reduction cases. A typical example
703 ** #line <lineno> <grammarfile>
704 ** { ... } // User supplied code
705 ** #line <lineno> <thisfile>
708 /********** Begin reduce actions **********************************************/
710 /********** End reduce actions ************************************************/
712 assert( yyruleno
<sizeof(yyRuleInfo
)/sizeof(yyRuleInfo
[0]) );
713 yygoto
= yyRuleInfo
[yyruleno
].lhs
;
714 yysize
= yyRuleInfo
[yyruleno
].nrhs
;
715 yyact
= yy_find_reduce_action(yymsp
[yysize
].stateno
,(YYCODETYPE
)yygoto
);
717 /* There are no SHIFTREDUCE actions on nonterminals because the table
718 ** generator has simplified them to pure REDUCE actions. */
719 assert( !(yyact
>YY_MAX_SHIFT
&& yyact
<=YY_MAX_SHIFTREDUCE
) );
721 /* It is not possible for a REDUCE to be followed by an error */
722 assert( yyact
!=YY_ERROR_ACTION
);
724 if( yyact
==YY_ACCEPT_ACTION
){
725 yypParser
->yytos
+= yysize
;
726 yy_accept(yypParser
);
729 yypParser
->yytos
= yymsp
;
730 yymsp
->stateno
= (YYACTIONTYPE
)yyact
;
731 yymsp
->major
= (YYCODETYPE
)yygoto
;
732 yyTraceShift(yypParser
, yyact
);
737 ** The following code executes when the parse fails
739 #ifndef YYNOERRORRECOVERY
740 static void yy_parse_failed(
741 yyParser
*yypParser
/* The parser */
746 fprintf(yyTraceFILE
,"%sFail!\n",yyTracePrompt
);
749 while( yypParser
->yytos
>yypParser
->yystack
) yy_pop_parser_stack(yypParser
);
750 /* Here code is inserted which will be executed whenever the
752 /************ Begin %parse_failure code ***************************************/
754 /************ End %parse_failure code *****************************************/
755 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
757 #endif /* YYNOERRORRECOVERY */
760 ** The following code executes when a syntax error first occurs.
762 static void yy_syntax_error(
763 yyParser
*yypParser
, /* The parser */
764 int yymajor
, /* The major type of the error token */
765 ParseTOKENTYPE yyminor
/* The minor type of the error token */
768 #define TOKEN yyminor
769 /************ Begin %syntax_error code ****************************************/
771 /************ End %syntax_error code ******************************************/
772 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
776 ** The following is executed when the parser accepts
778 static void yy_accept(
779 yyParser
*yypParser
/* The parser */
784 fprintf(yyTraceFILE
,"%sAccept!\n",yyTracePrompt
);
787 #ifndef YYNOERRORRECOVERY
788 yypParser
->yyerrcnt
= -1;
790 assert( yypParser
->yytos
==yypParser
->yystack
);
791 /* Here code is inserted which will be executed whenever the
793 /*********** Begin %parse_accept code *****************************************/
795 /*********** End %parse_accept code *******************************************/
796 ParseARG_STORE
; /* Suppress warning about unused %extra_argument variable */
799 /* The main parser program.
800 ** The first argument is a pointer to a structure obtained from
801 ** "ParseAlloc" which describes the current state of the parser.
802 ** The second argument is the major token number. The third is
803 ** the minor token. The fourth optional argument is whatever the
804 ** user wants (and specified in the grammar) and is available for
805 ** use by the action routines.
809 ** <li> A pointer to the parser (an opaque structure.)
810 ** <li> The major token number.
811 ** <li> The minor token number.
812 ** <li> An option argument of a grammar-specified type.
819 void *yyp
, /* The parser */
820 int yymajor
, /* The major token code number */
821 ParseTOKENTYPE yyminor
/* The value for the token */
822 ParseARG_PDECL
/* Optional %extra_argument parameter */
824 YYMINORTYPE yyminorunion
;
825 unsigned int yyact
; /* The parser action. */
826 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
827 int yyendofinput
; /* True if we are at the end of input */
830 int yyerrorhit
= 0; /* True if yymajor has invoked an error */
832 yyParser
*yypParser
; /* The parser */
834 yypParser
= (yyParser
*)yyp
;
835 assert( yypParser
->yytos
!=0 );
836 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
837 yyendofinput
= (yymajor
==0);
843 fprintf(yyTraceFILE
,"%sInput '%s'\n",yyTracePrompt
,yyTokenName
[yymajor
]);
848 yyact
= yy_find_shift_action(yypParser
,(YYCODETYPE
)yymajor
);
849 if( yyact
<= YY_MAX_SHIFTREDUCE
){
850 yy_shift(yypParser
,yyact
,yymajor
,yyminor
);
851 #ifndef YYNOERRORRECOVERY
852 yypParser
->yyerrcnt
--;
855 }else if( yyact
<= YY_MAX_REDUCE
){
856 yy_reduce(yypParser
,yyact
-YY_MIN_REDUCE
);
858 assert( yyact
== YY_ERROR_ACTION
);
859 yyminorunion
.yy0
= yyminor
;
865 fprintf(yyTraceFILE
,"%sSyntax Error!\n",yyTracePrompt
);
869 /* A syntax error has occurred.
870 ** The response to an error depends upon whether or not the
871 ** grammar defines an error token "ERROR".
873 ** This is what we do if the grammar does define ERROR:
875 ** * Call the %syntax_error function.
877 ** * Begin popping the stack until we enter a state where
878 ** it is legal to shift the error symbol, then shift
881 ** * Set the error count to three.
883 ** * Begin accepting and shifting new tokens. No new error
884 ** processing will occur until three tokens have been
885 ** shifted successfully.
888 if( yypParser
->yyerrcnt
<0 ){
889 yy_syntax_error(yypParser
,yymajor
,yyminor
);
891 yymx
= yypParser
->yytos
->major
;
892 if( yymx
==YYERRORSYMBOL
|| yyerrorhit
){
895 fprintf(yyTraceFILE
,"%sDiscard input token %s\n",
896 yyTracePrompt
,yyTokenName
[yymajor
]);
899 yy_destructor(yypParser
, (YYCODETYPE
)yymajor
, &yyminorunion
);
902 while( yypParser
->yytos
>= yypParser
->yystack
903 && yymx
!= YYERRORSYMBOL
904 && (yyact
= yy_find_reduce_action(
905 yypParser
->yytos
->stateno
,
906 YYERRORSYMBOL
)) >= YY_MIN_REDUCE
908 yy_pop_parser_stack(yypParser
);
910 if( yypParser
->yytos
< yypParser
->yystack
|| yymajor
==0 ){
911 yy_destructor(yypParser
,(YYCODETYPE
)yymajor
,&yyminorunion
);
912 yy_parse_failed(yypParser
);
913 #ifndef YYNOERRORRECOVERY
914 yypParser
->yyerrcnt
= -1;
917 }else if( yymx
!=YYERRORSYMBOL
){
918 yy_shift(yypParser
,yyact
,YYERRORSYMBOL
,yyminor
);
921 yypParser
->yyerrcnt
= 3;
923 #elif defined(YYNOERRORRECOVERY)
924 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
925 ** do any kind of error recovery. Instead, simply invoke the syntax
926 ** error routine and continue going as if nothing had happened.
928 ** Applications can set this macro (for example inside %include) if
929 ** they intend to abandon the parse upon the first syntax error seen.
931 yy_syntax_error(yypParser
,yymajor
, yyminor
);
932 yy_destructor(yypParser
,(YYCODETYPE
)yymajor
,&yyminorunion
);
935 #else /* YYERRORSYMBOL is not defined */
936 /* This is what we do if the grammar does not define ERROR:
938 ** * Report an error message, and throw away the input token.
940 ** * If the input token is $, then fail the parse.
942 ** As before, subsequent error messages are suppressed until
943 ** three input tokens have been successfully shifted.
945 if( yypParser
->yyerrcnt
<=0 ){
946 yy_syntax_error(yypParser
,yymajor
, yyminor
);
948 yypParser
->yyerrcnt
= 3;
949 yy_destructor(yypParser
,(YYCODETYPE
)yymajor
,&yyminorunion
);
951 yy_parse_failed(yypParser
);
952 #ifndef YYNOERRORRECOVERY
953 yypParser
->yyerrcnt
= -1;
959 }while( yymajor
!=YYNOCODE
&& yypParser
->yytos
>yypParser
->yystack
);
964 fprintf(yyTraceFILE
,"%sReturn. Stack=",yyTracePrompt
);
965 for(i
=&yypParser
->yystack
[1]; i
<=yypParser
->yytos
; i
++){
966 fprintf(yyTraceFILE
,"%c%s", cDiv
, yyTokenName
[i
->major
]);
969 fprintf(yyTraceFILE
,"]\n");