blob: 5a7933f41bbd618b654a6ee2316772d34c1f8c13 [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
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
drhfdbf9282003-10-21 16:34:41 +000067/* Next are that 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**
122** appears in the grammer, then ID becomes a fallback token for X, Y,
123** 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;
drh01495b92008-01-23 12:52:40 +0000407 assert( stateno<=YY_REDUCE_MAX );
408 i = yy_reduce_ofst[stateno];
409 assert( i!=YY_REDUCE_USE_DFLT );
410 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000411 i += iLookAhead;
drh01495b92008-01-23 12:52:40 +0000412 assert( i>=0 && i<YY_SZ_ACTTAB );
413 assert( yy_lookahead[i]==iLookAhead );
414 return yy_action[i];
drh75897232000-05-29 14:26:00 +0000415}
416
417/*
drhb19fd012007-03-29 01:44:45 +0000418** The following routine is called if the stack overflows.
419*/
drhb6018612007-03-30 13:35:05 +0000420static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
drhb19fd012007-03-29 01:44:45 +0000421 ParseARG_FETCH;
422 yypParser->yyidx--;
423#ifndef NDEBUG
424 if( yyTraceFILE ){
425 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
426 }
427#endif
428 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
429 /* Here code is inserted which will execute if the parser
430 ** stack every overflows */
431%%
432 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
433}
434
435/*
drh75897232000-05-29 14:26:00 +0000436** Perform a shift action.
437*/
438static void yy_shift(
439 yyParser *yypParser, /* The parser to be shifted */
440 int yyNewState, /* The new state to shift in */
441 int yyMajor, /* The major token to shift in */
442 YYMINORTYPE *yypMinor /* Pointer ot the minor token to shift in */
443){
drh3ddfdf72003-12-22 14:53:19 +0000444 yyStackEntry *yytos;
drh1f245e42002-03-11 13:55:50 +0000445 yypParser->yyidx++;
drhb19fd012007-03-29 01:44:45 +0000446#if YYSTACKDEPTH>0
drh1f245e42002-03-11 13:55:50 +0000447 if( yypParser->yyidx>=YYSTACKDEPTH ){
drhb6018612007-03-30 13:35:05 +0000448 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000449 return;
drh75897232000-05-29 14:26:00 +0000450 }
drhb19fd012007-03-29 01:44:45 +0000451#else
452 if( yypParser->yyidx>=yypParser->yystksz ){
453 yyGrowStack(yypParser);
454 if( yypParser->yyidx>=yypParser->yystksz ){
drhb6018612007-03-30 13:35:05 +0000455 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000456 return;
457 }
458 }
459#endif
drh3ddfdf72003-12-22 14:53:19 +0000460 yytos = &yypParser->yystack[yypParser->yyidx];
461 yytos->stateno = yyNewState;
462 yytos->major = yyMajor;
463 yytos->minor = *yypMinor;
drh75897232000-05-29 14:26:00 +0000464#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000465 if( yyTraceFILE && yypParser->yyidx>0 ){
drh75897232000-05-29 14:26:00 +0000466 int i;
467 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
468 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
drh1f245e42002-03-11 13:55:50 +0000469 for(i=1; i<=yypParser->yyidx; i++)
470 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
drh75897232000-05-29 14:26:00 +0000471 fprintf(yyTraceFILE,"\n");
472 }
473#endif
474}
475
476/* The following table contains information about every rule that
477** is used during the reduce.
478*/
drh57196282004-10-06 15:41:16 +0000479static const struct {
drh75897232000-05-29 14:26:00 +0000480 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
481 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
482} yyRuleInfo[] = {
483%%
484};
485
drh1f245e42002-03-11 13:55:50 +0000486static void yy_accept(yyParser*); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000487
488/*
489** Perform a reduce action and the shift that must immediately
490** follow the reduce.
491*/
492static void yy_reduce(
493 yyParser *yypParser, /* The parser */
494 int yyruleno /* Number of the rule by which to reduce */
drh75897232000-05-29 14:26:00 +0000495){
496 int yygoto; /* The next state */
497 int yyact; /* The next action */
498 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
drh1f245e42002-03-11 13:55:50 +0000499 yyStackEntry *yymsp; /* The top of the parser's stack */
drh75897232000-05-29 14:26:00 +0000500 int yysize; /* Amount to pop the stack */
drh1f245e42002-03-11 13:55:50 +0000501 ParseARG_FETCH;
drh3ddfdf72003-12-22 14:53:19 +0000502 yymsp = &yypParser->yystack[yypParser->yyidx];
drh0bd1f4e2002-06-06 18:54:39 +0000503#ifndef NDEBUG
504 if( yyTraceFILE && yyruleno>=0
drhd9f291e2006-06-13 11:15:47 +0000505 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
drh0bd1f4e2002-06-06 18:54:39 +0000506 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
507 yyRuleName[yyruleno]);
508 }
509#endif /* NDEBUG */
510
drh7bec5052005-02-06 02:45:41 +0000511 /* Silence complaints from purify about yygotominor being uninitialized
512 ** in some cases when it is copied into the stack after the following
513 ** switch. yygotominor is uninitialized when a rule reduces that does
514 ** not set the value of its left-hand side nonterminal. Leaving the
515 ** value of the nonterminal uninitialized is utterly harmless as long
516 ** as the value is never used. So really the only thing this code
517 ** accomplishes is to quieten purify.
drhcb6c5652007-01-16 18:19:12 +0000518 **
519 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
520 ** without this code, their parser segfaults. I'm not sure what there
521 ** parser is doing to make this happen. This is the second bug report
522 ** from wireshark this week. Clearly they are stressing Lemon in ways
523 ** that it has not been previously stressed... (SQLite ticket #2172)
drh7bec5052005-02-06 02:45:41 +0000524 */
drh26c9b5e2008-04-11 14:56:53 +0000525 /*memset(&yygotominor, 0, sizeof(yygotominor));*/
526 yygotominor = yyzerominor;
drhcb6c5652007-01-16 18:19:12 +0000527
drh7bec5052005-02-06 02:45:41 +0000528
drh75897232000-05-29 14:26:00 +0000529 switch( yyruleno ){
530 /* Beginning here are the reduction cases. A typical example
531 ** follows:
532 ** case 0:
drh75897232000-05-29 14:26:00 +0000533 ** #line <lineno> <grammarfile>
534 ** { ... } // User supplied code
535 ** #line <lineno> <thisfile>
536 ** break;
537 */
538%%
539 };
540 yygoto = yyRuleInfo[yyruleno].lhs;
541 yysize = yyRuleInfo[yyruleno].nrhs;
drh1f245e42002-03-11 13:55:50 +0000542 yypParser->yyidx -= yysize;
drh161aba32005-02-01 04:09:36 +0000543 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,yygoto);
drh75897232000-05-29 14:26:00 +0000544 if( yyact < YYNSTATE ){
drh490a73b2005-02-01 03:20:00 +0000545#ifdef NDEBUG
546 /* If we are not debugging and the reduce action popped at least
547 ** one element off the stack, then we can push the new element back
548 ** onto the stack here, and skip the stack overflow test in yy_shift().
549 ** That gives a significant speed improvement. */
550 if( yysize ){
551 yypParser->yyidx++;
552 yymsp -= yysize-1;
553 yymsp->stateno = yyact;
554 yymsp->major = yygoto;
555 yymsp->minor = yygotominor;
556 }else
557#endif
558 {
559 yy_shift(yypParser,yyact,yygoto,&yygotominor);
560 }
drh4b2f9362008-01-22 23:37:09 +0000561 }else{
562 assert( yyact == YYNSTATE + YYNRULE + 1 );
drh1f245e42002-03-11 13:55:50 +0000563 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000564 }
565}
566
567/*
568** The following code executes when the parse fails
569*/
570static void yy_parse_failed(
571 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000572){
drh1f245e42002-03-11 13:55:50 +0000573 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000574#ifndef NDEBUG
575 if( yyTraceFILE ){
576 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
577 }
578#endif
drh1f245e42002-03-11 13:55:50 +0000579 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000580 /* Here code is inserted which will be executed whenever the
581 ** parser fails */
582%%
drh1f245e42002-03-11 13:55:50 +0000583 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000584}
585
586/*
587** The following code executes when a syntax error first occurs.
588*/
589static void yy_syntax_error(
590 yyParser *yypParser, /* The parser */
591 int yymajor, /* The major type of the error token */
592 YYMINORTYPE yyminor /* The minor type of the error token */
drh75897232000-05-29 14:26:00 +0000593){
drh1f245e42002-03-11 13:55:50 +0000594 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000595#define TOKEN (yyminor.yy0)
596%%
drh1f245e42002-03-11 13:55:50 +0000597 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000598}
599
600/*
601** The following is executed when the parser accepts
602*/
603static void yy_accept(
604 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000605){
drh1f245e42002-03-11 13:55:50 +0000606 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000607#ifndef NDEBUG
608 if( yyTraceFILE ){
609 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
610 }
611#endif
drh1f245e42002-03-11 13:55:50 +0000612 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000613 /* Here code is inserted which will be executed whenever the
614 ** parser accepts */
615%%
drh1f245e42002-03-11 13:55:50 +0000616 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000617}
618
619/* The main parser program.
620** The first argument is a pointer to a structure obtained from
621** "ParseAlloc" which describes the current state of the parser.
622** The second argument is the major token number. The third is
623** the minor token. The fourth optional argument is whatever the
624** user wants (and specified in the grammar) and is available for
625** use by the action routines.
626**
627** Inputs:
628** <ul>
629** <li> A pointer to the parser (an opaque structure.)
630** <li> The major token number.
631** <li> The minor token number.
632** <li> An option argument of a grammar-specified type.
633** </ul>
634**
635** Outputs:
636** None.
637*/
drh75897232000-05-29 14:26:00 +0000638void Parse(
639 void *yyp, /* The parser */
640 int yymajor, /* The major token code number */
641 ParseTOKENTYPE yyminor /* The value for the token */
drh1f245e42002-03-11 13:55:50 +0000642 ParseARG_PDECL /* Optional %extra_argument parameter */
drh75897232000-05-29 14:26:00 +0000643){
644 YYMINORTYPE yyminorunion;
645 int yyact; /* The parser action. */
646 int yyendofinput; /* True if we are at the end of input */
drhc4dd3fd2008-01-22 01:48:05 +0000647#ifdef YYERRORSYMBOL
drh75897232000-05-29 14:26:00 +0000648 int yyerrorhit = 0; /* True if yymajor has invoked an error */
drhc4dd3fd2008-01-22 01:48:05 +0000649#endif
drh75897232000-05-29 14:26:00 +0000650 yyParser *yypParser; /* The parser */
651
652 /* (re)initialize the parser, if necessary */
653 yypParser = (yyParser*)yyp;
drh1f245e42002-03-11 13:55:50 +0000654 if( yypParser->yyidx<0 ){
drhb7bac722007-03-29 02:26:45 +0000655#if YYSTACKDEPTH<=0
drhb19fd012007-03-29 01:44:45 +0000656 if( yypParser->yystksz <=0 ){
drh26c9b5e2008-04-11 14:56:53 +0000657 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
658 yyminorunion = yyzerominor;
drhb6018612007-03-30 13:35:05 +0000659 yyStackOverflow(yypParser, &yyminorunion);
drhb19fd012007-03-29 01:44:45 +0000660 return;
661 }
drhb7bac722007-03-29 02:26:45 +0000662#endif
drh1f245e42002-03-11 13:55:50 +0000663 yypParser->yyidx = 0;
664 yypParser->yyerrcnt = -1;
drh3ddfdf72003-12-22 14:53:19 +0000665 yypParser->yystack[0].stateno = 0;
666 yypParser->yystack[0].major = 0;
drh75897232000-05-29 14:26:00 +0000667 }
668 yyminorunion.yy0 = yyminor;
669 yyendofinput = (yymajor==0);
drh1f245e42002-03-11 13:55:50 +0000670 ParseARG_STORE;
drh75897232000-05-29 14:26:00 +0000671
672#ifndef NDEBUG
673 if( yyTraceFILE ){
674 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
675 }
676#endif
677
678 do{
drh8b582012003-10-21 13:16:03 +0000679 yyact = yy_find_shift_action(yypParser,yymajor);
drh75897232000-05-29 14:26:00 +0000680 if( yyact<YYNSTATE ){
drh55176252008-01-22 14:50:16 +0000681 assert( !yyendofinput ); /* Impossible to shift the $ token */
drh75897232000-05-29 14:26:00 +0000682 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000683 yypParser->yyerrcnt--;
drh55176252008-01-22 14:50:16 +0000684 yymajor = YYNOCODE;
drh75897232000-05-29 14:26:00 +0000685 }else if( yyact < YYNSTATE + YYNRULE ){
drh1f245e42002-03-11 13:55:50 +0000686 yy_reduce(yypParser,yyact-YYNSTATE);
drhc4dd3fd2008-01-22 01:48:05 +0000687 }else{
688 assert( yyact == YY_ERROR_ACTION );
689#ifdef YYERRORSYMBOL
drh3ddfdf72003-12-22 14:53:19 +0000690 int yymx;
drhc4dd3fd2008-01-22 01:48:05 +0000691#endif
drh75897232000-05-29 14:26:00 +0000692#ifndef NDEBUG
693 if( yyTraceFILE ){
694 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
695 }
696#endif
697#ifdef YYERRORSYMBOL
698 /* A syntax error has occurred.
699 ** The response to an error depends upon whether or not the
700 ** grammar defines an error token "ERROR".
701 **
702 ** This is what we do if the grammar does define ERROR:
703 **
704 ** * Call the %syntax_error function.
705 **
706 ** * Begin popping the stack until we enter a state where
707 ** it is legal to shift the error symbol, then shift
708 ** the error symbol.
709 **
710 ** * Set the error count to three.
711 **
712 ** * Begin accepting and shifting new tokens. No new error
713 ** processing will occur until three tokens have been
714 ** shifted successfully.
715 **
716 */
drh1f245e42002-03-11 13:55:50 +0000717 if( yypParser->yyerrcnt<0 ){
718 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000719 }
drh3ddfdf72003-12-22 14:53:19 +0000720 yymx = yypParser->yystack[yypParser->yyidx].major;
721 if( yymx==YYERRORSYMBOL || yyerrorhit ){
drh75897232000-05-29 14:26:00 +0000722#ifndef NDEBUG
723 if( yyTraceFILE ){
724 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
725 yyTracePrompt,yyTokenName[yymajor]);
726 }
727#endif
728 yy_destructor(yymajor,&yyminorunion);
729 yymajor = YYNOCODE;
730 }else{
731 while(
drh1f245e42002-03-11 13:55:50 +0000732 yypParser->yyidx >= 0 &&
drh3ddfdf72003-12-22 14:53:19 +0000733 yymx != YYERRORSYMBOL &&
drhf3a58882006-05-08 15:14:19 +0000734 (yyact = yy_find_reduce_action(
735 yypParser->yystack[yypParser->yyidx].stateno,
736 YYERRORSYMBOL)) >= YYNSTATE
drh75897232000-05-29 14:26:00 +0000737 ){
738 yy_pop_parser_stack(yypParser);
739 }
drh1f245e42002-03-11 13:55:50 +0000740 if( yypParser->yyidx < 0 || yymajor==0 ){
drh75897232000-05-29 14:26:00 +0000741 yy_destructor(yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000742 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000743 yymajor = YYNOCODE;
drh3ddfdf72003-12-22 14:53:19 +0000744 }else if( yymx!=YYERRORSYMBOL ){
drh75897232000-05-29 14:26:00 +0000745 YYMINORTYPE u2;
746 u2.YYERRSYMDT = 0;
747 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
748 }
749 }
drh1f245e42002-03-11 13:55:50 +0000750 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000751 yyerrorhit = 1;
752#else /* YYERRORSYMBOL is not defined */
753 /* This is what we do if the grammar does not define ERROR:
754 **
755 ** * Report an error message, and throw away the input token.
756 **
757 ** * If the input token is $, then fail the parse.
758 **
759 ** As before, subsequent error messages are suppressed until
760 ** three input tokens have been successfully shifted.
761 */
drh1f245e42002-03-11 13:55:50 +0000762 if( yypParser->yyerrcnt<=0 ){
763 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000764 }
drh1f245e42002-03-11 13:55:50 +0000765 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000766 yy_destructor(yymajor,&yyminorunion);
767 if( yyendofinput ){
drh1f245e42002-03-11 13:55:50 +0000768 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000769 }
770 yymajor = YYNOCODE;
771#endif
drh75897232000-05-29 14:26:00 +0000772 }
drh1f245e42002-03-11 13:55:50 +0000773 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
drh75897232000-05-29 14:26:00 +0000774 return;
775}