blob: cdf4ca5a1a54e9d10eb642454ff5904f1750d99d [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** YYERRORSYMBOL is the code number of the error symbol. If not
54** defined, then do no error processing.
drh3bd48ab2015-09-07 18:23:37 +000055** YYNSTATE the combined number of states.
56** YYNRULE the number of rules in the grammar
57** YY_MAX_SHIFT Maximum value for shift actions
58** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
59** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
60** YY_MIN_REDUCE Maximum value for reduce actions
61** YY_ERROR_ACTION The yy_action[] code for syntax error
62** YY_ACCEPT_ACTION The yy_action[] code for accept
63** YY_NO_ACTION The yy_action[] code for no-op
drh75897232000-05-29 14:26:00 +000064*/
65%%
drh75897232000-05-29 14:26:00 +000066
drh26c9b5e2008-04-11 14:56:53 +000067/* The yyzerominor constant is used to initialize instances of
68** YYMINORTYPE objects to zero. */
drh15b024c2008-12-11 02:20:43 +000069static const YYMINORTYPE yyzerominor = { 0 };
70
drh8a415d32009-06-12 13:53:51 +000071/* Define the yytestcase() macro to be a no-op if is not already defined
72** otherwise.
73**
74** Applications can choose to define yytestcase() in the %include section
75** to a macro that can assist in verifying code coverage. For production
76** code the yytestcase() macro should be turned off. But it is useful
77** for testing.
78*/
79#ifndef yytestcase
80# define yytestcase(X)
81#endif
82
drh26c9b5e2008-04-11 14:56:53 +000083
drh34ff57b2008-07-14 12:27:51 +000084/* Next are the tables used to determine what action to take based on the
drh8b582012003-10-21 13:16:03 +000085** current state and lookahead token. These tables are used to implement
86** functions that take a state number and lookahead value and return an
87** action integer.
drh75897232000-05-29 14:26:00 +000088**
drh8548a052003-10-22 22:15:27 +000089** Suppose the action integer is N. Then the action is determined as
90** follows
drh75897232000-05-29 14:26:00 +000091**
drh3bd48ab2015-09-07 18:23:37 +000092** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
drh8548a052003-10-22 22:15:27 +000093** token onto the stack and goto state N.
94**
drh3bd48ab2015-09-07 18:23:37 +000095** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
96** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
drh8548a052003-10-22 22:15:27 +000097**
drh3bd48ab2015-09-07 18:23:37 +000098** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
99** and YY_MAX_REDUCE
100
101** N == YY_ERROR_ACTION A syntax error has occurred.
drh8548a052003-10-22 22:15:27 +0000102**
drh3bd48ab2015-09-07 18:23:37 +0000103** N == YY_ACCEPT_ACTION The parser accepts its input.
drh8548a052003-10-22 22:15:27 +0000104**
drh3bd48ab2015-09-07 18:23:37 +0000105** N == YY_NO_ACTION No such action. Denotes unused
drh8548a052003-10-22 22:15:27 +0000106** slots in the yy_action[] table.
107**
108** The action table is constructed as a single large table named yy_action[].
109** Given state S and lookahead X, the action is computed as
drh75897232000-05-29 14:26:00 +0000110**
drh8b582012003-10-21 13:16:03 +0000111** yy_action[ yy_shift_ofst[S] + X ]
112**
drh8548a052003-10-22 22:15:27 +0000113** If the index value yy_shift_ofst[S]+X is out of range or if the value
drh8b582012003-10-21 13:16:03 +0000114** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
115** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
116** and that yy_default[S] should be used instead.
117**
118** The formula above is for computing the action when the lookahead is
119** a terminal symbol. If the lookahead is a non-terminal (as occurs after
120** a reduce action) then the yy_reduce_ofst[] array is used in place of
121** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
122** YY_SHIFT_USE_DFLT.
123**
124** The following are the tables generated in this section:
125**
126** yy_action[] A single table containing all actions.
127** yy_lookahead[] A table containing the lookahead for each entry in
128** yy_action. Used to detect hash collisions.
129** yy_shift_ofst[] For each state, the offset into yy_action for
130** shifting terminals.
131** yy_reduce_ofst[] For each state, the offset into yy_action for
132** shifting non-terminals after a reduce.
133** yy_default[] Default action for each state.
drh75897232000-05-29 14:26:00 +0000134*/
drh75897232000-05-29 14:26:00 +0000135%%
drh75897232000-05-29 14:26:00 +0000136
drh0bd1f4e2002-06-06 18:54:39 +0000137/* The next table maps tokens into fallback tokens. If a construct
138** like the following:
139**
140** %fallback ID X Y Z.
141**
drh34ff57b2008-07-14 12:27:51 +0000142** appears in the grammar, then ID becomes a fallback token for X, Y,
drh0bd1f4e2002-06-06 18:54:39 +0000143** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
144** but it does not parse, the type of the token is changed to ID and
145** the parse is retried before an error is thrown.
146*/
147#ifdef YYFALLBACK
148static const YYCODETYPE yyFallback[] = {
149%%
150};
151#endif /* YYFALLBACK */
152
drh75897232000-05-29 14:26:00 +0000153/* The following structure represents a single element of the
154** parser's stack. Information stored includes:
155**
156** + The state number for the parser at this level of the stack.
157**
158** + The value of the token stored at this level of the stack.
159** (In other words, the "major" token.)
160**
161** + The semantic value stored at this level of the stack. This is
162** the information used by the action routines in the grammar.
163** It is sometimes called the "minor" token.
drha248a722015-09-07 19:52:55 +0000164**
165** After the "shift" half of a SHIFTREDUCE action, the stateno field
166** actually contains the reduce action for the second half of the
167** SHIFTREDUCE.
drh75897232000-05-29 14:26:00 +0000168*/
169struct yyStackEntry {
drha248a722015-09-07 19:52:55 +0000170 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
drh2abcd582008-07-24 23:34:07 +0000171 YYCODETYPE major; /* The major token value. This is the code
172 ** number for the token at this stack level */
173 YYMINORTYPE minor; /* The user-supplied minor token value. This
174 ** is the value of the token */
drh75897232000-05-29 14:26:00 +0000175};
drh1f245e42002-03-11 13:55:50 +0000176typedef struct yyStackEntry yyStackEntry;
drh75897232000-05-29 14:26:00 +0000177
178/* The state of the parser is completely contained in an instance of
179** the following structure */
180struct yyParser {
drh1f245e42002-03-11 13:55:50 +0000181 int yyidx; /* Index of top element in stack */
drhec424a52008-07-25 15:39:03 +0000182#ifdef YYTRACKMAXSTACKDEPTH
183 int yyidxMax; /* Maximum value of yyidx */
184#endif
drh1f245e42002-03-11 13:55:50 +0000185 int yyerrcnt; /* Shifts left before out of the error */
drh1f245e42002-03-11 13:55:50 +0000186 ParseARG_SDECL /* A place to hold %extra_argument */
drhb19fd012007-03-29 01:44:45 +0000187#if YYSTACKDEPTH<=0
188 int yystksz; /* Current side of the stack */
189 yyStackEntry *yystack; /* The parser's stack */
190#else
drh1f245e42002-03-11 13:55:50 +0000191 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
drhb19fd012007-03-29 01:44:45 +0000192#endif
drh75897232000-05-29 14:26:00 +0000193};
194typedef struct yyParser yyParser;
195
196#ifndef NDEBUG
197#include <stdio.h>
198static FILE *yyTraceFILE = 0;
199static char *yyTracePrompt = 0;
drh0bd1f4e2002-06-06 18:54:39 +0000200#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000201
drh0bd1f4e2002-06-06 18:54:39 +0000202#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000203/*
204** Turn parser tracing on by giving a stream to which to write the trace
205** and a prompt to preface each trace message. Tracing is turned off
206** by making either argument NULL
207**
208** Inputs:
209** <ul>
210** <li> A FILE* to which trace output should be written.
211** If NULL, then tracing is turned off.
212** <li> A prefix string written at the beginning of every
213** line of trace output. If NULL, then tracing is
214** turned off.
215** </ul>
216**
217** Outputs:
218** None.
219*/
drh75897232000-05-29 14:26:00 +0000220void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
221 yyTraceFILE = TraceFILE;
222 yyTracePrompt = zTracePrompt;
223 if( yyTraceFILE==0 ) yyTracePrompt = 0;
224 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
225}
drh0bd1f4e2002-06-06 18:54:39 +0000226#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000227
drh0bd1f4e2002-06-06 18:54:39 +0000228#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000229/* For tracing shifts, the names of all terminals and nonterminals
230** are required. The following table supplies these names */
drh57196282004-10-06 15:41:16 +0000231static const char *const yyTokenName[] = {
drh75897232000-05-29 14:26:00 +0000232%%
233};
drh0bd1f4e2002-06-06 18:54:39 +0000234#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000235
drh0bd1f4e2002-06-06 18:54:39 +0000236#ifndef NDEBUG
237/* For tracing reduce actions, the names of all rules are required.
238*/
drh57196282004-10-06 15:41:16 +0000239static const char *const yyRuleName[] = {
drh0bd1f4e2002-06-06 18:54:39 +0000240%%
241};
242#endif /* NDEBUG */
drha1b351a2001-09-14 16:42:12 +0000243
drh960e8c62001-04-03 16:53:21 +0000244
drhb19fd012007-03-29 01:44:45 +0000245#if YYSTACKDEPTH<=0
246/*
247** Try to increase the size of the parser stack.
248*/
249static void yyGrowStack(yyParser *p){
250 int newSize;
251 yyStackEntry *pNew;
252
253 newSize = p->yystksz*2 + 100;
254 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
255 if( pNew ){
256 p->yystack = pNew;
257 p->yystksz = newSize;
258#ifndef NDEBUG
259 if( yyTraceFILE ){
260 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
261 yyTracePrompt, p->yystksz);
262 }
263#endif
264 }
265}
266#endif
267
drh75897232000-05-29 14:26:00 +0000268/*
269** This function allocates a new parser.
270** The only argument is a pointer to a function which works like
271** malloc.
272**
273** Inputs:
274** A pointer to the function used to allocate memory.
275**
276** Outputs:
277** A pointer to a parser. This pointer is used in subsequent calls
278** to Parse and ParseFree.
279*/
drh7218ac72002-03-10 21:21:00 +0000280void *ParseAlloc(void *(*mallocProc)(size_t)){
drh75897232000-05-29 14:26:00 +0000281 yyParser *pParser;
drh7218ac72002-03-10 21:21:00 +0000282 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
drh75897232000-05-29 14:26:00 +0000283 if( pParser ){
drh1f245e42002-03-11 13:55:50 +0000284 pParser->yyidx = -1;
drhec424a52008-07-25 15:39:03 +0000285#ifdef YYTRACKMAXSTACKDEPTH
286 pParser->yyidxMax = 0;
287#endif
drhb19fd012007-03-29 01:44:45 +0000288#if YYSTACKDEPTH<=0
drh4c651782008-11-18 23:25:54 +0000289 pParser->yystack = NULL;
290 pParser->yystksz = 0;
drhb19fd012007-03-29 01:44:45 +0000291 yyGrowStack(pParser);
292#endif
drh75897232000-05-29 14:26:00 +0000293 }
294 return pParser;
295}
296
297/* The following function deletes the value associated with a
298** symbol. The symbol can be either a terminal or nonterminal.
299** "yymajor" is the symbol code, and "yypminor" is a pointer to
300** the value.
301*/
drh633e6d52008-07-28 19:34:53 +0000302static void yy_destructor(
303 yyParser *yypParser, /* The parser */
304 YYCODETYPE yymajor, /* Type code for object to destroy */
305 YYMINORTYPE *yypminor /* The object to be destroyed */
306){
307 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000308 switch( yymajor ){
309 /* Here is inserted the actions which take place when a
310 ** terminal or non-terminal is destroyed. This can happen
311 ** when the symbol is popped from the stack during a
312 ** reduce or during error processing or when a parser is
313 ** being destroyed before it is finished parsing.
314 **
315 ** Note: during a reduce, the only symbols destroyed are those
316 ** which appear on the RHS of the rule, but which are not used
317 ** inside the C code.
318 */
319%%
320 default: break; /* If no destructor action specified: do nothing */
321 }
322}
323
324/*
325** Pop the parser's stack once.
326**
327** If there is a destructor routine associated with the token which
328** is popped from the stack, then call it.
329**
330** Return the major token number for the symbol popped.
331*/
332static int yy_pop_parser_stack(yyParser *pParser){
333 YYCODETYPE yymajor;
drh3ddfdf72003-12-22 14:53:19 +0000334 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
drh75897232000-05-29 14:26:00 +0000335
drh1f245e42002-03-11 13:55:50 +0000336 if( pParser->yyidx<0 ) return 0;
drh75897232000-05-29 14:26:00 +0000337#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000338 if( yyTraceFILE && pParser->yyidx>=0 ){
drh75897232000-05-29 14:26:00 +0000339 fprintf(yyTraceFILE,"%sPopping %s\n",
340 yyTracePrompt,
drh3ddfdf72003-12-22 14:53:19 +0000341 yyTokenName[yytos->major]);
drh75897232000-05-29 14:26:00 +0000342 }
343#endif
drh3ddfdf72003-12-22 14:53:19 +0000344 yymajor = yytos->major;
drh633e6d52008-07-28 19:34:53 +0000345 yy_destructor(pParser, yymajor, &yytos->minor);
drh1f245e42002-03-11 13:55:50 +0000346 pParser->yyidx--;
drh75897232000-05-29 14:26:00 +0000347 return yymajor;
348}
349
350/*
351** Deallocate and destroy a parser. Destructors are all called for
352** all stack elements before shutting the parser down.
353**
354** Inputs:
355** <ul>
356** <li> A pointer to the parser. This should be a pointer
357** obtained from ParseAlloc.
358** <li> A pointer to a function used to reclaim memory obtained
359** from malloc.
360** </ul>
361*/
drh75897232000-05-29 14:26:00 +0000362void ParseFree(
drh960e8c62001-04-03 16:53:21 +0000363 void *p, /* The parser to be deleted */
364 void (*freeProc)(void*) /* Function used to reclaim memory */
drh75897232000-05-29 14:26:00 +0000365){
366 yyParser *pParser = (yyParser*)p;
367 if( pParser==0 ) return;
drh1f245e42002-03-11 13:55:50 +0000368 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
drhb19fd012007-03-29 01:44:45 +0000369#if YYSTACKDEPTH<=0
370 free(pParser->yystack);
371#endif
drh960e8c62001-04-03 16:53:21 +0000372 (*freeProc)((void*)pParser);
drh75897232000-05-29 14:26:00 +0000373}
374
375/*
drhec424a52008-07-25 15:39:03 +0000376** Return the peak depth of the stack for a parser.
377*/
378#ifdef YYTRACKMAXSTACKDEPTH
379int ParseStackPeak(void *p){
380 yyParser *pParser = (yyParser*)p;
381 return pParser->yyidxMax;
382}
383#endif
384
385/*
drh8b582012003-10-21 13:16:03 +0000386** Find the appropriate action for a parser given the terminal
387** look-ahead token iLookAhead.
drh75897232000-05-29 14:26:00 +0000388**
389** If the look-ahead token is YYNOCODE, then check to see if the action is
390** independent of the look-ahead. If it is, return the action, otherwise
391** return YY_NO_ACTION.
392*/
drh8b582012003-10-21 13:16:03 +0000393static int yy_find_shift_action(
drh75897232000-05-29 14:26:00 +0000394 yyParser *pParser, /* The parser */
drhd9f291e2006-06-13 11:15:47 +0000395 YYCODETYPE iLookAhead /* The look-ahead token */
drh75897232000-05-29 14:26:00 +0000396){
drh8b582012003-10-21 13:16:03 +0000397 int i;
drh3ddfdf72003-12-22 14:53:19 +0000398 int stateno = pParser->yystack[pParser->yyidx].stateno;
drha248a722015-09-07 19:52:55 +0000399
400 if( stateno>=YY_MIN_REDUCE ) return stateno;
drhae2a4082015-09-07 20:02:39 +0000401 assert( stateno <= YY_SHIFT_COUNT );
402 i = yy_shift_ofst[stateno];
403 if( i==YY_SHIFT_USE_DFLT ) return yy_default[stateno];
drh01495b92008-01-23 12:52:40 +0000404 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000405 i += iLookAhead;
drhf16371d2009-11-03 19:18:31 +0000406 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
drhe09daa92006-06-10 13:29:31 +0000407 if( iLookAhead>0 ){
drh4767d972006-06-14 15:03:49 +0000408#ifdef YYFALLBACK
drhb27b7f52008-12-10 18:03:45 +0000409 YYCODETYPE iFallback; /* Fallback token */
drhe09daa92006-06-10 13:29:31 +0000410 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
411 && (iFallback = yyFallback[iLookAhead])!=0 ){
drh4767d972006-06-14 15:03:49 +0000412#ifndef NDEBUG
drhe09daa92006-06-10 13:29:31 +0000413 if( yyTraceFILE ){
414 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
415 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
416 }
drh4767d972006-06-14 15:03:49 +0000417#endif
drhe09daa92006-06-10 13:29:31 +0000418 return yy_find_shift_action(pParser, iFallback);
drh0bd1f4e2002-06-06 18:54:39 +0000419 }
drh4767d972006-06-14 15:03:49 +0000420#endif
421#ifdef YYWILDCARD
422 {
423 int j = i - iLookAhead + YYWILDCARD;
drhf16371d2009-11-03 19:18:31 +0000424 if(
425#if YY_SHIFT_MIN+YYWILDCARD<0
426 j>=0 &&
427#endif
428#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
429 j<YY_ACTTAB_COUNT &&
430#endif
431 yy_lookahead[j]==YYWILDCARD
432 ){
drh4767d972006-06-14 15:03:49 +0000433#ifndef NDEBUG
434 if( yyTraceFILE ){
435 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
436 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
437 }
438#endif /* NDEBUG */
439 return yy_action[j];
drhe09daa92006-06-10 13:29:31 +0000440 }
drhe09daa92006-06-10 13:29:31 +0000441 }
drh4767d972006-06-14 15:03:49 +0000442#endif /* YYWILDCARD */
drh0bd1f4e2002-06-06 18:54:39 +0000443 }
drh3ddfdf72003-12-22 14:53:19 +0000444 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000445 }else{
446 return yy_action[i];
447 }
448}
449
450/*
451** Find the appropriate action for a parser given the non-terminal
452** look-ahead token iLookAhead.
453**
454** If the look-ahead token is YYNOCODE, then check to see if the action is
455** independent of the look-ahead. If it is, return the action, otherwise
456** return YY_NO_ACTION.
457*/
458static int yy_find_reduce_action(
drh161aba32005-02-01 04:09:36 +0000459 int stateno, /* Current state number */
drhd9f291e2006-06-13 11:15:47 +0000460 YYCODETYPE iLookAhead /* The look-ahead token */
drh8b582012003-10-21 13:16:03 +0000461){
462 int i;
drh7f7c2572008-04-27 18:45:10 +0000463#ifdef YYERRORSYMBOL
drhf16371d2009-11-03 19:18:31 +0000464 if( stateno>YY_REDUCE_COUNT ){
drh7f7c2572008-04-27 18:45:10 +0000465 return yy_default[stateno];
466 }
467#else
drhf16371d2009-11-03 19:18:31 +0000468 assert( stateno<=YY_REDUCE_COUNT );
drh7f7c2572008-04-27 18:45:10 +0000469#endif
drh01495b92008-01-23 12:52:40 +0000470 i = yy_reduce_ofst[stateno];
471 assert( i!=YY_REDUCE_USE_DFLT );
472 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000473 i += iLookAhead;
drh7f7c2572008-04-27 18:45:10 +0000474#ifdef YYERRORSYMBOL
drhf16371d2009-11-03 19:18:31 +0000475 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
drh7f7c2572008-04-27 18:45:10 +0000476 return yy_default[stateno];
477 }
478#else
drhf16371d2009-11-03 19:18:31 +0000479 assert( i>=0 && i<YY_ACTTAB_COUNT );
drh01495b92008-01-23 12:52:40 +0000480 assert( yy_lookahead[i]==iLookAhead );
drh7f7c2572008-04-27 18:45:10 +0000481#endif
drh01495b92008-01-23 12:52:40 +0000482 return yy_action[i];
drh75897232000-05-29 14:26:00 +0000483}
484
485/*
drhb19fd012007-03-29 01:44:45 +0000486** The following routine is called if the stack overflows.
487*/
drhb6018612007-03-30 13:35:05 +0000488static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
drhb19fd012007-03-29 01:44:45 +0000489 ParseARG_FETCH;
490 yypParser->yyidx--;
491#ifndef NDEBUG
492 if( yyTraceFILE ){
493 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
494 }
495#endif
496 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
497 /* Here code is inserted which will execute if the parser
498 ** stack every overflows */
499%%
500 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
501}
502
503/*
drha248a722015-09-07 19:52:55 +0000504** Print tracing information for a SHIFT action
505*/
506#ifndef NDEBUG
507static void yyTraceShift(yyParser *yypParser, int yyNewState){
508 if( yyTraceFILE ){
509 int i;
510 if( yyNewState<YYNSTATE ){
511 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
512 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
513 for(i=1; i<=yypParser->yyidx; i++)
514 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
515 fprintf(yyTraceFILE,"\n");
516 }else{
517 fprintf(yyTraceFILE,"%sShift *\n",yyTracePrompt);
518 }
519 }
520}
521#else
522# define yyTraceShift(X,Y)
523#endif
524
525/*
drh3bd48ab2015-09-07 18:23:37 +0000526** Perform a shift action. Return the number of errors.
drh75897232000-05-29 14:26:00 +0000527*/
drha248a722015-09-07 19:52:55 +0000528static void yy_shift(
drh75897232000-05-29 14:26:00 +0000529 yyParser *yypParser, /* The parser to be shifted */
530 int yyNewState, /* The new state to shift in */
531 int yyMajor, /* The major token to shift in */
drh34ff57b2008-07-14 12:27:51 +0000532 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
drh75897232000-05-29 14:26:00 +0000533){
drh3ddfdf72003-12-22 14:53:19 +0000534 yyStackEntry *yytos;
drh1f245e42002-03-11 13:55:50 +0000535 yypParser->yyidx++;
drhec424a52008-07-25 15:39:03 +0000536#ifdef YYTRACKMAXSTACKDEPTH
537 if( yypParser->yyidx>yypParser->yyidxMax ){
538 yypParser->yyidxMax = yypParser->yyidx;
539 }
540#endif
drhb19fd012007-03-29 01:44:45 +0000541#if YYSTACKDEPTH>0
drh1f245e42002-03-11 13:55:50 +0000542 if( yypParser->yyidx>=YYSTACKDEPTH ){
drhb6018612007-03-30 13:35:05 +0000543 yyStackOverflow(yypParser, yypMinor);
drha248a722015-09-07 19:52:55 +0000544 return;
drh75897232000-05-29 14:26:00 +0000545 }
drhb19fd012007-03-29 01:44:45 +0000546#else
547 if( yypParser->yyidx>=yypParser->yystksz ){
548 yyGrowStack(yypParser);
549 if( yypParser->yyidx>=yypParser->yystksz ){
drhb6018612007-03-30 13:35:05 +0000550 yyStackOverflow(yypParser, yypMinor);
drha248a722015-09-07 19:52:55 +0000551 return;
drhb19fd012007-03-29 01:44:45 +0000552 }
553 }
554#endif
drh3ddfdf72003-12-22 14:53:19 +0000555 yytos = &yypParser->yystack[yypParser->yyidx];
drhb27b7f52008-12-10 18:03:45 +0000556 yytos->stateno = (YYACTIONTYPE)yyNewState;
557 yytos->major = (YYCODETYPE)yyMajor;
drh3ddfdf72003-12-22 14:53:19 +0000558 yytos->minor = *yypMinor;
drha248a722015-09-07 19:52:55 +0000559 yyTraceShift(yypParser, yyNewState);
drh75897232000-05-29 14:26:00 +0000560}
561
562/* The following table contains information about every rule that
563** is used during the reduce.
564*/
drh57196282004-10-06 15:41:16 +0000565static const struct {
drh75897232000-05-29 14:26:00 +0000566 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
567 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
568} yyRuleInfo[] = {
569%%
570};
571
drh1f245e42002-03-11 13:55:50 +0000572static void yy_accept(yyParser*); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000573
574/*
575** Perform a reduce action and the shift that must immediately
576** follow the reduce.
577*/
578static void yy_reduce(
579 yyParser *yypParser, /* The parser */
580 int yyruleno /* Number of the rule by which to reduce */
drh75897232000-05-29 14:26:00 +0000581){
582 int yygoto; /* The next state */
583 int yyact; /* The next action */
584 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
drh1f245e42002-03-11 13:55:50 +0000585 yyStackEntry *yymsp; /* The top of the parser's stack */
drh75897232000-05-29 14:26:00 +0000586 int yysize; /* Amount to pop the stack */
drh1f245e42002-03-11 13:55:50 +0000587 ParseARG_FETCH;
drh3ddfdf72003-12-22 14:53:19 +0000588 yymsp = &yypParser->yystack[yypParser->yyidx];
drh0bd1f4e2002-06-06 18:54:39 +0000589#ifndef NDEBUG
590 if( yyTraceFILE && yyruleno>=0
drhd9f291e2006-06-13 11:15:47 +0000591 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
drh3bd48ab2015-09-07 18:23:37 +0000592 yysize = yyRuleInfo[yyruleno].nrhs;
593 fprintf(yyTraceFILE, "%sReduce [%s] -> state %d.\n", yyTracePrompt,
594 yyRuleName[yyruleno], yymsp[-yysize].stateno);
drh0bd1f4e2002-06-06 18:54:39 +0000595 }
596#endif /* NDEBUG */
597
drh7bec5052005-02-06 02:45:41 +0000598 /* Silence complaints from purify about yygotominor being uninitialized
599 ** in some cases when it is copied into the stack after the following
600 ** switch. yygotominor is uninitialized when a rule reduces that does
601 ** not set the value of its left-hand side nonterminal. Leaving the
602 ** value of the nonterminal uninitialized is utterly harmless as long
603 ** as the value is never used. So really the only thing this code
604 ** accomplishes is to quieten purify.
drhcb6c5652007-01-16 18:19:12 +0000605 **
606 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
607 ** without this code, their parser segfaults. I'm not sure what there
608 ** parser is doing to make this happen. This is the second bug report
609 ** from wireshark this week. Clearly they are stressing Lemon in ways
610 ** that it has not been previously stressed... (SQLite ticket #2172)
drh7bec5052005-02-06 02:45:41 +0000611 */
drh26c9b5e2008-04-11 14:56:53 +0000612 /*memset(&yygotominor, 0, sizeof(yygotominor));*/
613 yygotominor = yyzerominor;
drhcb6c5652007-01-16 18:19:12 +0000614
drh7bec5052005-02-06 02:45:41 +0000615
drh75897232000-05-29 14:26:00 +0000616 switch( yyruleno ){
617 /* Beginning here are the reduction cases. A typical example
618 ** follows:
619 ** case 0:
drh75897232000-05-29 14:26:00 +0000620 ** #line <lineno> <grammarfile>
621 ** { ... } // User supplied code
622 ** #line <lineno> <thisfile>
623 ** break;
624 */
625%%
626 };
627 yygoto = yyRuleInfo[yyruleno].lhs;
628 yysize = yyRuleInfo[yyruleno].nrhs;
drh1f245e42002-03-11 13:55:50 +0000629 yypParser->yyidx -= yysize;
drh3abbd392008-12-10 23:04:13 +0000630 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
drha248a722015-09-07 19:52:55 +0000631 if( yyact <= YY_MAX_SHIFTREDUCE ){
632 if( yyact>YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
drh3bd48ab2015-09-07 18:23:37 +0000633 /* If the reduce action popped at least
drh490a73b2005-02-01 03:20:00 +0000634 ** one element off the stack, then we can push the new element back
635 ** onto the stack here, and skip the stack overflow test in yy_shift().
636 ** That gives a significant speed improvement. */
637 if( yysize ){
638 yypParser->yyidx++;
639 yymsp -= yysize-1;
shaned87897d2009-01-30 05:40:27 +0000640 yymsp->stateno = (YYACTIONTYPE)yyact;
641 yymsp->major = (YYCODETYPE)yygoto;
drh490a73b2005-02-01 03:20:00 +0000642 yymsp->minor = yygotominor;
drha248a722015-09-07 19:52:55 +0000643 yyTraceShift(yypParser, yyact);
drh3bd48ab2015-09-07 18:23:37 +0000644 }else{
drha248a722015-09-07 19:52:55 +0000645 yy_shift(yypParser,yyact,yygoto,&yygotominor);
drh490a73b2005-02-01 03:20:00 +0000646 }
drh4b2f9362008-01-22 23:37:09 +0000647 }else{
drh3bd48ab2015-09-07 18:23:37 +0000648 assert( yyact == YY_ACCEPT_ACTION );
drh1f245e42002-03-11 13:55:50 +0000649 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000650 }
651}
652
653/*
654** The following code executes when the parse fails
655*/
drhd3ec02d2009-06-12 02:27:14 +0000656#ifndef YYNOERRORRECOVERY
drh75897232000-05-29 14:26:00 +0000657static void yy_parse_failed(
658 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000659){
drh1f245e42002-03-11 13:55:50 +0000660 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000661#ifndef NDEBUG
662 if( yyTraceFILE ){
663 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
664 }
665#endif
drh1f245e42002-03-11 13:55:50 +0000666 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000667 /* Here code is inserted which will be executed whenever the
668 ** parser fails */
669%%
drh1f245e42002-03-11 13:55:50 +0000670 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000671}
drhd3ec02d2009-06-12 02:27:14 +0000672#endif /* YYNOERRORRECOVERY */
drh75897232000-05-29 14:26:00 +0000673
674/*
675** The following code executes when a syntax error first occurs.
676*/
677static void yy_syntax_error(
678 yyParser *yypParser, /* The parser */
679 int yymajor, /* The major type of the error token */
680 YYMINORTYPE yyminor /* The minor type of the error token */
drh75897232000-05-29 14:26:00 +0000681){
drh1f245e42002-03-11 13:55:50 +0000682 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000683#define TOKEN (yyminor.yy0)
684%%
drh1f245e42002-03-11 13:55:50 +0000685 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000686}
687
688/*
689** The following is executed when the parser accepts
690*/
691static void yy_accept(
692 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000693){
drh1f245e42002-03-11 13:55:50 +0000694 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000695#ifndef NDEBUG
696 if( yyTraceFILE ){
697 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
698 }
699#endif
drh1f245e42002-03-11 13:55:50 +0000700 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000701 /* Here code is inserted which will be executed whenever the
702 ** parser accepts */
703%%
drh1f245e42002-03-11 13:55:50 +0000704 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000705}
706
707/* The main parser program.
708** The first argument is a pointer to a structure obtained from
709** "ParseAlloc" which describes the current state of the parser.
710** The second argument is the major token number. The third is
711** the minor token. The fourth optional argument is whatever the
712** user wants (and specified in the grammar) and is available for
713** use by the action routines.
714**
715** Inputs:
716** <ul>
717** <li> A pointer to the parser (an opaque structure.)
718** <li> The major token number.
719** <li> The minor token number.
720** <li> An option argument of a grammar-specified type.
721** </ul>
722**
723** Outputs:
724** None.
725*/
drh75897232000-05-29 14:26:00 +0000726void Parse(
727 void *yyp, /* The parser */
728 int yymajor, /* The major token code number */
729 ParseTOKENTYPE yyminor /* The value for the token */
drh1f245e42002-03-11 13:55:50 +0000730 ParseARG_PDECL /* Optional %extra_argument parameter */
drh75897232000-05-29 14:26:00 +0000731){
732 YYMINORTYPE yyminorunion;
733 int yyact; /* The parser action. */
734 int yyendofinput; /* True if we are at the end of input */
drhc4dd3fd2008-01-22 01:48:05 +0000735#ifdef YYERRORSYMBOL
drh75897232000-05-29 14:26:00 +0000736 int yyerrorhit = 0; /* True if yymajor has invoked an error */
drhc4dd3fd2008-01-22 01:48:05 +0000737#endif
drh75897232000-05-29 14:26:00 +0000738 yyParser *yypParser; /* The parser */
739
740 /* (re)initialize the parser, if necessary */
741 yypParser = (yyParser*)yyp;
drh1f245e42002-03-11 13:55:50 +0000742 if( yypParser->yyidx<0 ){
drhb7bac722007-03-29 02:26:45 +0000743#if YYSTACKDEPTH<=0
drhb19fd012007-03-29 01:44:45 +0000744 if( yypParser->yystksz <=0 ){
drh26c9b5e2008-04-11 14:56:53 +0000745 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
746 yyminorunion = yyzerominor;
drhb6018612007-03-30 13:35:05 +0000747 yyStackOverflow(yypParser, &yyminorunion);
drhb19fd012007-03-29 01:44:45 +0000748 return;
749 }
drhb7bac722007-03-29 02:26:45 +0000750#endif
drh1f245e42002-03-11 13:55:50 +0000751 yypParser->yyidx = 0;
752 yypParser->yyerrcnt = -1;
drh3ddfdf72003-12-22 14:53:19 +0000753 yypParser->yystack[0].stateno = 0;
754 yypParser->yystack[0].major = 0;
drh75897232000-05-29 14:26:00 +0000755 }
756 yyminorunion.yy0 = yyminor;
757 yyendofinput = (yymajor==0);
drh1f245e42002-03-11 13:55:50 +0000758 ParseARG_STORE;
drh75897232000-05-29 14:26:00 +0000759
760#ifndef NDEBUG
761 if( yyTraceFILE ){
762 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
763 }
764#endif
765
766 do{
drh3abbd392008-12-10 23:04:13 +0000767 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
drh3bd48ab2015-09-07 18:23:37 +0000768 if( yyact <= YY_MAX_SHIFTREDUCE ){
drha248a722015-09-07 19:52:55 +0000769 if( yyact > YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
770 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
771 yypParser->yyerrcnt--;
772 yymajor = YYNOCODE;
drh3bd48ab2015-09-07 18:23:37 +0000773 }else if( yyact <= YY_MAX_REDUCE ){
774 yy_reduce(yypParser,yyact-YY_MIN_REDUCE);
drhc4dd3fd2008-01-22 01:48:05 +0000775 }else{
776 assert( yyact == YY_ERROR_ACTION );
777#ifdef YYERRORSYMBOL
drh3ddfdf72003-12-22 14:53:19 +0000778 int yymx;
drhc4dd3fd2008-01-22 01:48:05 +0000779#endif
drh75897232000-05-29 14:26:00 +0000780#ifndef NDEBUG
781 if( yyTraceFILE ){
782 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
783 }
784#endif
785#ifdef YYERRORSYMBOL
786 /* A syntax error has occurred.
787 ** The response to an error depends upon whether or not the
788 ** grammar defines an error token "ERROR".
789 **
790 ** This is what we do if the grammar does define ERROR:
791 **
792 ** * Call the %syntax_error function.
793 **
794 ** * Begin popping the stack until we enter a state where
795 ** it is legal to shift the error symbol, then shift
796 ** the error symbol.
797 **
798 ** * Set the error count to three.
799 **
800 ** * Begin accepting and shifting new tokens. No new error
801 ** processing will occur until three tokens have been
802 ** shifted successfully.
803 **
804 */
drh1f245e42002-03-11 13:55:50 +0000805 if( yypParser->yyerrcnt<0 ){
806 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000807 }
drh3ddfdf72003-12-22 14:53:19 +0000808 yymx = yypParser->yystack[yypParser->yyidx].major;
809 if( yymx==YYERRORSYMBOL || yyerrorhit ){
drh75897232000-05-29 14:26:00 +0000810#ifndef NDEBUG
811 if( yyTraceFILE ){
812 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
813 yyTracePrompt,yyTokenName[yymajor]);
814 }
815#endif
drhb27b7f52008-12-10 18:03:45 +0000816 yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
drh75897232000-05-29 14:26:00 +0000817 yymajor = YYNOCODE;
818 }else{
819 while(
drh1f245e42002-03-11 13:55:50 +0000820 yypParser->yyidx >= 0 &&
drh3ddfdf72003-12-22 14:53:19 +0000821 yymx != YYERRORSYMBOL &&
drhf3a58882006-05-08 15:14:19 +0000822 (yyact = yy_find_reduce_action(
823 yypParser->yystack[yypParser->yyidx].stateno,
drh3bd48ab2015-09-07 18:23:37 +0000824 YYERRORSYMBOL)) >= YY_MIN_REDUCE
drh75897232000-05-29 14:26:00 +0000825 ){
826 yy_pop_parser_stack(yypParser);
827 }
drh1f245e42002-03-11 13:55:50 +0000828 if( yypParser->yyidx < 0 || yymajor==0 ){
drhb27b7f52008-12-10 18:03:45 +0000829 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000830 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000831 yymajor = YYNOCODE;
drh3ddfdf72003-12-22 14:53:19 +0000832 }else if( yymx!=YYERRORSYMBOL ){
drh75897232000-05-29 14:26:00 +0000833 YYMINORTYPE u2;
834 u2.YYERRSYMDT = 0;
835 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
836 }
837 }
drh1f245e42002-03-11 13:55:50 +0000838 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000839 yyerrorhit = 1;
drhd3ec02d2009-06-12 02:27:14 +0000840#elif defined(YYNOERRORRECOVERY)
841 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
842 ** do any kind of error recovery. Instead, simply invoke the syntax
843 ** error routine and continue going as if nothing had happened.
844 **
845 ** Applications can set this macro (for example inside %include) if
846 ** they intend to abandon the parse upon the first syntax error seen.
847 */
848 yy_syntax_error(yypParser,yymajor,yyminorunion);
849 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
850 yymajor = YYNOCODE;
851
drh75897232000-05-29 14:26:00 +0000852#else /* YYERRORSYMBOL is not defined */
853 /* This is what we do if the grammar does not define ERROR:
854 **
855 ** * Report an error message, and throw away the input token.
856 **
857 ** * If the input token is $, then fail the parse.
858 **
859 ** As before, subsequent error messages are suppressed until
860 ** three input tokens have been successfully shifted.
861 */
drh1f245e42002-03-11 13:55:50 +0000862 if( yypParser->yyerrcnt<=0 ){
863 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000864 }
drh1f245e42002-03-11 13:55:50 +0000865 yypParser->yyerrcnt = 3;
drhb27b7f52008-12-10 18:03:45 +0000866 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
drh75897232000-05-29 14:26:00 +0000867 if( yyendofinput ){
drh1f245e42002-03-11 13:55:50 +0000868 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000869 }
870 yymajor = YYNOCODE;
871#endif
drh75897232000-05-29 14:26:00 +0000872 }
drh1f245e42002-03-11 13:55:50 +0000873 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
drh3bd48ab2015-09-07 18:23:37 +0000874#ifndef NDEBUG
875 if( yyTraceFILE ){
876 fprintf(yyTraceFILE,"%sReturn\n",yyTracePrompt);
877 }
878#endif
drh75897232000-05-29 14:26:00 +0000879 return;
880}