1 /* Driver template for the LEMON parser generator.
2 ** Copyright 1991-1995 by D. Richard Hipp.
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Library General Public License for more details.
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the
16 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 ** Boston, MA 02111-1307, USA.
19 ** Modified 1997 to make it suitable for use with makeheaders.
20 * Updated to sqlite lemon version 1.22
22 /* First off, code is include which follows the "include" declaration
23 ** in the input file. */
27 /* Next is all token values, in a form suitable for use by makeheaders.
28 ** This section will be null unless lemon is run with the -m switch.
31 ** These constants (all generated automatically by the parser generator)
32 ** specify the various kinds of tokens (terminals) that the parser
35 ** Each symbol here is a terminal symbol in the grammar.
38 /* Make sure the INTERFACE macro is defined.
43 /* The next thing included is series of defines which control
44 ** various aspects of the generated parser.
45 ** YYCODETYPE is the data type used for storing terminal
46 ** and nonterminal numbers. "unsigned char" is
47 ** used if there are fewer than 250 terminals
48 ** and nonterminals. "int" is used otherwise.
49 ** YYNOCODE is a number of type YYCODETYPE which corresponds
50 ** to no legal terminal or nonterminal number. This
51 ** number is used to fill in empty slots of the hash
53 ** YYFALLBACK If defined, this indicates that one or more tokens
54 ** have fall-back values which should be used if the
55 ** original value of the token will not parse.
56 ** YYACTIONTYPE is the data type used for storing terminal
57 ** and nonterminal numbers. "unsigned char" is
58 ** used if there are fewer than 250 rules and
59 ** states combined. "int" is used otherwise.
60 ** ParseTOKENTYPE is the data type used for minor tokens given
61 ** directly to the parser from the tokenizer.
62 ** YYMINORTYPE is the data type used for all minor tokens.
63 ** This is typically a union of many types, one of
64 ** which is ParseTOKENTYPE. The entry in the union
65 ** for base tokens is called "yy0".
66 ** YYSTACKDEPTH is the maximum depth of the parser's stack.
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 ** YYNSTATE the combined number of states.
72 ** YYNRULE the number of rules in the grammar
73 ** YYERRORSYMBOL is the code number of the error symbol. If not
74 ** defined, then do no error processing.
77 #define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
78 #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
79 #define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
81 /* Next are that tables used to determine what action to take based on the
82 ** current state and lookahead token. These tables are used to implement
83 ** functions that take a state number and lookahead value and return an
86 ** Suppose the action integer is N. Then the action is determined as
89 ** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
90 ** token onto the stack and goto state N.
92 ** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
94 ** N == YYNSTATE+YYNRULE A syntax error has occurred.
96 ** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
98 ** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
99 ** slots in the yy_action[] table.
101 ** The action table is constructed as a single large table named yy_action[].
102 ** Given state S and lookahead X, the action is computed as
104 ** yy_action[ yy_shift_ofst[S] + X ]
106 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
107 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
108 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
109 ** and that yy_default[S] should be used instead.
111 ** The formula above is for computing the action when the lookahead is
112 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
113 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
114 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
115 ** YY_SHIFT_USE_DFLT.
117 ** The following are the tables generated in this section:
119 ** yy_action[] A single table containing all actions.
120 ** yy_lookahead[] A table containing the lookahead for each entry in
121 ** yy_action. Used to detect hash collisions.
122 ** yy_shift_ofst[] For each state, the offset into yy_action for
123 ** shifting terminals.
124 ** yy_reduce_ofst[] For each state, the offset into yy_action for
125 ** shifting non-terminals after a reduce.
126 ** yy_default[] Default action for each state.
129 #define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0]))
132 /* The next table maps tokens into fallback tokens. If a construct
133 ** like the following:
135 ** %fallback ID X Y Z.
137 ** appears in the grammer, then ID becomes a fallback token for X, Y,
138 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
139 ** but it does not parse, the type of the token is changed to ID and
140 ** the parse is retried before an error is thrown.
143 static const YYCODETYPE yyFallback[] = {
146 #endif /* YYFALLBACK */
148 /* The following structure represents a single element of the
149 ** parser's stack. Information stored includes:
151 ** + The state number for the parser at this level of the stack.
153 ** + The value of the token stored at this level of the stack.
154 ** (In other words, the "major" token.)
156 ** + The semantic value stored at this level of the stack. This is
157 ** the information used by the action routines in the grammar.
158 ** It is sometimes called the "minor" token.
160 struct yyStackEntry {
161 int stateno; /* The state-number */
162 int major; /* The major token value. This is the code
163 ** number for the token at this stack level */
164 YYMINORTYPE minor; /* The user-supplied minor token value. This
165 ** is the value of the token */
167 typedef struct yyStackEntry yyStackEntry;
169 /* The state of the parser is completely contained in an instance of
170 ** the following structure */
172 int yyidx; /* Index of top element in stack */
173 int yyerrcnt; /* Shifts left before out of the error */
174 ParseARG_SDECL /* A place to hold %extra_argument */
175 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
177 typedef struct yyParser yyParser;
181 static FILE *yyTraceFILE = 0;
182 static char *yyTracePrompt = 0;
187 ** Turn parser tracing on by giving a stream to which to write the trace
188 ** and a prompt to preface each trace message. Tracing is turned off
189 ** by making either argument NULL
193 ** <li> A FILE* to which trace output should be written.
194 ** If NULL, then tracing is turned off.
195 ** <li> A prefix string written at the beginning of every
196 ** line of trace output. If NULL, then tracing is
203 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
204 yyTraceFILE = TraceFILE;
205 yyTracePrompt = zTracePrompt;
206 if( yyTraceFILE==0 ) yyTracePrompt = 0;
207 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
212 /* For tracing shifts, the names of all terminals and nonterminals
213 ** are required. The following table supplies these names */
214 static const char *const yyTokenName[] = {
220 /* For tracing reduce actions, the names of all rules are required.
222 static const char *const yyRuleName[] = {
228 ** This function returns the symbolic name associated with a token
231 const char *ParseTokenName(int tokenType){
233 if( tokenType>0 && tokenType<(int)(sizeof(yyTokenName)/sizeof(yyTokenName[0])) ){
234 return yyTokenName[tokenType];
244 ** This function allocates a new parser.
245 ** The only argument is a pointer to a function which works like
249 ** A pointer to the function used to allocate memory.
252 ** A pointer to a parser. This pointer is used in subsequent calls
253 ** to Parse and ParseFree.
255 #if GLIB_CHECK_VERSION(2,16,0)
256 void *ParseAlloc(void *(*mallocProc)(gsize)){
258 pParser = (yyParser*)(*mallocProc)( (gsize)sizeof(yyParser) );
260 void *ParseAlloc(void *(*mallocProc)(gulong)){
262 pParser = (yyParser*)(*mallocProc)( (gulong)sizeof(yyParser) );
270 /* The following function deletes the value associated with a
271 ** symbol. The symbol can be either a terminal or nonterminal.
272 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
275 static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
277 /* Here is inserted the actions which take place when a
278 ** terminal or non-terminal is destroyed. This can happen
279 ** when the symbol is popped from the stack during a
280 ** reduce or during error processing or when a parser is
281 ** being destroyed before it is finished parsing.
283 ** Note: during a reduce, the only symbols destroyed are those
284 ** which appear on the RHS of the rule, but which are not used
285 ** inside the C code.
288 default: break; /* If no destructor action specified: do nothing */
293 ** Pop the parser's stack once.
295 ** If there is a destructor routine associated with the token which
296 ** is popped from the stack, then call it.
298 ** Return the major token number for the symbol popped.
300 static int yy_pop_parser_stack(yyParser *pParser){
302 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
304 if( pParser->yyidx<0 ) return 0;
306 if( yyTraceFILE && pParser->yyidx>=0 ){
307 fprintf(yyTraceFILE,"%sPopping %s\n",
309 yyTokenName[yytos->major]);
312 yymajor = yytos->major;
313 yy_destructor( yymajor, &yytos->minor);
319 ** Deallocate and destroy a parser. Destructors are all called for
320 ** all stack elements before shutting the parser down.
324 ** <li> A pointer to the parser. This should be a pointer
325 ** obtained from ParseAlloc.
326 ** <li> A pointer to a function used to reclaim memory obtained
331 void *p, /* The parser to be deleted */
332 void (*freeProc)(void*) /* Function used to reclaim memory */
334 yyParser *pParser = (yyParser*)p;
335 if( pParser==0 ) return;
336 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
337 (*freeProc)(pParser);
341 ** Find the appropriate action for a parser given the terminal
342 ** look-ahead token iLookAhead.
344 ** If the look-ahead token is YYNOCODE, then check to see if the action is
345 ** independent of the look-ahead. If it is, return the action, otherwise
346 ** return YY_NO_ACTION.
348 static int yy_find_shift_action(
349 yyParser *pParser, /* The parser */
350 YYCODETYPE iLookAhead /* The look-ahead token */
353 int stateno = pParser->yystack[pParser->yyidx].stateno;
355 if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
356 return yy_default[stateno];
358 if( iLookAhead==YYNOCODE ){
362 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
365 int iFallback; /* Fallback token */
366 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
367 && (iFallback = yyFallback[iLookAhead])!=0 ){
370 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
371 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
374 return yy_find_shift_action(pParser, iFallback);
378 int j = i - iLookAhead + YYWILDCARD;
379 if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){
382 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
383 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
389 #endif /* YYWILDCARD */
391 return yy_default[stateno];
398 ** Find the appropriate action for a parser given the non-terminal
399 ** look-ahead token iLookAhead.
401 ** If the look-ahead token is YYNOCODE, then check to see if the action is
402 ** independent of the look-ahead. If it is, return the action, otherwise
403 ** return YY_NO_ACTION.
405 static int yy_find_reduce_action(
406 int stateno, /* Current state number */
407 YYCODETYPE iLookAhead /* The look-ahead token */
410 /* int stateno = pParser->yystack[pParser->yyidx].stateno; */
412 if( stateno>YY_REDUCE_MAX ||
413 (i = yy_reduce_ofst[stateno])==YY_REDUCE_USE_DFLT ){
414 return yy_default[stateno];
416 if( iLookAhead==YYNOCODE ){
420 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
421 return yy_default[stateno];
428 ** Perform a shift action.
430 static void yy_shift(
431 yyParser *yypParser, /* The parser to be shifted */
432 int yyNewState, /* The new state to shift in */
433 int yyMajor, /* The major token to shift in */
434 YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */
438 if( yypParser->yyidx>=YYSTACKDEPTH ){
443 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
446 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
447 /* Here code is inserted which will execute if the parser
448 ** stack every overflows */
450 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
453 yytos = &yypParser->yystack[yypParser->yyidx];
454 yytos->stateno = yyNewState;
455 yytos->major = yyMajor;
456 yytos->minor = *yypMinor;
458 if( yyTraceFILE && yypParser->yyidx>0 ){
460 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
461 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
462 for(i=1; i<=yypParser->yyidx; i++)
463 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
464 fprintf(yyTraceFILE,"\n");
469 /* The following table contains information about every rule that
470 ** is used during the reduce.
472 static const struct {
473 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
474 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
479 static void yy_accept(
480 yyParser *yypParser /* The parser */
481 ); /* Forward declaration */
484 ** Perform a reduce action and the shift that must immediately
485 ** follow the reduce.
487 static void yy_reduce(
488 yyParser *yypParser, /* The parser */
489 int yyruleno /* Number of the rule by which to reduce */
491 int yygoto; /* The next state */
492 int yyact; /* The next action */
493 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
494 yyStackEntry *yymsp; /* The top of the parser's stack */
495 int yysize; /* Amount to pop the stack */
497 yymsp = &yypParser->yystack[yypParser->yyidx];
499 if( yyTraceFILE && yyruleno>=0
500 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
501 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
502 yyRuleName[yyruleno]);
507 /* Silence complaints from purify about yygotominor being uninitialized
508 ** in some cases when it is copied into the stack after the following
509 ** switch. yygotominor is uninitialized when a rule reduces that does
510 ** not set the value of its left-hand side nonterminal. Leaving the
511 ** value of the nonterminal uninitialized is utterly harmless as long
512 ** as the value is never used. So really the only thing this code
513 ** accomplishes is to quieten purify.
515 memset(&yygotominor, 0, sizeof(yygotominor));
519 /* Beginning here are the reduction cases. A typical example
522 ** #line <lineno> <grammarfile>
523 ** { ... } // User supplied code
524 ** #line <lineno> <thisfile>
529 yygoto = yyRuleInfo[yyruleno].lhs;
530 yysize = yyRuleInfo[yyruleno].nrhs;
531 yypParser->yyidx -= yysize;
532 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
533 if( yyact < YYNSTATE ){
535 /* If we are not debugging and the reduce action popped at least
536 ** one element off the stack, then we can push the new element back
537 ** onto the stack here, and skip the stack overflow test in yy_shift().
538 ** That gives a significant speed improvement. */
542 yymsp->stateno = yyact;
543 yymsp->major = yygoto;
544 yymsp->minor = yygotominor;
548 yy_shift(yypParser,yyact,yygoto,&yygotominor);
550 }else if( yyact == YYNSTATE + YYNRULE + 1 ){
551 yy_accept(yypParser);
556 ** The following code executes when the parse fails
558 static void yy_parse_failed(
559 yyParser *yypParser /* The parser */
564 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
567 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
568 /* Here code is inserted which will be executed whenever the
571 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
575 ** The following code executes when a syntax error first occurs.
577 static void yy_syntax_error(
578 yyParser *yypParser _U_, /* The parser */
579 int yymajor _U_, /* The major type of the error token */
580 YYMINORTYPE yyminor /* The minor type of the error token */
583 #define TOKEN (yyminor.yy0)
585 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
589 ** The following is executed when the parser accepts
591 static void yy_accept(
592 yyParser *yypParser /* The parser */
597 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
600 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
601 /* Here code is inserted which will be executed whenever the
604 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
607 /* The main parser program.
608 ** The first argument is a pointer to a structure obtained from
609 ** "ParseAlloc" which describes the current state of the parser.
610 ** The second argument is the major token number. The third is
611 ** the minor token. The fourth optional argument is whatever the
612 ** user wants (and specified in the grammar) and is available for
613 ** use by the action routines.
617 ** <li> A pointer to the parser (an opaque structure.)
618 ** <li> The major token number.
619 ** <li> The minor token number.
620 ** <li> An option argument of a grammar-specified type.
627 void *yyp, /* The parser */
628 int yymajor, /* The major token code number */
629 ParseTOKENTYPE yyminor /* The value for the token */
630 ParseARG_PDECL /* Optional %extra_argument parameter */
632 YYMINORTYPE yyminorunion;
633 int yyact; /* The parser action. */
634 int yyendofinput; /* True if we are at the end of input */
635 int yyerrorhit = 0; /* True if yymajor has invoked an error */
636 yyParser *yypParser; /* The parser */
638 /* (re)initialize the parser, if necessary */
639 yypParser = (yyParser*)yyp;
640 if( yypParser->yyidx<0 ){
641 /* if( yymajor==0 ) return; // not sure why this was here... */
642 yypParser->yyidx = 0;
643 yypParser->yyerrcnt = -1;
644 yypParser->yystack[0].stateno = 0;
645 yypParser->yystack[0].major = 0;
647 yyminorunion.yy0 = yyminor;
648 yyendofinput = (yymajor==0);
653 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
658 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
659 if( yyact<YYNSTATE ){
660 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
661 yypParser->yyerrcnt--;
662 if( yyendofinput && yypParser->yyidx>=0 ){
667 }else if( yyact < YYNSTATE + YYNRULE ){
668 yy_reduce(yypParser,yyact-YYNSTATE);
669 }else if( yyact == YY_ERROR_ACTION ){
673 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
677 /* A syntax error has occurred.
678 ** The response to an error depends upon whether or not the
679 ** grammar defines an error token "ERROR".
681 ** This is what we do if the grammar does define ERROR:
683 ** * Call the %syntax_error function.
685 ** * Begin popping the stack until we enter a state where
686 ** it is legal to shift the error symbol, then shift
689 ** * Set the error count to three.
691 ** * Begin accepting and shifting new tokens. No new error
692 ** processing will occur until three tokens have been
693 ** shifted successfully.
696 if( yypParser->yyerrcnt<0 ){
697 yy_syntax_error(yypParser,yymajor,yyminorunion);
699 yymx = yypParser->yystack[yypParser->yyidx].major;
700 if( yymx==YYERRORSYMBOL || yyerrorhit ){
703 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
704 yyTracePrompt,yyTokenName[yymajor]);
707 yy_destructor((YYCODETYPE)yymajor,&yyminorunion);
711 yypParser->yyidx >= 0 &&
712 yymx != YYERRORSYMBOL &&
713 (yyact = yy_find_shift_action(yypParser,YYERRORSYMBOL)) >= YYNSTATE
715 yy_pop_parser_stack(yypParser);
717 if( yypParser->yyidx < 0 || yymajor==0 ){
718 yy_destructor((YYCODETYPE)yymajor,&yyminorunion);
719 yy_parse_failed(yypParser);
721 }else if( yymx!=YYERRORSYMBOL ){
724 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
727 yypParser->yyerrcnt = 3;
729 #else /* YYERRORSYMBOL is not defined */
730 /* This is what we do if the grammar does not define ERROR:
732 ** * Report an error message, and throw away the input token.
734 ** * If the input token is $, then fail the parse.
736 ** As before, subsequent error messages are suppressed until
737 ** three input tokens have been successfully shifted.
739 if( yypParser->yyerrcnt<=0 ){
740 yy_syntax_error(yypParser,yymajor,yyminorunion);
742 yypParser->yyerrcnt = 3;
743 yy_destructor(yymajor,&yyminorunion);
745 yy_parse_failed(yypParser);
750 yy_accept(yypParser);
753 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );