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