OLD | NEW |
| (Empty) |
1 /* Driver template for the LEMON parser generator. | |
2 ** The author disclaims copyright to this source code. | |
3 */ | |
4 /* First off, code is included that follows the "include" declaration | |
5 ** in the input grammar 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. | |
34 ** 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. | |
37 ** 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". | |
47 ** YYSTACKDEPTH is the maximum depth of the parser's stack. If | |
48 ** zero the stack is dynamically sized using realloc() | |
49 ** 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 | |
53 ** 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) | |
62 | |
63 /* The yyzerominor constant is used to initialize instances of | |
64 ** YYMINORTYPE objects to zero. */ | |
65 static const YYMINORTYPE yyzerominor = { 0 }; | |
66 | |
67 /* 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 | |
79 | |
80 /* Next are the tables used to determine what action to take based on the | |
81 ** 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. | |
84 ** | |
85 ** Suppose the action integer is N. Then the action is determined as | |
86 ** follows | |
87 ** | |
88 ** 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 | |
102 ** | |
103 ** yy_action[ yy_shift_ofst[S] + X ] | |
104 ** | |
105 ** If the index value yy_shift_ofst[S]+X is out of range or if the value | |
106 ** 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. | |
126 */ | |
127 %% | |
128 | |
129 /* The next table maps tokens into fallback tokens. If a construct | |
130 ** like the following: | |
131 ** | |
132 ** %fallback ID X Y Z. | |
133 ** | |
134 ** appears in the grammar, then ID becomes a fallback token for X, Y, | |
135 ** 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 | |
140 static const YYCODETYPE yyFallback[] = { | |
141 %% | |
142 }; | |
143 #endif /* YYFALLBACK */ | |
144 | |
145 /* 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 */ | |
157 struct yyStackEntry { | |
158 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 */ | |
163 }; | |
164 typedef struct yyStackEntry yyStackEntry; | |
165 | |
166 /* The state of the parser is completely contained in an instance of | |
167 ** the following structure */ | |
168 struct yyParser { | |
169 int yyidx; /* Index of top element in stack */ | |
170 #ifdef YYTRACKMAXSTACKDEPTH | |
171 int yyidxMax; /* Maximum value of yyidx */ | |
172 #endif | |
173 int yyerrcnt; /* Shifts left before out of the error */ | |
174 ParseARG_SDECL /* A place to hold %extra_argument */ | |
175 #if YYSTACKDEPTH<=0 | |
176 int yystksz; /* Current side of the stack */ | |
177 yyStackEntry *yystack; /* The parser's stack */ | |
178 #else | |
179 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ | |
180 #endif | |
181 }; | |
182 typedef struct yyParser yyParser; | |
183 | |
184 #ifndef NDEBUG | |
185 #include <stdio.h> | |
186 static FILE *yyTraceFILE = 0; | |
187 static char *yyTracePrompt = 0; | |
188 #endif /* NDEBUG */ | |
189 | |
190 #ifndef NDEBUG | |
191 /* | |
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 */ | |
208 void 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 } | |
214 #endif /* NDEBUG */ | |
215 | |
216 #ifndef NDEBUG | |
217 /* For tracing shifts, the names of all terminals and nonterminals | |
218 ** are required. The following table supplies these names */ | |
219 static const char *const yyTokenName[] = { | |
220 %% | |
221 }; | |
222 #endif /* NDEBUG */ | |
223 | |
224 #ifndef NDEBUG | |
225 /* For tracing reduce actions, the names of all rules are required. | |
226 */ | |
227 static const char *const yyRuleName[] = { | |
228 %% | |
229 }; | |
230 #endif /* NDEBUG */ | |
231 | |
232 | |
233 #if YYSTACKDEPTH<=0 | |
234 /* | |
235 ** Try to increase the size of the parser stack. | |
236 */ | |
237 static 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 | |
256 /* | |
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 */ | |
268 void *ParseAlloc(void *(*mallocProc)(size_t)){ | |
269 yyParser *pParser; | |
270 pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) ); | |
271 if( pParser ){ | |
272 pParser->yyidx = -1; | |
273 #ifdef YYTRACKMAXSTACKDEPTH | |
274 pParser->yyidxMax = 0; | |
275 #endif | |
276 #if YYSTACKDEPTH<=0 | |
277 pParser->yystack = NULL; | |
278 pParser->yystksz = 0; | |
279 yyGrowStack(pParser); | |
280 #endif | |
281 } | |
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 */ | |
290 static 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; | |
296 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 */ | |
320 static int yy_pop_parser_stack(yyParser *pParser){ | |
321 YYCODETYPE yymajor; | |
322 yyStackEntry *yytos = &pParser->yystack[pParser->yyidx]; | |
323 | |
324 if( pParser->yyidx<0 ) return 0; | |
325 #ifndef NDEBUG | |
326 if( yyTraceFILE && pParser->yyidx>=0 ){ | |
327 fprintf(yyTraceFILE,"%sPopping %s\n", | |
328 yyTracePrompt, | |
329 yyTokenName[yytos->major]); | |
330 } | |
331 #endif | |
332 yymajor = yytos->major; | |
333 yy_destructor(pParser, yymajor, &yytos->minor); | |
334 pParser->yyidx--; | |
335 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 */ | |
350 void ParseFree( | |
351 void *p, /* The parser to be deleted */ | |
352 void (*freeProc)(void*) /* Function used to reclaim memory */ | |
353 ){ | |
354 yyParser *pParser = (yyParser*)p; | |
355 if( pParser==0 ) return; | |
356 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); | |
357 #if YYSTACKDEPTH<=0 | |
358 free(pParser->yystack); | |
359 #endif | |
360 (*freeProc)((void*)pParser); | |
361 } | |
362 | |
363 /* | |
364 ** Return the peak depth of the stack for a parser. | |
365 */ | |
366 #ifdef YYTRACKMAXSTACKDEPTH | |
367 int ParseStackPeak(void *p){ | |
368 yyParser *pParser = (yyParser*)p; | |
369 return pParser->yyidxMax; | |
370 } | |
371 #endif | |
372 | |
373 /* | |
374 ** Find the appropriate action for a parser given the terminal | |
375 ** look-ahead token iLookAhead. | |
376 ** | |
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 */ | |
381 static int yy_find_shift_action( | |
382 yyParser *pParser, /* The parser */ | |
383 YYCODETYPE iLookAhead /* The look-ahead token */ | |
384 ){ | |
385 int i; | |
386 int stateno = pParser->yystack[pParser->yyidx].stateno; | |
387 | |
388 if( stateno>YY_SHIFT_COUNT | |
389 || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){ | |
390 return yy_default[stateno]; | |
391 } | |
392 assert( iLookAhead!=YYNOCODE ); | |
393 i += iLookAhead; | |
394 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ | |
395 if( iLookAhead>0 ){ | |
396 #ifdef YYFALLBACK | |
397 YYCODETYPE iFallback; /* Fallback token */ | |
398 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) | |
399 && (iFallback = yyFallback[iLookAhead])!=0 ){ | |
400 #ifndef NDEBUG | |
401 if( yyTraceFILE ){ | |
402 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n", | |
403 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); | |
404 } | |
405 #endif | |
406 return yy_find_shift_action(pParser, iFallback); | |
407 } | |
408 #endif | |
409 #ifdef YYWILDCARD | |
410 { | |
411 int j = i - iLookAhead + YYWILDCARD; | |
412 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 ){ | |
421 #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]; | |
428 } | |
429 } | |
430 #endif /* YYWILDCARD */ | |
431 } | |
432 return yy_default[stateno]; | |
433 }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 */ | |
446 static int yy_find_reduce_action( | |
447 int stateno, /* Current state number */ | |
448 YYCODETYPE iLookAhead /* The look-ahead token */ | |
449 ){ | |
450 int i; | |
451 #ifdef YYERRORSYMBOL | |
452 if( stateno>YY_REDUCE_COUNT ){ | |
453 return yy_default[stateno]; | |
454 } | |
455 #else | |
456 assert( stateno<=YY_REDUCE_COUNT ); | |
457 #endif | |
458 i = yy_reduce_ofst[stateno]; | |
459 assert( i!=YY_REDUCE_USE_DFLT ); | |
460 assert( iLookAhead!=YYNOCODE ); | |
461 i += iLookAhead; | |
462 #ifdef YYERRORSYMBOL | |
463 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ | |
464 return yy_default[stateno]; | |
465 } | |
466 #else | |
467 assert( i>=0 && i<YY_ACTTAB_COUNT ); | |
468 assert( yy_lookahead[i]==iLookAhead ); | |
469 #endif | |
470 return yy_action[i]; | |
471 } | |
472 | |
473 /* | |
474 ** The following routine is called if the stack overflows. | |
475 */ | |
476 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){ | |
477 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 /* | |
492 ** Perform a shift action. | |
493 */ | |
494 static 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 */ | |
498 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */ | |
499 ){ | |
500 yyStackEntry *yytos; | |
501 yypParser->yyidx++; | |
502 #ifdef YYTRACKMAXSTACKDEPTH | |
503 if( yypParser->yyidx>yypParser->yyidxMax ){ | |
504 yypParser->yyidxMax = yypParser->yyidx; | |
505 } | |
506 #endif | |
507 #if YYSTACKDEPTH>0 | |
508 if( yypParser->yyidx>=YYSTACKDEPTH ){ | |
509 yyStackOverflow(yypParser, yypMinor); | |
510 return; | |
511 } | |
512 #else | |
513 if( yypParser->yyidx>=yypParser->yystksz ){ | |
514 yyGrowStack(yypParser); | |
515 if( yypParser->yyidx>=yypParser->yystksz ){ | |
516 yyStackOverflow(yypParser, yypMinor); | |
517 return; | |
518 } | |
519 } | |
520 #endif | |
521 yytos = &yypParser->yystack[yypParser->yyidx]; | |
522 yytos->stateno = (YYACTIONTYPE)yyNewState; | |
523 yytos->major = (YYCODETYPE)yyMajor; | |
524 yytos->minor = *yypMinor; | |
525 #ifndef NDEBUG | |
526 if( yyTraceFILE && yypParser->yyidx>0 ){ | |
527 int i; | |
528 fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState); | |
529 fprintf(yyTraceFILE,"%sStack:",yyTracePrompt); | |
530 for(i=1; i<=yypParser->yyidx; i++) | |
531 fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]); | |
532 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 */ | |
540 static const struct { | |
541 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 | |
547 static void yy_accept(yyParser*); /* Forward Declaration */ | |
548 | |
549 /* | |
550 ** Perform a reduce action and the shift that must immediately | |
551 ** follow the reduce. | |
552 */ | |
553 static void yy_reduce( | |
554 yyParser *yypParser, /* The parser */ | |
555 int yyruleno /* Number of the rule by which to reduce */ | |
556 ){ | |
557 int yygoto; /* The next state */ | |
558 int yyact; /* The next action */ | |
559 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ | |
560 yyStackEntry *yymsp; /* The top of the parser's stack */ | |
561 int yysize; /* Amount to pop the stack */ | |
562 ParseARG_FETCH; | |
563 yymsp = &yypParser->yystack[yypParser->yyidx]; | |
564 #ifndef NDEBUG | |
565 if( yyTraceFILE && yyruleno>=0 | |
566 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ | |
567 fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt, | |
568 yyRuleName[yyruleno]); | |
569 } | |
570 #endif /* NDEBUG */ | |
571 | |
572 /* 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. | |
579 ** | |
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) | |
585 */ | |
586 /*memset(&yygotominor, 0, sizeof(yygotominor));*/ | |
587 yygotominor = yyzerominor; | |
588 | |
589 | |
590 switch( yyruleno ){ | |
591 /* Beginning here are the reduction cases. A typical example | |
592 ** follows: | |
593 ** case 0: | |
594 ** #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; | |
603 yypParser->yyidx -= yysize; | |
604 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); | |
605 if( yyact < YYNSTATE ){ | |
606 #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; | |
614 yymsp->stateno = (YYACTIONTYPE)yyact; | |
615 yymsp->major = (YYCODETYPE)yygoto; | |
616 yymsp->minor = yygotominor; | |
617 }else | |
618 #endif | |
619 { | |
620 yy_shift(yypParser,yyact,yygoto,&yygotominor); | |
621 } | |
622 }else{ | |
623 assert( yyact == YYNSTATE + YYNRULE + 1 ); | |
624 yy_accept(yypParser); | |
625 } | |
626 } | |
627 | |
628 /* | |
629 ** The following code executes when the parse fails | |
630 */ | |
631 #ifndef YYNOERRORRECOVERY | |
632 static void yy_parse_failed( | |
633 yyParser *yypParser /* The parser */ | |
634 ){ | |
635 ParseARG_FETCH; | |
636 #ifndef NDEBUG | |
637 if( yyTraceFILE ){ | |
638 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); | |
639 } | |
640 #endif | |
641 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
642 /* Here code is inserted which will be executed whenever the | |
643 ** parser fails */ | |
644 %% | |
645 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
646 } | |
647 #endif /* YYNOERRORRECOVERY */ | |
648 | |
649 /* | |
650 ** The following code executes when a syntax error first occurs. | |
651 */ | |
652 static 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 */ | |
656 ){ | |
657 ParseARG_FETCH; | |
658 #define TOKEN (yyminor.yy0) | |
659 %% | |
660 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
661 } | |
662 | |
663 /* | |
664 ** The following is executed when the parser accepts | |
665 */ | |
666 static void yy_accept( | |
667 yyParser *yypParser /* The parser */ | |
668 ){ | |
669 ParseARG_FETCH; | |
670 #ifndef NDEBUG | |
671 if( yyTraceFILE ){ | |
672 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); | |
673 } | |
674 #endif | |
675 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
676 /* Here code is inserted which will be executed whenever the | |
677 ** parser accepts */ | |
678 %% | |
679 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
680 } | |
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 */ | |
701 void Parse( | |
702 void *yyp, /* The parser */ | |
703 int yymajor, /* The major token code number */ | |
704 ParseTOKENTYPE yyminor /* The value for the token */ | |
705 ParseARG_PDECL /* Optional %extra_argument parameter */ | |
706 ){ | |
707 YYMINORTYPE yyminorunion; | |
708 int yyact; /* The parser action. */ | |
709 int yyendofinput; /* True if we are at the end of input */ | |
710 #ifdef YYERRORSYMBOL | |
711 int yyerrorhit = 0; /* True if yymajor has invoked an error */ | |
712 #endif | |
713 yyParser *yypParser; /* The parser */ | |
714 | |
715 /* (re)initialize the parser, if necessary */ | |
716 yypParser = (yyParser*)yyp; | |
717 if( yypParser->yyidx<0 ){ | |
718 #if YYSTACKDEPTH<=0 | |
719 if( yypParser->yystksz <=0 ){ | |
720 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/ | |
721 yyminorunion = yyzerominor; | |
722 yyStackOverflow(yypParser, &yyminorunion); | |
723 return; | |
724 } | |
725 #endif | |
726 yypParser->yyidx = 0; | |
727 yypParser->yyerrcnt = -1; | |
728 yypParser->yystack[0].stateno = 0; | |
729 yypParser->yystack[0].major = 0; | |
730 } | |
731 yyminorunion.yy0 = yyminor; | |
732 yyendofinput = (yymajor==0); | |
733 ParseARG_STORE; | |
734 | |
735 #ifndef NDEBUG | |
736 if( yyTraceFILE ){ | |
737 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]); | |
738 } | |
739 #endif | |
740 | |
741 do{ | |
742 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); | |
743 if( yyact<YYNSTATE ){ | |
744 assert( !yyendofinput ); /* Impossible to shift the $ token */ | |
745 yy_shift(yypParser,yyact,yymajor,&yyminorunion); | |
746 yypParser->yyerrcnt--; | |
747 yymajor = YYNOCODE; | |
748 }else if( yyact < YYNSTATE + YYNRULE ){ | |
749 yy_reduce(yypParser,yyact-YYNSTATE); | |
750 }else{ | |
751 assert( yyact == YY_ERROR_ACTION ); | |
752 #ifdef YYERRORSYMBOL | |
753 int yymx; | |
754 #endif | |
755 #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 */ | |
780 if( yypParser->yyerrcnt<0 ){ | |
781 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
782 } | |
783 yymx = yypParser->yystack[yypParser->yyidx].major; | |
784 if( yymx==YYERRORSYMBOL || yyerrorhit ){ | |
785 #ifndef NDEBUG | |
786 if( yyTraceFILE ){ | |
787 fprintf(yyTraceFILE,"%sDiscard input token %s\n", | |
788 yyTracePrompt,yyTokenName[yymajor]); | |
789 } | |
790 #endif | |
791 yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion); | |
792 yymajor = YYNOCODE; | |
793 }else{ | |
794 while( | |
795 yypParser->yyidx >= 0 && | |
796 yymx != YYERRORSYMBOL && | |
797 (yyact = yy_find_reduce_action( | |
798 yypParser->yystack[yypParser->yyidx].stateno, | |
799 YYERRORSYMBOL)) >= YYNSTATE | |
800 ){ | |
801 yy_pop_parser_stack(yypParser); | |
802 } | |
803 if( yypParser->yyidx < 0 || yymajor==0 ){ | |
804 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
805 yy_parse_failed(yypParser); | |
806 yymajor = YYNOCODE; | |
807 }else if( yymx!=YYERRORSYMBOL ){ | |
808 YYMINORTYPE u2; | |
809 u2.YYERRSYMDT = 0; | |
810 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2); | |
811 } | |
812 } | |
813 yypParser->yyerrcnt = 3; | |
814 yyerrorhit = 1; | |
815 #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 | |
827 #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 */ | |
837 if( yypParser->yyerrcnt<=0 ){ | |
838 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
839 } | |
840 yypParser->yyerrcnt = 3; | |
841 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
842 if( yyendofinput ){ | |
843 yy_parse_failed(yypParser); | |
844 } | |
845 yymajor = YYNOCODE; | |
846 #endif | |
847 } | |
848 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); | |
849 return; | |
850 } | |
OLD | NEW |