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