blob: f5fafd4c28b583058a3267ca69fb07dfc03df969 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/* Driver template for the LEMON parser generator.
drhb19a2bc2001-09-16 00:13:26 +00002** The author disclaims copyright to this source code.
drh75897232000-05-29 14:26:00 +00003*/
drh34ff57b2008-07-14 12:27:51 +00004/* First off, code is included that follows the "include" declaration
5** in the input grammar file. */
drh75897232000-05-29 14:26:00 +00006#include <stdio.h>
7%%
8/* Next is all token values, in a form suitable for use by makeheaders.
9** This section will be null unless lemon is run with the -m switch.
10*/
11/*
12** These constants (all generated automatically by the parser generator)
13** specify the various kinds of tokens (terminals) that the parser
14** understands.
15**
16** Each symbol here is a terminal symbol in the grammar.
17*/
18%%
19/* Make sure the INTERFACE macro is defined.
20*/
21#ifndef INTERFACE
22# define INTERFACE 1
23#endif
24/* The next thing included is series of defines which control
25** various aspects of the generated parser.
26** YYCODETYPE is the data type used for storing terminal
27** and nonterminal numbers. "unsigned char" is
28** used if there are fewer than 250 terminals
29** and nonterminals. "int" is used otherwise.
30** YYNOCODE is a number of type YYCODETYPE which corresponds
31** to no legal terminal or nonterminal number. This
32** number is used to fill in empty slots of the hash
33** table.
drh0bd1f4e2002-06-06 18:54:39 +000034** YYFALLBACK If defined, this indicates that one or more tokens
35** have fall-back values which should be used if the
36** original value of the token will not parse.
drh75897232000-05-29 14:26:00 +000037** YYACTIONTYPE is the data type used for storing terminal
38** and nonterminal numbers. "unsigned char" is
39** used if there are fewer than 250 rules and
40** states combined. "int" is used otherwise.
41** ParseTOKENTYPE is the data type used for minor tokens given
42** directly to the parser from the tokenizer.
43** YYMINORTYPE is the data type used for all minor tokens.
44** This is typically a union of many types, one of
45** which is ParseTOKENTYPE. The entry in the union
46** for base tokens is called "yy0".
drhb19fd012007-03-29 01:44:45 +000047** YYSTACKDEPTH is the maximum depth of the parser's stack. If
48** zero the stack is dynamically sized using realloc()
drh1f245e42002-03-11 13:55:50 +000049** ParseARG_SDECL A static variable declaration for the %extra_argument
50** ParseARG_PDECL A parameter declaration for the %extra_argument
51** ParseARG_STORE Code to store %extra_argument into yypParser
52** ParseARG_FETCH Code to extract %extra_argument from yypParser
drh75897232000-05-29 14:26:00 +000053** YYNSTATE the combined number of states.
54** YYNRULE the number of rules in the grammar
55** YYERRORSYMBOL is the code number of the error symbol. If not
56** defined, then do no error processing.
57*/
58%%
59#define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
60#define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
61#define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
drh75897232000-05-29 14:26:00 +000062
drh26c9b5e2008-04-11 14:56:53 +000063/* The yyzerominor constant is used to initialize instances of
64** YYMINORTYPE objects to zero. */
65static const YYMINORTYPE yyzerominor;
66
drh34ff57b2008-07-14 12:27:51 +000067/* Next are the tables used to determine what action to take based on the
drh8b582012003-10-21 13:16:03 +000068** current state and lookahead token. These tables are used to implement
69** functions that take a state number and lookahead value and return an
70** action integer.
drh75897232000-05-29 14:26:00 +000071**
drh8548a052003-10-22 22:15:27 +000072** Suppose the action integer is N. Then the action is determined as
73** follows
drh75897232000-05-29 14:26:00 +000074**
drh8548a052003-10-22 22:15:27 +000075** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
76** token onto the stack and goto state N.
77**
78** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
79**
80** N == YYNSTATE+YYNRULE A syntax error has occurred.
81**
82** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
83**
84** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
85** slots in the yy_action[] table.
86**
87** The action table is constructed as a single large table named yy_action[].
88** Given state S and lookahead X, the action is computed as
drh75897232000-05-29 14:26:00 +000089**
drh8b582012003-10-21 13:16:03 +000090** yy_action[ yy_shift_ofst[S] + X ]
91**
drh8548a052003-10-22 22:15:27 +000092** If the index value yy_shift_ofst[S]+X is out of range or if the value
drh8b582012003-10-21 13:16:03 +000093** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
94** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
95** and that yy_default[S] should be used instead.
96**
97** The formula above is for computing the action when the lookahead is
98** a terminal symbol. If the lookahead is a non-terminal (as occurs after
99** a reduce action) then the yy_reduce_ofst[] array is used in place of
100** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
101** YY_SHIFT_USE_DFLT.
102**
103** The following are the tables generated in this section:
104**
105** yy_action[] A single table containing all actions.
106** yy_lookahead[] A table containing the lookahead for each entry in
107** yy_action. Used to detect hash collisions.
108** yy_shift_ofst[] For each state, the offset into yy_action for
109** shifting terminals.
110** yy_reduce_ofst[] For each state, the offset into yy_action for
111** shifting non-terminals after a reduce.
112** yy_default[] Default action for each state.
drh75897232000-05-29 14:26:00 +0000113*/
drh75897232000-05-29 14:26:00 +0000114%%
drhd9f291e2006-06-13 11:15:47 +0000115#define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0]))
drh75897232000-05-29 14:26:00 +0000116
drh0bd1f4e2002-06-06 18:54:39 +0000117/* The next table maps tokens into fallback tokens. If a construct
118** like the following:
119**
120** %fallback ID X Y Z.
121**
drh34ff57b2008-07-14 12:27:51 +0000122** appears in the grammar, then ID becomes a fallback token for X, Y,
drh0bd1f4e2002-06-06 18:54:39 +0000123** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
124** but it does not parse, the type of the token is changed to ID and
125** the parse is retried before an error is thrown.
126*/
127#ifdef YYFALLBACK
128static const YYCODETYPE yyFallback[] = {
129%%
130};
131#endif /* YYFALLBACK */
132
drh75897232000-05-29 14:26:00 +0000133/* The following structure represents a single element of the
134** parser's stack. Information stored includes:
135**
136** + The state number for the parser at this level of the stack.
137**
138** + The value of the token stored at this level of the stack.
139** (In other words, the "major" token.)
140**
141** + The semantic value stored at this level of the stack. This is
142** the information used by the action routines in the grammar.
143** It is sometimes called the "minor" token.
144*/
145struct yyStackEntry {
drh2abcd582008-07-24 23:34:07 +0000146 YYACTIONTYPE stateno; /* The state-number */
147 YYCODETYPE major; /* The major token value. This is the code
148 ** number for the token at this stack level */
149 YYMINORTYPE minor; /* The user-supplied minor token value. This
150 ** is the value of the token */
drh75897232000-05-29 14:26:00 +0000151};
drh1f245e42002-03-11 13:55:50 +0000152typedef struct yyStackEntry yyStackEntry;
drh75897232000-05-29 14:26:00 +0000153
154/* The state of the parser is completely contained in an instance of
155** the following structure */
156struct yyParser {
drh1f245e42002-03-11 13:55:50 +0000157 int yyidx; /* Index of top element in stack */
drhec424a52008-07-25 15:39:03 +0000158#ifdef YYTRACKMAXSTACKDEPTH
159 int yyidxMax; /* Maximum value of yyidx */
160#endif
drh1f245e42002-03-11 13:55:50 +0000161 int yyerrcnt; /* Shifts left before out of the error */
drh1f245e42002-03-11 13:55:50 +0000162 ParseARG_SDECL /* A place to hold %extra_argument */
drhb19fd012007-03-29 01:44:45 +0000163#if YYSTACKDEPTH<=0
164 int yystksz; /* Current side of the stack */
165 yyStackEntry *yystack; /* The parser's stack */
166#else
drh1f245e42002-03-11 13:55:50 +0000167 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
drhb19fd012007-03-29 01:44:45 +0000168#endif
drh75897232000-05-29 14:26:00 +0000169};
170typedef struct yyParser yyParser;
171
172#ifndef NDEBUG
173#include <stdio.h>
174static FILE *yyTraceFILE = 0;
175static char *yyTracePrompt = 0;
drh0bd1f4e2002-06-06 18:54:39 +0000176#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000177
drh0bd1f4e2002-06-06 18:54:39 +0000178#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000179/*
180** Turn parser tracing on by giving a stream to which to write the trace
181** and a prompt to preface each trace message. Tracing is turned off
182** by making either argument NULL
183**
184** Inputs:
185** <ul>
186** <li> A FILE* to which trace output should be written.
187** If NULL, then tracing is turned off.
188** <li> A prefix string written at the beginning of every
189** line of trace output. If NULL, then tracing is
190** turned off.
191** </ul>
192**
193** Outputs:
194** None.
195*/
drh75897232000-05-29 14:26:00 +0000196void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
197 yyTraceFILE = TraceFILE;
198 yyTracePrompt = zTracePrompt;
199 if( yyTraceFILE==0 ) yyTracePrompt = 0;
200 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
201}
drh0bd1f4e2002-06-06 18:54:39 +0000202#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000203
drh0bd1f4e2002-06-06 18:54:39 +0000204#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000205/* For tracing shifts, the names of all terminals and nonterminals
206** are required. The following table supplies these names */
drh57196282004-10-06 15:41:16 +0000207static const char *const yyTokenName[] = {
drh75897232000-05-29 14:26:00 +0000208%%
209};
drh0bd1f4e2002-06-06 18:54:39 +0000210#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000211
drh0bd1f4e2002-06-06 18:54:39 +0000212#ifndef NDEBUG
213/* For tracing reduce actions, the names of all rules are required.
214*/
drh57196282004-10-06 15:41:16 +0000215static const char *const yyRuleName[] = {
drh0bd1f4e2002-06-06 18:54:39 +0000216%%
217};
218#endif /* NDEBUG */
drha1b351a2001-09-14 16:42:12 +0000219
drh960e8c62001-04-03 16:53:21 +0000220
drhb19fd012007-03-29 01:44:45 +0000221#if YYSTACKDEPTH<=0
222/*
223** Try to increase the size of the parser stack.
224*/
225static void yyGrowStack(yyParser *p){
226 int newSize;
227 yyStackEntry *pNew;
228
229 newSize = p->yystksz*2 + 100;
230 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
231 if( pNew ){
232 p->yystack = pNew;
233 p->yystksz = newSize;
234#ifndef NDEBUG
235 if( yyTraceFILE ){
236 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
237 yyTracePrompt, p->yystksz);
238 }
239#endif
240 }
241}
242#endif
243
drh75897232000-05-29 14:26:00 +0000244/*
245** This function allocates a new parser.
246** The only argument is a pointer to a function which works like
247** malloc.
248**
249** Inputs:
250** A pointer to the function used to allocate memory.
251**
252** Outputs:
253** A pointer to a parser. This pointer is used in subsequent calls
254** to Parse and ParseFree.
255*/
drh7218ac72002-03-10 21:21:00 +0000256void *ParseAlloc(void *(*mallocProc)(size_t)){
drh75897232000-05-29 14:26:00 +0000257 yyParser *pParser;
drh7218ac72002-03-10 21:21:00 +0000258 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
drh75897232000-05-29 14:26:00 +0000259 if( pParser ){
drh1f245e42002-03-11 13:55:50 +0000260 pParser->yyidx = -1;
drhec424a52008-07-25 15:39:03 +0000261#ifdef YYTRACKMAXSTACKDEPTH
262 pParser->yyidxMax = 0;
263#endif
drhb19fd012007-03-29 01:44:45 +0000264#if YYSTACKDEPTH<=0
265 yyGrowStack(pParser);
266#endif
drh75897232000-05-29 14:26:00 +0000267 }
268 return pParser;
269}
270
271/* The following function deletes the value associated with a
272** symbol. The symbol can be either a terminal or nonterminal.
273** "yymajor" is the symbol code, and "yypminor" is a pointer to
274** the value.
275*/
drh633e6d52008-07-28 19:34:53 +0000276static void yy_destructor(
277 yyParser *yypParser, /* The parser */
278 YYCODETYPE yymajor, /* Type code for object to destroy */
279 YYMINORTYPE *yypminor /* The object to be destroyed */
280){
281 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000282 switch( yymajor ){
283 /* Here is inserted the actions which take place when a
284 ** terminal or non-terminal is destroyed. This can happen
285 ** when the symbol is popped from the stack during a
286 ** reduce or during error processing or when a parser is
287 ** being destroyed before it is finished parsing.
288 **
289 ** Note: during a reduce, the only symbols destroyed are those
290 ** which appear on the RHS of the rule, but which are not used
291 ** inside the C code.
292 */
293%%
294 default: break; /* If no destructor action specified: do nothing */
295 }
296}
297
298/*
299** Pop the parser's stack once.
300**
301** If there is a destructor routine associated with the token which
302** is popped from the stack, then call it.
303**
304** Return the major token number for the symbol popped.
305*/
306static int yy_pop_parser_stack(yyParser *pParser){
307 YYCODETYPE yymajor;
drh3ddfdf72003-12-22 14:53:19 +0000308 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
drh75897232000-05-29 14:26:00 +0000309
drh1f245e42002-03-11 13:55:50 +0000310 if( pParser->yyidx<0 ) return 0;
drh75897232000-05-29 14:26:00 +0000311#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000312 if( yyTraceFILE && pParser->yyidx>=0 ){
drh75897232000-05-29 14:26:00 +0000313 fprintf(yyTraceFILE,"%sPopping %s\n",
314 yyTracePrompt,
drh3ddfdf72003-12-22 14:53:19 +0000315 yyTokenName[yytos->major]);
drh75897232000-05-29 14:26:00 +0000316 }
317#endif
drh3ddfdf72003-12-22 14:53:19 +0000318 yymajor = yytos->major;
drh633e6d52008-07-28 19:34:53 +0000319 yy_destructor(pParser, yymajor, &yytos->minor);
drh1f245e42002-03-11 13:55:50 +0000320 pParser->yyidx--;
drh75897232000-05-29 14:26:00 +0000321 return yymajor;
322}
323
324/*
325** Deallocate and destroy a parser. Destructors are all called for
326** all stack elements before shutting the parser down.
327**
328** Inputs:
329** <ul>
330** <li> A pointer to the parser. This should be a pointer
331** obtained from ParseAlloc.
332** <li> A pointer to a function used to reclaim memory obtained
333** from malloc.
334** </ul>
335*/
drh75897232000-05-29 14:26:00 +0000336void ParseFree(
drh960e8c62001-04-03 16:53:21 +0000337 void *p, /* The parser to be deleted */
338 void (*freeProc)(void*) /* Function used to reclaim memory */
drh75897232000-05-29 14:26:00 +0000339){
340 yyParser *pParser = (yyParser*)p;
341 if( pParser==0 ) return;
drh1f245e42002-03-11 13:55:50 +0000342 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
drhb19fd012007-03-29 01:44:45 +0000343#if YYSTACKDEPTH<=0
344 free(pParser->yystack);
345#endif
drh960e8c62001-04-03 16:53:21 +0000346 (*freeProc)((void*)pParser);
drh75897232000-05-29 14:26:00 +0000347}
348
349/*
drhec424a52008-07-25 15:39:03 +0000350** Return the peak depth of the stack for a parser.
351*/
352#ifdef YYTRACKMAXSTACKDEPTH
353int ParseStackPeak(void *p){
354 yyParser *pParser = (yyParser*)p;
355 return pParser->yyidxMax;
356}
357#endif
358
359/*
drh8b582012003-10-21 13:16:03 +0000360** Find the appropriate action for a parser given the terminal
361** look-ahead token iLookAhead.
drh75897232000-05-29 14:26:00 +0000362**
363** If the look-ahead token is YYNOCODE, then check to see if the action is
364** independent of the look-ahead. If it is, return the action, otherwise
365** return YY_NO_ACTION.
366*/
drh8b582012003-10-21 13:16:03 +0000367static int yy_find_shift_action(
drh75897232000-05-29 14:26:00 +0000368 yyParser *pParser, /* The parser */
drhd9f291e2006-06-13 11:15:47 +0000369 YYCODETYPE iLookAhead /* The look-ahead token */
drh75897232000-05-29 14:26:00 +0000370){
drh8b582012003-10-21 13:16:03 +0000371 int i;
drh3ddfdf72003-12-22 14:53:19 +0000372 int stateno = pParser->yystack[pParser->yyidx].stateno;
drh75897232000-05-29 14:26:00 +0000373
drhada354d2005-11-05 15:03:59 +0000374 if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
drh3ddfdf72003-12-22 14:53:19 +0000375 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000376 }
drh01495b92008-01-23 12:52:40 +0000377 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000378 i += iLookAhead;
379 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
drhe09daa92006-06-10 13:29:31 +0000380 if( iLookAhead>0 ){
drh4767d972006-06-14 15:03:49 +0000381#ifdef YYFALLBACK
drhe09daa92006-06-10 13:29:31 +0000382 int iFallback; /* Fallback token */
383 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
384 && (iFallback = yyFallback[iLookAhead])!=0 ){
drh4767d972006-06-14 15:03:49 +0000385#ifndef NDEBUG
drhe09daa92006-06-10 13:29:31 +0000386 if( yyTraceFILE ){
387 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
388 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
389 }
drh4767d972006-06-14 15:03:49 +0000390#endif
drhe09daa92006-06-10 13:29:31 +0000391 return yy_find_shift_action(pParser, iFallback);
drh0bd1f4e2002-06-06 18:54:39 +0000392 }
drh4767d972006-06-14 15:03:49 +0000393#endif
394#ifdef YYWILDCARD
395 {
396 int j = i - iLookAhead + YYWILDCARD;
397 if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){
398#ifndef NDEBUG
399 if( yyTraceFILE ){
400 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
401 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
402 }
403#endif /* NDEBUG */
404 return yy_action[j];
drhe09daa92006-06-10 13:29:31 +0000405 }
drhe09daa92006-06-10 13:29:31 +0000406 }
drh4767d972006-06-14 15:03:49 +0000407#endif /* YYWILDCARD */
drh0bd1f4e2002-06-06 18:54:39 +0000408 }
drh3ddfdf72003-12-22 14:53:19 +0000409 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000410 }else{
411 return yy_action[i];
412 }
413}
414
415/*
416** Find the appropriate action for a parser given the non-terminal
417** look-ahead token iLookAhead.
418**
419** If the look-ahead token is YYNOCODE, then check to see if the action is
420** independent of the look-ahead. If it is, return the action, otherwise
421** return YY_NO_ACTION.
422*/
423static int yy_find_reduce_action(
drh161aba32005-02-01 04:09:36 +0000424 int stateno, /* Current state number */
drhd9f291e2006-06-13 11:15:47 +0000425 YYCODETYPE iLookAhead /* The look-ahead token */
drh8b582012003-10-21 13:16:03 +0000426){
427 int i;
drh7f7c2572008-04-27 18:45:10 +0000428#ifdef YYERRORSYMBOL
429 if( stateno>YY_REDUCE_MAX ){
430 return yy_default[stateno];
431 }
432#else
drh01495b92008-01-23 12:52:40 +0000433 assert( stateno<=YY_REDUCE_MAX );
drh7f7c2572008-04-27 18:45:10 +0000434#endif
drh01495b92008-01-23 12:52:40 +0000435 i = yy_reduce_ofst[stateno];
436 assert( i!=YY_REDUCE_USE_DFLT );
437 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000438 i += iLookAhead;
drh7f7c2572008-04-27 18:45:10 +0000439#ifdef YYERRORSYMBOL
440 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
441 return yy_default[stateno];
442 }
443#else
drh01495b92008-01-23 12:52:40 +0000444 assert( i>=0 && i<YY_SZ_ACTTAB );
445 assert( yy_lookahead[i]==iLookAhead );
drh7f7c2572008-04-27 18:45:10 +0000446#endif
drh01495b92008-01-23 12:52:40 +0000447 return yy_action[i];
drh75897232000-05-29 14:26:00 +0000448}
449
450/*
drhb19fd012007-03-29 01:44:45 +0000451** The following routine is called if the stack overflows.
452*/
drhb6018612007-03-30 13:35:05 +0000453static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
drhb19fd012007-03-29 01:44:45 +0000454 ParseARG_FETCH;
455 yypParser->yyidx--;
456#ifndef NDEBUG
457 if( yyTraceFILE ){
458 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
459 }
460#endif
461 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
462 /* Here code is inserted which will execute if the parser
463 ** stack every overflows */
464%%
465 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
466}
467
468/*
drh75897232000-05-29 14:26:00 +0000469** Perform a shift action.
470*/
471static void yy_shift(
472 yyParser *yypParser, /* The parser to be shifted */
473 int yyNewState, /* The new state to shift in */
474 int yyMajor, /* The major token to shift in */
drh34ff57b2008-07-14 12:27:51 +0000475 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
drh75897232000-05-29 14:26:00 +0000476){
drh3ddfdf72003-12-22 14:53:19 +0000477 yyStackEntry *yytos;
drh1f245e42002-03-11 13:55:50 +0000478 yypParser->yyidx++;
drhec424a52008-07-25 15:39:03 +0000479#ifdef YYTRACKMAXSTACKDEPTH
480 if( yypParser->yyidx>yypParser->yyidxMax ){
481 yypParser->yyidxMax = yypParser->yyidx;
482 }
483#endif
drhb19fd012007-03-29 01:44:45 +0000484#if YYSTACKDEPTH>0
drh1f245e42002-03-11 13:55:50 +0000485 if( yypParser->yyidx>=YYSTACKDEPTH ){
drhb6018612007-03-30 13:35:05 +0000486 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000487 return;
drh75897232000-05-29 14:26:00 +0000488 }
drhb19fd012007-03-29 01:44:45 +0000489#else
490 if( yypParser->yyidx>=yypParser->yystksz ){
491 yyGrowStack(yypParser);
492 if( yypParser->yyidx>=yypParser->yystksz ){
drhb6018612007-03-30 13:35:05 +0000493 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000494 return;
495 }
496 }
497#endif
drh3ddfdf72003-12-22 14:53:19 +0000498 yytos = &yypParser->yystack[yypParser->yyidx];
499 yytos->stateno = yyNewState;
500 yytos->major = yyMajor;
501 yytos->minor = *yypMinor;
drh75897232000-05-29 14:26:00 +0000502#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000503 if( yyTraceFILE && yypParser->yyidx>0 ){
drh75897232000-05-29 14:26:00 +0000504 int i;
505 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
506 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
drh1f245e42002-03-11 13:55:50 +0000507 for(i=1; i<=yypParser->yyidx; i++)
508 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
drh75897232000-05-29 14:26:00 +0000509 fprintf(yyTraceFILE,"\n");
510 }
511#endif
512}
513
514/* The following table contains information about every rule that
515** is used during the reduce.
516*/
drh57196282004-10-06 15:41:16 +0000517static const struct {
drh75897232000-05-29 14:26:00 +0000518 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
519 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
520} yyRuleInfo[] = {
521%%
522};
523
drh1f245e42002-03-11 13:55:50 +0000524static void yy_accept(yyParser*); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000525
526/*
527** Perform a reduce action and the shift that must immediately
528** follow the reduce.
529*/
530static void yy_reduce(
531 yyParser *yypParser, /* The parser */
532 int yyruleno /* Number of the rule by which to reduce */
drh75897232000-05-29 14:26:00 +0000533){
534 int yygoto; /* The next state */
535 int yyact; /* The next action */
536 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
drh1f245e42002-03-11 13:55:50 +0000537 yyStackEntry *yymsp; /* The top of the parser's stack */
drh75897232000-05-29 14:26:00 +0000538 int yysize; /* Amount to pop the stack */
drh1f245e42002-03-11 13:55:50 +0000539 ParseARG_FETCH;
drh3ddfdf72003-12-22 14:53:19 +0000540 yymsp = &yypParser->yystack[yypParser->yyidx];
drh0bd1f4e2002-06-06 18:54:39 +0000541#ifndef NDEBUG
542 if( yyTraceFILE && yyruleno>=0
drhd9f291e2006-06-13 11:15:47 +0000543 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
drh0bd1f4e2002-06-06 18:54:39 +0000544 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
545 yyRuleName[yyruleno]);
546 }
547#endif /* NDEBUG */
548
drh7bec5052005-02-06 02:45:41 +0000549 /* Silence complaints from purify about yygotominor being uninitialized
550 ** in some cases when it is copied into the stack after the following
551 ** switch. yygotominor is uninitialized when a rule reduces that does
552 ** not set the value of its left-hand side nonterminal. Leaving the
553 ** value of the nonterminal uninitialized is utterly harmless as long
554 ** as the value is never used. So really the only thing this code
555 ** accomplishes is to quieten purify.
drhcb6c5652007-01-16 18:19:12 +0000556 **
557 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
558 ** without this code, their parser segfaults. I'm not sure what there
559 ** parser is doing to make this happen. This is the second bug report
560 ** from wireshark this week. Clearly they are stressing Lemon in ways
561 ** that it has not been previously stressed... (SQLite ticket #2172)
drh7bec5052005-02-06 02:45:41 +0000562 */
drh26c9b5e2008-04-11 14:56:53 +0000563 /*memset(&yygotominor, 0, sizeof(yygotominor));*/
564 yygotominor = yyzerominor;
drhcb6c5652007-01-16 18:19:12 +0000565
drh7bec5052005-02-06 02:45:41 +0000566
drh75897232000-05-29 14:26:00 +0000567 switch( yyruleno ){
568 /* Beginning here are the reduction cases. A typical example
569 ** follows:
570 ** case 0:
drh75897232000-05-29 14:26:00 +0000571 ** #line <lineno> <grammarfile>
572 ** { ... } // User supplied code
573 ** #line <lineno> <thisfile>
574 ** break;
575 */
576%%
577 };
578 yygoto = yyRuleInfo[yyruleno].lhs;
579 yysize = yyRuleInfo[yyruleno].nrhs;
drh1f245e42002-03-11 13:55:50 +0000580 yypParser->yyidx -= yysize;
drh161aba32005-02-01 04:09:36 +0000581 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,yygoto);
drh75897232000-05-29 14:26:00 +0000582 if( yyact < YYNSTATE ){
drh490a73b2005-02-01 03:20:00 +0000583#ifdef NDEBUG
584 /* If we are not debugging and the reduce action popped at least
585 ** one element off the stack, then we can push the new element back
586 ** onto the stack here, and skip the stack overflow test in yy_shift().
587 ** That gives a significant speed improvement. */
588 if( yysize ){
589 yypParser->yyidx++;
590 yymsp -= yysize-1;
591 yymsp->stateno = yyact;
592 yymsp->major = yygoto;
593 yymsp->minor = yygotominor;
594 }else
595#endif
596 {
597 yy_shift(yypParser,yyact,yygoto,&yygotominor);
598 }
drh4b2f9362008-01-22 23:37:09 +0000599 }else{
600 assert( yyact == YYNSTATE + YYNRULE + 1 );
drh1f245e42002-03-11 13:55:50 +0000601 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000602 }
603}
604
605/*
606** The following code executes when the parse fails
607*/
608static void yy_parse_failed(
609 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000610){
drh1f245e42002-03-11 13:55:50 +0000611 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000612#ifndef NDEBUG
613 if( yyTraceFILE ){
614 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
615 }
616#endif
drh1f245e42002-03-11 13:55:50 +0000617 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000618 /* Here code is inserted which will be executed whenever the
619 ** parser fails */
620%%
drh1f245e42002-03-11 13:55:50 +0000621 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000622}
623
624/*
625** The following code executes when a syntax error first occurs.
626*/
627static void yy_syntax_error(
628 yyParser *yypParser, /* The parser */
629 int yymajor, /* The major type of the error token */
630 YYMINORTYPE yyminor /* The minor type of the error token */
drh75897232000-05-29 14:26:00 +0000631){
drh1f245e42002-03-11 13:55:50 +0000632 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000633#define TOKEN (yyminor.yy0)
634%%
drh1f245e42002-03-11 13:55:50 +0000635 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000636}
637
638/*
639** The following is executed when the parser accepts
640*/
641static void yy_accept(
642 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000643){
drh1f245e42002-03-11 13:55:50 +0000644 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000645#ifndef NDEBUG
646 if( yyTraceFILE ){
647 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
648 }
649#endif
drh1f245e42002-03-11 13:55:50 +0000650 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000651 /* Here code is inserted which will be executed whenever the
652 ** parser accepts */
653%%
drh1f245e42002-03-11 13:55:50 +0000654 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000655}
656
657/* The main parser program.
658** The first argument is a pointer to a structure obtained from
659** "ParseAlloc" which describes the current state of the parser.
660** The second argument is the major token number. The third is
661** the minor token. The fourth optional argument is whatever the
662** user wants (and specified in the grammar) and is available for
663** use by the action routines.
664**
665** Inputs:
666** <ul>
667** <li> A pointer to the parser (an opaque structure.)
668** <li> The major token number.
669** <li> The minor token number.
670** <li> An option argument of a grammar-specified type.
671** </ul>
672**
673** Outputs:
674** None.
675*/
drh75897232000-05-29 14:26:00 +0000676void Parse(
677 void *yyp, /* The parser */
678 int yymajor, /* The major token code number */
679 ParseTOKENTYPE yyminor /* The value for the token */
drh1f245e42002-03-11 13:55:50 +0000680 ParseARG_PDECL /* Optional %extra_argument parameter */
drh75897232000-05-29 14:26:00 +0000681){
682 YYMINORTYPE yyminorunion;
683 int yyact; /* The parser action. */
684 int yyendofinput; /* True if we are at the end of input */
drhc4dd3fd2008-01-22 01:48:05 +0000685#ifdef YYERRORSYMBOL
drh75897232000-05-29 14:26:00 +0000686 int yyerrorhit = 0; /* True if yymajor has invoked an error */
drhc4dd3fd2008-01-22 01:48:05 +0000687#endif
drh75897232000-05-29 14:26:00 +0000688 yyParser *yypParser; /* The parser */
689
690 /* (re)initialize the parser, if necessary */
691 yypParser = (yyParser*)yyp;
drh1f245e42002-03-11 13:55:50 +0000692 if( yypParser->yyidx<0 ){
drhb7bac722007-03-29 02:26:45 +0000693#if YYSTACKDEPTH<=0
drhb19fd012007-03-29 01:44:45 +0000694 if( yypParser->yystksz <=0 ){
drh26c9b5e2008-04-11 14:56:53 +0000695 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
696 yyminorunion = yyzerominor;
drhb6018612007-03-30 13:35:05 +0000697 yyStackOverflow(yypParser, &yyminorunion);
drhb19fd012007-03-29 01:44:45 +0000698 return;
699 }
drhb7bac722007-03-29 02:26:45 +0000700#endif
drh1f245e42002-03-11 13:55:50 +0000701 yypParser->yyidx = 0;
702 yypParser->yyerrcnt = -1;
drh3ddfdf72003-12-22 14:53:19 +0000703 yypParser->yystack[0].stateno = 0;
704 yypParser->yystack[0].major = 0;
drh75897232000-05-29 14:26:00 +0000705 }
706 yyminorunion.yy0 = yyminor;
707 yyendofinput = (yymajor==0);
drh1f245e42002-03-11 13:55:50 +0000708 ParseARG_STORE;
drh75897232000-05-29 14:26:00 +0000709
710#ifndef NDEBUG
711 if( yyTraceFILE ){
712 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
713 }
714#endif
715
716 do{
drh8b582012003-10-21 13:16:03 +0000717 yyact = yy_find_shift_action(yypParser,yymajor);
drh75897232000-05-29 14:26:00 +0000718 if( yyact<YYNSTATE ){
drh55176252008-01-22 14:50:16 +0000719 assert( !yyendofinput ); /* Impossible to shift the $ token */
drh75897232000-05-29 14:26:00 +0000720 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000721 yypParser->yyerrcnt--;
drh55176252008-01-22 14:50:16 +0000722 yymajor = YYNOCODE;
drh75897232000-05-29 14:26:00 +0000723 }else if( yyact < YYNSTATE + YYNRULE ){
drh1f245e42002-03-11 13:55:50 +0000724 yy_reduce(yypParser,yyact-YYNSTATE);
drhc4dd3fd2008-01-22 01:48:05 +0000725 }else{
726 assert( yyact == YY_ERROR_ACTION );
727#ifdef YYERRORSYMBOL
drh3ddfdf72003-12-22 14:53:19 +0000728 int yymx;
drhc4dd3fd2008-01-22 01:48:05 +0000729#endif
drh75897232000-05-29 14:26:00 +0000730#ifndef NDEBUG
731 if( yyTraceFILE ){
732 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
733 }
734#endif
735#ifdef YYERRORSYMBOL
736 /* A syntax error has occurred.
737 ** The response to an error depends upon whether or not the
738 ** grammar defines an error token "ERROR".
739 **
740 ** This is what we do if the grammar does define ERROR:
741 **
742 ** * Call the %syntax_error function.
743 **
744 ** * Begin popping the stack until we enter a state where
745 ** it is legal to shift the error symbol, then shift
746 ** the error symbol.
747 **
748 ** * Set the error count to three.
749 **
750 ** * Begin accepting and shifting new tokens. No new error
751 ** processing will occur until three tokens have been
752 ** shifted successfully.
753 **
754 */
drh1f245e42002-03-11 13:55:50 +0000755 if( yypParser->yyerrcnt<0 ){
756 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000757 }
drh3ddfdf72003-12-22 14:53:19 +0000758 yymx = yypParser->yystack[yypParser->yyidx].major;
759 if( yymx==YYERRORSYMBOL || yyerrorhit ){
drh75897232000-05-29 14:26:00 +0000760#ifndef NDEBUG
761 if( yyTraceFILE ){
762 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
763 yyTracePrompt,yyTokenName[yymajor]);
764 }
765#endif
drh633e6d52008-07-28 19:34:53 +0000766 yy_destructor(yypParser, yymajor,&yyminorunion);
drh75897232000-05-29 14:26:00 +0000767 yymajor = YYNOCODE;
768 }else{
769 while(
drh1f245e42002-03-11 13:55:50 +0000770 yypParser->yyidx >= 0 &&
drh3ddfdf72003-12-22 14:53:19 +0000771 yymx != YYERRORSYMBOL &&
drhf3a58882006-05-08 15:14:19 +0000772 (yyact = yy_find_reduce_action(
773 yypParser->yystack[yypParser->yyidx].stateno,
774 YYERRORSYMBOL)) >= YYNSTATE
drh75897232000-05-29 14:26:00 +0000775 ){
776 yy_pop_parser_stack(yypParser);
777 }
drh1f245e42002-03-11 13:55:50 +0000778 if( yypParser->yyidx < 0 || yymajor==0 ){
drh633e6d52008-07-28 19:34:53 +0000779 yy_destructor(yypParser,yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000780 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000781 yymajor = YYNOCODE;
drh3ddfdf72003-12-22 14:53:19 +0000782 }else if( yymx!=YYERRORSYMBOL ){
drh75897232000-05-29 14:26:00 +0000783 YYMINORTYPE u2;
784 u2.YYERRSYMDT = 0;
785 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
786 }
787 }
drh1f245e42002-03-11 13:55:50 +0000788 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000789 yyerrorhit = 1;
790#else /* YYERRORSYMBOL is not defined */
791 /* This is what we do if the grammar does not define ERROR:
792 **
793 ** * Report an error message, and throw away the input token.
794 **
795 ** * If the input token is $, then fail the parse.
796 **
797 ** As before, subsequent error messages are suppressed until
798 ** three input tokens have been successfully shifted.
799 */
drh1f245e42002-03-11 13:55:50 +0000800 if( yypParser->yyerrcnt<=0 ){
801 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000802 }
drh1f245e42002-03-11 13:55:50 +0000803 yypParser->yyerrcnt = 3;
drh633e6d52008-07-28 19:34:53 +0000804 yy_destructor(yypParser,yymajor,&yyminorunion);
drh75897232000-05-29 14:26:00 +0000805 if( yyendofinput ){
drh1f245e42002-03-11 13:55:50 +0000806 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000807 }
808 yymajor = YYNOCODE;
809#endif
drh75897232000-05-29 14:26:00 +0000810 }
drh1f245e42002-03-11 13:55:50 +0000811 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
drh75897232000-05-29 14:26:00 +0000812 return;
813}