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.
10 **
11 *************************************************************************
12 ** Driver template for the LEMON parser generator.
13 **
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.
21 **
22 ** The following is the concatenation of all %include directives from the
23 ** input grammar file:
24 */
25 /************ Begin %include sections from the grammar ************************/
26 %%
27 /**************** End of %include directives **********************************/
28 /* These constants specify the various numeric values for terminal symbols.
29 ***************** Begin token definitions *************************************/
30 %%
31 /**************** End token definitions ***************************************/
32
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
83 */
84 #ifndef INTERFACE
85 # define INTERFACE 1
86 #endif
87 /************* Begin control #defines *****************************************/
88 %%
89 /************* End control #defines *******************************************/
90 #define YY_NLOOKAHEAD ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
91
92 /* Define the yytestcase() macro to be a no-op if is not already defined
93 ** otherwise.
94 **
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.
99 */
100 #ifndef yytestcase
101 # define yytestcase(X)
102 #endif
103
104
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.
109 **
110 ** Suppose the action integer is N. Then the action is determined as
111 ** follows
112 **
113 ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
114 ** token onto the stack and goto state N.
115 **
116 ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
117 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
118 **
119 ** N == YY_ERROR_ACTION A syntax error has occurred.
120 **
121 ** N == YY_ACCEPT_ACTION The parser accepts its input.
122 **
123 ** N == YY_NO_ACTION No such action. Denotes unused
124 ** slots in the yy_action[] table.
125 **
126 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
127 ** and YY_MAX_REDUCE
128 **
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:
131 **
132 ** (A) N = yy_action[ yy_shift_ofst[S] + X ]
133 ** (B) N = yy_default[S]
134 **
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.
137 **
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.
142 **
143 ** The following are the tables generated in this section:
144 **
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.
153 **
154 *********** Begin parsing tables **********************************************/
155 %%
156 /********** End of lemon-generated parsing tables *****************************/
157
158 /* The next table maps tokens (terminal symbols) into fallback tokens.
159 ** If a construct like the following:
160 **
161 ** %fallback ID X Y Z.
162 **
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.
167 **
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.
171 */
172 #ifdef YYFALLBACK
173 static const YYCODETYPE yyFallback[] = {
174 %%
175 };
176 #endif /* YYFALLBACK */
177
178 /* The following structure represents a single element of the
179 ** parser's stack. Information stored includes:
180 **
181 ** + The state number for the parser at this level of the stack.
182 **
183 ** + The value of the token stored at this level of the stack.
184 ** (In other words, the "major" token.)
185 **
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.
189 **
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.
193 */
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 */
200 };
201 typedef struct yyStackEntry yyStackEntry;
202
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
223 };
224 typedef struct yyParser yyParser;
225
226 #include <assert.h>
227 #ifndef NDEBUG
228 #include <stdio.h>
229 static FILE *yyTraceFILE = 0;
230 static char *yyTracePrompt = 0;
231 #endif /* NDEBUG */
232
233 #ifndef NDEBUG
234 /*
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
238 **
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>
247 **
248 ** Outputs:
249 ** None.
250 */
ParseTrace(FILE * TraceFILE,char * zTracePrompt)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;
256 }
257 #endif /* NDEBUG */
258
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[] = {
263 %%
264 };
265 #endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
266
267 #ifndef NDEBUG
268 /* For tracing reduce actions, the names of all rules are required.
269 */
270 static const char *const yyRuleName[] = {
271 %%
272 };
273 #endif /* NDEBUG */
274
275
276 #if YYSTACKDEPTH<=0
277 /*
278 ** Try to increase the size of the parser stack. Return the number
279 ** of errors. Return 0 on success.
280 */
yyGrowStack(yyParser * p)281 static int yyGrowStack(yyParser *p){
282 int newSize;
283 int idx;
284 yyStackEntry *pNew;
285
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]));
293 }
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);
301 }
302 #endif
303 p->yystksz = newSize;
304 }
305 return pNew==0;
306 }
307 #endif
308
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.
313 */
314 #ifndef YYMALLOCARGTYPE
315 # define YYMALLOCARGTYPE size_t
316 #endif
317
318 /* Initialize a new parser that has already been allocated.
319 */
ParseInit(void * yypRawParser ParseCTX_PDECL)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;
333 }
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
344 }
345
346 #ifndef Parse_ENGINEALWAYSONSTACK
347 /*
348 ** This function allocates a new parser.
349 ** The only argument is a pointer to a function which works like
350 ** malloc.
351 **
352 ** Inputs:
353 ** A pointer to the function used to allocate memory.
354 **
355 ** Outputs:
356 ** A pointer to a parser. This pointer is used in subsequent calls
357 ** to Parse and ParseFree.
358 */
ParseAlloc(void * (* mallocProc)(YYMALLOCARGTYPE)ParseCTX_PDECL)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);
365 }
366 return (void*)yypParser;
367 }
368 #endif /* Parse_ENGINEALWAYSONSTACK */
369
370
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.
377 */
yy_destructor(yyParser * yypParser,YYCODETYPE yymajor,YYMINORTYPE * yypminor)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 */
382 ){
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.
391 **
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.
395 */
396 /********* Begin destructor definitions ***************************************/
397 %%
398 /********* End destructor definitions *****************************************/
399 default: break; /* If no destructor action specified: do nothing */
400 }
401 }
402
403 /*
404 ** Pop the parser's stack once.
405 **
406 ** If there is a destructor routine associated with the token which
407 ** is popped from the stack, then call it.
408 */
yy_pop_parser_stack(yyParser * pParser)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]);
419 }
420 #endif
421 yy_destructor(pParser, yytos->major, &yytos->minor);
422 }
423
424 /*
425 ** Clear all secondary memory allocations from the parser
426 */
ParseFinalize(void * p)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
433 }
434
435 #ifndef Parse_ENGINEALWAYSONSTACK
436 /*
437 ** Deallocate and destroy a parser. Destructors are called for
438 ** all stack elements before shutting the parser down.
439 **
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.
443 */
ParseFree(void * p,void (* freeProc)(void *))444 void ParseFree(
445 void *p, /* The parser to be deleted */
446 void (*freeProc)(void*) /* Function used to reclaim memory */
447 ){
448 #ifndef YYPARSEFREENEVERNULL
449 if( p==0 ) return;
450 #endif
451 ParseFinalize(p);
452 (*freeProc)(p);
453 }
454 #endif /* Parse_ENGINEALWAYSONSTACK */
455
456 /*
457 ** Return the peak depth of the stack for a parser.
458 */
459 #ifdef YYTRACKMAXSTACKDEPTH
ParseStackPeak(void * p)460 int ParseStackPeak(void *p){
461 yyParser *pParser = (yyParser*)p;
462 return pParser->yyhwm;
463 }
464 #endif
465
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.
470 */
471 #if defined(YYCOVERAGE)
472 static unsigned char yycoverage[YYNSTATE][YYNTOKEN];
473 #endif
474
475 /*
476 ** Write into out a description of every state/lookahead combination that
477 **
478 ** (1) has not been used by the parser, and
479 ** (2) is not a syntax error.
480 **
481 ** Return the number of missed state/lookahead combinations.
482 */
483 #if defined(YYCOVERAGE)
ParseCoverage(FILE * out)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");
496 }
497 }
498 }
499 return nMissed;
500 }
501 #endif
502
503 /*
504 ** Find the appropriate action for a parser given the terminal
505 ** look-ahead token iLookAhead.
506 */
yy_find_shift_action(YYCODETYPE iLookAhead,YYACTIONTYPE stateno)507 static YYACTIONTYPE yy_find_shift_action(
508 YYCODETYPE iLookAhead, /* The look-ahead token */
509 YYACTIONTYPE stateno /* Current state number */
510 ){
511 int i;
512
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
518 do{
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]);
537 }
538 #endif
539 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */
540 iLookAhead = iFallback;
541 continue;
542 }
543 #endif
544 #ifdef YYWILDCARD
545 {
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]);
554 }
555 #endif /* NDEBUG */
556 return yy_action[j];
557 }
558 }
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];
564 }
565 }while(1);
566 }
567
568 /*
569 ** Find the appropriate action for a parser given the non-terminal
570 ** look-ahead token iLookAhead.
571 */
yy_find_reduce_action(YYACTIONTYPE stateno,YYCODETYPE iLookAhead)572 static YYACTIONTYPE yy_find_reduce_action(
573 YYACTIONTYPE stateno, /* Current state number */
574 YYCODETYPE iLookAhead /* The look-ahead token */
575 ){
576 int i;
577 #ifdef YYERRORSYMBOL
578 if( stateno>YY_REDUCE_COUNT ){
579 return yy_default[stateno];
580 }
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];
590 }
591 #else
592 assert( i>=0 && i<YY_ACTTAB_COUNT );
593 assert( yy_lookahead[i]==iLookAhead );
594 #endif
595 return yy_action[i];
596 }
597
598 /*
599 ** The following routine is called if the stack overflows.
600 */
yyStackOverflow(yyParser * yypParser)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);
607 }
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 ******************************************/
613 %%
614 /******** End %stack_overflow code ********************************************/
615 ParseARG_STORE /* Suppress warning about unused %extra_argument var */
616 ParseCTX_STORE
617 }
618
619 /*
620 ** Print tracing information for a SHIFT action
621 */
622 #ifndef NDEBUG
yyTraceShift(yyParser * yypParser,int yyNewState,const char * zTag)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);
633 }
634 }
635 }
636 #else
637 # define yyTraceShift(X,Y,Z)
638 #endif
639
640 /*
641 ** Perform a shift action.
642 */
yy_shift(yyParser * yypParser,YYACTIONTYPE yyNewState,YYCODETYPE yyMajor,ParseTOKENTYPE yyMinor)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 */
648 ){
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) );
655 }
656 #endif
657 #if YYSTACKDEPTH>0
658 if( yypParser->yytos>yypParser->yystackEnd ){
659 yypParser->yytos--;
660 yyStackOverflow(yypParser);
661 return;
662 }
663 #else
664 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
665 if( yyGrowStack(yypParser) ){
666 yypParser->yytos--;
667 yyStackOverflow(yypParser);
668 return;
669 }
670 }
671 #endif
672 if( yyNewState > YY_MAX_SHIFT ){
673 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
674 }
675 yytos = yypParser->yytos;
676 yytos->stateno = yyNewState;
677 yytos->major = yyMajor;
678 yytos->minor.yy0 = yyMinor;
679 yyTraceShift(yypParser, yyNewState, "Shift");
680 }
681
682 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
683 ** of that rule */
684 static const YYCODETYPE yyRuleInfoLhs[] = {
685 %%
686 };
687
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[] = {
691 %%
692 };
693
694 static void yy_accept(yyParser*); /* Forward Declaration */
695
696 /*
697 ** Perform a reduce action and the shift that must immediately
698 ** follow the reduce.
699 **
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.
705 */
yy_reduce(yyParser * yypParser,unsigned int yyruleno,int yyLookahead,ParseTOKENTYPE yyLookaheadToken ParseCTX_PDECL)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 */
712 ){
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;
721
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;
730 */
731 /********** Begin reduce actions **********************************************/
732 %%
733 /********** End reduce actions ************************************************/
734 };
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);
739
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) );
743
744 /* It is not possible for a REDUCE to be followed by an error */
745 assert( yyact!=YY_ERROR_ACTION );
746
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;
753 }
754
755 /*
756 ** The following code executes when the parse fails
757 */
758 #ifndef YYNOERRORRECOVERY
yy_parse_failed(yyParser * yypParser)759 static void yy_parse_failed(
760 yyParser *yypParser /* The parser */
761 ){
762 ParseARG_FETCH
763 ParseCTX_FETCH
764 #ifndef NDEBUG
765 if( yyTraceFILE ){
766 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
767 }
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 ***************************************/
773 %%
774 /************ End %parse_failure code *****************************************/
775 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
776 ParseCTX_STORE
777 }
778 #endif /* YYNOERRORRECOVERY */
779
780 /*
781 ** The following code executes when a syntax error first occurs.
782 */
yy_syntax_error(yyParser * yypParser,int yymajor,ParseTOKENTYPE yyminor)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 */
787 ){
788 (void)yymajor;
789 (void)yyminor;
790 ParseARG_FETCH
791 ParseCTX_FETCH
792 #define TOKEN yyminor
793 /************ Begin %syntax_error code ****************************************/
794 %%
795 /************ End %syntax_error code ******************************************/
796 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
797 ParseCTX_STORE
798 }
799
800 /*
801 ** The following is executed when the parser accepts
802 */
yy_accept(yyParser * yypParser)803 static void yy_accept(
804 yyParser *yypParser /* The parser */
805 ){
806 ParseARG_FETCH
807 ParseCTX_FETCH
808 #ifndef NDEBUG
809 if( yyTraceFILE ){
810 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
811 }
812 #endif
813 #ifndef YYNOERRORRECOVERY
814 yypParser->yyerrcnt = -1;
815 #endif
816 assert( yypParser->yytos==yypParser->yystack );
817 /* Here code is inserted which will be executed whenever the
818 ** parser accepts */
819 /*********** Begin %parse_accept code *****************************************/
820 %%
821 /*********** End %parse_accept code *******************************************/
822 ParseARG_STORE /* Suppress warning about unused %extra_argument variable */
823 ParseCTX_STORE
824 }
825
826 /* The main parser program.
827 ** The first argument is a pointer to a structure obtained from
828 ** "ParseAlloc" which describes the current state of the parser.
829 ** The second argument is the major token number. The third is
830 ** the minor token. The fourth optional argument is whatever the
831 ** user wants (and specified in the grammar) and is available for
832 ** use by the action routines.
833 **
834 ** Inputs:
835 ** <ul>
836 ** <li> A pointer to the parser (an opaque structure.)
837 ** <li> The major token number.
838 ** <li> The minor token number.
839 ** <li> An option argument of a grammar-specified type.
840 ** </ul>
841 **
842 ** Outputs:
843 ** None.
844 */
Parse(void * yyp,int yymajor,ParseTOKENTYPE yyminor ParseARG_PDECL)845 void Parse(
846 void *yyp, /* The parser */
847 int yymajor, /* The major token code number */
848 ParseTOKENTYPE yyminor /* The value for the token */
849 ParseARG_PDECL /* Optional %extra_argument parameter */
850 ){
851 YYMINORTYPE yyminorunion;
852 YYACTIONTYPE yyact; /* The parser action. */
853 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
854 int yyendofinput; /* True if we are at the end of input */
855 #endif
856 #ifdef YYERRORSYMBOL
857 int yyerrorhit = 0; /* True if yymajor has invoked an error */
858 #endif
859 yyParser *yypParser = (yyParser*)yyp; /* The parser */
860 ParseCTX_FETCH
861 ParseARG_STORE
862
863 assert( yypParser->yytos!=0 );
864 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
865 yyendofinput = (yymajor==0);
866 #endif
867
868 yyact = yypParser->yytos->stateno;
869 #ifndef NDEBUG
870 if( yyTraceFILE ){
871 if( yyact < YY_MIN_REDUCE ){
872 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
873 yyTracePrompt,yyTokenName[yymajor],yyact);
874 }else{
875 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
876 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
877 }
878 }
879 #endif
880
881 while(1){ /* Exit by "break" */
882 assert( yypParser->yytos>=yypParser->yystack );
883 assert( yyact==yypParser->yytos->stateno );
884 yyact = yy_find_shift_action((YYCODETYPE)yymajor,yyact);
885 if( yyact >= YY_MIN_REDUCE ){
886 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
887 #ifndef NDEBUG
888 assert( yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) );
889 if( yyTraceFILE ){
890 int yysize = yyRuleInfoNRhs[yyruleno];
891 if( yysize ){
892 fprintf(yyTraceFILE, "%sReduce %d [%s]%s, pop back to state %d.\n",
893 yyTracePrompt,
894 yyruleno, yyRuleName[yyruleno],
895 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action",
896 yypParser->yytos[yysize].stateno);
897 }else{
898 fprintf(yyTraceFILE, "%sReduce %d [%s]%s.\n",
899 yyTracePrompt, yyruleno, yyRuleName[yyruleno],
900 yyruleno<YYNRULE_WITH_ACTION ? "" : " without external action");
901 }
902 }
903 #endif /* NDEBUG */
904
905 /* Check that the stack is large enough to grow by a single entry
906 ** if the RHS of the rule is empty. This ensures that there is room
907 ** enough on the stack to push the LHS value */
908 if( yyRuleInfoNRhs[yyruleno]==0 ){
909 #ifdef YYTRACKMAXSTACKDEPTH
910 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
911 yypParser->yyhwm++;
912 assert( yypParser->yyhwm ==
913 (int)(yypParser->yytos - yypParser->yystack));
914 }
915 #endif
916 #if YYSTACKDEPTH>0
917 if( yypParser->yytos>=yypParser->yystackEnd ){
918 yyStackOverflow(yypParser);
919 break;
920 }
921 #else
922 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
923 if( yyGrowStack(yypParser) ){
924 yyStackOverflow(yypParser);
925 break;
926 }
927 }
928 #endif
929 }
930 yyact = yy_reduce(yypParser,yyruleno,yymajor,yyminor ParseCTX_PARAM);
931 }else if( yyact <= YY_MAX_SHIFTREDUCE ){
932 yy_shift(yypParser,yyact,(YYCODETYPE)yymajor,yyminor);
933 #ifndef YYNOERRORRECOVERY
934 yypParser->yyerrcnt--;
935 #endif
936 break;
937 }else if( yyact==YY_ACCEPT_ACTION ){
938 yypParser->yytos--;
939 yy_accept(yypParser);
940 return;
941 }else{
942 assert( yyact == YY_ERROR_ACTION );
943 yyminorunion.yy0 = yyminor;
944 #ifdef YYERRORSYMBOL
945 int yymx;
946 #endif
947 #ifndef NDEBUG
948 if( yyTraceFILE ){
949 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
950 }
951 #endif
952 #ifdef YYERRORSYMBOL
953 /* A syntax error has occurred.
954 ** The response to an error depends upon whether or not the
955 ** grammar defines an error token "ERROR".
956 **
957 ** This is what we do if the grammar does define ERROR:
958 **
959 ** * Call the %syntax_error function.
960 **
961 ** * Begin popping the stack until we enter a state where
962 ** it is legal to shift the error symbol, then shift
963 ** the error symbol.
964 **
965 ** * Set the error count to three.
966 **
967 ** * Begin accepting and shifting new tokens. No new error
968 ** processing will occur until three tokens have been
969 ** shifted successfully.
970 **
971 */
972 if( yypParser->yyerrcnt<0 ){
973 yy_syntax_error(yypParser,yymajor,yyminor);
974 }
975 yymx = yypParser->yytos->major;
976 if( yymx==YYERRORSYMBOL || yyerrorhit ){
977 #ifndef NDEBUG
978 if( yyTraceFILE ){
979 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
980 yyTracePrompt,yyTokenName[yymajor]);
981 }
982 #endif
983 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion);
984 yymajor = YYNOCODE;
985 }else{
986 while( yypParser->yytos > yypParser->yystack ){
987 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
988 YYERRORSYMBOL);
989 if( yyact<=YY_MAX_SHIFTREDUCE ) break;
990 yy_pop_parser_stack(yypParser);
991 }
992 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
993 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
994 yy_parse_failed(yypParser);
995 #ifndef YYNOERRORRECOVERY
996 yypParser->yyerrcnt = -1;
997 #endif
998 yymajor = YYNOCODE;
999 }else if( yymx!=YYERRORSYMBOL ){
1000 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
1001 }
1002 }
1003 yypParser->yyerrcnt = 3;
1004 yyerrorhit = 1;
1005 if( yymajor==YYNOCODE ) break;
1006 yyact = yypParser->yytos->stateno;
1007 #elif defined(YYNOERRORRECOVERY)
1008 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
1009 ** do any kind of error recovery. Instead, simply invoke the syntax
1010 ** error routine and continue going as if nothing had happened.
1011 **
1012 ** Applications can set this macro (for example inside %include) if
1013 ** they intend to abandon the parse upon the first syntax error seen.
1014 */
1015 yy_syntax_error(yypParser,yymajor, yyminor);
1016 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1017 break;
1018 #else /* YYERRORSYMBOL is not defined */
1019 /* This is what we do if the grammar does not define ERROR:
1020 **
1021 ** * Report an error message, and throw away the input token.
1022 **
1023 ** * If the input token is $, then fail the parse.
1024 **
1025 ** As before, subsequent error messages are suppressed until
1026 ** three input tokens have been successfully shifted.
1027 */
1028 if( yypParser->yyerrcnt<=0 ){
1029 yy_syntax_error(yypParser,yymajor, yyminor);
1030 }
1031 yypParser->yyerrcnt = 3;
1032 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
1033 if( yyendofinput ){
1034 yy_parse_failed(yypParser);
1035 #ifndef YYNOERRORRECOVERY
1036 yypParser->yyerrcnt = -1;
1037 #endif
1038 }
1039 break;
1040 #endif
1041 }
1042 }
1043 #ifndef NDEBUG
1044 if( yyTraceFILE ){
1045 yyStackEntry *i;
1046 char cDiv = '[';
1047 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
1048 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
1049 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
1050 cDiv = ' ';
1051 }
1052 fprintf(yyTraceFILE,"]\n");
1053 }
1054 #endif
1055 return;
1056 }
1057
1058 /*
1059 ** Return the fallback token corresponding to canonical token iToken, or
1060 ** 0 if iToken has no fallback.
1061 */
ParseFallback(int iToken)1062 int ParseFallback(int iToken){
1063 #ifdef YYFALLBACK
1064 assert( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) );
1065 return yyFallback[iToken];
1066 #else
1067 (void)iToken;
1068 return 0;
1069 #endif
1070 }
1071