blob: f061961b770971f9dd90c74076fb964fe23255b0 [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*/
4/* First off, code is include which follows the "include" declaration
5** in the input file. */
6#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
drhfdbf9282003-10-21 16:34:41 +000063/* Next are that tables used to determine what action to take based on the
drh8b582012003-10-21 13:16:03 +000064** current state and lookahead token. These tables are used to implement
65** functions that take a state number and lookahead value and return an
66** action integer.
drh75897232000-05-29 14:26:00 +000067**
drh8548a052003-10-22 22:15:27 +000068** Suppose the action integer is N. Then the action is determined as
69** follows
drh75897232000-05-29 14:26:00 +000070**
drh8548a052003-10-22 22:15:27 +000071** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
72** token onto the stack and goto state N.
73**
74** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
75**
76** N == YYNSTATE+YYNRULE A syntax error has occurred.
77**
78** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
79**
80** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
81** slots in the yy_action[] table.
82**
83** The action table is constructed as a single large table named yy_action[].
84** Given state S and lookahead X, the action is computed as
drh75897232000-05-29 14:26:00 +000085**
drh8b582012003-10-21 13:16:03 +000086** yy_action[ yy_shift_ofst[S] + X ]
87**
drh8548a052003-10-22 22:15:27 +000088** If the index value yy_shift_ofst[S]+X is out of range or if the value
drh8b582012003-10-21 13:16:03 +000089** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
90** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
91** and that yy_default[S] should be used instead.
92**
93** The formula above is for computing the action when the lookahead is
94** a terminal symbol. If the lookahead is a non-terminal (as occurs after
95** a reduce action) then the yy_reduce_ofst[] array is used in place of
96** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
97** YY_SHIFT_USE_DFLT.
98**
99** The following are the tables generated in this section:
100**
101** yy_action[] A single table containing all actions.
102** yy_lookahead[] A table containing the lookahead for each entry in
103** yy_action. Used to detect hash collisions.
104** yy_shift_ofst[] For each state, the offset into yy_action for
105** shifting terminals.
106** yy_reduce_ofst[] For each state, the offset into yy_action for
107** shifting non-terminals after a reduce.
108** yy_default[] Default action for each state.
drh75897232000-05-29 14:26:00 +0000109*/
drh75897232000-05-29 14:26:00 +0000110%%
drhd9f291e2006-06-13 11:15:47 +0000111#define YY_SZ_ACTTAB (int)(sizeof(yy_action)/sizeof(yy_action[0]))
drh75897232000-05-29 14:26:00 +0000112
drh0bd1f4e2002-06-06 18:54:39 +0000113/* The next table maps tokens into fallback tokens. If a construct
114** like the following:
115**
116** %fallback ID X Y Z.
117**
118** appears in the grammer, then ID becomes a fallback token for X, Y,
119** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
120** but it does not parse, the type of the token is changed to ID and
121** the parse is retried before an error is thrown.
122*/
123#ifdef YYFALLBACK
124static const YYCODETYPE yyFallback[] = {
125%%
126};
127#endif /* YYFALLBACK */
128
drh75897232000-05-29 14:26:00 +0000129/* The following structure represents a single element of the
130** parser's stack. Information stored includes:
131**
132** + The state number for the parser at this level of the stack.
133**
134** + The value of the token stored at this level of the stack.
135** (In other words, the "major" token.)
136**
137** + The semantic value stored at this level of the stack. This is
138** the information used by the action routines in the grammar.
139** It is sometimes called the "minor" token.
140*/
141struct yyStackEntry {
142 int stateno; /* The state-number */
143 int major; /* The major token value. This is the code
144 ** number for the token at this stack level */
145 YYMINORTYPE minor; /* The user-supplied minor token value. This
146 ** is the value of the token */
147};
drh1f245e42002-03-11 13:55:50 +0000148typedef struct yyStackEntry yyStackEntry;
drh75897232000-05-29 14:26:00 +0000149
150/* The state of the parser is completely contained in an instance of
151** the following structure */
152struct yyParser {
drh1f245e42002-03-11 13:55:50 +0000153 int yyidx; /* Index of top element in stack */
154 int yyerrcnt; /* Shifts left before out of the error */
drh1f245e42002-03-11 13:55:50 +0000155 ParseARG_SDECL /* A place to hold %extra_argument */
drhb19fd012007-03-29 01:44:45 +0000156#if YYSTACKDEPTH<=0
157 int yystksz; /* Current side of the stack */
158 yyStackEntry *yystack; /* The parser's stack */
159#else
drh1f245e42002-03-11 13:55:50 +0000160 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
drhb19fd012007-03-29 01:44:45 +0000161#endif
drh75897232000-05-29 14:26:00 +0000162};
163typedef struct yyParser yyParser;
164
165#ifndef NDEBUG
166#include <stdio.h>
167static FILE *yyTraceFILE = 0;
168static char *yyTracePrompt = 0;
drh0bd1f4e2002-06-06 18:54:39 +0000169#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000170
drh0bd1f4e2002-06-06 18:54:39 +0000171#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000172/*
173** Turn parser tracing on by giving a stream to which to write the trace
174** and a prompt to preface each trace message. Tracing is turned off
175** by making either argument NULL
176**
177** Inputs:
178** <ul>
179** <li> A FILE* to which trace output should be written.
180** If NULL, then tracing is turned off.
181** <li> A prefix string written at the beginning of every
182** line of trace output. If NULL, then tracing is
183** turned off.
184** </ul>
185**
186** Outputs:
187** None.
188*/
drh75897232000-05-29 14:26:00 +0000189void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
190 yyTraceFILE = TraceFILE;
191 yyTracePrompt = zTracePrompt;
192 if( yyTraceFILE==0 ) yyTracePrompt = 0;
193 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
194}
drh0bd1f4e2002-06-06 18:54:39 +0000195#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000196
drh0bd1f4e2002-06-06 18:54:39 +0000197#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000198/* For tracing shifts, the names of all terminals and nonterminals
199** are required. The following table supplies these names */
drh57196282004-10-06 15:41:16 +0000200static const char *const yyTokenName[] = {
drh75897232000-05-29 14:26:00 +0000201%%
202};
drh0bd1f4e2002-06-06 18:54:39 +0000203#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000204
drh0bd1f4e2002-06-06 18:54:39 +0000205#ifndef NDEBUG
206/* For tracing reduce actions, the names of all rules are required.
207*/
drh57196282004-10-06 15:41:16 +0000208static const char *const yyRuleName[] = {
drh0bd1f4e2002-06-06 18:54:39 +0000209%%
210};
211#endif /* NDEBUG */
drha1b351a2001-09-14 16:42:12 +0000212
drh960e8c62001-04-03 16:53:21 +0000213
drhb19fd012007-03-29 01:44:45 +0000214#if YYSTACKDEPTH<=0
215/*
216** Try to increase the size of the parser stack.
217*/
218static void yyGrowStack(yyParser *p){
219 int newSize;
220 yyStackEntry *pNew;
221
222 newSize = p->yystksz*2 + 100;
223 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
224 if( pNew ){
225 p->yystack = pNew;
226 p->yystksz = newSize;
227#ifndef NDEBUG
228 if( yyTraceFILE ){
229 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
230 yyTracePrompt, p->yystksz);
231 }
232#endif
233 }
234}
235#endif
236
drh75897232000-05-29 14:26:00 +0000237/*
238** This function allocates a new parser.
239** The only argument is a pointer to a function which works like
240** malloc.
241**
242** Inputs:
243** A pointer to the function used to allocate memory.
244**
245** Outputs:
246** A pointer to a parser. This pointer is used in subsequent calls
247** to Parse and ParseFree.
248*/
drh7218ac72002-03-10 21:21:00 +0000249void *ParseAlloc(void *(*mallocProc)(size_t)){
drh75897232000-05-29 14:26:00 +0000250 yyParser *pParser;
drh7218ac72002-03-10 21:21:00 +0000251 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
drh75897232000-05-29 14:26:00 +0000252 if( pParser ){
drh1f245e42002-03-11 13:55:50 +0000253 pParser->yyidx = -1;
drhb19fd012007-03-29 01:44:45 +0000254#if YYSTACKDEPTH<=0
255 yyGrowStack(pParser);
256#endif
drh75897232000-05-29 14:26:00 +0000257 }
258 return pParser;
259}
260
261/* The following function deletes the value associated with a
262** symbol. The symbol can be either a terminal or nonterminal.
263** "yymajor" is the symbol code, and "yypminor" is a pointer to
264** the value.
265*/
266static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){
267 switch( yymajor ){
268 /* Here is inserted the actions which take place when a
269 ** terminal or non-terminal is destroyed. This can happen
270 ** when the symbol is popped from the stack during a
271 ** reduce or during error processing or when a parser is
272 ** being destroyed before it is finished parsing.
273 **
274 ** Note: during a reduce, the only symbols destroyed are those
275 ** which appear on the RHS of the rule, but which are not used
276 ** inside the C code.
277 */
278%%
279 default: break; /* If no destructor action specified: do nothing */
280 }
281}
282
283/*
284** Pop the parser's stack once.
285**
286** If there is a destructor routine associated with the token which
287** is popped from the stack, then call it.
288**
289** Return the major token number for the symbol popped.
290*/
291static int yy_pop_parser_stack(yyParser *pParser){
292 YYCODETYPE yymajor;
drh3ddfdf72003-12-22 14:53:19 +0000293 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
drh75897232000-05-29 14:26:00 +0000294
drh1f245e42002-03-11 13:55:50 +0000295 if( pParser->yyidx<0 ) return 0;
drh75897232000-05-29 14:26:00 +0000296#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000297 if( yyTraceFILE && pParser->yyidx>=0 ){
drh75897232000-05-29 14:26:00 +0000298 fprintf(yyTraceFILE,"%sPopping %s\n",
299 yyTracePrompt,
drh3ddfdf72003-12-22 14:53:19 +0000300 yyTokenName[yytos->major]);
drh75897232000-05-29 14:26:00 +0000301 }
302#endif
drh3ddfdf72003-12-22 14:53:19 +0000303 yymajor = yytos->major;
304 yy_destructor( yymajor, &yytos->minor);
drh1f245e42002-03-11 13:55:50 +0000305 pParser->yyidx--;
drh75897232000-05-29 14:26:00 +0000306 return yymajor;
307}
308
309/*
310** Deallocate and destroy a parser. Destructors are all called for
311** all stack elements before shutting the parser down.
312**
313** Inputs:
314** <ul>
315** <li> A pointer to the parser. This should be a pointer
316** obtained from ParseAlloc.
317** <li> A pointer to a function used to reclaim memory obtained
318** from malloc.
319** </ul>
320*/
drh75897232000-05-29 14:26:00 +0000321void ParseFree(
drh960e8c62001-04-03 16:53:21 +0000322 void *p, /* The parser to be deleted */
323 void (*freeProc)(void*) /* Function used to reclaim memory */
drh75897232000-05-29 14:26:00 +0000324){
325 yyParser *pParser = (yyParser*)p;
326 if( pParser==0 ) return;
drh1f245e42002-03-11 13:55:50 +0000327 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
drhb19fd012007-03-29 01:44:45 +0000328#if YYSTACKDEPTH<=0
329 free(pParser->yystack);
330#endif
drh960e8c62001-04-03 16:53:21 +0000331 (*freeProc)((void*)pParser);
drh75897232000-05-29 14:26:00 +0000332}
333
334/*
drh8b582012003-10-21 13:16:03 +0000335** Find the appropriate action for a parser given the terminal
336** look-ahead token iLookAhead.
drh75897232000-05-29 14:26:00 +0000337**
338** If the look-ahead token is YYNOCODE, then check to see if the action is
339** independent of the look-ahead. If it is, return the action, otherwise
340** return YY_NO_ACTION.
341*/
drh8b582012003-10-21 13:16:03 +0000342static int yy_find_shift_action(
drh75897232000-05-29 14:26:00 +0000343 yyParser *pParser, /* The parser */
drhd9f291e2006-06-13 11:15:47 +0000344 YYCODETYPE iLookAhead /* The look-ahead token */
drh75897232000-05-29 14:26:00 +0000345){
drh8b582012003-10-21 13:16:03 +0000346 int i;
drh3ddfdf72003-12-22 14:53:19 +0000347 int stateno = pParser->yystack[pParser->yyidx].stateno;
drh75897232000-05-29 14:26:00 +0000348
drhada354d2005-11-05 15:03:59 +0000349 if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
drh3ddfdf72003-12-22 14:53:19 +0000350 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000351 }
352 if( iLookAhead==YYNOCODE ){
353 return YY_NO_ACTION;
354 }
355 i += iLookAhead;
356 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
drhe09daa92006-06-10 13:29:31 +0000357 if( iLookAhead>0 ){
drh4767d972006-06-14 15:03:49 +0000358#ifdef YYFALLBACK
drhe09daa92006-06-10 13:29:31 +0000359 int iFallback; /* Fallback token */
360 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
361 && (iFallback = yyFallback[iLookAhead])!=0 ){
drh4767d972006-06-14 15:03:49 +0000362#ifndef NDEBUG
drhe09daa92006-06-10 13:29:31 +0000363 if( yyTraceFILE ){
364 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
365 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
366 }
drh4767d972006-06-14 15:03:49 +0000367#endif
drhe09daa92006-06-10 13:29:31 +0000368 return yy_find_shift_action(pParser, iFallback);
drh0bd1f4e2002-06-06 18:54:39 +0000369 }
drh4767d972006-06-14 15:03:49 +0000370#endif
371#ifdef YYWILDCARD
372 {
373 int j = i - iLookAhead + YYWILDCARD;
374 if( j>=0 && j<YY_SZ_ACTTAB && yy_lookahead[j]==YYWILDCARD ){
375#ifndef NDEBUG
376 if( yyTraceFILE ){
377 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
378 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
379 }
380#endif /* NDEBUG */
381 return yy_action[j];
drhe09daa92006-06-10 13:29:31 +0000382 }
drhe09daa92006-06-10 13:29:31 +0000383 }
drh4767d972006-06-14 15:03:49 +0000384#endif /* YYWILDCARD */
drh0bd1f4e2002-06-06 18:54:39 +0000385 }
drh3ddfdf72003-12-22 14:53:19 +0000386 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000387 }else{
388 return yy_action[i];
389 }
390}
391
392/*
393** Find the appropriate action for a parser given the non-terminal
394** look-ahead token iLookAhead.
395**
396** If the look-ahead token is YYNOCODE, then check to see if the action is
397** independent of the look-ahead. If it is, return the action, otherwise
398** return YY_NO_ACTION.
399*/
400static int yy_find_reduce_action(
drh161aba32005-02-01 04:09:36 +0000401 int stateno, /* Current state number */
drhd9f291e2006-06-13 11:15:47 +0000402 YYCODETYPE iLookAhead /* The look-ahead token */
drh8b582012003-10-21 13:16:03 +0000403){
404 int i;
drh161aba32005-02-01 04:09:36 +0000405 /* int stateno = pParser->yystack[pParser->yyidx].stateno; */
drh8b582012003-10-21 13:16:03 +0000406
drhada354d2005-11-05 15:03:59 +0000407 if( stateno>YY_REDUCE_MAX ||
408 (i = yy_reduce_ofst[stateno])==YY_REDUCE_USE_DFLT ){
drh3ddfdf72003-12-22 14:53:19 +0000409 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000410 }
411 if( iLookAhead==YYNOCODE ){
drh75897232000-05-29 14:26:00 +0000412 return YY_NO_ACTION;
413 }
drh8b582012003-10-21 13:16:03 +0000414 i += iLookAhead;
415 if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){
drh3ddfdf72003-12-22 14:53:19 +0000416 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000417 }else{
418 return yy_action[i];
419 }
drh75897232000-05-29 14:26:00 +0000420}
421
422/*
drhb19fd012007-03-29 01:44:45 +0000423** The following routine is called if the stack overflows.
424*/
drhb6018612007-03-30 13:35:05 +0000425static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
drhb19fd012007-03-29 01:44:45 +0000426 ParseARG_FETCH;
427 yypParser->yyidx--;
428#ifndef NDEBUG
429 if( yyTraceFILE ){
430 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
431 }
432#endif
433 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
434 /* Here code is inserted which will execute if the parser
435 ** stack every overflows */
436%%
437 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
438}
439
440/*
drh75897232000-05-29 14:26:00 +0000441** Perform a shift action.
442*/
443static void yy_shift(
444 yyParser *yypParser, /* The parser to be shifted */
445 int yyNewState, /* The new state to shift in */
446 int yyMajor, /* The major token to shift in */
447 YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */
448){
drh3ddfdf72003-12-22 14:53:19 +0000449 yyStackEntry *yytos;
drh1f245e42002-03-11 13:55:50 +0000450 yypParser->yyidx++;
drhb19fd012007-03-29 01:44:45 +0000451#if YYSTACKDEPTH>0
drh1f245e42002-03-11 13:55:50 +0000452 if( yypParser->yyidx>=YYSTACKDEPTH ){
drhb6018612007-03-30 13:35:05 +0000453 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000454 return;
drh75897232000-05-29 14:26:00 +0000455 }
drhb19fd012007-03-29 01:44:45 +0000456#else
457 if( yypParser->yyidx>=yypParser->yystksz ){
458 yyGrowStack(yypParser);
459 if( yypParser->yyidx>=yypParser->yystksz ){
drhb6018612007-03-30 13:35:05 +0000460 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000461 return;
462 }
463 }
464#endif
drh3ddfdf72003-12-22 14:53:19 +0000465 yytos = &yypParser->yystack[yypParser->yyidx];
466 yytos->stateno = yyNewState;
467 yytos->major = yyMajor;
468 yytos->minor = *yypMinor;
drh75897232000-05-29 14:26:00 +0000469#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000470 if( yyTraceFILE && yypParser->yyidx>0 ){
drh75897232000-05-29 14:26:00 +0000471 int i;
472 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
473 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
drh1f245e42002-03-11 13:55:50 +0000474 for(i=1; i<=yypParser->yyidx; i++)
475 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
drh75897232000-05-29 14:26:00 +0000476 fprintf(yyTraceFILE,"\n");
477 }
478#endif
479}
480
481/* The following table contains information about every rule that
482** is used during the reduce.
483*/
drh57196282004-10-06 15:41:16 +0000484static const struct {
drh75897232000-05-29 14:26:00 +0000485 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
486 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
487} yyRuleInfo[] = {
488%%
489};
490
drh1f245e42002-03-11 13:55:50 +0000491static void yy_accept(yyParser*); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000492
493/*
494** Perform a reduce action and the shift that must immediately
495** follow the reduce.
496*/
497static void yy_reduce(
498 yyParser *yypParser, /* The parser */
499 int yyruleno /* Number of the rule by which to reduce */
drh75897232000-05-29 14:26:00 +0000500){
501 int yygoto; /* The next state */
502 int yyact; /* The next action */
503 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
drh1f245e42002-03-11 13:55:50 +0000504 yyStackEntry *yymsp; /* The top of the parser's stack */
drh75897232000-05-29 14:26:00 +0000505 int yysize; /* Amount to pop the stack */
drh1f245e42002-03-11 13:55:50 +0000506 ParseARG_FETCH;
drh3ddfdf72003-12-22 14:53:19 +0000507 yymsp = &yypParser->yystack[yypParser->yyidx];
drh0bd1f4e2002-06-06 18:54:39 +0000508#ifndef NDEBUG
509 if( yyTraceFILE && yyruleno>=0
drhd9f291e2006-06-13 11:15:47 +0000510 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
drh0bd1f4e2002-06-06 18:54:39 +0000511 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
512 yyRuleName[yyruleno]);
513 }
514#endif /* NDEBUG */
515
drh7bec5052005-02-06 02:45:41 +0000516 /* Silence complaints from purify about yygotominor being uninitialized
517 ** in some cases when it is copied into the stack after the following
518 ** switch. yygotominor is uninitialized when a rule reduces that does
519 ** not set the value of its left-hand side nonterminal. Leaving the
520 ** value of the nonterminal uninitialized is utterly harmless as long
521 ** as the value is never used. So really the only thing this code
522 ** accomplishes is to quieten purify.
drhcb6c5652007-01-16 18:19:12 +0000523 **
524 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
525 ** without this code, their parser segfaults. I'm not sure what there
526 ** parser is doing to make this happen. This is the second bug report
527 ** from wireshark this week. Clearly they are stressing Lemon in ways
528 ** that it has not been previously stressed... (SQLite ticket #2172)
drh7bec5052005-02-06 02:45:41 +0000529 */
530 memset(&yygotominor, 0, sizeof(yygotominor));
drhcb6c5652007-01-16 18:19:12 +0000531
drh7bec5052005-02-06 02:45:41 +0000532
drh75897232000-05-29 14:26:00 +0000533 switch( yyruleno ){
534 /* Beginning here are the reduction cases. A typical example
535 ** follows:
536 ** case 0:
drh75897232000-05-29 14:26:00 +0000537 ** #line <lineno> <grammarfile>
538 ** { ... } // User supplied code
539 ** #line <lineno> <thisfile>
540 ** break;
541 */
542%%
543 };
544 yygoto = yyRuleInfo[yyruleno].lhs;
545 yysize = yyRuleInfo[yyruleno].nrhs;
drh1f245e42002-03-11 13:55:50 +0000546 yypParser->yyidx -= yysize;
drh161aba32005-02-01 04:09:36 +0000547 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,yygoto);
drh75897232000-05-29 14:26:00 +0000548 if( yyact < YYNSTATE ){
drh490a73b2005-02-01 03:20:00 +0000549#ifdef NDEBUG
550 /* If we are not debugging and the reduce action popped at least
551 ** one element off the stack, then we can push the new element back
552 ** onto the stack here, and skip the stack overflow test in yy_shift().
553 ** That gives a significant speed improvement. */
554 if( yysize ){
555 yypParser->yyidx++;
556 yymsp -= yysize-1;
557 yymsp->stateno = yyact;
558 yymsp->major = yygoto;
559 yymsp->minor = yygotominor;
560 }else
561#endif
562 {
563 yy_shift(yypParser,yyact,yygoto,&yygotominor);
564 }
drh75897232000-05-29 14:26:00 +0000565 }else if( yyact == YYNSTATE + YYNRULE + 1 ){
drh1f245e42002-03-11 13:55:50 +0000566 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000567 }
568}
569
570/*
571** The following code executes when the parse fails
572*/
573static void yy_parse_failed(
574 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000575){
drh1f245e42002-03-11 13:55:50 +0000576 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000577#ifndef NDEBUG
578 if( yyTraceFILE ){
579 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
580 }
581#endif
drh1f245e42002-03-11 13:55:50 +0000582 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000583 /* Here code is inserted which will be executed whenever the
584 ** parser fails */
585%%
drh1f245e42002-03-11 13:55:50 +0000586 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000587}
588
589/*
590** The following code executes when a syntax error first occurs.
591*/
592static void yy_syntax_error(
593 yyParser *yypParser, /* The parser */
594 int yymajor, /* The major type of the error token */
595 YYMINORTYPE yyminor /* The minor type of the error token */
drh75897232000-05-29 14:26:00 +0000596){
drh1f245e42002-03-11 13:55:50 +0000597 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000598#define TOKEN (yyminor.yy0)
599%%
drh1f245e42002-03-11 13:55:50 +0000600 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000601}
602
603/*
604** The following is executed when the parser accepts
605*/
606static void yy_accept(
607 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000608){
drh1f245e42002-03-11 13:55:50 +0000609 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000610#ifndef NDEBUG
611 if( yyTraceFILE ){
612 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
613 }
614#endif
drh1f245e42002-03-11 13:55:50 +0000615 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000616 /* Here code is inserted which will be executed whenever the
617 ** parser accepts */
618%%
drh1f245e42002-03-11 13:55:50 +0000619 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000620}
621
622/* The main parser program.
623** The first argument is a pointer to a structure obtained from
624** "ParseAlloc" which describes the current state of the parser.
625** The second argument is the major token number. The third is
626** the minor token. The fourth optional argument is whatever the
627** user wants (and specified in the grammar) and is available for
628** use by the action routines.
629**
630** Inputs:
631** <ul>
632** <li> A pointer to the parser (an opaque structure.)
633** <li> The major token number.
634** <li> The minor token number.
635** <li> An option argument of a grammar-specified type.
636** </ul>
637**
638** Outputs:
639** None.
640*/
drh75897232000-05-29 14:26:00 +0000641void Parse(
642 void *yyp, /* The parser */
643 int yymajor, /* The major token code number */
644 ParseTOKENTYPE yyminor /* The value for the token */
drh1f245e42002-03-11 13:55:50 +0000645 ParseARG_PDECL /* Optional %extra_argument parameter */
drh75897232000-05-29 14:26:00 +0000646){
647 YYMINORTYPE yyminorunion;
648 int yyact; /* The parser action. */
649 int yyendofinput; /* True if we are at the end of input */
drhc4dd3fd2008-01-22 01:48:05 +0000650#ifdef YYERRORSYMBOL
drh75897232000-05-29 14:26:00 +0000651 int yyerrorhit = 0; /* True if yymajor has invoked an error */
drhc4dd3fd2008-01-22 01:48:05 +0000652#endif
drh75897232000-05-29 14:26:00 +0000653 yyParser *yypParser; /* The parser */
654
655 /* (re)initialize the parser, if necessary */
656 yypParser = (yyParser*)yyp;
drh1f245e42002-03-11 13:55:50 +0000657 if( yypParser->yyidx<0 ){
drhb7bac722007-03-29 02:26:45 +0000658#if YYSTACKDEPTH<=0
drhb19fd012007-03-29 01:44:45 +0000659 if( yypParser->yystksz <=0 ){
drhb6018612007-03-30 13:35:05 +0000660 memset(&yyminorunion, 0, sizeof(yyminorunion));
661 yyStackOverflow(yypParser, &yyminorunion);
drhb19fd012007-03-29 01:44:45 +0000662 return;
663 }
drhb7bac722007-03-29 02:26:45 +0000664#endif
drh1f245e42002-03-11 13:55:50 +0000665 yypParser->yyidx = 0;
666 yypParser->yyerrcnt = -1;
drh3ddfdf72003-12-22 14:53:19 +0000667 yypParser->yystack[0].stateno = 0;
668 yypParser->yystack[0].major = 0;
drh75897232000-05-29 14:26:00 +0000669 }
670 yyminorunion.yy0 = yyminor;
671 yyendofinput = (yymajor==0);
drh1f245e42002-03-11 13:55:50 +0000672 ParseARG_STORE;
drh75897232000-05-29 14:26:00 +0000673
674#ifndef NDEBUG
675 if( yyTraceFILE ){
676 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
677 }
678#endif
679
680 do{
drh8b582012003-10-21 13:16:03 +0000681 yyact = yy_find_shift_action(yypParser,yymajor);
drh75897232000-05-29 14:26:00 +0000682 if( yyact<YYNSTATE ){
drh55176252008-01-22 14:50:16 +0000683 assert( !yyendofinput ); /* Impossible to shift the $ token */
drh75897232000-05-29 14:26:00 +0000684 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000685 yypParser->yyerrcnt--;
drh55176252008-01-22 14:50:16 +0000686 yymajor = YYNOCODE;
drh75897232000-05-29 14:26:00 +0000687 }else if( yyact < YYNSTATE + YYNRULE ){
drh1f245e42002-03-11 13:55:50 +0000688 yy_reduce(yypParser,yyact-YYNSTATE);
drhc4dd3fd2008-01-22 01:48:05 +0000689 }else{
690 assert( yyact == YY_ERROR_ACTION );
691#ifdef YYERRORSYMBOL
drh3ddfdf72003-12-22 14:53:19 +0000692 int yymx;
drhc4dd3fd2008-01-22 01:48:05 +0000693#endif
drh75897232000-05-29 14:26:00 +0000694#ifndef NDEBUG
695 if( yyTraceFILE ){
696 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
697 }
698#endif
699#ifdef YYERRORSYMBOL
700 /* A syntax error has occurred.
701 ** The response to an error depends upon whether or not the
702 ** grammar defines an error token "ERROR".
703 **
704 ** This is what we do if the grammar does define ERROR:
705 **
706 ** * Call the %syntax_error function.
707 **
708 ** * Begin popping the stack until we enter a state where
709 ** it is legal to shift the error symbol, then shift
710 ** the error symbol.
711 **
712 ** * Set the error count to three.
713 **
714 ** * Begin accepting and shifting new tokens. No new error
715 ** processing will occur until three tokens have been
716 ** shifted successfully.
717 **
718 */
drh1f245e42002-03-11 13:55:50 +0000719 if( yypParser->yyerrcnt<0 ){
720 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000721 }
drh3ddfdf72003-12-22 14:53:19 +0000722 yymx = yypParser->yystack[yypParser->yyidx].major;
723 if( yymx==YYERRORSYMBOL || yyerrorhit ){
drh75897232000-05-29 14:26:00 +0000724#ifndef NDEBUG
725 if( yyTraceFILE ){
726 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
727 yyTracePrompt,yyTokenName[yymajor]);
728 }
729#endif
730 yy_destructor(yymajor,&yyminorunion);
731 yymajor = YYNOCODE;
732 }else{
733 while(
drh1f245e42002-03-11 13:55:50 +0000734 yypParser->yyidx >= 0 &&
drh3ddfdf72003-12-22 14:53:19 +0000735 yymx != YYERRORSYMBOL &&
drhf3a58882006-05-08 15:14:19 +0000736 (yyact = yy_find_reduce_action(
737 yypParser->yystack[yypParser->yyidx].stateno,
738 YYERRORSYMBOL)) >= YYNSTATE
drh75897232000-05-29 14:26:00 +0000739 ){
740 yy_pop_parser_stack(yypParser);
741 }
drh1f245e42002-03-11 13:55:50 +0000742 if( yypParser->yyidx < 0 || yymajor==0 ){
drh75897232000-05-29 14:26:00 +0000743 yy_destructor(yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000744 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000745 yymajor = YYNOCODE;
drh3ddfdf72003-12-22 14:53:19 +0000746 }else if( yymx!=YYERRORSYMBOL ){
drh75897232000-05-29 14:26:00 +0000747 YYMINORTYPE u2;
748 u2.YYERRSYMDT = 0;
749 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
750 }
751 }
drh1f245e42002-03-11 13:55:50 +0000752 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000753 yyerrorhit = 1;
754#else /* YYERRORSYMBOL is not defined */
755 /* This is what we do if the grammar does not define ERROR:
756 **
757 ** * Report an error message, and throw away the input token.
758 **
759 ** * If the input token is $, then fail the parse.
760 **
761 ** As before, subsequent error messages are suppressed until
762 ** three input tokens have been successfully shifted.
763 */
drh1f245e42002-03-11 13:55:50 +0000764 if( yypParser->yyerrcnt<=0 ){
765 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000766 }
drh1f245e42002-03-11 13:55:50 +0000767 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000768 yy_destructor(yymajor,&yyminorunion);
769 if( yyendofinput ){
drh1f245e42002-03-11 13:55:50 +0000770 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000771 }
772 yymajor = YYNOCODE;
773#endif
drh75897232000-05-29 14:26:00 +0000774 }
drh1f245e42002-03-11 13:55:50 +0000775 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
drh75897232000-05-29 14:26:00 +0000776 return;
777}