OLD | NEW |
| (Empty) |
1 /* | |
2 ** 2000-05-29 | |
3 ** | |
4 ** The author disclaims copyright to this source code. In place of | |
5 ** a legal notice, here is a blessing: | |
6 ** | |
7 ** May you do good and not evil. | |
8 ** May you find forgiveness for yourself and forgive others. | |
9 ** May you share freely, never taking more than you give. | |
10 ** | |
11 ************************************************************************* | |
12 ** Driver template for the LEMON parser generator. | |
13 ** | |
14 ** The "lemon" program processes an LALR(1) input grammar file, then uses | |
15 ** this template to construct a parser. The "lemon" program inserts text | |
16 ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the | |
17 ** interstitial "-" characters) contained in this template is changed into | |
18 ** the value of the %name directive from the grammar. Otherwise, the content | |
19 ** of this template is copied straight through into the generate parser | |
20 ** source file. | |
21 ** | |
22 ** The following is the concatenation of all %include directives from the | |
23 ** input grammar file: | |
24 */ | |
25 #include <stdio.h> | |
26 /************ Begin %include sections from the grammar ************************/ | |
27 %% | |
28 /**************** End of %include directives **********************************/ | |
29 /* These constants specify the various numeric values for terminal symbols | |
30 ** in a format understandable to "makeheaders". This section is blank unless | |
31 ** "lemon" is run with the "-m" command-line option. | |
32 ***************** Begin makeheaders token definitions *************************/ | |
33 %% | |
34 /**************** End makeheaders token definitions ***************************/ | |
35 | |
36 /* The next sections is a series of control #defines. | |
37 ** various aspects of the generated parser. | |
38 ** YYCODETYPE is the data type used to store the integer codes | |
39 ** that represent terminal and non-terminal symbols. | |
40 ** "unsigned char" is used if there are fewer than | |
41 ** 256 symbols. Larger types otherwise. | |
42 ** YYNOCODE is a number of type YYCODETYPE that is not used for | |
43 ** any terminal or nonterminal symbol. | |
44 ** YYFALLBACK If defined, this indicates that one or more tokens | |
45 ** (also known as: "terminal symbols") have fall-back | |
46 ** values which should be used if the original symbol | |
47 ** would not parse. This permits keywords to sometimes | |
48 ** be used as identifiers, for example. | |
49 ** YYACTIONTYPE is the data type used for "action codes" - numbers | |
50 ** that indicate what to do in response to the next | |
51 ** token. | |
52 ** ParseTOKENTYPE is the data type used for minor type for terminal | |
53 ** symbols. Background: A "minor type" is a semantic | |
54 ** value associated with a terminal or non-terminal | |
55 ** symbols. For example, for an "ID" terminal symbol, | |
56 ** the minor type might be the name of the identifier. | |
57 ** Each non-terminal can have a different minor type. | |
58 ** Terminal symbols all have the same minor type, though. | |
59 ** This macros defines the minor type for terminal | |
60 ** symbols. | |
61 ** YYMINORTYPE is the data type used for all minor types. | |
62 ** This is typically a union of many types, one of | |
63 ** which is ParseTOKENTYPE. The entry in the union | |
64 ** for terminal symbols is called "yy0". | |
65 ** YYSTACKDEPTH is the maximum depth of the parser's stack. If | |
66 ** zero the stack is dynamically sized using realloc() | |
67 ** ParseARG_SDECL A static variable declaration for the %extra_argument | |
68 ** ParseARG_PDECL A parameter declaration for the %extra_argument | |
69 ** ParseARG_STORE Code to store %extra_argument into yypParser | |
70 ** ParseARG_FETCH Code to extract %extra_argument from yypParser | |
71 ** YYERRORSYMBOL is the code number of the error symbol. If not | |
72 ** defined, then do no error processing. | |
73 ** YYNSTATE the combined number of states. | |
74 ** YYNRULE the number of rules in the grammar | |
75 ** YY_MAX_SHIFT Maximum value for shift actions | |
76 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions | |
77 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions | |
78 ** YY_MIN_REDUCE Maximum value for reduce actions | |
79 ** YY_ERROR_ACTION The yy_action[] code for syntax error | |
80 ** YY_ACCEPT_ACTION The yy_action[] code for accept | |
81 ** YY_NO_ACTION The yy_action[] code for no-op | |
82 */ | |
83 #ifndef INTERFACE | |
84 # define INTERFACE 1 | |
85 #endif | |
86 /************* Begin control #defines *****************************************/ | |
87 %% | |
88 /************* End control #defines *******************************************/ | |
89 | |
90 /* The yyzerominor constant is used to initialize instances of | |
91 ** YYMINORTYPE objects to zero. */ | |
92 static const YYMINORTYPE yyzerominor = { 0 }; | |
93 | |
94 /* Define the yytestcase() macro to be a no-op if is not already defined | |
95 ** otherwise. | |
96 ** | |
97 ** Applications can choose to define yytestcase() in the %include section | |
98 ** to a macro that can assist in verifying code coverage. For production | |
99 ** code the yytestcase() macro should be turned off. But it is useful | |
100 ** for testing. | |
101 */ | |
102 #ifndef yytestcase | |
103 # define yytestcase(X) | |
104 #endif | |
105 | |
106 | |
107 /* Next are the tables used to determine what action to take based on the | |
108 ** current state and lookahead token. These tables are used to implement | |
109 ** functions that take a state number and lookahead value and return an | |
110 ** action integer. | |
111 ** | |
112 ** Suppose the action integer is N. Then the action is determined as | |
113 ** follows | |
114 ** | |
115 ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead | |
116 ** token onto the stack and goto state N. | |
117 ** | |
118 ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then | |
119 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE. | |
120 ** | |
121 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE | |
122 ** and YY_MAX_REDUCE | |
123 | |
124 ** N == YY_ERROR_ACTION A syntax error has occurred. | |
125 ** | |
126 ** N == YY_ACCEPT_ACTION The parser accepts its input. | |
127 ** | |
128 ** N == YY_NO_ACTION No such action. Denotes unused | |
129 ** slots in the yy_action[] table. | |
130 ** | |
131 ** The action table is constructed as a single large table named yy_action[]. | |
132 ** Given state S and lookahead X, the action is computed as | |
133 ** | |
134 ** yy_action[ yy_shift_ofst[S] + X ] | |
135 ** | |
136 ** If the index value yy_shift_ofst[S]+X is out of range or if the value | |
137 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S] | |
138 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table | |
139 ** and that yy_default[S] should be used instead. | |
140 ** | |
141 ** The formula above is for computing the action when the lookahead is | |
142 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after | |
143 ** a reduce action) then the yy_reduce_ofst[] array is used in place of | |
144 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of | |
145 ** YY_SHIFT_USE_DFLT. | |
146 ** | |
147 ** The following are the tables generated in this section: | |
148 ** | |
149 ** yy_action[] A single table containing all actions. | |
150 ** yy_lookahead[] A table containing the lookahead for each entry in | |
151 ** yy_action. Used to detect hash collisions. | |
152 ** yy_shift_ofst[] For each state, the offset into yy_action for | |
153 ** shifting terminals. | |
154 ** yy_reduce_ofst[] For each state, the offset into yy_action for | |
155 ** shifting non-terminals after a reduce. | |
156 ** yy_default[] Default action for each state. | |
157 ** | |
158 *********** Begin parsing tables **********************************************/ | |
159 %% | |
160 /********** End of lemon-generated parsing tables *****************************/ | |
161 | |
162 /* The next table maps tokens (terminal symbols) into fallback tokens. | |
163 ** If a construct like the following: | |
164 ** | |
165 ** %fallback ID X Y Z. | |
166 ** | |
167 ** appears in the grammar, then ID becomes a fallback token for X, Y, | |
168 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser | |
169 ** but it does not parse, the type of the token is changed to ID and | |
170 ** the parse is retried before an error is thrown. | |
171 ** | |
172 ** This feature can be used, for example, to cause some keywords in a language | |
173 ** to revert to identifiers if they keyword does not apply in the context where | |
174 ** it appears. | |
175 */ | |
176 #ifdef YYFALLBACK | |
177 static const YYCODETYPE yyFallback[] = { | |
178 %% | |
179 }; | |
180 #endif /* YYFALLBACK */ | |
181 | |
182 /* The following structure represents a single element of the | |
183 ** parser's stack. Information stored includes: | |
184 ** | |
185 ** + The state number for the parser at this level of the stack. | |
186 ** | |
187 ** + The value of the token stored at this level of the stack. | |
188 ** (In other words, the "major" token.) | |
189 ** | |
190 ** + The semantic value stored at this level of the stack. This is | |
191 ** the information used by the action routines in the grammar. | |
192 ** It is sometimes called the "minor" token. | |
193 ** | |
194 ** After the "shift" half of a SHIFTREDUCE action, the stateno field | |
195 ** actually contains the reduce action for the second half of the | |
196 ** SHIFTREDUCE. | |
197 */ | |
198 struct yyStackEntry { | |
199 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */ | |
200 YYCODETYPE major; /* The major token value. This is the code | |
201 ** number for the token at this stack level */ | |
202 YYMINORTYPE minor; /* The user-supplied minor token value. This | |
203 ** is the value of the token */ | |
204 }; | |
205 typedef struct yyStackEntry yyStackEntry; | |
206 | |
207 /* The state of the parser is completely contained in an instance of | |
208 ** the following structure */ | |
209 struct yyParser { | |
210 int yyidx; /* Index of top element in stack */ | |
211 #ifdef YYTRACKMAXSTACKDEPTH | |
212 int yyidxMax; /* Maximum value of yyidx */ | |
213 #endif | |
214 int yyerrcnt; /* Shifts left before out of the error */ | |
215 ParseARG_SDECL /* A place to hold %extra_argument */ | |
216 #if YYSTACKDEPTH<=0 | |
217 int yystksz; /* Current side of the stack */ | |
218 yyStackEntry *yystack; /* The parser's stack */ | |
219 #else | |
220 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ | |
221 #endif | |
222 }; | |
223 typedef struct yyParser yyParser; | |
224 | |
225 #ifndef NDEBUG | |
226 #include <stdio.h> | |
227 static FILE *yyTraceFILE = 0; | |
228 static char *yyTracePrompt = 0; | |
229 #endif /* NDEBUG */ | |
230 | |
231 #ifndef NDEBUG | |
232 /* | |
233 ** Turn parser tracing on by giving a stream to which to write the trace | |
234 ** and a prompt to preface each trace message. Tracing is turned off | |
235 ** by making either argument NULL | |
236 ** | |
237 ** Inputs: | |
238 ** <ul> | |
239 ** <li> A FILE* to which trace output should be written. | |
240 ** If NULL, then tracing is turned off. | |
241 ** <li> A prefix string written at the beginning of every | |
242 ** line of trace output. If NULL, then tracing is | |
243 ** turned off. | |
244 ** </ul> | |
245 ** | |
246 ** Outputs: | |
247 ** None. | |
248 */ | |
249 void ParseTrace(FILE *TraceFILE, char *zTracePrompt){ | |
250 yyTraceFILE = TraceFILE; | |
251 yyTracePrompt = zTracePrompt; | |
252 if( yyTraceFILE==0 ) yyTracePrompt = 0; | |
253 else if( yyTracePrompt==0 ) yyTraceFILE = 0; | |
254 } | |
255 #endif /* NDEBUG */ | |
256 | |
257 #ifndef NDEBUG | |
258 /* For tracing shifts, the names of all terminals and nonterminals | |
259 ** are required. The following table supplies these names */ | |
260 static const char *const yyTokenName[] = { | |
261 %% | |
262 }; | |
263 #endif /* NDEBUG */ | |
264 | |
265 #ifndef NDEBUG | |
266 /* For tracing reduce actions, the names of all rules are required. | |
267 */ | |
268 static const char *const yyRuleName[] = { | |
269 %% | |
270 }; | |
271 #endif /* NDEBUG */ | |
272 | |
273 | |
274 #if YYSTACKDEPTH<=0 | |
275 /* | |
276 ** Try to increase the size of the parser stack. | |
277 */ | |
278 static void yyGrowStack(yyParser *p){ | |
279 int newSize; | |
280 yyStackEntry *pNew; | |
281 | |
282 newSize = p->yystksz*2 + 100; | |
283 pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); | |
284 if( pNew ){ | |
285 p->yystack = pNew; | |
286 p->yystksz = newSize; | |
287 #ifndef NDEBUG | |
288 if( yyTraceFILE ){ | |
289 fprintf(yyTraceFILE,"%sStack grows to %d entries!\n", | |
290 yyTracePrompt, p->yystksz); | |
291 } | |
292 #endif | |
293 } | |
294 } | |
295 #endif | |
296 | |
297 /* Datatype of the argument to the memory allocated passed as the | |
298 ** second argument to ParseAlloc() below. This can be changed by | |
299 ** putting an appropriate #define in the %include section of the input | |
300 ** grammar. | |
301 */ | |
302 #ifndef YYMALLOCARGTYPE | |
303 # define YYMALLOCARGTYPE size_t | |
304 #endif | |
305 | |
306 /* | |
307 ** This function allocates a new parser. | |
308 ** The only argument is a pointer to a function which works like | |
309 ** malloc. | |
310 ** | |
311 ** Inputs: | |
312 ** A pointer to the function used to allocate memory. | |
313 ** | |
314 ** Outputs: | |
315 ** A pointer to a parser. This pointer is used in subsequent calls | |
316 ** to Parse and ParseFree. | |
317 */ | |
318 void *ParseAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){ | |
319 yyParser *pParser; | |
320 pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); | |
321 if( pParser ){ | |
322 pParser->yyidx = -1; | |
323 #ifdef YYTRACKMAXSTACKDEPTH | |
324 pParser->yyidxMax = 0; | |
325 #endif | |
326 #if YYSTACKDEPTH<=0 | |
327 pParser->yystack = NULL; | |
328 pParser->yystksz = 0; | |
329 yyGrowStack(pParser); | |
330 #endif | |
331 } | |
332 return pParser; | |
333 } | |
334 | |
335 /* The following function deletes the "minor type" or semantic value | |
336 ** associated with a symbol. The symbol can be either a terminal | |
337 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is | |
338 ** a pointer to the value to be deleted. The code used to do the | |
339 ** deletions is derived from the %destructor and/or %token_destructor | |
340 ** directives of the input grammar. | |
341 */ | |
342 static void yy_destructor( | |
343 yyParser *yypParser, /* The parser */ | |
344 YYCODETYPE yymajor, /* Type code for object to destroy */ | |
345 YYMINORTYPE *yypminor /* The object to be destroyed */ | |
346 ){ | |
347 ParseARG_FETCH; | |
348 switch( yymajor ){ | |
349 /* Here is inserted the actions which take place when a | |
350 ** terminal or non-terminal is destroyed. This can happen | |
351 ** when the symbol is popped from the stack during a | |
352 ** reduce or during error processing or when a parser is | |
353 ** being destroyed before it is finished parsing. | |
354 ** | |
355 ** Note: during a reduce, the only symbols destroyed are those | |
356 ** which appear on the RHS of the rule, but which are *not* used | |
357 ** inside the C code. | |
358 */ | |
359 /********* Begin destructor definitions ***************************************/ | |
360 %% | |
361 /********* End destructor definitions *****************************************/ | |
362 default: break; /* If no destructor action specified: do nothing */ | |
363 } | |
364 } | |
365 | |
366 /* | |
367 ** Pop the parser's stack once. | |
368 ** | |
369 ** If there is a destructor routine associated with the token which | |
370 ** is popped from the stack, then call it. | |
371 */ | |
372 static void yy_pop_parser_stack(yyParser *pParser){ | |
373 yyStackEntry *yytos; | |
374 assert( pParser->yyidx>=0 ); | |
375 yytos = &pParser->yystack[pParser->yyidx--]; | |
376 #ifndef NDEBUG | |
377 if( yyTraceFILE ){ | |
378 fprintf(yyTraceFILE,"%sPopping %s\n", | |
379 yyTracePrompt, | |
380 yyTokenName[yytos->major]); | |
381 } | |
382 #endif | |
383 yy_destructor(pParser, yytos->major, &yytos->minor); | |
384 } | |
385 | |
386 /* | |
387 ** Deallocate and destroy a parser. Destructors are called for | |
388 ** all stack elements before shutting the parser down. | |
389 ** | |
390 ** If the YYPARSEFREENEVERNULL macro exists (for example because it | |
391 ** is defined in a %include section of the input grammar) then it is | |
392 ** assumed that the input pointer is never NULL. | |
393 */ | |
394 void ParseFree( | |
395 void *p, /* The parser to be deleted */ | |
396 void (*freeProc)(void*) /* Function used to reclaim memory */ | |
397 ){ | |
398 yyParser *pParser = (yyParser*)p; | |
399 #ifndef YYPARSEFREENEVERNULL | |
400 if( pParser==0 ) return; | |
401 #endif | |
402 while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); | |
403 #if YYSTACKDEPTH<=0 | |
404 free(pParser->yystack); | |
405 #endif | |
406 (*freeProc)((void*)pParser); | |
407 } | |
408 | |
409 /* | |
410 ** Return the peak depth of the stack for a parser. | |
411 */ | |
412 #ifdef YYTRACKMAXSTACKDEPTH | |
413 int ParseStackPeak(void *p){ | |
414 yyParser *pParser = (yyParser*)p; | |
415 return pParser->yyidxMax; | |
416 } | |
417 #endif | |
418 | |
419 /* | |
420 ** Find the appropriate action for a parser given the terminal | |
421 ** look-ahead token iLookAhead. | |
422 */ | |
423 static int yy_find_shift_action( | |
424 yyParser *pParser, /* The parser */ | |
425 YYCODETYPE iLookAhead /* The look-ahead token */ | |
426 ){ | |
427 int i; | |
428 int stateno = pParser->yystack[pParser->yyidx].stateno; | |
429 | |
430 if( stateno>=YY_MIN_REDUCE ) return stateno; | |
431 assert( stateno <= YY_SHIFT_COUNT ); | |
432 do{ | |
433 i = yy_shift_ofst[stateno]; | |
434 if( i==YY_SHIFT_USE_DFLT ) return yy_default[stateno]; | |
435 assert( iLookAhead!=YYNOCODE ); | |
436 i += iLookAhead; | |
437 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ | |
438 if( iLookAhead>0 ){ | |
439 #ifdef YYFALLBACK | |
440 YYCODETYPE iFallback; /* Fallback token */ | |
441 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) | |
442 && (iFallback = yyFallback[iLookAhead])!=0 ){ | |
443 #ifndef NDEBUG | |
444 if( yyTraceFILE ){ | |
445 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n", | |
446 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); | |
447 } | |
448 #endif | |
449 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */ | |
450 iLookAhead = iFallback; | |
451 continue; | |
452 } | |
453 #endif | |
454 #ifdef YYWILDCARD | |
455 { | |
456 int j = i - iLookAhead + YYWILDCARD; | |
457 if( | |
458 #if YY_SHIFT_MIN+YYWILDCARD<0 | |
459 j>=0 && | |
460 #endif | |
461 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT | |
462 j<YY_ACTTAB_COUNT && | |
463 #endif | |
464 yy_lookahead[j]==YYWILDCARD | |
465 ){ | |
466 #ifndef NDEBUG | |
467 if( yyTraceFILE ){ | |
468 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", | |
469 yyTracePrompt, yyTokenName[iLookAhead], | |
470 yyTokenName[YYWILDCARD]); | |
471 } | |
472 #endif /* NDEBUG */ | |
473 return yy_action[j]; | |
474 } | |
475 } | |
476 #endif /* YYWILDCARD */ | |
477 } | |
478 return yy_default[stateno]; | |
479 }else{ | |
480 return yy_action[i]; | |
481 } | |
482 }while(1); | |
483 } | |
484 | |
485 /* | |
486 ** Find the appropriate action for a parser given the non-terminal | |
487 ** look-ahead token iLookAhead. | |
488 */ | |
489 static int yy_find_reduce_action( | |
490 int stateno, /* Current state number */ | |
491 YYCODETYPE iLookAhead /* The look-ahead token */ | |
492 ){ | |
493 int i; | |
494 #ifdef YYERRORSYMBOL | |
495 if( stateno>YY_REDUCE_COUNT ){ | |
496 return yy_default[stateno]; | |
497 } | |
498 #else | |
499 assert( stateno<=YY_REDUCE_COUNT ); | |
500 #endif | |
501 i = yy_reduce_ofst[stateno]; | |
502 assert( i!=YY_REDUCE_USE_DFLT ); | |
503 assert( iLookAhead!=YYNOCODE ); | |
504 i += iLookAhead; | |
505 #ifdef YYERRORSYMBOL | |
506 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ | |
507 return yy_default[stateno]; | |
508 } | |
509 #else | |
510 assert( i>=0 && i<YY_ACTTAB_COUNT ); | |
511 assert( yy_lookahead[i]==iLookAhead ); | |
512 #endif | |
513 return yy_action[i]; | |
514 } | |
515 | |
516 /* | |
517 ** The following routine is called if the stack overflows. | |
518 */ | |
519 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){ | |
520 ParseARG_FETCH; | |
521 yypParser->yyidx--; | |
522 #ifndef NDEBUG | |
523 if( yyTraceFILE ){ | |
524 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); | |
525 } | |
526 #endif | |
527 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
528 /* Here code is inserted which will execute if the parser | |
529 ** stack every overflows */ | |
530 /******** Begin %stack_overflow code ******************************************/ | |
531 %% | |
532 /******** End %stack_overflow code ********************************************/ | |
533 ParseARG_STORE; /* Suppress warning about unused %extra_argument var */ | |
534 } | |
535 | |
536 /* | |
537 ** Print tracing information for a SHIFT action | |
538 */ | |
539 #ifndef NDEBUG | |
540 static void yyTraceShift(yyParser *yypParser, int yyNewState){ | |
541 if( yyTraceFILE ){ | |
542 if( yyNewState<YYNSTATE ){ | |
543 fprintf(yyTraceFILE,"%sShift '%s', go to state %d\n", | |
544 yyTracePrompt,yyTokenName[yypParser->yystack[yypParser->yyidx].major], | |
545 yyNewState); | |
546 }else{ | |
547 fprintf(yyTraceFILE,"%sShift '%s'\n", | |
548 yyTracePrompt,yyTokenName[yypParser->yystack[yypParser->yyidx].major]); | |
549 } | |
550 } | |
551 } | |
552 #else | |
553 # define yyTraceShift(X,Y) | |
554 #endif | |
555 | |
556 /* | |
557 ** Perform a shift action. | |
558 */ | |
559 static void yy_shift( | |
560 yyParser *yypParser, /* The parser to be shifted */ | |
561 int yyNewState, /* The new state to shift in */ | |
562 int yyMajor, /* The major token to shift in */ | |
563 YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */ | |
564 ){ | |
565 yyStackEntry *yytos; | |
566 yypParser->yyidx++; | |
567 #ifdef YYTRACKMAXSTACKDEPTH | |
568 if( yypParser->yyidx>yypParser->yyidxMax ){ | |
569 yypParser->yyidxMax = yypParser->yyidx; | |
570 } | |
571 #endif | |
572 #if YYSTACKDEPTH>0 | |
573 if( yypParser->yyidx>=YYSTACKDEPTH ){ | |
574 yyStackOverflow(yypParser, yypMinor); | |
575 return; | |
576 } | |
577 #else | |
578 if( yypParser->yyidx>=yypParser->yystksz ){ | |
579 yyGrowStack(yypParser); | |
580 if( yypParser->yyidx>=yypParser->yystksz ){ | |
581 yyStackOverflow(yypParser, yypMinor); | |
582 return; | |
583 } | |
584 } | |
585 #endif | |
586 yytos = &yypParser->yystack[yypParser->yyidx]; | |
587 yytos->stateno = (YYACTIONTYPE)yyNewState; | |
588 yytos->major = (YYCODETYPE)yyMajor; | |
589 yytos->minor = *yypMinor; | |
590 yyTraceShift(yypParser, yyNewState); | |
591 } | |
592 | |
593 /* The following table contains information about every rule that | |
594 ** is used during the reduce. | |
595 */ | |
596 static const struct { | |
597 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ | |
598 unsigned char nrhs; /* Number of right-hand side symbols in the rule */ | |
599 } yyRuleInfo[] = { | |
600 %% | |
601 }; | |
602 | |
603 static void yy_accept(yyParser*); /* Forward Declaration */ | |
604 | |
605 /* | |
606 ** Perform a reduce action and the shift that must immediately | |
607 ** follow the reduce. | |
608 */ | |
609 static void yy_reduce( | |
610 yyParser *yypParser, /* The parser */ | |
611 int yyruleno /* Number of the rule by which to reduce */ | |
612 ){ | |
613 int yygoto; /* The next state */ | |
614 int yyact; /* The next action */ | |
615 YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ | |
616 yyStackEntry *yymsp; /* The top of the parser's stack */ | |
617 int yysize; /* Amount to pop the stack */ | |
618 ParseARG_FETCH; | |
619 yymsp = &yypParser->yystack[yypParser->yyidx]; | |
620 #ifndef NDEBUG | |
621 if( yyTraceFILE && yyruleno>=0 | |
622 && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ | |
623 yysize = yyRuleInfo[yyruleno].nrhs; | |
624 fprintf(yyTraceFILE, "%sReduce [%s], go to state %d.\n", yyTracePrompt, | |
625 yyRuleName[yyruleno], yymsp[-yysize].stateno); | |
626 } | |
627 #endif /* NDEBUG */ | |
628 yygotominor = yyzerominor; | |
629 | |
630 switch( yyruleno ){ | |
631 /* Beginning here are the reduction cases. A typical example | |
632 ** follows: | |
633 ** case 0: | |
634 ** #line <lineno> <grammarfile> | |
635 ** { ... } // User supplied code | |
636 ** #line <lineno> <thisfile> | |
637 ** break; | |
638 */ | |
639 /********** Begin reduce actions **********************************************/ | |
640 %% | |
641 /********** End reduce actions ************************************************/ | |
642 }; | |
643 assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) ); | |
644 yygoto = yyRuleInfo[yyruleno].lhs; | |
645 yysize = yyRuleInfo[yyruleno].nrhs; | |
646 yypParser->yyidx -= yysize; | |
647 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); | |
648 if( yyact <= YY_MAX_SHIFTREDUCE ){ | |
649 if( yyact>YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; | |
650 /* If the reduce action popped at least | |
651 ** one element off the stack, then we can push the new element back | |
652 ** onto the stack here, and skip the stack overflow test in yy_shift(). | |
653 ** That gives a significant speed improvement. */ | |
654 if( yysize ){ | |
655 yypParser->yyidx++; | |
656 yymsp -= yysize-1; | |
657 yymsp->stateno = (YYACTIONTYPE)yyact; | |
658 yymsp->major = (YYCODETYPE)yygoto; | |
659 yymsp->minor = yygotominor; | |
660 yyTraceShift(yypParser, yyact); | |
661 }else{ | |
662 yy_shift(yypParser,yyact,yygoto,&yygotominor); | |
663 } | |
664 }else{ | |
665 assert( yyact == YY_ACCEPT_ACTION ); | |
666 yy_accept(yypParser); | |
667 } | |
668 } | |
669 | |
670 /* | |
671 ** The following code executes when the parse fails | |
672 */ | |
673 #ifndef YYNOERRORRECOVERY | |
674 static void yy_parse_failed( | |
675 yyParser *yypParser /* The parser */ | |
676 ){ | |
677 ParseARG_FETCH; | |
678 #ifndef NDEBUG | |
679 if( yyTraceFILE ){ | |
680 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); | |
681 } | |
682 #endif | |
683 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
684 /* Here code is inserted which will be executed whenever the | |
685 ** parser fails */ | |
686 /************ Begin %parse_failure code ***************************************/ | |
687 %% | |
688 /************ End %parse_failure code *****************************************/ | |
689 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
690 } | |
691 #endif /* YYNOERRORRECOVERY */ | |
692 | |
693 /* | |
694 ** The following code executes when a syntax error first occurs. | |
695 */ | |
696 static void yy_syntax_error( | |
697 yyParser *yypParser, /* The parser */ | |
698 int yymajor, /* The major type of the error token */ | |
699 YYMINORTYPE yyminor /* The minor type of the error token */ | |
700 ){ | |
701 ParseARG_FETCH; | |
702 #define TOKEN (yyminor.yy0) | |
703 /************ Begin %syntax_error code ****************************************/ | |
704 %% | |
705 /************ End %syntax_error code ******************************************/ | |
706 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
707 } | |
708 | |
709 /* | |
710 ** The following is executed when the parser accepts | |
711 */ | |
712 static void yy_accept( | |
713 yyParser *yypParser /* The parser */ | |
714 ){ | |
715 ParseARG_FETCH; | |
716 #ifndef NDEBUG | |
717 if( yyTraceFILE ){ | |
718 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); | |
719 } | |
720 #endif | |
721 while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); | |
722 /* Here code is inserted which will be executed whenever the | |
723 ** parser accepts */ | |
724 /*********** Begin %parse_accept code *****************************************/ | |
725 %% | |
726 /*********** End %parse_accept code *******************************************/ | |
727 ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ | |
728 } | |
729 | |
730 /* The main parser program. | |
731 ** The first argument is a pointer to a structure obtained from | |
732 ** "ParseAlloc" which describes the current state of the parser. | |
733 ** The second argument is the major token number. The third is | |
734 ** the minor token. The fourth optional argument is whatever the | |
735 ** user wants (and specified in the grammar) and is available for | |
736 ** use by the action routines. | |
737 ** | |
738 ** Inputs: | |
739 ** <ul> | |
740 ** <li> A pointer to the parser (an opaque structure.) | |
741 ** <li> The major token number. | |
742 ** <li> The minor token number. | |
743 ** <li> An option argument of a grammar-specified type. | |
744 ** </ul> | |
745 ** | |
746 ** Outputs: | |
747 ** None. | |
748 */ | |
749 void Parse( | |
750 void *yyp, /* The parser */ | |
751 int yymajor, /* The major token code number */ | |
752 ParseTOKENTYPE yyminor /* The value for the token */ | |
753 ParseARG_PDECL /* Optional %extra_argument parameter */ | |
754 ){ | |
755 YYMINORTYPE yyminorunion; | |
756 int yyact; /* The parser action. */ | |
757 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) | |
758 int yyendofinput; /* True if we are at the end of input */ | |
759 #endif | |
760 #ifdef YYERRORSYMBOL | |
761 int yyerrorhit = 0; /* True if yymajor has invoked an error */ | |
762 #endif | |
763 yyParser *yypParser; /* The parser */ | |
764 | |
765 /* (re)initialize the parser, if necessary */ | |
766 yypParser = (yyParser*)yyp; | |
767 if( yypParser->yyidx<0 ){ | |
768 #if YYSTACKDEPTH<=0 | |
769 if( yypParser->yystksz <=0 ){ | |
770 /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/ | |
771 yyminorunion = yyzerominor; | |
772 yyStackOverflow(yypParser, &yyminorunion); | |
773 return; | |
774 } | |
775 #endif | |
776 yypParser->yyidx = 0; | |
777 yypParser->yyerrcnt = -1; | |
778 yypParser->yystack[0].stateno = 0; | |
779 yypParser->yystack[0].major = 0; | |
780 #ifndef NDEBUG | |
781 if( yyTraceFILE ){ | |
782 fprintf(yyTraceFILE,"%sInitialize. Empty stack. State 0\n", | |
783 yyTracePrompt); | |
784 } | |
785 #endif | |
786 } | |
787 yyminorunion.yy0 = yyminor; | |
788 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) | |
789 yyendofinput = (yymajor==0); | |
790 #endif | |
791 ParseARG_STORE; | |
792 | |
793 #ifndef NDEBUG | |
794 if( yyTraceFILE ){ | |
795 fprintf(yyTraceFILE,"%sInput '%s'\n",yyTracePrompt,yyTokenName[yymajor]); | |
796 } | |
797 #endif | |
798 | |
799 do{ | |
800 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); | |
801 if( yyact <= YY_MAX_SHIFTREDUCE ){ | |
802 if( yyact > YY_MAX_SHIFT ) yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; | |
803 yy_shift(yypParser,yyact,yymajor,&yyminorunion); | |
804 yypParser->yyerrcnt--; | |
805 yymajor = YYNOCODE; | |
806 }else if( yyact <= YY_MAX_REDUCE ){ | |
807 yy_reduce(yypParser,yyact-YY_MIN_REDUCE); | |
808 }else{ | |
809 assert( yyact == YY_ERROR_ACTION ); | |
810 #ifdef YYERRORSYMBOL | |
811 int yymx; | |
812 #endif | |
813 #ifndef NDEBUG | |
814 if( yyTraceFILE ){ | |
815 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); | |
816 } | |
817 #endif | |
818 #ifdef YYERRORSYMBOL | |
819 /* A syntax error has occurred. | |
820 ** The response to an error depends upon whether or not the | |
821 ** grammar defines an error token "ERROR". | |
822 ** | |
823 ** This is what we do if the grammar does define ERROR: | |
824 ** | |
825 ** * Call the %syntax_error function. | |
826 ** | |
827 ** * Begin popping the stack until we enter a state where | |
828 ** it is legal to shift the error symbol, then shift | |
829 ** the error symbol. | |
830 ** | |
831 ** * Set the error count to three. | |
832 ** | |
833 ** * Begin accepting and shifting new tokens. No new error | |
834 ** processing will occur until three tokens have been | |
835 ** shifted successfully. | |
836 ** | |
837 */ | |
838 if( yypParser->yyerrcnt<0 ){ | |
839 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
840 } | |
841 yymx = yypParser->yystack[yypParser->yyidx].major; | |
842 if( yymx==YYERRORSYMBOL || yyerrorhit ){ | |
843 #ifndef NDEBUG | |
844 if( yyTraceFILE ){ | |
845 fprintf(yyTraceFILE,"%sDiscard input token %s\n", | |
846 yyTracePrompt,yyTokenName[yymajor]); | |
847 } | |
848 #endif | |
849 yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion); | |
850 yymajor = YYNOCODE; | |
851 }else{ | |
852 while( | |
853 yypParser->yyidx >= 0 && | |
854 yymx != YYERRORSYMBOL && | |
855 (yyact = yy_find_reduce_action( | |
856 yypParser->yystack[yypParser->yyidx].stateno, | |
857 YYERRORSYMBOL)) >= YY_MIN_REDUCE | |
858 ){ | |
859 yy_pop_parser_stack(yypParser); | |
860 } | |
861 if( yypParser->yyidx < 0 || yymajor==0 ){ | |
862 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
863 yy_parse_failed(yypParser); | |
864 yymajor = YYNOCODE; | |
865 }else if( yymx!=YYERRORSYMBOL ){ | |
866 YYMINORTYPE u2; | |
867 u2.YYERRSYMDT = 0; | |
868 yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2); | |
869 } | |
870 } | |
871 yypParser->yyerrcnt = 3; | |
872 yyerrorhit = 1; | |
873 #elif defined(YYNOERRORRECOVERY) | |
874 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to | |
875 ** do any kind of error recovery. Instead, simply invoke the syntax | |
876 ** error routine and continue going as if nothing had happened. | |
877 ** | |
878 ** Applications can set this macro (for example inside %include) if | |
879 ** they intend to abandon the parse upon the first syntax error seen. | |
880 */ | |
881 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
882 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
883 yymajor = YYNOCODE; | |
884 | |
885 #else /* YYERRORSYMBOL is not defined */ | |
886 /* This is what we do if the grammar does not define ERROR: | |
887 ** | |
888 ** * Report an error message, and throw away the input token. | |
889 ** | |
890 ** * If the input token is $, then fail the parse. | |
891 ** | |
892 ** As before, subsequent error messages are suppressed until | |
893 ** three input tokens have been successfully shifted. | |
894 */ | |
895 if( yypParser->yyerrcnt<=0 ){ | |
896 yy_syntax_error(yypParser,yymajor,yyminorunion); | |
897 } | |
898 yypParser->yyerrcnt = 3; | |
899 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); | |
900 if( yyendofinput ){ | |
901 yy_parse_failed(yypParser); | |
902 } | |
903 yymajor = YYNOCODE; | |
904 #endif | |
905 } | |
906 }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); | |
907 #ifndef NDEBUG | |
908 if( yyTraceFILE ){ | |
909 int i; | |
910 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt); | |
911 for(i=1; i<=yypParser->yyidx; i++) | |
912 fprintf(yyTraceFILE,"%c%s", i==1 ? '[' : ' ', | |
913 yyTokenName[yypParser->yystack[i].major]); | |
914 fprintf(yyTraceFILE,"]\n"); | |
915 } | |
916 #endif | |
917 return; | |
918 } | |
OLD | NEW |