blob: fe56d2dc1661eb9c2468ab9b0c763238e79f6252 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/* Driver template for the LEMON parser generator.
drhb19a2bc2001-09-16 00:13:26 +00002** The author disclaims copyright to this source code.
drh75897232000-05-29 14:26:00 +00003*/
drh34ff57b2008-07-14 12:27:51 +00004/* First off, code is included that follows the "include" declaration
5** in the input grammar file. */
drh75897232000-05-29 14:26:00 +00006#include <stdio.h>
7%%
8/* Next is all token values, in a form suitable for use by makeheaders.
9** This section will be null unless lemon is run with the -m switch.
10*/
11/*
12** These constants (all generated automatically by the parser generator)
13** specify the various kinds of tokens (terminals) that the parser
14** understands.
15**
16** Each symbol here is a terminal symbol in the grammar.
17*/
18%%
19/* Make sure the INTERFACE macro is defined.
20*/
21#ifndef INTERFACE
22# define INTERFACE 1
23#endif
24/* The next thing included is series of defines which control
25** various aspects of the generated parser.
26** YYCODETYPE is the data type used for storing terminal
27** and nonterminal numbers. "unsigned char" is
28** used if there are fewer than 250 terminals
29** and nonterminals. "int" is used otherwise.
30** YYNOCODE is a number of type YYCODETYPE which corresponds
31** to no legal terminal or nonterminal number. This
32** number is used to fill in empty slots of the hash
33** table.
drh0bd1f4e2002-06-06 18:54:39 +000034** YYFALLBACK If defined, this indicates that one or more tokens
35** have fall-back values which should be used if the
36** original value of the token will not parse.
drh75897232000-05-29 14:26:00 +000037** YYACTIONTYPE is the data type used for storing terminal
38** and nonterminal numbers. "unsigned char" is
39** used if there are fewer than 250 rules and
40** states combined. "int" is used otherwise.
41** ParseTOKENTYPE is the data type used for minor tokens given
42** directly to the parser from the tokenizer.
43** YYMINORTYPE is the data type used for all minor tokens.
44** This is typically a union of many types, one of
45** which is ParseTOKENTYPE. The entry in the union
46** for base tokens is called "yy0".
drhb19fd012007-03-29 01:44:45 +000047** YYSTACKDEPTH is the maximum depth of the parser's stack. If
48** zero the stack is dynamically sized using realloc()
drh1f245e42002-03-11 13:55:50 +000049** ParseARG_SDECL A static variable declaration for the %extra_argument
50** ParseARG_PDECL A parameter declaration for the %extra_argument
51** ParseARG_STORE Code to store %extra_argument into yypParser
52** ParseARG_FETCH Code to extract %extra_argument from yypParser
drh75897232000-05-29 14:26:00 +000053** YYNSTATE the combined number of states.
54** YYNRULE the number of rules in the grammar
55** YYERRORSYMBOL is the code number of the error symbol. If not
56** defined, then do no error processing.
57*/
58%%
59#define YY_NO_ACTION (YYNSTATE+YYNRULE+2)
60#define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1)
61#define YY_ERROR_ACTION (YYNSTATE+YYNRULE)
drh75897232000-05-29 14:26:00 +000062
drh26c9b5e2008-04-11 14:56:53 +000063/* The yyzerominor constant is used to initialize instances of
64** YYMINORTYPE objects to zero. */
drh15b024c2008-12-11 02:20:43 +000065static const YYMINORTYPE yyzerominor = { 0 };
66
drh8a415d32009-06-12 13:53:51 +000067/* Define the yytestcase() macro to be a no-op if is not already defined
68** otherwise.
69**
70** Applications can choose to define yytestcase() in the %include section
71** to a macro that can assist in verifying code coverage. For production
72** code the yytestcase() macro should be turned off. But it is useful
73** for testing.
74*/
75#ifndef yytestcase
76# define yytestcase(X)
77#endif
78
drh26c9b5e2008-04-11 14:56:53 +000079
drh34ff57b2008-07-14 12:27:51 +000080/* Next are the tables used to determine what action to take based on the
drh8b582012003-10-21 13:16:03 +000081** current state and lookahead token. These tables are used to implement
82** functions that take a state number and lookahead value and return an
83** action integer.
drh75897232000-05-29 14:26:00 +000084**
drh8548a052003-10-22 22:15:27 +000085** Suppose the action integer is N. Then the action is determined as
86** follows
drh75897232000-05-29 14:26:00 +000087**
drh8548a052003-10-22 22:15:27 +000088** 0 <= N < YYNSTATE Shift N. That is, push the lookahead
89** token onto the stack and goto state N.
90**
91** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE.
92**
93** N == YYNSTATE+YYNRULE A syntax error has occurred.
94**
95** N == YYNSTATE+YYNRULE+1 The parser accepts its input.
96**
97** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused
98** slots in the yy_action[] table.
99**
100** The action table is constructed as a single large table named yy_action[].
101** Given state S and lookahead X, the action is computed as
drh75897232000-05-29 14:26:00 +0000102**
drh8b582012003-10-21 13:16:03 +0000103** yy_action[ yy_shift_ofst[S] + X ]
104**
drh8548a052003-10-22 22:15:27 +0000105** If the index value yy_shift_ofst[S]+X is out of range or if the value
drh8b582012003-10-21 13:16:03 +0000106** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
107** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
108** and that yy_default[S] should be used instead.
109**
110** The formula above is for computing the action when the lookahead is
111** a terminal symbol. If the lookahead is a non-terminal (as occurs after
112** a reduce action) then the yy_reduce_ofst[] array is used in place of
113** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
114** YY_SHIFT_USE_DFLT.
115**
116** The following are the tables generated in this section:
117**
118** yy_action[] A single table containing all actions.
119** yy_lookahead[] A table containing the lookahead for each entry in
120** yy_action. Used to detect hash collisions.
121** yy_shift_ofst[] For each state, the offset into yy_action for
122** shifting terminals.
123** yy_reduce_ofst[] For each state, the offset into yy_action for
124** shifting non-terminals after a reduce.
125** yy_default[] Default action for each state.
drh75897232000-05-29 14:26:00 +0000126*/
drh75897232000-05-29 14:26:00 +0000127%%
drh75897232000-05-29 14:26:00 +0000128
drh0bd1f4e2002-06-06 18:54:39 +0000129/* The next table maps tokens into fallback tokens. If a construct
130** like the following:
131**
132** %fallback ID X Y Z.
133**
drh34ff57b2008-07-14 12:27:51 +0000134** appears in the grammar, then ID becomes a fallback token for X, Y,
drh0bd1f4e2002-06-06 18:54:39 +0000135** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
136** but it does not parse, the type of the token is changed to ID and
137** the parse is retried before an error is thrown.
138*/
139#ifdef YYFALLBACK
140static const YYCODETYPE yyFallback[] = {
141%%
142};
143#endif /* YYFALLBACK */
144
drh75897232000-05-29 14:26:00 +0000145/* The following structure represents a single element of the
146** parser's stack. Information stored includes:
147**
148** + The state number for the parser at this level of the stack.
149**
150** + The value of the token stored at this level of the stack.
151** (In other words, the "major" token.)
152**
153** + The semantic value stored at this level of the stack. This is
154** the information used by the action routines in the grammar.
155** It is sometimes called the "minor" token.
156*/
157struct yyStackEntry {
drh2abcd582008-07-24 23:34:07 +0000158 YYACTIONTYPE stateno; /* The state-number */
159 YYCODETYPE major; /* The major token value. This is the code
160 ** number for the token at this stack level */
161 YYMINORTYPE minor; /* The user-supplied minor token value. This
162 ** is the value of the token */
drh75897232000-05-29 14:26:00 +0000163};
drh1f245e42002-03-11 13:55:50 +0000164typedef struct yyStackEntry yyStackEntry;
drh75897232000-05-29 14:26:00 +0000165
166/* The state of the parser is completely contained in an instance of
167** the following structure */
168struct yyParser {
drh1f245e42002-03-11 13:55:50 +0000169 int yyidx; /* Index of top element in stack */
drhec424a52008-07-25 15:39:03 +0000170#ifdef YYTRACKMAXSTACKDEPTH
171 int yyidxMax; /* Maximum value of yyidx */
172#endif
drh1f245e42002-03-11 13:55:50 +0000173 int yyerrcnt; /* Shifts left before out of the error */
drh1f245e42002-03-11 13:55:50 +0000174 ParseARG_SDECL /* A place to hold %extra_argument */
drhb19fd012007-03-29 01:44:45 +0000175#if YYSTACKDEPTH<=0
176 int yystksz; /* Current side of the stack */
177 yyStackEntry *yystack; /* The parser's stack */
178#else
drh1f245e42002-03-11 13:55:50 +0000179 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */
drhb19fd012007-03-29 01:44:45 +0000180#endif
drh75897232000-05-29 14:26:00 +0000181};
182typedef struct yyParser yyParser;
183
184#ifndef NDEBUG
185#include <stdio.h>
186static FILE *yyTraceFILE = 0;
187static char *yyTracePrompt = 0;
drh0bd1f4e2002-06-06 18:54:39 +0000188#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000189
drh0bd1f4e2002-06-06 18:54:39 +0000190#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000191/*
192** Turn parser tracing on by giving a stream to which to write the trace
193** and a prompt to preface each trace message. Tracing is turned off
194** by making either argument NULL
195**
196** Inputs:
197** <ul>
198** <li> A FILE* to which trace output should be written.
199** If NULL, then tracing is turned off.
200** <li> A prefix string written at the beginning of every
201** line of trace output. If NULL, then tracing is
202** turned off.
203** </ul>
204**
205** Outputs:
206** None.
207*/
drh75897232000-05-29 14:26:00 +0000208void ParseTrace(FILE *TraceFILE, char *zTracePrompt){
209 yyTraceFILE = TraceFILE;
210 yyTracePrompt = zTracePrompt;
211 if( yyTraceFILE==0 ) yyTracePrompt = 0;
212 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
213}
drh0bd1f4e2002-06-06 18:54:39 +0000214#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000215
drh0bd1f4e2002-06-06 18:54:39 +0000216#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000217/* For tracing shifts, the names of all terminals and nonterminals
218** are required. The following table supplies these names */
drh57196282004-10-06 15:41:16 +0000219static const char *const yyTokenName[] = {
drh75897232000-05-29 14:26:00 +0000220%%
221};
drh0bd1f4e2002-06-06 18:54:39 +0000222#endif /* NDEBUG */
drh75897232000-05-29 14:26:00 +0000223
drh0bd1f4e2002-06-06 18:54:39 +0000224#ifndef NDEBUG
225/* For tracing reduce actions, the names of all rules are required.
226*/
drh57196282004-10-06 15:41:16 +0000227static const char *const yyRuleName[] = {
drh0bd1f4e2002-06-06 18:54:39 +0000228%%
229};
230#endif /* NDEBUG */
drha1b351a2001-09-14 16:42:12 +0000231
drh960e8c62001-04-03 16:53:21 +0000232
drhb19fd012007-03-29 01:44:45 +0000233#if YYSTACKDEPTH<=0
234/*
235** Try to increase the size of the parser stack.
236*/
237static void yyGrowStack(yyParser *p){
238 int newSize;
239 yyStackEntry *pNew;
240
241 newSize = p->yystksz*2 + 100;
242 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
243 if( pNew ){
244 p->yystack = pNew;
245 p->yystksz = newSize;
246#ifndef NDEBUG
247 if( yyTraceFILE ){
248 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
249 yyTracePrompt, p->yystksz);
250 }
251#endif
252 }
253}
254#endif
255
drh75897232000-05-29 14:26:00 +0000256/*
257** This function allocates a new parser.
258** The only argument is a pointer to a function which works like
259** malloc.
260**
261** Inputs:
262** A pointer to the function used to allocate memory.
263**
264** Outputs:
265** A pointer to a parser. This pointer is used in subsequent calls
266** to Parse and ParseFree.
267*/
drh7218ac72002-03-10 21:21:00 +0000268void *ParseAlloc(void *(*mallocProc)(size_t)){
drh75897232000-05-29 14:26:00 +0000269 yyParser *pParser;
drh7218ac72002-03-10 21:21:00 +0000270 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
drh75897232000-05-29 14:26:00 +0000271 if( pParser ){
drh1f245e42002-03-11 13:55:50 +0000272 pParser->yyidx = -1;
drhec424a52008-07-25 15:39:03 +0000273#ifdef YYTRACKMAXSTACKDEPTH
274 pParser->yyidxMax = 0;
275#endif
drhb19fd012007-03-29 01:44:45 +0000276#if YYSTACKDEPTH<=0
drh4c651782008-11-18 23:25:54 +0000277 pParser->yystack = NULL;
278 pParser->yystksz = 0;
drhb19fd012007-03-29 01:44:45 +0000279 yyGrowStack(pParser);
280#endif
drh75897232000-05-29 14:26:00 +0000281 }
282 return pParser;
283}
284
285/* The following function deletes the value associated with a
286** symbol. The symbol can be either a terminal or nonterminal.
287** "yymajor" is the symbol code, and "yypminor" is a pointer to
288** the value.
289*/
drh633e6d52008-07-28 19:34:53 +0000290static void yy_destructor(
291 yyParser *yypParser, /* The parser */
292 YYCODETYPE yymajor, /* Type code for object to destroy */
293 YYMINORTYPE *yypminor /* The object to be destroyed */
294){
295 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000296 switch( yymajor ){
297 /* Here is inserted the actions which take place when a
298 ** terminal or non-terminal is destroyed. This can happen
299 ** when the symbol is popped from the stack during a
300 ** reduce or during error processing or when a parser is
301 ** being destroyed before it is finished parsing.
302 **
303 ** Note: during a reduce, the only symbols destroyed are those
304 ** which appear on the RHS of the rule, but which are not used
305 ** inside the C code.
306 */
307%%
308 default: break; /* If no destructor action specified: do nothing */
309 }
310}
311
312/*
313** Pop the parser's stack once.
314**
315** If there is a destructor routine associated with the token which
316** is popped from the stack, then call it.
317**
318** Return the major token number for the symbol popped.
319*/
320static int yy_pop_parser_stack(yyParser *pParser){
321 YYCODETYPE yymajor;
drh3ddfdf72003-12-22 14:53:19 +0000322 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
drh75897232000-05-29 14:26:00 +0000323
drh1f245e42002-03-11 13:55:50 +0000324 if( pParser->yyidx<0 ) return 0;
drh75897232000-05-29 14:26:00 +0000325#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000326 if( yyTraceFILE && pParser->yyidx>=0 ){
drh75897232000-05-29 14:26:00 +0000327 fprintf(yyTraceFILE,"%sPopping %s\n",
328 yyTracePrompt,
drh3ddfdf72003-12-22 14:53:19 +0000329 yyTokenName[yytos->major]);
drh75897232000-05-29 14:26:00 +0000330 }
331#endif
drh3ddfdf72003-12-22 14:53:19 +0000332 yymajor = yytos->major;
drh633e6d52008-07-28 19:34:53 +0000333 yy_destructor(pParser, yymajor, &yytos->minor);
drh1f245e42002-03-11 13:55:50 +0000334 pParser->yyidx--;
drh75897232000-05-29 14:26:00 +0000335 return yymajor;
336}
337
338/*
339** Deallocate and destroy a parser. Destructors are all called for
340** all stack elements before shutting the parser down.
341**
342** Inputs:
343** <ul>
344** <li> A pointer to the parser. This should be a pointer
345** obtained from ParseAlloc.
346** <li> A pointer to a function used to reclaim memory obtained
347** from malloc.
348** </ul>
349*/
drh75897232000-05-29 14:26:00 +0000350void ParseFree(
drh960e8c62001-04-03 16:53:21 +0000351 void *p, /* The parser to be deleted */
352 void (*freeProc)(void*) /* Function used to reclaim memory */
drh75897232000-05-29 14:26:00 +0000353){
354 yyParser *pParser = (yyParser*)p;
355 if( pParser==0 ) return;
drh1f245e42002-03-11 13:55:50 +0000356 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
drhb19fd012007-03-29 01:44:45 +0000357#if YYSTACKDEPTH<=0
358 free(pParser->yystack);
359#endif
drh960e8c62001-04-03 16:53:21 +0000360 (*freeProc)((void*)pParser);
drh75897232000-05-29 14:26:00 +0000361}
362
363/*
drhec424a52008-07-25 15:39:03 +0000364** Return the peak depth of the stack for a parser.
365*/
366#ifdef YYTRACKMAXSTACKDEPTH
367int ParseStackPeak(void *p){
368 yyParser *pParser = (yyParser*)p;
369 return pParser->yyidxMax;
370}
371#endif
372
373/*
drh8b582012003-10-21 13:16:03 +0000374** Find the appropriate action for a parser given the terminal
375** look-ahead token iLookAhead.
drh75897232000-05-29 14:26:00 +0000376**
377** If the look-ahead token is YYNOCODE, then check to see if the action is
378** independent of the look-ahead. If it is, return the action, otherwise
379** return YY_NO_ACTION.
380*/
drh8b582012003-10-21 13:16:03 +0000381static int yy_find_shift_action(
drh75897232000-05-29 14:26:00 +0000382 yyParser *pParser, /* The parser */
drhd9f291e2006-06-13 11:15:47 +0000383 YYCODETYPE iLookAhead /* The look-ahead token */
drh75897232000-05-29 14:26:00 +0000384){
drh8b582012003-10-21 13:16:03 +0000385 int i;
drh3ddfdf72003-12-22 14:53:19 +0000386 int stateno = pParser->yystack[pParser->yyidx].stateno;
drh75897232000-05-29 14:26:00 +0000387
drhf16371d2009-11-03 19:18:31 +0000388 if( stateno>YY_SHIFT_COUNT
389 || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
drh3ddfdf72003-12-22 14:53:19 +0000390 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000391 }
drh01495b92008-01-23 12:52:40 +0000392 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000393 i += iLookAhead;
drhf16371d2009-11-03 19:18:31 +0000394 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
drhe09daa92006-06-10 13:29:31 +0000395 if( iLookAhead>0 ){
drh4767d972006-06-14 15:03:49 +0000396#ifdef YYFALLBACK
drhb27b7f52008-12-10 18:03:45 +0000397 YYCODETYPE iFallback; /* Fallback token */
drhe09daa92006-06-10 13:29:31 +0000398 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
399 && (iFallback = yyFallback[iLookAhead])!=0 ){
drh4767d972006-06-14 15:03:49 +0000400#ifndef NDEBUG
drhe09daa92006-06-10 13:29:31 +0000401 if( yyTraceFILE ){
402 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
403 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
404 }
drh4767d972006-06-14 15:03:49 +0000405#endif
drhe09daa92006-06-10 13:29:31 +0000406 return yy_find_shift_action(pParser, iFallback);
drh0bd1f4e2002-06-06 18:54:39 +0000407 }
drh4767d972006-06-14 15:03:49 +0000408#endif
409#ifdef YYWILDCARD
410 {
411 int j = i - iLookAhead + YYWILDCARD;
drhf16371d2009-11-03 19:18:31 +0000412 if(
413#if YY_SHIFT_MIN+YYWILDCARD<0
414 j>=0 &&
415#endif
416#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
417 j<YY_ACTTAB_COUNT &&
418#endif
419 yy_lookahead[j]==YYWILDCARD
420 ){
drh4767d972006-06-14 15:03:49 +0000421#ifndef NDEBUG
422 if( yyTraceFILE ){
423 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
424 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
425 }
426#endif /* NDEBUG */
427 return yy_action[j];
drhe09daa92006-06-10 13:29:31 +0000428 }
drhe09daa92006-06-10 13:29:31 +0000429 }
drh4767d972006-06-14 15:03:49 +0000430#endif /* YYWILDCARD */
drh0bd1f4e2002-06-06 18:54:39 +0000431 }
drh3ddfdf72003-12-22 14:53:19 +0000432 return yy_default[stateno];
drh8b582012003-10-21 13:16:03 +0000433 }else{
434 return yy_action[i];
435 }
436}
437
438/*
439** Find the appropriate action for a parser given the non-terminal
440** look-ahead token iLookAhead.
441**
442** If the look-ahead token is YYNOCODE, then check to see if the action is
443** independent of the look-ahead. If it is, return the action, otherwise
444** return YY_NO_ACTION.
445*/
446static int yy_find_reduce_action(
drh161aba32005-02-01 04:09:36 +0000447 int stateno, /* Current state number */
drhd9f291e2006-06-13 11:15:47 +0000448 YYCODETYPE iLookAhead /* The look-ahead token */
drh8b582012003-10-21 13:16:03 +0000449){
450 int i;
drh7f7c2572008-04-27 18:45:10 +0000451#ifdef YYERRORSYMBOL
drhf16371d2009-11-03 19:18:31 +0000452 if( stateno>YY_REDUCE_COUNT ){
drh7f7c2572008-04-27 18:45:10 +0000453 return yy_default[stateno];
454 }
455#else
drhf16371d2009-11-03 19:18:31 +0000456 assert( stateno<=YY_REDUCE_COUNT );
drh7f7c2572008-04-27 18:45:10 +0000457#endif
drh01495b92008-01-23 12:52:40 +0000458 i = yy_reduce_ofst[stateno];
459 assert( i!=YY_REDUCE_USE_DFLT );
460 assert( iLookAhead!=YYNOCODE );
drh8b582012003-10-21 13:16:03 +0000461 i += iLookAhead;
drh7f7c2572008-04-27 18:45:10 +0000462#ifdef YYERRORSYMBOL
drhf16371d2009-11-03 19:18:31 +0000463 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
drh7f7c2572008-04-27 18:45:10 +0000464 return yy_default[stateno];
465 }
466#else
drhf16371d2009-11-03 19:18:31 +0000467 assert( i>=0 && i<YY_ACTTAB_COUNT );
drh01495b92008-01-23 12:52:40 +0000468 assert( yy_lookahead[i]==iLookAhead );
drh7f7c2572008-04-27 18:45:10 +0000469#endif
drh01495b92008-01-23 12:52:40 +0000470 return yy_action[i];
drh75897232000-05-29 14:26:00 +0000471}
472
473/*
drhb19fd012007-03-29 01:44:45 +0000474** The following routine is called if the stack overflows.
475*/
drhb6018612007-03-30 13:35:05 +0000476static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
drhb19fd012007-03-29 01:44:45 +0000477 ParseARG_FETCH;
478 yypParser->yyidx--;
479#ifndef NDEBUG
480 if( yyTraceFILE ){
481 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
482 }
483#endif
484 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
485 /* Here code is inserted which will execute if the parser
486 ** stack every overflows */
487%%
488 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
489}
490
491/*
drh75897232000-05-29 14:26:00 +0000492** Perform a shift action.
493*/
494static void yy_shift(
495 yyParser *yypParser, /* The parser to be shifted */
496 int yyNewState, /* The new state to shift in */
497 int yyMajor, /* The major token to shift in */
drh34ff57b2008-07-14 12:27:51 +0000498 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */
drh75897232000-05-29 14:26:00 +0000499){
drh3ddfdf72003-12-22 14:53:19 +0000500 yyStackEntry *yytos;
drh1f245e42002-03-11 13:55:50 +0000501 yypParser->yyidx++;
drhec424a52008-07-25 15:39:03 +0000502#ifdef YYTRACKMAXSTACKDEPTH
503 if( yypParser->yyidx>yypParser->yyidxMax ){
504 yypParser->yyidxMax = yypParser->yyidx;
505 }
506#endif
drhb19fd012007-03-29 01:44:45 +0000507#if YYSTACKDEPTH>0
drh1f245e42002-03-11 13:55:50 +0000508 if( yypParser->yyidx>=YYSTACKDEPTH ){
drhb6018612007-03-30 13:35:05 +0000509 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000510 return;
drh75897232000-05-29 14:26:00 +0000511 }
drhb19fd012007-03-29 01:44:45 +0000512#else
513 if( yypParser->yyidx>=yypParser->yystksz ){
514 yyGrowStack(yypParser);
515 if( yypParser->yyidx>=yypParser->yystksz ){
drhb6018612007-03-30 13:35:05 +0000516 yyStackOverflow(yypParser, yypMinor);
drhb19fd012007-03-29 01:44:45 +0000517 return;
518 }
519 }
520#endif
drh3ddfdf72003-12-22 14:53:19 +0000521 yytos = &yypParser->yystack[yypParser->yyidx];
drhb27b7f52008-12-10 18:03:45 +0000522 yytos->stateno = (YYACTIONTYPE)yyNewState;
523 yytos->major = (YYCODETYPE)yyMajor;
drh3ddfdf72003-12-22 14:53:19 +0000524 yytos->minor = *yypMinor;
drh75897232000-05-29 14:26:00 +0000525#ifndef NDEBUG
drh1f245e42002-03-11 13:55:50 +0000526 if( yyTraceFILE && yypParser->yyidx>0 ){
drh75897232000-05-29 14:26:00 +0000527 int i;
528 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
529 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
drh1f245e42002-03-11 13:55:50 +0000530 for(i=1; i<=yypParser->yyidx; i++)
531 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
drh75897232000-05-29 14:26:00 +0000532 fprintf(yyTraceFILE,"\n");
533 }
534#endif
535}
536
537/* The following table contains information about every rule that
538** is used during the reduce.
539*/
drh57196282004-10-06 15:41:16 +0000540static const struct {
drh75897232000-05-29 14:26:00 +0000541 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */
542 unsigned char nrhs; /* Number of right-hand side symbols in the rule */
543} yyRuleInfo[] = {
544%%
545};
546
drh1f245e42002-03-11 13:55:50 +0000547static void yy_accept(yyParser*); /* Forward Declaration */
drh75897232000-05-29 14:26:00 +0000548
549/*
550** Perform a reduce action and the shift that must immediately
551** follow the reduce.
552*/
553static void yy_reduce(
554 yyParser *yypParser, /* The parser */
555 int yyruleno /* Number of the rule by which to reduce */
drh75897232000-05-29 14:26:00 +0000556){
557 int yygoto; /* The next state */
558 int yyact; /* The next action */
559 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */
drh1f245e42002-03-11 13:55:50 +0000560 yyStackEntry *yymsp; /* The top of the parser's stack */
drh75897232000-05-29 14:26:00 +0000561 int yysize; /* Amount to pop the stack */
drh1f245e42002-03-11 13:55:50 +0000562 ParseARG_FETCH;
drh3ddfdf72003-12-22 14:53:19 +0000563 yymsp = &yypParser->yystack[yypParser->yyidx];
drh0bd1f4e2002-06-06 18:54:39 +0000564#ifndef NDEBUG
565 if( yyTraceFILE && yyruleno>=0
drhd9f291e2006-06-13 11:15:47 +0000566 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
drh0bd1f4e2002-06-06 18:54:39 +0000567 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
568 yyRuleName[yyruleno]);
569 }
570#endif /* NDEBUG */
571
drh7bec5052005-02-06 02:45:41 +0000572 /* Silence complaints from purify about yygotominor being uninitialized
573 ** in some cases when it is copied into the stack after the following
574 ** switch. yygotominor is uninitialized when a rule reduces that does
575 ** not set the value of its left-hand side nonterminal. Leaving the
576 ** value of the nonterminal uninitialized is utterly harmless as long
577 ** as the value is never used. So really the only thing this code
578 ** accomplishes is to quieten purify.
drhcb6c5652007-01-16 18:19:12 +0000579 **
580 ** 2007-01-16: The wireshark project (www.wireshark.org) reports that
581 ** without this code, their parser segfaults. I'm not sure what there
582 ** parser is doing to make this happen. This is the second bug report
583 ** from wireshark this week. Clearly they are stressing Lemon in ways
584 ** that it has not been previously stressed... (SQLite ticket #2172)
drh7bec5052005-02-06 02:45:41 +0000585 */
drh26c9b5e2008-04-11 14:56:53 +0000586 /*memset(&yygotominor, 0, sizeof(yygotominor));*/
587 yygotominor = yyzerominor;
drhcb6c5652007-01-16 18:19:12 +0000588
drh7bec5052005-02-06 02:45:41 +0000589
drh75897232000-05-29 14:26:00 +0000590 switch( yyruleno ){
591 /* Beginning here are the reduction cases. A typical example
592 ** follows:
593 ** case 0:
drh75897232000-05-29 14:26:00 +0000594 ** #line <lineno> <grammarfile>
595 ** { ... } // User supplied code
596 ** #line <lineno> <thisfile>
597 ** break;
598 */
599%%
600 };
601 yygoto = yyRuleInfo[yyruleno].lhs;
602 yysize = yyRuleInfo[yyruleno].nrhs;
drh1f245e42002-03-11 13:55:50 +0000603 yypParser->yyidx -= yysize;
drh3abbd392008-12-10 23:04:13 +0000604 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
drh75897232000-05-29 14:26:00 +0000605 if( yyact < YYNSTATE ){
drh490a73b2005-02-01 03:20:00 +0000606#ifdef NDEBUG
607 /* If we are not debugging and the reduce action popped at least
608 ** one element off the stack, then we can push the new element back
609 ** onto the stack here, and skip the stack overflow test in yy_shift().
610 ** That gives a significant speed improvement. */
611 if( yysize ){
612 yypParser->yyidx++;
613 yymsp -= yysize-1;
shaned87897d2009-01-30 05:40:27 +0000614 yymsp->stateno = (YYACTIONTYPE)yyact;
615 yymsp->major = (YYCODETYPE)yygoto;
drh490a73b2005-02-01 03:20:00 +0000616 yymsp->minor = yygotominor;
617 }else
618#endif
619 {
620 yy_shift(yypParser,yyact,yygoto,&yygotominor);
621 }
drh4b2f9362008-01-22 23:37:09 +0000622 }else{
623 assert( yyact == YYNSTATE + YYNRULE + 1 );
drh1f245e42002-03-11 13:55:50 +0000624 yy_accept(yypParser);
drh75897232000-05-29 14:26:00 +0000625 }
626}
627
628/*
629** The following code executes when the parse fails
630*/
drhd3ec02d2009-06-12 02:27:14 +0000631#ifndef YYNOERRORRECOVERY
drh75897232000-05-29 14:26:00 +0000632static void yy_parse_failed(
633 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000634){
drh1f245e42002-03-11 13:55:50 +0000635 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000636#ifndef NDEBUG
637 if( yyTraceFILE ){
638 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
639 }
640#endif
drh1f245e42002-03-11 13:55:50 +0000641 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000642 /* Here code is inserted which will be executed whenever the
643 ** parser fails */
644%%
drh1f245e42002-03-11 13:55:50 +0000645 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000646}
drhd3ec02d2009-06-12 02:27:14 +0000647#endif /* YYNOERRORRECOVERY */
drh75897232000-05-29 14:26:00 +0000648
649/*
650** The following code executes when a syntax error first occurs.
651*/
652static void yy_syntax_error(
653 yyParser *yypParser, /* The parser */
654 int yymajor, /* The major type of the error token */
655 YYMINORTYPE yyminor /* The minor type of the error token */
drh75897232000-05-29 14:26:00 +0000656){
drh1f245e42002-03-11 13:55:50 +0000657 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000658#define TOKEN (yyminor.yy0)
659%%
drh1f245e42002-03-11 13:55:50 +0000660 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000661}
662
663/*
664** The following is executed when the parser accepts
665*/
666static void yy_accept(
667 yyParser *yypParser /* The parser */
drh75897232000-05-29 14:26:00 +0000668){
drh1f245e42002-03-11 13:55:50 +0000669 ParseARG_FETCH;
drh75897232000-05-29 14:26:00 +0000670#ifndef NDEBUG
671 if( yyTraceFILE ){
672 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
673 }
674#endif
drh1f245e42002-03-11 13:55:50 +0000675 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
drh75897232000-05-29 14:26:00 +0000676 /* Here code is inserted which will be executed whenever the
677 ** parser accepts */
678%%
drh1f245e42002-03-11 13:55:50 +0000679 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
drh75897232000-05-29 14:26:00 +0000680}
681
682/* The main parser program.
683** The first argument is a pointer to a structure obtained from
684** "ParseAlloc" which describes the current state of the parser.
685** The second argument is the major token number. The third is
686** the minor token. The fourth optional argument is whatever the
687** user wants (and specified in the grammar) and is available for
688** use by the action routines.
689**
690** Inputs:
691** <ul>
692** <li> A pointer to the parser (an opaque structure.)
693** <li> The major token number.
694** <li> The minor token number.
695** <li> An option argument of a grammar-specified type.
696** </ul>
697**
698** Outputs:
699** None.
700*/
drh75897232000-05-29 14:26:00 +0000701void Parse(
702 void *yyp, /* The parser */
703 int yymajor, /* The major token code number */
704 ParseTOKENTYPE yyminor /* The value for the token */
drh1f245e42002-03-11 13:55:50 +0000705 ParseARG_PDECL /* Optional %extra_argument parameter */
drh75897232000-05-29 14:26:00 +0000706){
707 YYMINORTYPE yyminorunion;
708 int yyact; /* The parser action. */
709 int yyendofinput; /* True if we are at the end of input */
drhc4dd3fd2008-01-22 01:48:05 +0000710#ifdef YYERRORSYMBOL
drh75897232000-05-29 14:26:00 +0000711 int yyerrorhit = 0; /* True if yymajor has invoked an error */
drhc4dd3fd2008-01-22 01:48:05 +0000712#endif
drh75897232000-05-29 14:26:00 +0000713 yyParser *yypParser; /* The parser */
714
715 /* (re)initialize the parser, if necessary */
716 yypParser = (yyParser*)yyp;
drh1f245e42002-03-11 13:55:50 +0000717 if( yypParser->yyidx<0 ){
drhb7bac722007-03-29 02:26:45 +0000718#if YYSTACKDEPTH<=0
drhb19fd012007-03-29 01:44:45 +0000719 if( yypParser->yystksz <=0 ){
drh26c9b5e2008-04-11 14:56:53 +0000720 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
721 yyminorunion = yyzerominor;
drhb6018612007-03-30 13:35:05 +0000722 yyStackOverflow(yypParser, &yyminorunion);
drhb19fd012007-03-29 01:44:45 +0000723 return;
724 }
drhb7bac722007-03-29 02:26:45 +0000725#endif
drh1f245e42002-03-11 13:55:50 +0000726 yypParser->yyidx = 0;
727 yypParser->yyerrcnt = -1;
drh3ddfdf72003-12-22 14:53:19 +0000728 yypParser->yystack[0].stateno = 0;
729 yypParser->yystack[0].major = 0;
drh75897232000-05-29 14:26:00 +0000730 }
731 yyminorunion.yy0 = yyminor;
732 yyendofinput = (yymajor==0);
drh1f245e42002-03-11 13:55:50 +0000733 ParseARG_STORE;
drh75897232000-05-29 14:26:00 +0000734
735#ifndef NDEBUG
736 if( yyTraceFILE ){
737 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
738 }
739#endif
740
741 do{
drh3abbd392008-12-10 23:04:13 +0000742 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
drh75897232000-05-29 14:26:00 +0000743 if( yyact<YYNSTATE ){
drh55176252008-01-22 14:50:16 +0000744 assert( !yyendofinput ); /* Impossible to shift the $ token */
drh75897232000-05-29 14:26:00 +0000745 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000746 yypParser->yyerrcnt--;
drh55176252008-01-22 14:50:16 +0000747 yymajor = YYNOCODE;
drh75897232000-05-29 14:26:00 +0000748 }else if( yyact < YYNSTATE + YYNRULE ){
drh1f245e42002-03-11 13:55:50 +0000749 yy_reduce(yypParser,yyact-YYNSTATE);
drhc4dd3fd2008-01-22 01:48:05 +0000750 }else{
751 assert( yyact == YY_ERROR_ACTION );
752#ifdef YYERRORSYMBOL
drh3ddfdf72003-12-22 14:53:19 +0000753 int yymx;
drhc4dd3fd2008-01-22 01:48:05 +0000754#endif
drh75897232000-05-29 14:26:00 +0000755#ifndef NDEBUG
756 if( yyTraceFILE ){
757 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
758 }
759#endif
760#ifdef YYERRORSYMBOL
761 /* A syntax error has occurred.
762 ** The response to an error depends upon whether or not the
763 ** grammar defines an error token "ERROR".
764 **
765 ** This is what we do if the grammar does define ERROR:
766 **
767 ** * Call the %syntax_error function.
768 **
769 ** * Begin popping the stack until we enter a state where
770 ** it is legal to shift the error symbol, then shift
771 ** the error symbol.
772 **
773 ** * Set the error count to three.
774 **
775 ** * Begin accepting and shifting new tokens. No new error
776 ** processing will occur until three tokens have been
777 ** shifted successfully.
778 **
779 */
drh1f245e42002-03-11 13:55:50 +0000780 if( yypParser->yyerrcnt<0 ){
781 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000782 }
drh3ddfdf72003-12-22 14:53:19 +0000783 yymx = yypParser->yystack[yypParser->yyidx].major;
784 if( yymx==YYERRORSYMBOL || yyerrorhit ){
drh75897232000-05-29 14:26:00 +0000785#ifndef NDEBUG
786 if( yyTraceFILE ){
787 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
788 yyTracePrompt,yyTokenName[yymajor]);
789 }
790#endif
drhb27b7f52008-12-10 18:03:45 +0000791 yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
drh75897232000-05-29 14:26:00 +0000792 yymajor = YYNOCODE;
793 }else{
794 while(
drh1f245e42002-03-11 13:55:50 +0000795 yypParser->yyidx >= 0 &&
drh3ddfdf72003-12-22 14:53:19 +0000796 yymx != YYERRORSYMBOL &&
drhf3a58882006-05-08 15:14:19 +0000797 (yyact = yy_find_reduce_action(
798 yypParser->yystack[yypParser->yyidx].stateno,
799 YYERRORSYMBOL)) >= YYNSTATE
drh75897232000-05-29 14:26:00 +0000800 ){
801 yy_pop_parser_stack(yypParser);
802 }
drh1f245e42002-03-11 13:55:50 +0000803 if( yypParser->yyidx < 0 || yymajor==0 ){
drhb27b7f52008-12-10 18:03:45 +0000804 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
drh1f245e42002-03-11 13:55:50 +0000805 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000806 yymajor = YYNOCODE;
drh3ddfdf72003-12-22 14:53:19 +0000807 }else if( yymx!=YYERRORSYMBOL ){
drh75897232000-05-29 14:26:00 +0000808 YYMINORTYPE u2;
809 u2.YYERRSYMDT = 0;
810 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
811 }
812 }
drh1f245e42002-03-11 13:55:50 +0000813 yypParser->yyerrcnt = 3;
drh75897232000-05-29 14:26:00 +0000814 yyerrorhit = 1;
drhd3ec02d2009-06-12 02:27:14 +0000815#elif defined(YYNOERRORRECOVERY)
816 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
817 ** do any kind of error recovery. Instead, simply invoke the syntax
818 ** error routine and continue going as if nothing had happened.
819 **
820 ** Applications can set this macro (for example inside %include) if
821 ** they intend to abandon the parse upon the first syntax error seen.
822 */
823 yy_syntax_error(yypParser,yymajor,yyminorunion);
824 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
825 yymajor = YYNOCODE;
826
drh75897232000-05-29 14:26:00 +0000827#else /* YYERRORSYMBOL is not defined */
828 /* This is what we do if the grammar does not define ERROR:
829 **
830 ** * Report an error message, and throw away the input token.
831 **
832 ** * If the input token is $, then fail the parse.
833 **
834 ** As before, subsequent error messages are suppressed until
835 ** three input tokens have been successfully shifted.
836 */
drh1f245e42002-03-11 13:55:50 +0000837 if( yypParser->yyerrcnt<=0 ){
838 yy_syntax_error(yypParser,yymajor,yyminorunion);
drh75897232000-05-29 14:26:00 +0000839 }
drh1f245e42002-03-11 13:55:50 +0000840 yypParser->yyerrcnt = 3;
drhb27b7f52008-12-10 18:03:45 +0000841 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
drh75897232000-05-29 14:26:00 +0000842 if( yyendofinput ){
drh1f245e42002-03-11 13:55:50 +0000843 yy_parse_failed(yypParser);
drh75897232000-05-29 14:26:00 +0000844 }
845 yymajor = YYNOCODE;
846#endif
drh75897232000-05-29 14:26:00 +0000847 }
drh1f245e42002-03-11 13:55:50 +0000848 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
drh75897232000-05-29 14:26:00 +0000849 return;
850}