blob: 0992ecef0f84790949b3c1c815a4936f4beddc56 [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 {
146 int stateno; /* The state-number */
147 int 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 */
151};
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 */
158 int yyerrcnt; /* Shifts left before out of the error */
drh1f245e42002-03-11 13:55:50 +0000159 ParseARG_SDECL /* A place to hold %extra_argument */
drhb19fd012007-03-29 01:44:45 +0000160#if YYSTACKDEPTH<=0
161 int yystksz; /* Current side of the stack */
162 yyStackEntry *yystack; /* The parser's stack */
163#else
drh1f245e42002-03-11 13:55:50 +0000164 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
drhb19fd012007-03-29 01:44:45 +0000165#endif
drh75897232000-05-29 14:26:00 +0000166};
167typedef struct yyParser yyParser;
168
169#ifndef NDEBUG
170#include <stdio.h>
171static FILE *yyTraceFILE = 0;
172static char *yyTracePrompt = 0;
drh0bd1f4e2002-06-06 18:54:39 +0000173#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000174
drh0bd1f4e2002-06-06 18:54:39 +0000175#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000176/*
177** Turn parser tracing on by giving a stream to which to write the trace
178** and a prompt to preface each trace message. Tracing is turned off
179** by making either argument NULL
180**
181** Inputs:
182** <ul>
183** <li> A FILE* to which trace output should be written.
184** If NULL, then tracing is turned off.
185** <li> A prefix string written at the beginning of every
186** line of trace output. If NULL, then tracing is
187** turned off.
188** </ul>
189**
190** Outputs:
191** None.
192*/
drh75897232000-05-29 14:26:00 +0000193void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
194 yyTraceFILE = TraceFILE;
195 yyTracePrompt = zTracePrompt;
196 if( yyTraceFILE==0 ) yyTracePrompt = 0;
197 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
198}
drh0bd1f4e2002-06-06 18:54:39 +0000199#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000200
drh0bd1f4e2002-06-06 18:54:39 +0000201#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000202/* For tracing shifts, the names of all terminals and nonterminals
203** are required. The following table supplies these names */
drh57196282004-10-06 15:41:16 +0000204static const char *const yyTokenName[] = {
drh75897232000-05-29 14:26:00 +0000205%%
206};
drh0bd1f4e2002-06-06 18:54:39 +0000207#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000208
drh0bd1f4e2002-06-06 18:54:39 +0000209#ifndef NDEBUG
210/* For tracing reduce actions, the names of all rules are required.
211*/
drh57196282004-10-06 15:41:16 +0000212static const char *const yyRuleName[] = {
drh0bd1f4e2002-06-06 18:54:39 +0000213%%
214};
215#endif /* NDEBUG */
drha1b351a2001-09-14 16:42:12 +0000216
drh960e8c62001-04-03 16:53:21 +0000217
drhb19fd012007-03-29 01:44:45 +0000218#if YYSTACKDEPTH<=0
219/*
220** Try to increase the size of the parser stack.
221*/
222static void yyGrowStack(yyParser *p){
223 int newSize;
224 yyStackEntry *pNew;
225
226 newSize = p->yystksz*2 + 100;
227 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
228 if( pNew ){
229 p->yystack = pNew;
230 p->yystksz = newSize;
231#ifndef NDEBUG
232 if( yyTraceFILE ){
233 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
234 yyTracePrompt, p->yystksz);
235 }
236#endif
237 }
238}
239#endif
240
drh75897232000-05-29 14:26:00 +0000241/*
242** This function allocates a new parser.
243** The only argument is a pointer to a function which works like
244** malloc.
245**
246** Inputs:
247** A pointer to the function used to allocate memory.
248**
249** Outputs:
250** A pointer to a parser. This pointer is used in subsequent calls
251** to Parse and ParseFree.
252*/
drh7218ac72002-03-10 21:21:00 +0000253void *ParseAlloc(void *(*mallocProc)(size_t)){
drh75897232000-05-29 14:26:00 +0000254 yyParser *pParser;
drh7218ac72002-03-10 21:21:00 +0000255 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
drh75897232000-05-29 14:26:00 +0000256 if( pParser ){
drh1f245e42002-03-11 13:55:50 +0000257 pParser->yyidx = -1;
drhb19fd012007-03-29 01:44:45 +0000258#if YYSTACKDEPTH<=0
259 yyGrowStack(pParser);
260#endif
drh75897232000-05-29 14:26:00 +0000261 }
262 return pParser;
263}
264
265/* The following function deletes the value associated with a
266** symbol. The symbol can be either a terminal or nonterminal.
267** "yymajor" is the symbol code, and "yypminor" is a pointer to
268** the value.
269*/
270static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
271 switch( yymajor ){
272 /* Here is inserted the actions which take place when a
273 ** terminal or non-terminal is destroyed. This can happen
274 ** when the symbol is popped from the stack during a
275 ** reduce or during error processing or when a parser is
276 ** being destroyed before it is finished parsing.
277 **
278 ** Note: during a reduce, the only symbols destroyed are those
279 ** which appear on the RHS of the rule, but which are not used
280 ** inside the C code.
281 */
282%%
283 default: break; /* If no destructor action specified: do nothing */
284 }
285}
286
287/*
288** Pop the parser's stack once.
289**
290** If there is a destructor routine associated with the token which
291** is popped from the stack, then call it.
292**
293** Return the major token number for the symbol popped.
294*/
295static int yy_pop_parser_stack(yyParser *pParser){
296 YYCODETYPE yymajor;
drh3ddfdf72003-12-22 14:53:19 +0000297 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
drh75897232000-05-29 14:26:00 +0000298
drh1f245e42002-03-11 13:55:50 +0000299 if( pParser->yyidx<0 ) return 0;
drh75897232000-05-29 14:26:00 +0000300#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000301 if( yyTraceFILE && pParser->yyidx>=0 ){
drh75897232000-05-29 14:26:00 +0000302 fprintf(yyTraceFILE,"%sPopping %s\n",
303 yyTracePrompt,
drh3ddfdf72003-12-22 14:53:19 +0000304 yyTokenName[yytos->major]);
drh75897232000-05-29 14:26:00 +0000305 }
306#endif
drh3ddfdf72003-12-22 14:53:19 +0000307 yymajor = yytos->major;
308 yy_destructor( yymajor, &yytos->minor);
drh1f245e42002-03-11 13:55:50 +0000309 pParser->yyidx--;
drh75897232000-05-29 14:26:00 +0000310 return yymajor;
311}
312
313/*
314** Deallocate and destroy a parser. Destructors are all called for
315** all stack elements before shutting the parser down.
316**
317** Inputs:
318** <ul>
319** <li> A pointer to the parser. This should be a pointer
320** obtained from ParseAlloc.
321** <li> A pointer to a function used to reclaim memory obtained
322** from malloc.
323** </ul>
324*/
drh75897232000-05-29 14:26:00 +0000325void ParseFree(
drh960e8c62001-04-03 16:53:21 +0000326 void *p, /* The parser to be deleted */
327 void (*freeProc)(void*) /* Function used to reclaim memory */
drh75897232000-05-29 14:26:00 +0000328){
329 yyParser *pParser = (yyParser*)p;
330 if( pParser==0 ) return;
drh1f245e42002-03-11 13:55:50 +0000331 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
drhb19fd012007-03-29 01:44:45 +0000332#if YYSTACKDEPTH<=0
333 free(pParser->yystack);
334#endif
drh960e8c62001-04-03 16:53:21 +0000335 (*freeProc)((void*)pParser);
drh75897232000-05-29 14:26:00 +0000336}
337
338/*
drh8b582012003-10-21 13:16:03 +0000339** Find the appropriate action for a parser given the terminal
340** look-ahead token iLookAhead.
drh75897232000-05-29 14:26:00 +0000341**
342** If the look-ahead token is YYNOCODE, then check to see if the action is
343** independent of the look-ahead. If it is, return the action, otherwise
344** return YY_NO_ACTION.
345*/
drh8b582012003-10-21 13:16:03 +0000346static int yy_find_shift_action(
drh75897232000-05-29 14:26:00 +0000347 yyParser *pParser, /* The parser */
drhd9f291e2006-06-13 11:15:47 +0000348 YYCODETYPE iLookAhead /* The look-ahead token */
drh75897232000-05-29 14:26:00 +0000349){
drh8b582012003-10-21 13:16:03 +0000350 int i;
drh3ddfdf72003-12-22 14:53:19 +0000351 int stateno = pParser->yystack[pParser->yyidx].stateno;
drh75897232000-05-29 14:26:00 +0000352
drhada354d2005-11-05 15:03:59 +0000353 if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
drh3ddfdf72003-12-22 14:53:19 +0000354 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000355 }
drh01495b92008-01-23 12:52:40 +0000356 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000357 i += iLookAhead;
358 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
drhe09daa92006-06-10 13:29:31 +0000359 if( iLookAhead>0 ){
drh4767d972006-06-14 15:03:49 +0000360#ifdef YYFALLBACK
drhe09daa92006-06-10 13:29:31 +0000361 int iFallback; /* Fallback token */
362 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
363 && (iFallback = yyFallback[iLookAhead])!=0 ){
drh4767d972006-06-14 15:03:49 +0000364#ifndef NDEBUG
drhe09daa92006-06-10 13:29:31 +0000365 if( yyTraceFILE ){
366 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
367 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
368 }
drh4767d972006-06-14 15:03:49 +0000369#endif
drhe09daa92006-06-10 13:29:31 +0000370 return yy_find_shift_action(pParser, iFallback);
drh0bd1f4e2002-06-06 18:54:39 +0000371 }
drh4767d972006-06-14 15:03:49 +0000372#endif
373#ifdef YYWILDCARD
374 {
375 int j = i - iLookAhead + YYWILDCARD;
376 if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){
377#ifndef NDEBUG
378 if( yyTraceFILE ){
379 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
380 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
381 }
382#endif /* NDEBUG */
383 return yy_action[j];
drhe09daa92006-06-10 13:29:31 +0000384 }
drhe09daa92006-06-10 13:29:31 +0000385 }
drh4767d972006-06-14 15:03:49 +0000386#endif /* YYWILDCARD */
drh0bd1f4e2002-06-06 18:54:39 +0000387 }
drh3ddfdf72003-12-22 14:53:19 +0000388 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000389 }else{
390 return yy_action[i];
391 }
392}
393
394/*
395** Find the appropriate action for a parser given the non-terminal
396** look-ahead token iLookAhead.
397**
398** If the look-ahead token is YYNOCODE, then check to see if the action is
399** independent of the look-ahead. If it is, return the action, otherwise
400** return YY_NO_ACTION.
401*/
402static int yy_find_reduce_action(
drh161aba32005-02-01 04:09:36 +0000403 int stateno, /* Current state number */
drhd9f291e2006-06-13 11:15:47 +0000404 YYCODETYPE iLookAhead /* The look-ahead token */
drh8b582012003-10-21 13:16:03 +0000405){
406 int i;
drh7f7c2572008-04-27 18:45:10 +0000407#ifdef YYERRORSYMBOL
408 if( stateno>YY_REDUCE_MAX ){
409 return yy_default[stateno];
410 }
411#else
drh01495b92008-01-23 12:52:40 +0000412 assert( stateno<=YY_REDUCE_MAX );
drh7f7c2572008-04-27 18:45:10 +0000413#endif
drh01495b92008-01-23 12:52:40 +0000414 i = yy_reduce_ofst[stateno];
415 assert( i!=YY_REDUCE_USE_DFLT );
416 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000417 i += iLookAhead;
drh7f7c2572008-04-27 18:45:10 +0000418#ifdef YYERRORSYMBOL
419 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
420 return yy_default[stateno];
421 }
422#else
drh01495b92008-01-23 12:52:40 +0000423 assert( i>=0 && i<YY_SZ_ACTTAB );
424 assert( yy_lookahead[i]==iLookAhead );
drh7f7c2572008-04-27 18:45:10 +0000425#endif
drh01495b92008-01-23 12:52:40 +0000426 return yy_action[i];
drh75897232000-05-29 14:26:00 +0000427}
428
429/*
drhb19fd012007-03-29 01:44:45 +0000430** The following routine is called if the stack overflows.
431*/
drhb6018612007-03-30 13:35:05 +0000432static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
drhb19fd012007-03-29 01:44:45 +0000433 ParseARG_FETCH;
434 yypParser->yyidx--;
435#ifndef NDEBUG
436 if( yyTraceFILE ){
437 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
438 }
439#endif
440 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
441 /* Here code is inserted which will execute if the parser
442 ** stack every overflows */
443%%
444 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
445}
446
447/*
drh75897232000-05-29 14:26:00 +0000448** Perform a shift action.
449*/
450static void yy_shift(
451 yyParser *yypParser, /* The parser to be shifted */
452 int yyNewState, /* The new state to shift in */
453 int yyMajor, /* The major token to shift in */
drh34ff57b2008-07-14 12:27:51 +0000454 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
drh75897232000-05-29 14:26:00 +0000455){
drh3ddfdf72003-12-22 14:53:19 +0000456 yyStackEntry *yytos;
drh1f245e42002-03-11 13:55:50 +0000457 yypParser->yyidx++;
drhb19fd012007-03-29 01:44:45 +0000458#if YYSTACKDEPTH>0
drh1f245e42002-03-11 13:55:50 +0000459 if( yypParser->yyidx>=YYSTACKDEPTH ){
drhb6018612007-03-30 13:35:05 +0000460 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000461 return;
drh75897232000-05-29 14:26:00 +0000462 }
drhb19fd012007-03-29 01:44:45 +0000463#else
464 if( yypParser->yyidx>=yypParser->yystksz ){
465 yyGrowStack(yypParser);
466 if( yypParser->yyidx>=yypParser->yystksz ){
drhb6018612007-03-30 13:35:05 +0000467 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000468 return;
469 }
470 }
471#endif
drh3ddfdf72003-12-22 14:53:19 +0000472 yytos = &yypParser->yystack[yypParser->yyidx];
473 yytos->stateno = yyNewState;
474 yytos->major = yyMajor;
475 yytos->minor = *yypMinor;
drh75897232000-05-29 14:26:00 +0000476#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000477 if( yyTraceFILE && yypParser->yyidx>0 ){
drh75897232000-05-29 14:26:00 +0000478 int i;
479 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
480 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
drh1f245e42002-03-11 13:55:50 +0000481 for(i=1; i<=yypParser->yyidx; i++)
482 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
drh75897232000-05-29 14:26:00 +0000483 fprintf(yyTraceFILE,"\n");
484 }
485#endif
486}
487
488/* The following table contains information about every rule that
489** is used during the reduce.
490*/
drh57196282004-10-06 15:41:16 +0000491static const struct {
drh75897232000-05-29 14:26:00 +0000492 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
493 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
494} yyRuleInfo[] = {
495%%
496};
497
drh1f245e42002-03-11 13:55:50 +0000498static void yy_accept(yyParser*); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000499
500/*
501** Perform a reduce action and the shift that must immediately
502** follow the reduce.
503*/
504static void yy_reduce(
505 yyParser *yypParser, /* The parser */
506 int yyruleno /* Number of the rule by which to reduce */
drh75897232000-05-29 14:26:00 +0000507){
508 int yygoto; /* The next state */
509 int yyact; /* The next action */
510 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
drh1f245e42002-03-11 13:55:50 +0000511 yyStackEntry *yymsp; /* The top of the parser's stack */
drh75897232000-05-29 14:26:00 +0000512 int yysize; /* Amount to pop the stack */
drh1f245e42002-03-11 13:55:50 +0000513 ParseARG_FETCH;
drh3ddfdf72003-12-22 14:53:19 +0000514 yymsp = &yypParser->yystack[yypParser->yyidx];
drh0bd1f4e2002-06-06 18:54:39 +0000515#ifndef NDEBUG
516 if( yyTraceFILE && yyruleno>=0
drhd9f291e2006-06-13 11:15:47 +0000517 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
drh0bd1f4e2002-06-06 18:54:39 +0000518 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
519 yyRuleName[yyruleno]);
520 }
521#endif /* NDEBUG */
522
drh7bec5052005-02-06 02:45:41 +0000523 /* Silence complaints from purify about yygotominor being uninitialized
524 ** in some cases when it is copied into the stack after the following
525 ** switch. yygotominor is uninitialized when a rule reduces that does
526 ** not set the value of its left-hand side nonterminal. Leaving the
527 ** value of the nonterminal uninitialized is utterly harmless as long
528 ** as the value is never used. So really the only thing this code
529 ** accomplishes is to quieten purify.
drhcb6c5652007-01-16 18:19:12 +0000530 **
531 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
532 ** without this code, their parser segfaults. I'm not sure what there
533 ** parser is doing to make this happen. This is the second bug report
534 ** from wireshark this week. Clearly they are stressing Lemon in ways
535 ** that it has not been previously stressed... (SQLite ticket #2172)
drh7bec5052005-02-06 02:45:41 +0000536 */
drh26c9b5e2008-04-11 14:56:53 +0000537 /*memset(&yygotominor, 0, sizeof(yygotominor));*/
538 yygotominor = yyzerominor;
drhcb6c5652007-01-16 18:19:12 +0000539
drh7bec5052005-02-06 02:45:41 +0000540
drh75897232000-05-29 14:26:00 +0000541 switch( yyruleno ){
542 /* Beginning here are the reduction cases. A typical example
543 ** follows:
544 ** case 0:
drh75897232000-05-29 14:26:00 +0000545 ** #line <lineno> <grammarfile>
546 ** { ... } // User supplied code
547 ** #line <lineno> <thisfile>
548 ** break;
549 */
550%%
551 };
552 yygoto = yyRuleInfo[yyruleno].lhs;
553 yysize = yyRuleInfo[yyruleno].nrhs;
drh1f245e42002-03-11 13:55:50 +0000554 yypParser->yyidx -= yysize;
drh161aba32005-02-01 04:09:36 +0000555 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,yygoto);
drh75897232000-05-29 14:26:00 +0000556 if( yyact < YYNSTATE ){
drh490a73b2005-02-01 03:20:00 +0000557#ifdef NDEBUG
558 /* If we are not debugging and the reduce action popped at least
559 ** one element off the stack, then we can push the new element back
560 ** onto the stack here, and skip the stack overflow test in yy_shift().
561 ** That gives a significant speed improvement. */
562 if( yysize ){
563 yypParser->yyidx++;
564 yymsp -= yysize-1;
565 yymsp->stateno = yyact;
566 yymsp->major = yygoto;
567 yymsp->minor = yygotominor;
568 }else
569#endif
570 {
571 yy_shift(yypParser,yyact,yygoto,&yygotominor);
572 }
drh4b2f9362008-01-22 23:37:09 +0000573 }else{
574 assert( yyact == YYNSTATE + YYNRULE + 1 );
drh1f245e42002-03-11 13:55:50 +0000575 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000576 }
577}
578
579/*
580** The following code executes when the parse fails
581*/
582static void yy_parse_failed(
583 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000584){
drh1f245e42002-03-11 13:55:50 +0000585 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000586#ifndef NDEBUG
587 if( yyTraceFILE ){
588 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
589 }
590#endif
drh1f245e42002-03-11 13:55:50 +0000591 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000592 /* Here code is inserted which will be executed whenever the
593 ** parser fails */
594%%
drh1f245e42002-03-11 13:55:50 +0000595 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000596}
597
598/*
599** The following code executes when a syntax error first occurs.
600*/
601static void yy_syntax_error(
602 yyParser *yypParser, /* The parser */
603 int yymajor, /* The major type of the error token */
604 YYMINORTYPE yyminor /* The minor type of the error token */
drh75897232000-05-29 14:26:00 +0000605){
drh1f245e42002-03-11 13:55:50 +0000606 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000607#define TOKEN (yyminor.yy0)
608%%
drh1f245e42002-03-11 13:55:50 +0000609 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000610}
611
612/*
613** The following is executed when the parser accepts
614*/
615static void yy_accept(
616 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000617){
drh1f245e42002-03-11 13:55:50 +0000618 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000619#ifndef NDEBUG
620 if( yyTraceFILE ){
621 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
622 }
623#endif
drh1f245e42002-03-11 13:55:50 +0000624 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000625 /* Here code is inserted which will be executed whenever the
626 ** parser accepts */
627%%
drh1f245e42002-03-11 13:55:50 +0000628 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000629}
630
631/* The main parser program.
632** The first argument is a pointer to a structure obtained from
633** "ParseAlloc" which describes the current state of the parser.
634** The second argument is the major token number. The third is
635** the minor token. The fourth optional argument is whatever the
636** user wants (and specified in the grammar) and is available for
637** use by the action routines.
638**
639** Inputs:
640** <ul>
641** <li> A pointer to the parser (an opaque structure.)
642** <li> The major token number.
643** <li> The minor token number.
644** <li> An option argument of a grammar-specified type.
645** </ul>
646**
647** Outputs:
648** None.
649*/
drh75897232000-05-29 14:26:00 +0000650void Parse(
651 void *yyp, /* The parser */
652 int yymajor, /* The major token code number */
653 ParseTOKENTYPE yyminor /* The value for the token */
drh1f245e42002-03-11 13:55:50 +0000654 ParseARG_PDECL /* Optional %extra_argument parameter */
drh75897232000-05-29 14:26:00 +0000655){
656 YYMINORTYPE yyminorunion;
657 int yyact; /* The parser action. */
658 int yyendofinput; /* True if we are at the end of input */
drhc4dd3fd2008-01-22 01:48:05 +0000659#ifdef YYERRORSYMBOL
drh75897232000-05-29 14:26:00 +0000660 int yyerrorhit = 0; /* True if yymajor has invoked an error */
drhc4dd3fd2008-01-22 01:48:05 +0000661#endif
drh75897232000-05-29 14:26:00 +0000662 yyParser *yypParser; /* The parser */
663
664 /* (re)initialize the parser, if necessary */
665 yypParser = (yyParser*)yyp;
drh1f245e42002-03-11 13:55:50 +0000666 if( yypParser->yyidx<0 ){
drhb7bac722007-03-29 02:26:45 +0000667#if YYSTACKDEPTH<=0
drhb19fd012007-03-29 01:44:45 +0000668 if( yypParser->yystksz <=0 ){
drh26c9b5e2008-04-11 14:56:53 +0000669 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
670 yyminorunion = yyzerominor;
drhb6018612007-03-30 13:35:05 +0000671 yyStackOverflow(yypParser, &yyminorunion);
drhb19fd012007-03-29 01:44:45 +0000672 return;
673 }
drhb7bac722007-03-29 02:26:45 +0000674#endif
drh1f245e42002-03-11 13:55:50 +0000675 yypParser->yyidx = 0;
676 yypParser->yyerrcnt = -1;
drh3ddfdf72003-12-22 14:53:19 +0000677 yypParser->yystack[0].stateno = 0;
678 yypParser->yystack[0].major = 0;
drh75897232000-05-29 14:26:00 +0000679 }
680 yyminorunion.yy0 = yyminor;
681 yyendofinput = (yymajor==0);
drh1f245e42002-03-11 13:55:50 +0000682 ParseARG_STORE;
drh75897232000-05-29 14:26:00 +0000683
684#ifndef NDEBUG
685 if( yyTraceFILE ){
686 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
687 }
688#endif
689
690 do{
drh8b582012003-10-21 13:16:03 +0000691 yyact = yy_find_shift_action(yypParser,yymajor);
drh75897232000-05-29 14:26:00 +0000692 if( yyact<YYNSTATE ){
drh55176252008-01-22 14:50:16 +0000693 assert( !yyendofinput ); /* Impossible to shift the $ token */
drh75897232000-05-29 14:26:00 +0000694 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000695 yypParser->yyerrcnt--;
drh55176252008-01-22 14:50:16 +0000696 yymajor = YYNOCODE;
drh75897232000-05-29 14:26:00 +0000697 }else if( yyact < YYNSTATE + YYNRULE ){
drh1f245e42002-03-11 13:55:50 +0000698 yy_reduce(yypParser,yyact-YYNSTATE);
drhc4dd3fd2008-01-22 01:48:05 +0000699 }else{
700 assert( yyact == YY_ERROR_ACTION );
701#ifdef YYERRORSYMBOL
drh3ddfdf72003-12-22 14:53:19 +0000702 int yymx;
drhc4dd3fd2008-01-22 01:48:05 +0000703#endif
drh75897232000-05-29 14:26:00 +0000704#ifndef NDEBUG
705 if( yyTraceFILE ){
706 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
707 }
708#endif
709#ifdef YYERRORSYMBOL
710 /* A syntax error has occurred.
711 ** The response to an error depends upon whether or not the
712 ** grammar defines an error token "ERROR".
713 **
714 ** This is what we do if the grammar does define ERROR:
715 **
716 ** * Call the %syntax_error function.
717 **
718 ** * Begin popping the stack until we enter a state where
719 ** it is legal to shift the error symbol, then shift
720 ** the error symbol.
721 **
722 ** * Set the error count to three.
723 **
724 ** * Begin accepting and shifting new tokens. No new error
725 ** processing will occur until three tokens have been
726 ** shifted successfully.
727 **
728 */
drh1f245e42002-03-11 13:55:50 +0000729 if( yypParser->yyerrcnt<0 ){
730 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000731 }
drh3ddfdf72003-12-22 14:53:19 +0000732 yymx = yypParser->yystack[yypParser->yyidx].major;
733 if( yymx==YYERRORSYMBOL || yyerrorhit ){
drh75897232000-05-29 14:26:00 +0000734#ifndef NDEBUG
735 if( yyTraceFILE ){
736 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
737 yyTracePrompt,yyTokenName[yymajor]);
738 }
739#endif
740 yy_destructor(yymajor,&yyminorunion);
741 yymajor = YYNOCODE;
742 }else{
743 while(
drh1f245e42002-03-11 13:55:50 +0000744 yypParser->yyidx >= 0 &&
drh3ddfdf72003-12-22 14:53:19 +0000745 yymx != YYERRORSYMBOL &&
drhf3a58882006-05-08 15:14:19 +0000746 (yyact = yy_find_reduce_action(
747 yypParser->yystack[yypParser->yyidx].stateno,
748 YYERRORSYMBOL)) >= YYNSTATE
drh75897232000-05-29 14:26:00 +0000749 ){
750 yy_pop_parser_stack(yypParser);
751 }
drh1f245e42002-03-11 13:55:50 +0000752 if( yypParser->yyidx < 0 || yymajor==0 ){
drh75897232000-05-29 14:26:00 +0000753 yy_destructor(yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000754 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000755 yymajor = YYNOCODE;
drh3ddfdf72003-12-22 14:53:19 +0000756 }else if( yymx!=YYERRORSYMBOL ){
drh75897232000-05-29 14:26:00 +0000757 YYMINORTYPE u2;
758 u2.YYERRSYMDT = 0;
759 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
760 }
761 }
drh1f245e42002-03-11 13:55:50 +0000762 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000763 yyerrorhit = 1;
764#else /* YYERRORSYMBOL is not defined */
765 /* This is what we do if the grammar does not define ERROR:
766 **
767 ** * Report an error message, and throw away the input token.
768 **
769 ** * If the input token is $, then fail the parse.
770 **
771 ** As before, subsequent error messages are suppressed until
772 ** three input tokens have been successfully shifted.
773 */
drh1f245e42002-03-11 13:55:50 +0000774 if( yypParser->yyerrcnt<=0 ){
775 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000776 }
drh1f245e42002-03-11 13:55:50 +0000777 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000778 yy_destructor(yymajor,&yyminorunion);
779 if( yyendofinput ){
drh1f245e42002-03-11 13:55:50 +0000780 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000781 }
782 yymajor = YYNOCODE;
783#endif
drh75897232000-05-29 14:26:00 +0000784 }
drh1f245e42002-03-11 13:55:50 +0000785 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
drh75897232000-05-29 14:26:00 +0000786 return;
787}